An experimental implementation of Lua 5.2 written solely in pure Erlang.
Some things which are known not to be implemented or work properly:
-
label and goto
-
tail-call optimisation in return
-
only limited standard libraries
-
proper handling of __metatable
-
...
Luerl will be slower than a reasonable native Lua implementation. This is mainly due to having to emulate mutable data on top of an immutable world. There is really no way around this. An alternative would be to implement a special Lua memory outside of the normal Erlang, but this would defeat the purpose of Luerl. It would instead be then more logical to connect to a native Lua.
Some valid use cases for Luerl are:
-
Lua code will be run only occasionally and it wouldn't be worth managing an extra language implementation in the appkication.
-
The Lua code chunks are small so the slower speed are wayed up by Luerl's faster interface.
-
It is easy to run multiple instances of Luerl which could better utilise multicores.
There may be others.
All functions optionally accept a Lua State parameter. The Lua State is the state of a Lua VM instance. It can be carried from one call to the next. If no State is passed in, a new state is initiated for the function call.
eval and do functions differ only in what they return. The do functions return results and a new Lua State, the eval functions return a tuple starting on 'ok' or 'error', then the result, or cause of error.
do --> {Result, State}
eval --> {ok, Result} | {error, Reason}
The 'compile' functions double the 'load' function, with but a different name.
Evaluate a Lua expression passed in as a string or binary, and return its result.
Load and execute a file, and return the result.
Evaluate a Lua expression and return its result, and the new Lua State.
Load and execute the Lua code in the file and return its result, and the new Lua State. Equivalent to doing luerl:eval("dofile('FileName')").
Parse a Lua chunk as string or binary, and return a compiled chunk.
Parse a Lua file, and return a compiled chunk.
Parse a Lua chunk as string or binary, and return a compiled chunk. Same as load/1.
Parse a Lua file, and return a compiled chunk. Same as loadfile/1.
Get a new Lua State = a fresh Lua VM instance.
Execute a compiled chunk.
Garbage collects the state and (todo:) does away with it.
Runs the (experimental) garbage collector on a state and returns the new state.
N.B. This interface is subject to change!
Examples
luerl:do("print(\"Hello, Robert(o)!\")"),
luerl:dofile("./examples/hello/hello.lua"),
{ok, Chunk} = luerl:load("print(\"Hello, Chunk!\")"),
State = luerl:start(),
{_Ret, _NewState} = luerl:do(Chunk, State),
For more examples see examples/hello/hello2.erl
.
./hello.erl
is a very brief example while examples/hello/hello2.erl
is a comprehensive lists of most ways that come to mind of how to use the individual interface functions.
You can build and run these samples with:
make hello
make hello2
-
_G
-
_VERSION
-
assert
-
collectgarbage
-
dofile
-
eprint
-
error
-
getmetatable
-
ipairs
-
load
-
loadfile
-
next
-
pairs
-
print
-
rawequal
-
rawget
-
rawlen
-
rawset
-
select
-
setmetatable
-
tonumber
-
tostring
-
type
-
math.abs
-
math.acos
-
math.asin
-
math.atan
-
math.atan2
-
math.ceil
-
math.cos
-
math.cosh
-
math.deg
-
math.exp
-
math.floor
-
math.log
-
math.max
-
math.min
-
math.pi
-
math.pow
-
math.rad
-
math.sin
-
math.sinh
-
math.sqrt
-
math.tan
-
math.tanh
-
os.difftime
-
os.getenv
-
os.time
-
string.byte
-
string.char
-
string.format (very limited as yet)
-
string.len
-
string.lower
-
string.rep
-
string.reverse
-
string.sub
-
string.upper
-
table.concat
-
table.pack
-
table.unpack