chaos-ad / moon

An Erlang NIF-based application which allows to embed Lua into the Erlang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Moon

Library for calling Lua from Erlang, and back.

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 Lua Type/Remarks
nil nil nil
true true boolean
false false boolean
42 42 number
42.123 42.123 number
atom "atom" string
"string" {115,116,114,105,110,103} table with integers in lua, dont use it!
<<"binary">> "binary" string
[] {} empty table
[10, 100, <<"abc">>] {10, 100, "abc"} array
[{yet, value}, {another, value}] {yet="value", another="value"} table
[{ugly, "mixed"}, list] {ugly="mixed", "list"} "list" will be accessable at index [1], and "mixed" - under the "ugly" key. No guarantees about the ordering of elements in a table

Lua -> Erlang

Lua Erlang Type/Remarks
nil nil atom
true true atom
false false boolean
42 42 number
42.123 42.123 number
"string" <<"string">> binary
{} [] array is a list
{10, 100, "abc"} [10, 100, <<"abc">>] array is a list
{yet="value", another="value"} [{<<"another">>, <<"value">>}, {<<"yet">>, <<"value">>}] table is a proplist
{ugly="mixed", "list"} [<<"list">>, {<<"ugly">>, <<"mixed">>}] list with {key,value} tuples for values with keys, and plain values for the rest. No guarantees about the ordering of elements in a list

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

An Erlang NIF-based application which allows to embed Lua into the Erlang


Languages

Language:C++ 74.9%Language:Erlang 25.1%