@@ -769,7 +769,7 @@ pub const Lua = struct {
769
769
}
770
770
771
771
/// Similar to `Lua.setTable()` but does a raw assignment (without metamethods)
772
- pub fn rawSet (lua : * Lua , index : i32 ) void {
772
+ pub fn rawSetTable (lua : * Lua , index : i32 ) void {
773
773
c .lua_rawset (lua .state , index );
774
774
}
775
775
@@ -1242,8 +1242,10 @@ pub const Lua = struct {
1242
1242
/// Pushes onto the stack the field `e` from the metatable of the object at index `obj`
1243
1243
/// and returns the type of the pushed value
1244
1244
/// TODO: possibly return an error if nil
1245
- pub fn getMetaField (lua : * Lua , obj : i32 , field : [:0 ]const u8 ) LuaType {
1246
- return @intToEnum (LuaType , c .luaL_getmetafield (lua .state , obj , field ));
1245
+ pub fn getMetaField (lua : * Lua , obj : i32 , field : [:0 ]const u8 ) ! LuaType {
1246
+ const val_type = @intToEnum (LuaType , c .luaL_getmetafield (lua .state , obj , field ));
1247
+ if (val_type == .nil ) return Error .Fail ;
1248
+ return val_type ;
1247
1249
}
1248
1250
1249
1251
/// Pushes onto the stack the metatable associated with the name `type_name` in the registry
@@ -2382,8 +2384,28 @@ test "table access" {
2382
2384
try expectEqual (LuaType .number , lua .rawGetTable (1 ));
2383
2385
try expectEqual (@as (Integer , 1234 ), lua .toInteger (-1 ));
2384
2386
2387
+ // a.name = "ziglua"
2388
+ _ = lua .pushString ("name" );
2389
+ _ = lua .pushString ("ziglua" );
2390
+ lua .setTable (1 );
2391
+
2392
+ // a.lang = "zig"
2393
+ _ = lua .pushString ("lang" );
2394
+ _ = lua .pushString ("zig" );
2395
+ lua .rawSetTable (1 );
2396
+
2385
2397
try expectError (Error .Fail , lua .getMetatable (1 ));
2386
2398
2399
+ // create a metatable (it isn't a useful one)
2400
+ lua .newTable ();
2401
+ lua .pushCFunction (wrap (add ));
2402
+ lua .setField (-2 , "__len" );
2403
+ lua .setMetatable (1 );
2404
+
2405
+ try lua .getMetatable (1 );
2406
+ _ = try lua .getMetaField (1 , "__len" );
2407
+ try expectError (Error .Fail , lua .getMetaField (1 , "__index" ));
2408
+
2387
2409
lua .pushBoolean (true );
2388
2410
lua .setField (1 , "bool" );
2389
2411
@@ -2499,11 +2521,8 @@ test "refs" {
2499
2521
_ = Lua .newUserdataUV ;
2500
2522
_ = Lua .next ;
2501
2523
_ = Lua .rawGetP ;
2502
- _ = Lua .rawSet ;
2503
2524
_ = Lua .rawSetP ;
2504
2525
_ = Lua .setIUserValue ;
2505
- _ = Lua .setMetatable ;
2506
- _ = Lua .setTable ;
2507
2526
_ = Lua .toClose ;
2508
2527
_ = Lua .toPointer ;
2509
2528
_ = Lua .toUserdata ;
@@ -2544,7 +2563,6 @@ test "refs" {
2544
2563
_ = Lua .raiseErrorAux ;
2545
2564
_ = Lua .exeResult ;
2546
2565
_ = Lua .fileResult ;
2547
- _ = Lua .getMetaField ;
2548
2566
_ = Lua .getMetatableAux ;
2549
2567
_ = Lua .getSubtable ;
2550
2568
_ = Lua .gSub ;
0 commit comments