dawenxi-harvey / moon

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Moon

Library for calling Lua from Erlang, and back.

chanage it to lua5.1 and can call one luavm with multiple process

Dependencies:

Parts of the boost library, somewhat close to 1.48 Developement version of lua

These libraries easily can be obtained on ubuntu by running this:

sudo apt-get install libboost1.48-all-dev liblua5.2-dev

General usage:

Here an self-describing example of usage:

{ok, VM} = moon:start_vm(). %% Beware! It spawns the os thread.
{ok,{17,<<"hello">>}} = moon:eval(VM, "return 17, \"hello\"").
ok = moon:load(VM, "priv/test.lua").
moon:call(VM, test_function, [first_arg, <<"second_arg">>, [{key, val}, {key2, val2}]]).
ok = moon:stop_vm(VM).

Strictly speaking, moon:stop_vm/1 is used here just for symmetry. VM will be stopped and freed when the erlang garbage collector detects that VM become a garbage.

Callbacks from lua to erlang:

There is two possible modes in which callbacks are handled:

Permissive mode

This mode allows to invoke arbitrary function from lua with erlang.call(Module, Function, Args):

{ok, VM} = moon:start_vm().
{ok, <<"ok">>} = moon:eval(VM, <<"return erlang.call(\"io\", \"format\", {\"Look ma, im calling! ~s~n\", {\"Yay\"}}).result">>).

Though, it is not very useful, since type mapping is far from done, and there is no way
to construct atoms or strings from lua.

Restrictive mode

This mode passes all calls to erlang.call from lua to a single callback. Dispatching and/or type mapping can be done here.

Callback = fun(X) -> io:format("Callback called; args: ~p~n", [X]) end.
{ok, VM} = moon:start_vm([{callback, Callback}]).
{ok, <<"ok">>} = moon:eval(VM, "return erlang.call({\"hello\"}).result").

Type mapping:

Erlang Lua Erlang Remarks
nil nil nil nil in lua
true true true boolean in lua
false false false boolean in lua
42 42 42 number in lua
42.123 42.123 42.123 number in lua
atom "atom" <<"atom">> string in lua, binary, when comes back to erlang
"string" {115,116,114,105,110,103} "string" table with integers in lua, dont use it!
<<"binary">> "binary" <<"binary">> string in lua
[] {} []
[10, 100, <<"abc">>] {10, 100, "abc"} [10, 100, "abc"]
[{yet, value}, {another, value}] {yet="value", another="value"} [{<<"another">>, <<"value">>}, {<<"yet">>, <<"value">>}]
[{ugly, "mixed"}, list] {ugly="mixed", "list"} [<<"list">>, {<<"ugly">>, <<"mixed">>}] "list" will be accessable at index [1], and "mixed" - under the "ugly" key

Todo:

  • Get rid of libboost_thread dependency, and replace queue with just a mutex & condition variable
  • Embed header-only part of boost to the build
  • Convert erlang strings to lua strings properly

About


Languages

Language:C 54.6%Language:C++ 17.6%Language:Lua 14.7%Language:HTML 9.0%Language:Erlang 2.1%Language:Makefile 1.2%Language:CMake 0.4%Language:Shell 0.3%Language:Perl 0.1%Language:Python 0.1%