/
LuaTableTest.java
57 lines (48 loc) · 1.12 KB
/
LuaTableTest.java
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
package com.dafrito.lua.script;
import static org.junit.Assert.assertEquals;
import lua.LuaLibrary;
import lua.LuaLibrary.lua_State;
import org.junit.Before;
import org.junit.Test;
public class LuaTableTest {
LuaLibrary lua = LuaLibrary.INSTANCE;
private LuaBindings b;
private LuaScriptContext ctx;
@Test
public void getAndPutAValueIntoATable() throws Exception {
lua.lua_createtable(b.getState(), 0, 0);
LuaTable t = new LuaTable(new LuaReference(b));
t.set(1, "No time");
assertEquals("No time", t.get(1));
}
@Before
public void setup() {
ctx = new LuaScriptContext();
b = ctx.getGlobals();
}
class LuaTable {
private LuaBindings b;
private LuaReference ref;
private lua_State s;
public LuaTable(LuaReference ref) {
this.b = ref.getBindings();
this.ref = ref;
this.s = b.getState();
}
public Object get(Object k) {
ref.get();
b.toLua(k);
lua.lua_gettable(s, -2);
Object v = b.fromLua(-1);
lua.lua_settop(s, -2);
return v;
}
public void set(Object k, Object v) {
ref.get();
b.toLua(k);
b.toLua(v);
lua.lua_settable(s, -3);
lua.lua_settop(s, -2);
}
}
}