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

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement