Skip to content

Commit 6bc20e5

Browse files
committed
Add support for lua.pushValue()
1 parent 01db9b9 commit 6bc20e5

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pattern has changed, such as using the Zig `init()` function pattern instead of
160160
| `lua_pushnumber`| ☑️ `lua.pushNumber()` |
161161
| `lua_pushstring`| ☑️ `lua.pushString()` |
162162
| `lua_pushthread`||
163-
| `lua_pushvalue`||
163+
| `lua_pushvalue`| ☑️ `lua.pushValue()` |
164164
| `lua_pushvfstring`||
165165
| `lua_rawequal`||
166166
| `lua_rawgeti`||

src/lua_api.zig

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,6 @@ pub fn pushLiteral(lua: *Lua, s: []const u8) void;
9595
/// Stack Behavior: `[-0, +1, -]`
9696
pub fn pushThread(lua: *Lua) bool;
9797

98-
/// Pushes a copy of the element at the given valid index onto the stack.
99-
///
100-
/// From: `void lua_pushvalue(lua_State *L, int index);`
101-
/// Refer to: https://www.lua.org/manual/5.1/manual.html#lua_pushvalue
102-
/// Stack Behavior: `[-0, +1, -]`
103-
pub fn pushValue(lua: *Lua, index: i32) void;
104-
10598
/// Equivalent to pushFString, except that it receives a va_list instead of a variable number of arguments.
10699
///
107100
/// From: `const char *lua_pushvfstring(lua_State *L, const char *fmt, va_list argp);`

src/root.zig

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)