Yet another port of Lua to JavaScript using Emscripten. Demo using Lua 5.3: http://mpeterv.github.io/emlua-demo/.
- Compatible with Lua 5.1, Lua 5.2 and Lua 5.3.
- Provides rather complete bindings for Lua C API, although functions that take or return pointers
other than
lua_State *
are not very useful. Creating wrappers for them is a goal. - Allows binding arbitrary JavaScript values to full userdata and tables, so that it's possible to write any Lua <-> JS interoperability layer.
- Allows pushing JavaScript functions disguised as Lua C functions.
- Activate Emscripten tools.
- Optional:
cd
intolua
, runmake clean
, checkout Lua version to use,cd
back. - Run
make
, producingemlua.js
.
There are no real tests yet, but some example code is in test.js
, run it using Node.js.
var emlua = require('./emlua.js'); // Or <script src="emlua.js"></script> in browser
Creates a new Lua state, equivalent to luaL_newstate()
C API function. This state
contains C API functions that normally start with lua_
, as well as constants starting
with LUA_
. The functions automatically pass the state as the first argument when needed.
Names of functions and constants are lowercased and stripped of lua_
prefix.
var state = emlua.state();
state.pushstring('Lua version number is ' + state.version_num);
console.log(state.tostring(-1)); // Lua version number is 503
Container for C API functions from auxiliary library (their names normally start with luaL_
),
as well as constants starting with LUAL_
.
state.aux.openlibs();
state.aux.loadstring('print("Hello emlua!")');
state.call(0, 0); // Hello emlua!
Associates a JavaScript value with a Lua table or full userdata at an index.
Returns true
on success and false
on failure (if the Lua value has wrong type).
Retrieves JavaScript value associated with Lua value at an index.
Returns undefined
if no value is associated.
Equivalent to lua_pushcclosure
for JavaScript functions. Pushes a C function
that delegates to func
when called from Lua. func
is called with one argument, emlua state
object, which can be used to access arguments and upvalues using C API bindings.
state.pushstring('foo');
state.util.pushjsclosure(function(st) {
console.log('Argument: ' + st.tostring(1) + ', upvalue: ' + st.tostring(st.upvalueindex(1)));
}, 1)
state.pushstring('bar');
state.call(1, 0); // Argument: bar, upvalue: foo
Equivalent to lua_pushcfunction
for JavaScript functions.
Retrieves JavaScript function associated with a C function at an index.
Returns undefined
if the Lua value is not a function or has no corresponding JavaScript function.
An implementation of luaL_testudata
that exists even when using Lua 5.1.
C pointer of this Lua State as a number.