NodeLua is a module to expose Lua bindings to Node.JS.
This is still a work in progress, collaborators welcome.
Requires Lua 5.1, will not work with 5.2
Lua and it's C libraries are required for this module to work.
npm install nodelua
var nodelua = require('nodelua');
The NodeLua
module itself contains the objects LuaObject
, LuaFunction
, as well as some constants.
var lua = new nodelua.LuaObject()
STATUS
is an object that contains the constants for values returned by LuaObject.status()
.
nodelua.STATUS
conatins the following constants:
YIELD: 1
ERRRUN: 2
ERRSYNTAX: 3
ERRMEM: 4
ERRERR: 5
GC
is an object of constants used for controlling the lua garbage collector.
nodelua.GC
conatins the following constants:
STOP: 0
RESTART: 1
COLLECT: 2
COUNT: 3
COUNTB: 4
STEP: 5
SETPAUSE: 6
SETSTEPMUL: 7
INFO
is an object containing constants with information about the version of lua you are using.
nodelua.INFO
contains the following constants:
VERSION
VERSION_NUM
COPYRIGHT
AUTHORS
The LuaFunction
is used to initialize a javascript function for use by lua.
One caveat to using LuaFunction
s and multiple LuaObject
s is that LuaFunction
s regardless of which LuaObject
they are registered with are visable to ALL LuaObject
s.
The constructor for LuaFunction
requires the func_name
to use from within Lua (nodelua('name')
) as well as the function itself func
.
var func = new nodelua.LuaFunction('test', function(){
console.log('This Is A Test');
return 42;
});
The name
property of the LuaFunction
is exposed, but it cannot be changed.
The LuaObject
is an object wrapper around a lua_State
instance.
The doFile
method is used to load and execute lua code stored in file_name
.
lua.doFile('test.lua');
The doString
method is the same as doFile
except the code is loaded from lua_code
rather than from a file.
lua.doString("print('Hello, Lua')");
The setGlobal
method is used to provide lua with the global variable name
containing the value value
.
lua.setGlobal('test', 'value');
The getGlobal
method is used to retrieve either a value set by setGlobal
or a global variable in any lua code that has been run.
console.log(lua.getGlobal('test'));
registerFunction
is used to expose a LuaFunction
func
to lua.
var func = new nodelua.LuaFunction('add_them', function(a, b){
console.log(a+b);
});
lua.registerFunction(func);
lua.doString('nodelua("add_them", 2, 4)');
There are a few caveats with registerFunction
.
For starters in order to invoke the javascript function from within lua you must use an exposed nodelua
function as opposed to using the functions registered name
.
nodelua('add_them', 3, 5)
All LuaFunction
s registered with registerFunction
is registered globally for all LuaObject
s regardless of which object is used to register it.
status
will return the current status code for lua. The result can be 0
for normal or one of the error codes in nodelua.STATUS
.
if(lua.status() == nodelua.STATUS.ERRSYNTAX){
console.error('Lua Syntax Error');
}
collectGarbage
is used to control the lua garbage collector. GC_CODE
should be one of the codes taken from nodelua.GC
.
lua.collectGarbage(nodelua.GC.COLLECT);
Push value
onto the Lua stack.
lua.push(5);
Pop num
items from the stack. Default is 1.
lua.pop(5);
Return the number of elements on the Lua stack.
var num = lua.getTop();
Set the top of the Lua stack to index
.
lua.setTop(3);
Replaces the top stack element into the specified index
lua.repalce(3);
close
should be used whenever you have finished using a LuaObject
. This will simply call lua_close
on the lua_State
for that object.
See ./examples/
.
var nodelua = require('nodelua');
var lua = new nodelua.LuaObject();
var func = new nodelua.LuaFunction('add_them', function(a, b){
return a + b;
});
lua.doFile('some_file.lua');
console.dir(lua.getGlobal('some_var'));
The MIT License (MIT) Copyright (c) 2012 Brett Langdon brett@blangdon.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.