Skip to content

1.1 Questions and Answers

Jean Chassoul edited this page Jun 4, 2018 · 3 revisions

Questions and Answers

Q. How does Lua call Erlang? for example, Lua want to send socket through Erlang, or tell other Erlang node some information, how to do this?

A. You cannot directly call Erlang from the Lua inside luerl. To do this you need to define an interface module written in Erlang with functions you can call from Lua. There is a predefined way of building and loading such a module so it can be reached from Lua through a table like any other Lua function. There is also a predefined way of passing data in and out of these functions. If you look in the src directory all the modules luerl_lib_XXXX.erl are interface modules like this.

One reason for having it this way is that it makes sandboxing very easy as Lua code cannot do this loading, it has to be done in the surrounding Erlang. Which makes it trivial to set up a limited environment.

Q. How to set Lua script search path?

A. Luerl uses (package.config, loaded, preload, path, seachers, searchpath) as per the manual. At least that is the intent.

Q. How to read files

A. We tested this from the Erlang shell putting the config in the file "v.lua" then doing

7> luerl:do("local local = dofile(\"v.lua\")", S0).

and it seemed to work.

Q. Are there some known bugs?

A. not really, but some things which are known not to be implemented or work properly:

  • coroutines and goto

  • only limited standard libraries

  • proper handling of _ENV

  • tail-call optimization in return

Functions defined in a loop, while, repeat and for, and when the loop is exited with a break from inside an if will generate an error when called.

For example the functions defined in

for i=1,10 do
  a[i] = {set = function(x) i=x end, get = function () return i end}
  if i == 3 then break end
end

Note: This only occurs if the loop is actually exited with the break, otherwise there is no problem.

Q. Lua coroutines in Luerl?

A. The current implementation cannot directly be used to implement coroutines. While some data is explicitly kept in a stack not everything is, specifically the actual function call-stack is not. While this is definitely possible to do it would require changes in many parts internally.

One reason I have not really looked at it is that when I have needed something like coroutines I have instead used Erlang's processes. I then run Luerl inside each process and use Erlang messages for communication and leave the scheduling to Erlang. This works very well but it is not quite the same thing and it does mean you can't just use already coroutine-based code must it must be specially written for this.

Another issue is that as all these "Lua processes" are running in separate Erlang processes they can't share any state.