Skip to content
csrl edited this page Feb 19, 2023 · 4 revisions

To run the examples, do make and then start the Erlang command line with erl -pa ./ebin.
Don't be shocked by the very long dump following each function call.
At the command line you are seeing the Lua State dumped, that is returned by these calls:

5.1 execute a string

    luerl:do("print(\"Hello, Robert!\")", luerl:init()).

5.2 execute a file

    luerl:dofile("./examples/hello/hello.lua", luerl:init()).

5.3 separately parse, then execute

    EmptyState = luerl:init(),
    {ok, Chunk, State} = luerl:load("print(\"Hello, Chunk!\")", EmptyState),
    {_Ret, _NewState} = luerl:do(Chunk, State).

5.4 call a function in the state

    {Res,State1} = luerl:call_function([table,pack], [<<"a">>,<<"b">>,42], State0)

executes the call table.pack("a", "b", 42) in State0. E.g.:

    {Res,State1} = luerl:call([table,pack], [<<"a">>,<<"b">>,42]),
    io:format("~p~n",[Res]).

5.5 call a method in the state

    {Res,State1} = luerl:call_method([g,h,i], [<<"a">>,<<"b">>,42], State0)

executes the call g.h:i("a", "b", 42) in State0.

5.6 define a method in the state

    State1 = luerl:set_table([inc], fun([Val], State) -> {[Val+1], State} end)

the method can be called like this:

    luerl:do(<<"print(inc(4))">>, State1)

5.7 More examples

For more information see the examples directory:

./hello.erl is very brief while examples/hello/hello2.erl offers a comprehensive list on how use the individual interface functions.

You can build and run these samples with:

    make examples