Skip to content

Instantly share code, notes, and snippets.

@lastcanal
Last active December 13, 2015 22:59
Show Gist options
  • Select an option

  • Save lastcanal/4988515 to your computer and use it in GitHub Desktop.

Select an option

Save lastcanal/4988515 to your computer and use it in GitHub Desktop.
Proposed example interface for luerl tables
-module(luerl_table_function).
-export(run/0).
%% Get table or table key
%% luerl:table(Fpath, Lua) -> {ok, Value}
%% Set table or table key
%% luerl:table(Fpath, Value, Lua) -> {ok, Lua}
run() ->
Lua0 = luerl:init(),
%% Set a table using a dict
%% Equivalent Lua: "return my_table = {ping="pong", echo=function(msg) return msg end};"
MyTable = [{<<"ping">>,<<"pong">>},{<<"echo">>, fun(Msg) -> Msg end}],
{ok, Lua1} = luerl:table([my_table], MyTable, Lua0),
%% Get the whole table
%% Equivalent Lua: "return my_table;"
{ok, MyTable} = luerl:table([my_table], Lua1),
%% Get a value from a key in a table
%% Equivalent Lua: "return my_table.ping;"
{ok, <<"pong">>} = luerl:table([my_table,ping], Lua1),
%% Get a function in a table
%% Equivalent Lua: "return my_table.echo('hello');"
{ok, Func} = luerl:table([my_table,echo], Lua1),
[<<"hello">>] = Func(<<"hello">>),
%% Set a value for a key in a table
%% Equivalent Lua: "return my_table.ping = 'pang';"
{ok, Lua2} = luerl:table([my_table,ping], <<"pang">>, Lua1),
{ok, <<"pang">>} = luerl:table([my_table,ping], Lua2),
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment