/
table.d
288 lines (248 loc) · 5.81 KB
/
table.d
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
module luad.table;
import luad.c.all;
import luad.base;
import luad.reference;
import luad.stack;
import luad.conversions.structs;
/// Represents a Lua table.
struct LuaTable
{
LuaObject object;
alias object this;
package this(lua_State* L, int idx)
{
LuaObject.checkType(L, idx, LUA_TTABLE, "LuaTable");
object = LuaObject(L, idx);
}
/**
* Lookup a value in this table or in a sub-table of this table.
* Params:
* T = type of value
* args = list of keys, where all keys but the last one should result in a table
* Returns:
* t[k] where t is the table for the second-to-last parameter, and k is the last parameter
*
* Examples:
* ----------------------
auto execute = lua.get!LuaFunction("os", "execute");
execute(`echo hello, world!`);
* ----------------------
*/
T get(T, U...)(U args)
{
this.push();
foreach(key; args)
{
pushValue(this.state, key);
lua_gettable(this.state, -2);
}
auto ret = getValue!T(this.state, -1);
lua_pop(this.state, args.length + 1);
return ret;
}
/**
* Same as calling get!LuaObject with the same arguments.
* Examples:
* ---------------------
auto luapath = lua["package", "path"];
writefln("LUA_PATH:\n%s", luapath);
* ---------------------
* See_Also:
* get
*/
LuaObject opIndex(T...)(T args)
{
return get!LuaObject(args);
}
/**
* Sets a key-value pair in this table.
* Params:
* key = key to set
* value = value of key
*/
void set(T, U)(T key, U value)
{
this.push();
scope(success) lua_pop(this.state, 1);
pushValue(this.state, key);
pushValue(this.state, value);
lua_settable(this.state, -3);
}
/**
* Sets a key-value pair this table or in a sub-table of this table.
* Params:
* value = value to set
* args = list of keys, where all keys but the last one should result in a table
* Returns:
* t[k] = value, where t is the table for the second-to-last parameter in args,
* and k is the last parameter in args
*
* Examples:
* ----------------------
lua["string", "empty"] = (string s){ return s.length == 0; };
lua.doString(`assert(string.empty(""))`);
* ----------------------
*/
void opIndexAssign(T, U...)(T value, U args)
{
this.push();
scope(success) lua_pop(this.state, 1);
foreach(i, arg; args)
{
static if(i != args.length - 1)
{
pushValue(this.state, arg);
lua_gettable(this.state, -2);
}
}
pushValue(this.state, args[$-1]);
pushValue(this.state, value);
lua_settable(this.state, -3);
lua_pop(this.state, args.length - 1);
}
/**
* Create struct of type T and fill its members with fields from this table.
*
* Struct fields that are not present in this table are left at their default value.
*
* Params:
* T = any struct type
*
* Returns:
* Newly created struct
*/
T toStruct(T)() if (is(T == struct))
{
push();
return popValue!T(this.state);
}
/**
* Fills a struct's members with fields from this table.
* Params:
* s = struct to fill
*/
void copyTo(T)(ref T s) if (is(T == struct))
{
push();
fillStruct(this.state, -1, s);
lua_pop(L, 1);
}
/**
* Sets the metatable for this table.
* Params:
* meta = new metatable
*/
void setMetaTable(ref LuaTable meta)
in{ assert(this.state == meta.state); }
body
{
this.push();
meta.push();
lua_setmetatable(this.state, -2);
lua_pop(this.state, 1);
}
/**
* Gets the metatable for this table.
* Returns:
* A reference to the metatable for this table, or null if this table has no metatable.
*/
LuaTable getMetaTable()
{
this.push();
scope(success) lua_pop(this.state, 1);
return lua_getmetatable(this.state, -1) == 0? LuaTable() : popValue!LuaTable(this.state);
}
/**
* Iterates over the values in this table.
*/
int opApply(T)(int delegate(ref T value) dg)
{
this.push();
lua_pushnil(this.state);
while(lua_next(this.state, -2) != 0)
{
auto value = popValue!T(this.state);
int result = dg(value);
if(result != 0)
{
lua_pop(this.state, 2);
return result;
}
}
lua_pop(this.state, 1);
return 0;
}
/**
* Iterates over the key-value pairs in this table.
*/
int opApply(T, U)(int delegate(ref U key, ref T value) dg)
{
this.push();
lua_pushnil(this.state);
while(lua_next(this.state, -2) != 0)
{
auto value = popValue!T(this.state);
auto key = getValue!U(this.state, -1);
int result = dg(key, value);
if(result != 0)
{
lua_pop(this.state, 2);
return result;
}
}
lua_pop(this.state, 1);
return 0;
}
}
unittest
{
lua_State* L = luaL_newstate();
scope(success)
{
assert(lua_gettop(L) == 0);
lua_close(L);
}
lua_newtable(L);
auto t = popValue!LuaTable(L);
assert(t.type == LuaType.Table);
t.set("foo", "bar");
assert(t.get!string("foo") == "bar");
t.set("foo", nil);
assert(t.get!LuaObject("foo").isNil);
t.set("foo", ["outer": ["inner": "hi!"]]);
auto s = t.get!(string)("foo", "outer", "inner");
assert(s == "hi!");
auto o = t["foo", "outer"];
assert(o.type == LuaType.Table);
t["foo", "outer", "inner"] = "hello!";
auto s2 = t.get!(string)("foo", "outer", "inner");
assert(s2 == "hello!");
//metatable
pushValue(L, ["__index": (LuaObject self, string key){
return key;
}]);
auto meta = popValue!LuaTable(L);
lua_newtable(L);
auto t2 = new LuaTable(L, -1);
lua_pop(L, 1);
t2.setMetaTable(meta);
auto test = t2.get!string("foobar");
assert(test == "foobar");
assert(t2.getMetaTable().equals(meta));
// opApply
auto input = [1, 2, 3];
pushValue(L, input);
auto applyTest = popValue!LuaTable(L);
int i = 0;
foreach(int v; applyTest)
{
assert(input[i++] == v);
}
auto inputWithKeys = ["one": 1, "two": 2, "three": 3];
pushValue(L, inputWithKeys);
auto applyTestKeys = popValue!LuaTable(L);
foreach(string key, int value; applyTestKeys)
{
assert(inputWithKeys[key] == value);
}
}