Skip to content
Browse files

update examples

  • Loading branch information...
1 parent 449e61a commit 27dbd7fbc5eabcf81626c393f946bb515afa35df @brettlangdon committed
Showing with 34 additions and 28 deletions.
  1. +7 −7 examples/config/index.js
  2. +14 −0 examples/files/index.js
  3. +1 −0 examples/files/test.lua
  4. +6 −15 examples/functions/index.js
  5. +6 −6 examples/simple/index.js
View
14 examples/config/index.js
@@ -1,7 +1,7 @@
var path = require('path');
var nodelua = require('../../');
-var lua = new nodelua.LuaObject();
+var lua = new nodelua.LuaState('config');
// set a default value
lua.setGlobal('js_value', 'this is js_value');
@@ -9,9 +9,9 @@ console.log('js_value: ' + lua.getGlobal('js_value'));
console.log('Processing Config.lua');
var config = path.resolve(__dirname, 'config.lua');
-lua.doFile(config);
-
-console.log('js_value: ' + lua.getGlobal('js_value'));
-console.log('val_one: ' + lua.getGlobal('val_one'));
-console.log('val_two: ' + lua.getGlobal('val_two'));
-console.log('val_three: ' + lua.getGlobal('val_three'));
+lua.doFile(config, function(){
+ console.log('js_value: ' + lua.getGlobal('js_value'));
+ console.log('val_one: ' + lua.getGlobal('val_one'));
+ console.log('val_two: ' + lua.getGlobal('val_two'));
+ console.log('val_three: ' + lua.getGlobal('val_three'));
+ });
View
14 examples/files/index.js
@@ -0,0 +1,14 @@
+var nodelua = require('../../');
+
+var luastate = new nodelua.LuaState('files');
+
+luastate.setGlobal('test', 'some value');
+
+var file_name = __dirname + '/test.lua';
+luastate.doFile(file_name, function(err, ret){
+ if(!err && ret){
+ console.log(ret);
+ } else{
+ console.error(err);
+ }
+ });
View
1 examples/files/test.lua
@@ -0,0 +1 @@
+return "lua:" .. test
View
21 examples/functions/index.js
@@ -1,22 +1,13 @@
var nodelua = require('../../');
-var lua = new nodelua.LuaObject();
+var lua = new nodelua.LuaState('functions');
-var add_them = new nodelua.LuaFunction('add_them', function(a, b){
- console.log('Adding ' + a + ' and ' + b + ' in js');
+lua.registerFunction('add', function(a, b){
return a + b;
});
-lua.registerFunction(add_them);
-// Functionas are registered globally
-// for all LuaObjects
-var lua_two = new nodelua.LuaObject();
-lua_two.doString("print('Result in Lua: ' .. nodelua('add_them', 10, 5))");
+lua.doStringSync("print('Result in Lua: ' .. add(10, 5))");
-var subtract_them = new nodelua.LuaFunction('subtract_them', function(a, b){
- console.log('Subtracting ' + a + ' and ' + b + ' in js');
- return a - b;
- });
-
-lua_two.registerFunction(subtract_them);
-lua.doString("print('Result in Lua: ' .. nodelua('subtract_them', 10, 5))");
+var lua_two = new nodelua.LuaState('two');
+// this will throw an error
+lua_two.doStringSync("add(10, 5)");
View
12 examples/simple/index.js
@@ -1,11 +1,11 @@
var nodelua = require('../../');
-var lua = new nodelua.LuaObject();
+var lua = new nodelua.LuaState('simple');
lua.setGlobal('js_value', 500);
-lua.doString('print(js_value)');
-
-lua.doString('js_value = "something new"');
-
-console.dir(lua.getGlobal('js_value'));
+lua.doString('print("js_value: " .. js_value)', function(){
+ lua.doString('js_value = "something new"', function(){
+ console.log('js_value: ' + lua.getGlobal('js_value'));
+ });
+ });

0 comments on commit 27dbd7f

Please sign in to comment.
Something went wrong with that request. Please try again.