@@ -452,8 +452,7 @@ pub const Lua = struct {
452
452
453
453
/// Pushes onto the stack the value t[`i`] where t is the value at the given `index`
454
454
/// 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 {
457
456
return @intToEnum (LuaType , c .lua_geti (lua .state , index , i ));
458
457
}
459
458
@@ -463,10 +462,10 @@ pub const Lua = struct {
463
462
return @intToEnum (LuaType , c .lua_getiuservalue (lua .state , index , n ));
464
463
}
465
464
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 ;
470
469
}
471
470
472
471
/// 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 {
681
680
// lua_pushglobaltable is a macro and c-translate assumes it returns opaque
682
681
// so just reimplement the macro here
683
682
// c.lua_pushglobaltable(lua.state);
684
- _ = lua .rawGetI (registry_index , ridx_globals );
683
+ _ = lua .rawGetIndex (registry_index , ridx_globals );
685
684
}
686
685
687
686
/// Pushes an integer with value `n` onto the stack
@@ -718,6 +717,7 @@ pub const Lua = struct {
718
717
/// Lua makes a copy of the string so `str` may be freed immediately after return
719
718
/// Returns a pointer to the internal Lua string
720
719
/// If `str` is null pushes nil and returns null
720
+ /// TODO: is it useful to return null?
721
721
pub fn pushString (lua : * Lua , str : ? [* :0 ]const u8 ) ? [* ]const u8 {
722
722
const ptr = c .lua_pushstring (lua .state , str );
723
723
return @ptrCast (? [* ]const u8 , ptr );
@@ -744,13 +744,13 @@ pub const Lua = struct {
744
744
}
745
745
746
746
/// 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 {
748
748
return @intToEnum (LuaType , c .lua_rawget (lua .state , index ));
749
749
}
750
750
751
751
/// Pushes onto the stack the value t[n], where `t` is the table at the given `index`
752
752
/// 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 {
754
754
return @intToEnum (LuaType , c .lua_rawgeti (lua .state , index , n ));
755
755
}
756
756
@@ -2004,6 +2004,7 @@ test "type of" {
2004
2004
try expect (lua .isTable (2 ));
2005
2005
try expect (lua .isNumber (3 ));
2006
2006
try expect (lua .isLightUserdata (4 ));
2007
+ try expect (lua .isUserdata (4 ));
2007
2008
try expect (lua .isNil (5 ));
2008
2009
try expect (lua .isNumber (6 ));
2009
2010
try expect (lua .isThread (7 ));
@@ -2213,7 +2214,6 @@ test "global table" {
2213
2214
2214
2215
// open some libs so we can inspect them
2215
2216
lua .open (.{ .math = true , .base = true });
2216
- lua .openMath ();
2217
2217
lua .pushGlobalTable ();
2218
2218
2219
2219
// find the print function
@@ -2325,6 +2325,37 @@ test "extra space" {
2325
2325
try expectEqual (@as (usize , 1024 ), @ptrCast (* align (1 ) usize , thread .getExtraSpace ()).* );
2326
2326
}
2327
2327
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
+
2328
2359
test "refs" {
2329
2360
// temporary test that includes a reference to all functions so
2330
2361
// they will be type-checked
@@ -2335,33 +2366,27 @@ test "refs" {
2335
2366
_ = Lua .createTable ;
2336
2367
_ = Lua .dump ;
2337
2368
_ = Lua .raiseError ;
2338
- _ = Lua .getI ;
2339
2369
_ = Lua .getIUserValue ;
2340
- _ = Lua .getMetatable ;
2341
- _ = Lua .isUserdata ;
2342
2370
_ = Lua .isYieldable ;
2343
2371
_ = Lua .load ;
2344
2372
_ = Lua .newThread ;
2345
2373
_ = Lua .newUserdataUV ;
2346
2374
_ = Lua .next ;
2347
2375
_ = Lua .numberToInteger ;
2348
2376
_ = Lua .rawEqual ;
2349
- _ = Lua .rawGet ;
2350
2377
_ = Lua .rawGetP ;
2351
2378
_ = Lua .rawLen ;
2352
2379
_ = Lua .rawSet ;
2353
2380
_ = Lua .rawSetI ;
2354
2381
_ = Lua .rawSetP ;
2355
2382
_ = Lua .resetThread ;
2356
- _ = Lua .setField ;
2357
2383
_ = Lua .setI ;
2358
2384
_ = Lua .setIUserValue ;
2359
2385
_ = Lua .setMetatable ;
2360
2386
_ = Lua .setTable ;
2361
2387
_ = Lua .setWarnF ;
2362
2388
_ = Lua .status ;
2363
2389
_ = Lua .stringToNumber ;
2364
- _ = Lua .toBoolean ;
2365
2390
_ = Lua .toCFunction ;
2366
2391
_ = Lua .toClose ;
2367
2392
_ = Lua .toIntegerX ;
0 commit comments