[erlang-questions] Luerl - Lua in Erlang

Robert Virding robert.virding@REDACTED
Mon Feb 20 22:59:03 CET 2012


Yes, the interface is a bit of a mess. I just threw it together so people would have something. I had planned something along the lines of: 

luerl:eval(String|Chunk) -> Result. 
luerl:dofile(String) -> Result. 

Basic simple interface which initialises a state and evaluates the chunk String in it returning a list of return values (if any). For example luer:eval("local t={'a','b'} return t[1],t[2]") will return [<<"a">>,<<"b">>]. luerl:dofile/1 is not really necessary. 

luerl:new() -> State. 
luerl:eval(String|Chunk, State) -> {Result,NewState}. 

luerl:dofile(String, State) -> {Result,NewState}. 
luerl:compile(String) -> {ok,Chunk}. 

A more complex interface. luerl:new/0 creates an initial state. luerl:eval/2 will evaluate a chunk in a state and return the values and the updated state. This state can be reused to evaluate new chunks. Again luerl:dofile/2 is not really necessary. luerl:compile(String) compiles the string into an internal form ready to run in eval/1/2. 

Result is always a list of return values which may be empty if the chunk does not do a return with values. For data types: 

Lua strings are binaries 
Lua numbers are floats 
Lua tables are orddicts (property lists) of key-value tuples 
Lua true, false, nil are just the atoms true, false, nil 

Anyway something along those lines. It might be nice to have a function call wrapper which would allow you a more erlang like way of calling a luerl function. 

Robert 

----- Original Message -----

> Regarding interface function names:

> I wonder what logic Luerl's names of do and eval follow:

> dofile/1, like eval/1, returns a pragmatic Ret

> while do/2 returns {String, State}

> Since you are exporting ps/1, there should maybe be a dochunk/2?

> And /1, too?

> Or should it maybe be evalchunk/1, dochunk/2 (the /2s with State as
> second parameter)?

> Here are some relevant functions from Lua's C interface.

> > luaL_dofile
> 

> > [-0, +?, m]
> 
> > int luaL_dofile (lua_State *L, const char *filename);
> 
> > Loads and runs the given file. It is defined as the following
> > macro:
> 

> > (luaL_loadfile(L, filename) || lua_pcall(L, 0, LUA_MULTRET, 0))
> 
> > It returns false if there are no errors or true in case of errors.
> 

> > luaL_dostring
> 

> > [-0, +?, –]
> 
> > int luaL_dostring (lua_State *L, const char *str);
> 
> > Loads and runs the given string. It is defined as the following
> > macro:
> 

> > (luaL_loadstring(L, str) || lua_pcall(L, 0, LUA_MULTRET, 0))
> 
> > It returns false if there are no errors or true in case of errors.
> 

> > luaL_loadstring
> 

> > [-0, +1, –]
> 
> > int luaL_loadstring (lua_State *L, const char *s);
> 
> > Loads a string as a Lua chunk. This function uses lua_load to load
> > the chunk in the zero-terminated string s.
> 

> > This function returns the same results as lua_load.
> 

> > Also as lua_load, this function only loads the chunk; it does not
> > run
> > it.
> 

> > luaL_newstate
> 

> > [-0, +0, –]
> 
> > lua_State *luaL_newstate (void);
> 
> > Creates a new Lua state. It calls lua_newstate with an allocator
> > based on the standard C realloc function and then sets a panic
> > function (see §4.6) that prints an error message to the standard
> > error output in case of fatal errors.
> 

> > Returns the new state, or NULL if there is a memory allocation
> > error.
> 

> Source: http://www.lua.org/manual/5.2/manual.html
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20120220/54c75fdb/attachment.htm>


More information about the erlang-questions mailing list