@@ -703,6 +703,15 @@ pub const Lua = opaque {
703703 return @ptrCast (c .lua_touserdata (asState (lua ), index ));
704704 }
705705
706+ /// Pushes a copy of the element at the given valid index onto the stack.
707+ ///
708+ /// From: `void lua_pushvalue(lua_State *L, int index);`
709+ /// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_pushvalue
710+ /// Stack Behavior: `[-0, +1, -]`
711+ pub fn pushValue (lua : * Lua , index : i32 ) void {
712+ return c .lua_pushvalue (asState (lua ), index );
713+ }
714+
706715 /// Pushes the zero-terminated string onto the stack. Lua makes (or reuses) an internal copy of the given string,
707716 /// so the provided slice can be freed or reused immediately after the function returns. The given string cannot
708717 /// contain embedded zeros; it is assumed to end at the first zero (`'\x00'`) byte.
@@ -2309,3 +2318,25 @@ test "push and get light userdata" {
23092318 try std .testing .expectEqual (a1 .? , @as (* anyopaque , @ptrCast (& i )));
23102319 try std .testing .expectEqual (a1 .? , a2 .? );
23112320}
2321+
2322+ test "pushvalue" {
2323+ const lua = try Lua .init (std .testing .allocator );
2324+ defer lua .deinit ();
2325+
2326+ lua .pushInteger (42 );
2327+ lua .pushValue (-1 );
2328+ try std .testing .expect (lua .equal (-1 , -2 ));
2329+
2330+ const a1 = lua .toInteger (-1 );
2331+ const a2 = lua .toInteger (-2 );
2332+ try std .testing .expectEqual (a1 , a2 );
2333+
2334+ lua .setTop (0 );
2335+ lua .pushLString ("ASDF" );
2336+ lua .pushValue (-1 );
2337+ try std .testing .expect (lua .equal (-1 , -2 ));
2338+
2339+ const s1 = try lua .toLString (-1 );
2340+ const s2 = try lua .toLString (-2 );
2341+ try std .testing .expectEqualStrings (s1 , s2 );
2342+ }
0 commit comments