Skip to content
This repository
tree: 98dcd19cea
Fetching contributors…

Cannot retrieve contributors at this time

file 331 lines (285 sloc) 7.175 kb
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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
module luad.table;

import luad.c.all;

import luad.base;
import luad.stack;
import luad.conversions.structs;

/// Represents a Lua table.
struct LuaTable
{
/// LuaTable sub-types LuaObject through this reference.
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) @trusted
{
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;
}

/**
* Read a string value in this table without making a copy of the string.
* The read string is passed to dg, and should not be escaped.
* If the value for key is not a string, dg is not called.
* Params:
* key = lookup _key
* dg = delegate to receive string
* Returns:
* true if the value for key was a string and passed to dg, false otherwise
* Examples:
--------------------
t[2] = "two";
t.readString(2, str => assert(str == "two"));
--------------------
*/
bool readString(T)(T key, scope void delegate(in char[] str) dg) @trusted
{
this.push();
scope(exit) lua_pop(this.state, 1);

pushValue(this.state, key);

lua_gettable(this.state, -2);
scope(exit) lua_pop(this.state, 1);

size_t len;
const(char)* cstr = lua_tolstring(this.state, -1, &len);
if(cstr is null)
return false;

dg(cstr[0 .. len]);
return true;
}

/**
* 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);
}

/**
* Set a key-value pair in this table.
* Params:
* key = key to _set
* value = value of key
*/
void set(T, U)(T key, U value) @trusted
{
this.push();
scope(success) lua_pop(this.state, 1);

pushValue(this.state, key);
pushValue(this.state, value);
lua_settable(this.state, -3);
}

/**
* Set 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"] = (in char[] s){ return s.length == 0; };
lua.doString(`assert(string.empty(""))`);
* ----------------------
*/
void opIndexAssign(T, U...)(T value, U args) @trusted
{
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)() @trusted if (is(T == struct))
{
push();
return popValue!T(this.state);
}

/**
* Fill a struct's members with fields from this table.
* Params:
* s = struct to fill
*/
void copyTo(T)(ref T s) @trusted if (is(T == struct))
{
push();
fillStruct(this.state, -1, s);
lua_pop(L, 1);
}

/**
* Set the metatable for this table.
* Params:
* meta = new metatable
*/
void setMetaTable(ref LuaTable meta) @trusted
in{ assert(this.state == meta.state); }
body
{
this.push();
meta.push();
lua_setmetatable(this.state, -2);
lua_pop(this.state, 1);
}

/**
* Get the metatable for this table.
* Returns:
* A reference to the metatable for this table. The reference is nil if this table has no metatable.
*/
LuaTable getMetaTable() @trusted
{
this.push();
scope(success) lua_pop(this.state, 1);

return lua_getmetatable(this.state, -1) == 0? LuaTable() : popValue!LuaTable(this.state);
}

/**
* Iterate over the values in this table.
*/
int opApply(T)(int delegate(ref T value) dg) @trusted
{
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;
}

/**
* Iterate over the key-value pairs in this table.
*/
int opApply(T, U)(int delegate(ref U key, ref T value) dg) @trusted
{
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!");

// readString
t[2] = "two";
bool success = t.readString(2, (in char[] str) {
assert(str == "two");
});
assert(success);

t[2] = true;
success = t.readString(2, (in char[] str) { assert(false); });
assert(!success);

// metatable
pushValue(L, ["__index": (LuaObject self, string key){
return key;
}]);
auto meta = popValue!LuaTable(L);

lua_newtable(L);
auto t2 = popValue!LuaTable(L);

t2.setMetaTable(meta);

auto test = t2.get!string("foobar");
assert(test == "foobar");

assert(t2.getMetaTable() == 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);
}
}
Something went wrong with that request. Please try again.