-
Notifications
You must be signed in to change notification settings - Fork 143
0.3 Hello Lua
First you need to define an interface module written in Erlang with the functions you want to call from the standard Lua 5.3 implemented by Luerl.
There is a predefined way of building and loading such a modules so its functions 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. Take a look at the source code all the modules luerl_lib_XXXX.erl
are interface modules like this. If you check in the module luerl_emul.erl
in the function load_lib/3
you can see how it is installed.
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 environment.
Sometimes we want to do exactly the opposite, not add functionality to what the luerl interpreter can do but remove it. Since, as discussed above, the only way it can access functionality is if its stored in one of the tables (that reside in the state) restricting functionality is as simple as removing some of the tables.
If we for example we really hate math and want to make sure no mathematical functions are ever called in scripts we run we could do the following:
St0 = luerl:init().
St1 = luerl:set_table([math], <<"no math for you!">>, St0).
luerl:eval("return math", St1).
Of course this also works with other tables, like io
or file
which would prevent a Lua script to access files or perform IO-operations (not as bad as forbidding math but still helpful).