lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Most of the difference is just in terminology and from which side you view it, from erlang or Lua. For example, for me loading means actually getting it into the sytem not just compiling it into a runnable form. And I view these functions from the erlang side. I will also admit that I am a bit hazy as to how the C-side functions work, it seems easier within the language.

'eval' and 'do' work well for me, though I do wonder if we need 'evalfile' and 'dofile'. I prefer 'compile' instead of 'load' as this is what is does. 'call' is fine. What is the difference between 'call' and 'do'? For creating and destroying state I would prefer either 'new' / 'delete' or 'start' / 'stop' depending on whether you want to view the state as just data or as some form of concurrent activity, a separate process in Erlang.

I would use 'encode' / 'decode' for converting between an Erlang representation and an internal luerl representation. So:

luerl:encode(Erlang, State) -> {Luerl,State}.
luerl:decode(Luerl, State) -> Erlang.

Encoding will modify the state as tables only exist within that state. If it not done this way then there is no need of these functions as the representation is the same and this conversion will be done in the other interface functions. So for example a Lua strings are Erlang binaries and tables will are a list of tuple pairs,

Lua {['a']=97,['b']=98,10,20,30} <==> Erlang [{1.0,10.0},{2.0,20.0},{3.0,30.0},{<<"a">>,97.0},{<<"b">>,98.0}]

Putting Lua tables into the state is also the only way to handle the difference in the meaning of equality between Lua and Erlang.

For errors I have been thinking in the Erlang way. The only function to actually return an error value would be 'compile' (or 'load') which would become

luerl:compile(String) -> {ok,Chunk} | {error,Reason}.

All errors during the Lua execution, unless they are caught internally, would result in Erlang exceptions which would handled in the normal Erlang way, either by catching it or having the Erlang process die.

The result returned from Lua and the arguments to a call would be a list of Erlang values. If no values are returned the list is empty otherwise it would contain same number of elements as in the 'return' statement.

Those are my thoughts for the moment. Now I will go back and try and code the idiosyncrasies of string.sub.


Hi Robert,

To allow for String|Chunk, the chunks returned from ps/1 would have to be wrapped to be distinguishable from the Strings. I think 'functiondef' could be the right choice.

For the names, I'd propose to maybe stay closer to the Lua language function names and its C interface [1].

But at any rate, to maybe decide for one of "do" and "eval" to 1) return bare results 2) return {Result, State}. Rather than making this dependent on whether State was handed in or not as a parameter?

Since Lua uses dofile() both in the Lua language and the C interface, (and since of course neither case returns state), the "do" functions look earmarked for returning the simple, bare bones Result. However ... somehow "eval" is a better fit for a function that is expected to return something.

Lua's C interface uses "load" for parsing-only: load, loadfile, lua_load [2], lua_loadfile, lua_loadstring, lua_loadbuffer.

This could be an alternative to wrapping the chunks: for load, in Lua "the string mode controls whether the chunk can be text or binary (that is, a precompiled chunk). It may be the string "b" (only binary chunks), "t" (only text chunks), or "bt" (both binary and text). The default is "bt". [5]

The type that the loads return is 'function': "If there are no syntactic errors, returns the compiled chunk as a function; otherwise, returns nil plus the error message." --- therefore, the right chunk wrapper could be { functiondef, ... }, instead of compiled chunks being lists as outermost type.

Execution of pre-parsed/compiled chunks is "call": pcall, xpcall, lua_call, lua_pcall [3],  and lua_pcallk.

State is created and destroyed by lua_newstate and lua_close.

There is no "eval" in Lua.

So here's my proposal:

luerl:eval(String|Chunk[, State]) -> Result.
luerl:evalfile(PathString[, State]) -> Result.

luerl:do(String|Chunk[, State]) -> {Result, NewState}.
luerl:dofile(PathString[, State]) -> {Result, NewState}.

luerl:newstate() -> State.
luerl:close(State) -> ok.

luerl:load(String) -> {ok, Chunk}.
luerl:loadfile(PathString) -> {ok, Chunk}.
luerl:call(Chunk[, State][, ErlParamList()]) -> {Result, NewState}.

luerl:tolua(list()) -> LuerlTermsList().
luerl:toerlang(LuerlTermsList()) -> list().

This would be somewhat in keeping with Lua's naming.

I am unclear about error state returns. Simply in the Result I guess?

Relative to your proposal that is:
luerl:eval(String|Chunk) -> Result. => luerl:eval(String|Chunk[, State]) -> Result.
luerl:dofile(String) -> Result. => luerl:dofile(PathString[, State]) -> {Result,State}.
luerl:new() -> State. (currently luerl:init() -> State.) =>luerl:newstate() -> State.
luerl:eval(String|Chunk, State) -> {Result,NewState}. => luerl:eval(String|Chunk, State) -> {Result,NewState}.
luerl:dofile(String, State) -> {Result,NewState}. => same
luerl:compile(String) -> {ok,Chunk}. => luerl:load(String) -> {ok,Chunk}.

Beyond that, I had thought with 'interface' you would be addressing the direct interchange of values between Erlang and Lua. I'd be all for making the collection of tables in the Lua state accessible and writable, directly, somehow navigating into it using a key structure. And if possible, vice versa: giving Lua direct access to Erlang state.



[2] One note I like, in the description of the C function lua_load : "The source argument gives a name to the chunk, which is used for error messages and in debug information (see §4.9)." -

[3] When you use xpcall or lua_pcall, you may give a message handler to be called in case of errors. This function is called with the original error message and returns a new error message. It is called before the error unwinds the stack, so that it can gather more information about the error, for instance by inspecting the stack and creating a stack traceback. This message handler is still protected by the protected call; so, an error inside the message handler will call the message handler again. If this loop goes on, Lua breaks it and returns an appropriate message. -

[4] In Lua (not the C interface), dofile does not run in protected mode.


On 2/20/12 10:59 PM, Robert Virding wrote:
[snip] 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.


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.


[-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.


[-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.


[-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.


[-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.


erlang-questions mailing list