/
index.js
66 lines (44 loc) · 1.09 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const nodelua = require('../../index');
var lua = new nodelua.LuaState();
/**
* On enregistre une nouvelle fonction utilisable dans lua
*/
lua.RegisterFunction('add', function() {
var a = lua.ToValue(1);
var b = lua.ToValue(2);
lua.Pop(2);
lua.Push(a + b);
return 1;
});
lua.DoString("print('Result in Lua: ' .. add(10, 5))");
lua.DoFile(__dirname + "/test.lua");
/**
* Set / Get of a global lua variable
*/
lua.Push(5);
lua.SetGlobal('myVar');
lua.GetGlobal('myVar');
var myVar = lua.ToValue(-1);
console.log(myVar);
console.log("Lua.GetTop: " + lua.GetTop());
// Don't forget to pop the values so that your stack stays clean
lua.Pop();
console.log("Lua.GetTop: " + lua.GetTop());
/**
* Access object data
*/
lua.DoString("a = {}; a.t = 42;");
lua.GetField(nodelua.LUA.GLOBALSINDEX, "a"); // the same as lua.GetGlobal("a");
lua.GetField(-1, "t");
var t = lua.ToValue(-1);
lua.Pop(2);
console.log(t);
/**
* Function call
*/
lua.DoString("function test(a, b) print('hello ' .. a .. ', hello' .. b) end");
lua.GetGlobal("test");
lua.Push("1");
lua.Push("2");
lua.Call(2, 0);
lua.Pop(2);