Skip to content

Commit 3157d5c

Browse files
committed
tests: add a table access test
Exercises various table functions in the API
1 parent 09193ca commit 3157d5c

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

src/ziglua.zig

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,7 @@ pub const Lua = struct {
452452

453453
/// Pushes onto the stack the value t[`i`] where t is the value at the given `index`
454454
/// Returns the type of the pushed value
455-
/// TODO: rename getIndex
456-
pub fn getI(lua: *Lua, index: i32, i: Integer) LuaType {
455+
pub fn getIndex(lua: *Lua, index: i32, i: Integer) LuaType {
457456
return @intToEnum(LuaType, c.lua_geti(lua.state, index, i));
458457
}
459458

@@ -463,10 +462,10 @@ pub const Lua = struct {
463462
return @intToEnum(LuaType, c.lua_getiuservalue(lua.state, index, n));
464463
}
465464

466-
/// If the value at the given `index` has a metatable, the function pushes that metatable onto the stack and returns true
467-
/// Otherwise false is returned
468-
pub fn getMetatable(lua: *Lua, index: i32) bool {
469-
return c.lua_getmetatable(lua.state, index) != 0;
465+
/// If the value at the given `index` has a metatable, the function pushes that metatable onto the stack
466+
/// Otherwise an error is returned
467+
pub fn getMetatable(lua: *Lua, index: i32) !void {
468+
if (c.lua_getmetatable(lua.state, index) == 0) return Error.Fail;
470469
}
471470

472471
/// Pushes onto the stack the value t[k] where t is the value at the given `index` and k is the value on the top of the stack
@@ -681,7 +680,7 @@ pub const Lua = struct {
681680
// lua_pushglobaltable is a macro and c-translate assumes it returns opaque
682681
// so just reimplement the macro here
683682
// c.lua_pushglobaltable(lua.state);
684-
_ = lua.rawGetI(registry_index, ridx_globals);
683+
_ = lua.rawGetIndex(registry_index, ridx_globals);
685684
}
686685

687686
/// Pushes an integer with value `n` onto the stack
@@ -718,6 +717,7 @@ pub const Lua = struct {
718717
/// Lua makes a copy of the string so `str` may be freed immediately after return
719718
/// Returns a pointer to the internal Lua string
720719
/// If `str` is null pushes nil and returns null
720+
/// TODO: is it useful to return null?
721721
pub fn pushString(lua: *Lua, str: ?[*:0]const u8) ?[*]const u8 {
722722
const ptr = c.lua_pushstring(lua.state, str);
723723
return @ptrCast(?[*]const u8, ptr);
@@ -744,13 +744,13 @@ pub const Lua = struct {
744744
}
745745

746746
/// Similar to `Lua.getTable()` but does a raw access (without metamethods)
747-
pub fn rawGet(lua: *Lua, index: i32) LuaType {
747+
pub fn rawGetTable(lua: *Lua, index: i32) LuaType {
748748
return @intToEnum(LuaType, c.lua_rawget(lua.state, index));
749749
}
750750

751751
/// Pushes onto the stack the value t[n], where `t` is the table at the given `index`
752752
/// Returns the `LuaType` of the pushed value
753-
pub fn rawGetI(lua: *Lua, index: i32, n: Integer) LuaType {
753+
pub fn rawGetIndex(lua: *Lua, index: i32, n: Integer) LuaType {
754754
return @intToEnum(LuaType, c.lua_rawgeti(lua.state, index, n));
755755
}
756756

@@ -2004,6 +2004,7 @@ test "type of" {
20042004
try expect(lua.isTable(2));
20052005
try expect(lua.isNumber(3));
20062006
try expect(lua.isLightUserdata(4));
2007+
try expect(lua.isUserdata(4));
20072008
try expect(lua.isNil(5));
20082009
try expect(lua.isNumber(6));
20092010
try expect(lua.isThread(7));
@@ -2213,7 +2214,6 @@ test "global table" {
22132214

22142215
// open some libs so we can inspect them
22152216
lua.open(.{ .math = true, .base = true });
2216-
lua.openMath();
22172217
lua.pushGlobalTable();
22182218

22192219
// find the print function
@@ -2325,6 +2325,37 @@ test "extra space" {
23252325
try expectEqual(@as(usize, 1024), @ptrCast(*align(1) usize, thread.getExtraSpace()).*);
23262326
}
23272327

2328+
test "table access" {
2329+
var lua = try Lua.init(testing.allocator);
2330+
defer lua.deinit();
2331+
2332+
try lua.doString("a = { [1] = 'first', key = 'value', ['other one'] = 1234 }");
2333+
_ = lua.getGlobal("a");
2334+
2335+
try expectEqual(LuaType.string, lua.getIndex(1, 1));
2336+
try expectEqualStrings("first", lua.toString(-1).?);
2337+
2338+
try expectEqual(LuaType.string, lua.rawGetIndex(1, 1));
2339+
try expectEqualStrings("first", lua.toString(-1).?);
2340+
2341+
_ = lua.pushString("key");
2342+
try expectEqual(LuaType.string, lua.getTable(1));
2343+
try expectEqualStrings("value", lua.toString(-1).?);
2344+
2345+
_ = lua.pushString("other one");
2346+
try expectEqual(LuaType.number, lua.rawGetTable(1));
2347+
try expectEqual(@as(Integer, 1234), lua.toInteger(-1));
2348+
2349+
try expectError(Error.Fail, lua.getMetatable(1));
2350+
2351+
lua.pushBoolean(true);
2352+
lua.setField(1, "bool");
2353+
2354+
try lua.doString("b = a.bool");
2355+
try expectEqual(LuaType.boolean, lua.getGlobal("b"));
2356+
try expect(lua.toBoolean(-1));
2357+
}
2358+
23282359
test "refs" {
23292360
// temporary test that includes a reference to all functions so
23302361
// they will be type-checked
@@ -2335,33 +2366,27 @@ test "refs" {
23352366
_ = Lua.createTable;
23362367
_ = Lua.dump;
23372368
_ = Lua.raiseError;
2338-
_ = Lua.getI;
23392369
_ = Lua.getIUserValue;
2340-
_ = Lua.getMetatable;
2341-
_ = Lua.isUserdata;
23422370
_ = Lua.isYieldable;
23432371
_ = Lua.load;
23442372
_ = Lua.newThread;
23452373
_ = Lua.newUserdataUV;
23462374
_ = Lua.next;
23472375
_ = Lua.numberToInteger;
23482376
_ = Lua.rawEqual;
2349-
_ = Lua.rawGet;
23502377
_ = Lua.rawGetP;
23512378
_ = Lua.rawLen;
23522379
_ = Lua.rawSet;
23532380
_ = Lua.rawSetI;
23542381
_ = Lua.rawSetP;
23552382
_ = Lua.resetThread;
2356-
_ = Lua.setField;
23572383
_ = Lua.setI;
23582384
_ = Lua.setIUserValue;
23592385
_ = Lua.setMetatable;
23602386
_ = Lua.setTable;
23612387
_ = Lua.setWarnF;
23622388
_ = Lua.status;
23632389
_ = Lua.stringToNumber;
2364-
_ = Lua.toBoolean;
23652390
_ = Lua.toCFunction;
23662391
_ = Lua.toClose;
23672392
_ = Lua.toIntegerX;

0 commit comments

Comments
 (0)