|
|
13 years ago | |
|---|---|---|
| example | 13 years ago | |
| lib | 13 years ago | |
| src | 13 years ago | |
| .gitignore | 13 years ago | |
| README.md | 13 years ago | |
| binding.gyp | 13 years ago | |
| package.json | 13 years ago | |
NodeLua is a module to expose Lua bindings to Node.JS.
This is still a work in progress, collaborators welcome.
Lua and it's C libraries are required for this module to work.
npm install nodelua
var nodelua = require('nodelua');
The NodeLua module itself only contains a single Object LuaObject.
var lua = new nodelua.LuaObject()
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 javascript function func to lua with the name name.
lua.registerFunction('add_them', function(a, b){
console.log(a+b);
});
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)
As well, there are problems with using registerFunction with multiple LuaObjects, you will probably end up with a Segmentation fault: 11 error when running the code. I am working on this issue.
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 test/test.js
var nodelua = require('nodelua');
var lua = new nodelua.LuaObject();
lua.registerFunction('add_them', function(a, b){
console.dir(a+b);
});
lua.doFile('some_file.lua');
console.dir(lua.getGlobal('some_var'));
lua.close()
LuaObjects, this is my next task.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.