eskil

eskil

Lua function tables via luerl and threading through the lua state

Hello elixir friends,

I’ve been playing around with luerl recently, and generally, it’s been trouble-free; straight-forward and easy to use; works well with elixir.

The corner case I ran into is when fetching function tables from lua. I can call the returned function but I cannot find a way to thread through the lua state.

Given this example lua;

function get_function_table()
  table = {}
  table["increment"] = increment
  return table
end

state = 0
function increment()
  state = state + 1
  print("state is "..state)
  return state
end

And this elixir code;

# setup state, load script etc.
lua_state = :luerl.init()
script = File.read!("sample.lua")
{:ok, chunk, lua_state} = :luerl.load(script, lua_state)
{_, lua_state} = :luerl.do(chunk, lua_state)

# get the function table and call whatever "increment" points to
{[lua_map], _lua_state} = :luerl.call_function([:get_function_table], [], lua_state)
ex_map = Map.new(lua_map)
function = ex_map["increment"]
[number] = function.([]) # How can lua_state be threaded through?

It calls “increment”. But since the lua_state isn’t threaded through, side effects are lost. I cannot find a way to pass through the state.

function is a #Function with arity /1, and it’s env seems to include the lua state at the time get_function_table was called.

I’ve read through the luerl code to try and figure out how to accomplish this since I assume that internally it must be doing something similar in luerl_emul.erl. But I haven’t been able to figure it out.

There are workarounds, such as returning the function name as a string instead. But I’m curious about this.

My question is; is there a supported way in luerl, to call function and thread through the lua state?

There’s a complete example escript at https://github.com/eskil/luerl_map_to_function that demonstrates it.

Thanks!

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New

We're in Beta

About us Mission Statement