Skip to content

Commit 7650761

Browse files
committed
tests: add a metatable test
1 parent d0d3a95 commit 7650761

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

src/tests.zig

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,6 +1228,28 @@ test "get global fail" {
12281228
try expectError(Error.Fail, lua.getGlobal("foo"));
12291229
}
12301230

1231+
test "metatables" {
1232+
var lua = try Lua.init(testing.allocator);
1233+
defer lua.deinit();
1234+
1235+
try lua.doString("f = function() return 10 end");
1236+
1237+
try lua.newMetatable("mt");
1238+
_ = lua.getMetatableAux("mt");
1239+
try expect(lua.compare(1, 2, .eq));
1240+
lua.pop(1);
1241+
1242+
// set the len metamethod to the function f
1243+
try lua.getGlobal("f");
1244+
lua.setField(1, "__len");
1245+
1246+
lua.newTable();
1247+
lua.setMetatableAux("mt");
1248+
1249+
try lua.callMeta(-1, "__len");
1250+
try expectEqual(@as(Number, 10), try lua.toNumber(-1));
1251+
}
1252+
12311253
test "refs" {
12321254
// temporary test that includes a reference to all functions so
12331255
// they will be type-checked
@@ -1236,29 +1258,25 @@ test "refs" {
12361258
_ = Lua.argCheck;
12371259
_ = Lua.argError;
12381260
_ = Lua.argExpected;
1239-
_ = Lua.callMeta;
12401261
_ = Lua.checkOption;
12411262
_ = Lua.checkUserdata;
12421263
_ = Lua.checkVersion;
12431264
_ = Lua.doFile;
12441265
_ = Lua.raiseErrorAux;
12451266
_ = Lua.exeResult;
12461267
_ = Lua.fileResult;
1247-
_ = Lua.getMetatableAux;
12481268
_ = Lua.getSubtable;
12491269
_ = Lua.gSub;
12501270
_ = Lua.loadBuffer;
12511271
_ = Lua.loadBufferX;
12521272
_ = Lua.loadFile;
12531273
_ = Lua.loadFileX;
1254-
_ = Lua.newMetatable;
12551274
_ = Lua.optInteger;
12561275
_ = Lua.optLString;
12571276
_ = Lua.optNumber;
12581277
_ = Lua.optString;
12591278
_ = Lua.pushFail;
12601279
_ = Lua.requireF;
1261-
_ = Lua.setMetatableAux;
12621280
_ = Lua.testUserdata;
12631281
_ = Lua.toLStringAux;
12641282
_ = Lua.traceback;

src/ziglua.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,8 +1314,8 @@ pub const Lua = struct {
13141314
}
13151315

13161316
/// Calls a metamethod
1317-
pub fn callMeta(lua: *Lua, obj: i32, field: [:0]const u8) bool {
1318-
return c.luaL_callmeta(lua.state, obj, field) != 0;
1317+
pub fn callMeta(lua: *Lua, obj: i32, field: [:0]const u8) !void {
1318+
if (c.luaL_callmeta(lua.state, obj, field) == 0) return Error.Fail;
13191319
}
13201320

13211321
/// Checks whether the function has an argument of any type at position `arg`
@@ -1423,6 +1423,7 @@ pub const Lua = struct {
14231423

14241424
/// Pushes onto the stack the metatable associated with the name `type_name` in the registry
14251425
/// or nil if there is no metatable associated with that name. Returns the type of the pushed value
1426+
/// TODO: return error when type is nil?
14261427
pub fn getMetatableAux(lua: *Lua, type_name: [:0]const u8) LuaType {
14271428
return @intToEnum(LuaType, c.luaL_getmetatable(lua.state, type_name));
14281429
}
@@ -1520,7 +1521,7 @@ pub const Lua = struct {
15201521
lua.createTable(0, @intCast(i32, list.len));
15211522
}
15221523

1523-
/// If the registry already has the key `key`, returns 0
1524+
/// If the registry already has the key `key`, returns an error
15241525
/// Otherwise, creates a new table to be used as a metatable for userdata
15251526
pub fn newMetatable(lua: *Lua, key: [:0]const u8) !void {
15261527
if (c.luaL_newmetatable(lua.state, key) == 0) return Error.Fail;

0 commit comments

Comments
 (0)