Skip to content

Commit 95245a9

Browse files
committed
Make pushString and pushStringZ always return a string
In Lua 5.1, LuaJIT, and Luau, pushlstring does not return the pushed string. I think it is better to make this consistent with the later versions of Lua in the Zig wrapper, so we can use toString to get the most recently pushed string. Though this makes an additional API call for the above mentioned versions, I think the added consistency is worth it. Going forward I want to remove as much switch (lang) as possible.
1 parent 6a75ad1 commit 95245a9

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

src/lib.zig

+5-9
Original file line numberDiff line numberDiff line change
@@ -1530,15 +1530,13 @@ pub const Lua = struct {
15301530

15311531
/// Pushes the string onto the stack. Returns a slice pointing to Lua's internal copy of the string
15321532
/// See https://www.lua.org/manual/5.4/manual.html#lua_pushlstring
1533-
pub fn pushString(lua: *Lua, str: []const u8) switch (lang) {
1534-
.lua51, .luajit, .luau => void,
1535-
else => []const u8,
1536-
} {
1533+
pub fn pushString(lua: *Lua, str: []const u8) [:0]const u8 {
15371534
switch (lang) {
15381535
.lua51, .luajit, .luau => {
15391536
c.lua_pushlstring(lua.state, str.ptr, str.len);
1537+
return lua.toString(-1) catch unreachable;
15401538
},
1541-
else => return c.lua_pushlstring(lua.state, str.ptr, str.len)[0..str.len],
1539+
else => return c.lua_pushlstring(lua.state, str.ptr, str.len)[0..str.len :0],
15421540
}
15431541
}
15441542

@@ -1558,13 +1556,11 @@ pub const Lua = struct {
15581556
/// Lua makes a copy of the string so `str` may be freed immediately after return
15591557
/// Returns a pointer to the internal Lua string
15601558
/// See https://www.lua.org/manual/5.4/manual.html#lua_pushstring
1561-
pub fn pushStringZ(lua: *Lua, str: [:0]const u8) switch (lang) {
1562-
.lua51, .luajit, .luau => void,
1563-
else => [:0]const u8,
1564-
} {
1559+
pub fn pushStringZ(lua: *Lua, str: [:0]const u8) [:0]const u8 {
15651560
switch (lang) {
15661561
.lua51, .luajit, .luau => {
15671562
c.lua_pushstring(lua.state, str.ptr);
1563+
return lua.toString(-1) catch unreachable;
15681564
},
15691565
else => return c.lua_pushstring(lua.state, str.ptr)[0..str.len :0],
15701566
}

src/tests.zig

+5-5
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ test "type of and getting values" {
320320
try expect(lua.isThread(-1));
321321
try expectEqual(lua.state, (try lua.toThread(-1)).state);
322322

323-
_ = lua.pushStringZ("all your codebase are belong to us");
323+
try expectEqualStrings("all your codebase are belong to us", lua.pushStringZ("all your codebase are belong to us"));
324324
try expectEqual(.string, lua.typeOf(-1));
325325
try expect(lua.isString(-1));
326326

@@ -330,7 +330,7 @@ test "type of and getting values" {
330330
try expect(lua.isFunction(-1));
331331
try expectEqual(ziglua.wrap(add), try lua.toCFunction(-1));
332332

333-
_ = lua.pushString("hello world");
333+
try expectEqualStrings("hello world", lua.pushString("hello world"));
334334
try expectEqual(.string, lua.typeOf(-1));
335335
try expect(lua.isString(-1));
336336

@@ -1572,7 +1572,7 @@ test "ref luau" {
15721572

15731573
// In luau lua.ref does not pop the item from the stack
15741574
// and the data is stored in the registry_index by default
1575-
lua.pushString("Hello there");
1575+
_ = lua.pushString("Hello there");
15761576
const ref = try lua.ref(2);
15771577

15781578
_ = lua.rawGetIndex(ziglua.registry_index, ref);
@@ -1846,7 +1846,7 @@ test "objectLen" {
18461846
var lua = try Lua.init(&testing.allocator);
18471847
defer lua.deinit();
18481848

1849-
lua.pushStringZ("lua");
1849+
_ = lua.pushStringZ("lua");
18501850
try testing.expectEqual(3, lua.objectLen(-1));
18511851
}
18521852

@@ -2346,7 +2346,7 @@ test "namecall" {
23462346
lua.pushVector(0, 0, 0);
23472347

23482348
try lua.newMetatable("vector");
2349-
lua.pushStringZ("__namecall");
2349+
_ = lua.pushStringZ("__namecall");
23502350
lua.pushFunctionNamed(ziglua.wrap(funcs.vectorNamecall), "vector_namecall");
23512351
lua.setTable(-3);
23522352

0 commit comments

Comments
 (0)