@@ -851,6 +851,53 @@ pub const Lua = opaque {
851851 return 1 == c .lua_getmetatable (asState (lua ), index );
852852 }
853853
854+ /// Pushes onto the stack the value `t[k]`, where `t` is the value at the given valid index. As in Lua, this function
855+ /// may trigger a metamethod for the "index" event (see https://www.lua.org/manual/5.1/manual.html#2.8).
856+ ///
857+ /// From: `void lua_getfield(lua_State *L, int index, const char *k);`
858+ /// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_getfield
859+ /// Stack Behavior: `[-0, +1, e]`
860+ pub fn getField (lua : * Lua , index : i32 , key : [:0 ]const u8 ) Lua.Type {
861+ lua .validateStackIndex (index );
862+
863+ c .lua_getfield (asState (lua ), index , @ptrCast (key .ptr ));
864+ return lua .typeOf (-1 );
865+ }
866+
867+ /// Does the equivalent to `t[k] = v`, where `t` is the value at the given valid index and `v` is the value at the
868+ /// top of the stack. This function pops the value from the stack. As in Lua, this function may trigger a
869+ /// metamethod for the "newindex" event (see https://www.lua.org/manual/5.1/manual.html#2.8).
870+ ///
871+ /// From: `void lua_setfield(lua_State *L, int index, const char *k);`
872+ /// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_setfield
873+ /// Stack Behavior: `[-1, +0, e]`
874+ pub fn setField (lua : * Lua , index : i32 , key : [:0 ]const u8 ) void {
875+ lua .validateStackIndex (index );
876+
877+ return c .lua_setfield (asState (lua ), index , @ptrCast (key .ptr ));
878+ }
879+
880+ /// Pushes onto the stack the value of the global name.
881+ ///
882+ /// From: `void lua_getglobal(lua_State *L, const char *name);`
883+ /// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_getglobal
884+ /// Stack Behavior: `[-0, +1, e]`
885+ pub fn getGlobal (lua : * Lua , name : [:0 ]const u8 ) Lua.Type {
886+ c .lua_getglobal (asState (lua ), @as ([* :0 ]const u8 , @ptrCast (name .ptr )));
887+ return lua .typeOf (-1 );
888+ }
889+
890+ /// Pops a value from the stack and sets it as the new value of global `name`.
891+ ///
892+ /// From: `void lua_setglobal(lua_State *L, const char *name);`
893+ /// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_setglobal
894+ /// Stack Behavior: `[-1, +0, e]`
895+ pub fn setGlobal (lua : * Lua , name : [:0 ]const u8 ) void {
896+ assert (lua .getTop () > 0 );
897+
898+ return c .lua_setglobal (asState (lua ), @as ([* :0 ]const u8 , @ptrCast (name .ptr )));
899+ }
900+
854901 /// Does the equivalent of `t[k] = v`, where `t` is the acceptable index of the table on the stack, `v` is
855902 /// the value at the top of the stack, and `k` is the value just below the top. This function pops both the
856903 /// key and the value from the stack. As in Lua, this function may trigger a metamethod for the "newindex"
@@ -1701,6 +1748,35 @@ test "tables" {
17011748 try std .testing .expectEqual (Lua .Type .string , lua .getTableRaw (-2 ));
17021749}
17031750
1751+ test "getfield and setfield" {
1752+ const lua = try Lua .init (std .testing .allocator );
1753+ defer lua .deinit ();
1754+
1755+ lua .newTable ();
1756+ lua .pushInteger (42 );
1757+ lua .setField (-2 , "foo" );
1758+
1759+ try std .testing .expectEqual (1 , lua .getTop ());
1760+
1761+ const actual_type = lua .getField (-1 , "foo" );
1762+ try std .testing .expectEqual (Lua .Type .number , actual_type );
1763+ try std .testing .expectEqual (42 , lua .toInteger (-1 ));
1764+ }
1765+
1766+ test "getglobal and setglobal" {
1767+ const lua = try Lua .init (std .testing .allocator );
1768+ defer lua .deinit ();
1769+
1770+ lua .pushInteger (42 );
1771+ lua .setGlobal ("XXX" );
1772+
1773+ try std .testing .expectEqual (0 , lua .getTop ());
1774+
1775+ const actual_type = lua .getGlobal ("XXX" );
1776+ try std .testing .expectEqual (Lua .Type .number , actual_type );
1777+ try std .testing .expectEqual (42 , lua .toInteger (-1 ));
1778+ }
1779+
17041780test "lengthOf" {
17051781 const lua = try Lua .init (std .testing .allocator );
17061782 defer lua .deinit ();
0 commit comments