Browse Source

update examples

v0.2.x
Brett Langdon 13 years ago
parent
commit
27dbd7fbc5
5 changed files 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

+ 7
- 7
examples/config/index.js View File

@ -1,7 +1,7 @@
var path = require('path'); var path = require('path');
var nodelua = require('../../'); var nodelua = require('../../');
var lua = new nodelua.LuaObject();
var lua = new nodelua.LuaState('config');
// set a default value // set a default value
lua.setGlobal('js_value', 'this is js_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'); console.log('Processing Config.lua');
var config = path.resolve(__dirname, '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'));
});

+ 14
- 0
examples/files/index.js View File

@ -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);
}
});

+ 1
- 0
examples/files/test.lua View File

@ -0,0 +1 @@
return "lua:" .. test

+ 6
- 15
examples/functions/index.js View File

@ -1,22 +1,13 @@
var nodelua = require('../../'); 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; 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)");

+ 6
- 6
examples/simple/index.js View File

@ -1,11 +1,11 @@
var nodelua = require('../../'); var nodelua = require('../../');
var lua = new nodelua.LuaObject();
var lua = new nodelua.LuaState('simple');
lua.setGlobal('js_value', 500); 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'));
});
});

Loading…
Cancel
Save