Coder Social home page Coder Social logo

moon's Introduction

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

moon's People

Contributors

chaos-ad avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.