Skip to content

Commit 6be1cbb

Browse files
committed
add *Ex variants of pushString and pushBytes
I never care for the return value of this function, so writing _ = gets tedious. This adds two new versions that return the value, and the shorter non *Ex functions return void. This makes the common case easier, but the option to access the return is still available. This will be applied to other similar functions in the API.
1 parent 119e964 commit 6be1cbb

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

src/tests.zig

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,12 @@ test "type of and getting values" {
253253
lua.pushNil();
254254
lua.pushNumber(0.1);
255255
_ = lua.pushThread();
256-
_ = lua.pushString("all your codebase are belong to us");
256+
try expectEqualStrings(
257+
"all your codebase are belong to us",
258+
lua.pushStringEx("all your codebase are belong to us"),
259+
);
257260
lua.pushFunction(ziglua.wrap(add));
258-
_ = lua.pushBytes("hello world");
261+
try expectEqualStrings("hello world", lua.pushBytesEx("hello world"));
259262
_ = lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) });
260263
lua.pushValue(1);
261264

@@ -496,7 +499,7 @@ test "global table" {
496499
lua.pushGlobalTable();
497500

498501
// find the print function
499-
_ = lua.pushString("print");
502+
lua.pushString("print");
500503
try expectEqual(LuaType.function, lua.getTable(-2));
501504

502505
// index the global table in the global table
@@ -585,9 +588,9 @@ test "concat" {
585588
var lua = try Lua.init(testing.allocator);
586589
defer lua.deinit();
587590

588-
_ = lua.pushString("hello ");
591+
lua.pushString("hello ");
589592
lua.pushNumber(10);
590-
_ = lua.pushString(" wow!");
593+
lua.pushString(" wow!");
591594
lua.concat(3);
592595

593596
try expectEqualStrings("hello 10.0 wow!", try lua.toBytes(-1));
@@ -635,22 +638,22 @@ test "table access" {
635638
try expectEqual(LuaType.string, lua.rawGetIndex(1, 1));
636639
try expectEqualStrings("first", try lua.toBytes(-1));
637640

638-
_ = lua.pushString("key");
641+
lua.pushString("key");
639642
try expectEqual(LuaType.string, lua.getTable(1));
640643
try expectEqualStrings("value", try lua.toBytes(-1));
641644

642-
_ = lua.pushString("other one");
645+
lua.pushString("other one");
643646
try expectEqual(LuaType.number, lua.rawGetTable(1));
644647
try expectEqual(@as(Integer, 1234), try lua.toInteger(-1));
645648

646649
// a.name = "ziglua"
647-
_ = lua.pushString("name");
648-
_ = lua.pushString("ziglua");
650+
lua.pushString("name");
651+
lua.pushString("ziglua");
649652
lua.setTable(1);
650653

651654
// a.lang = "zig"
652-
_ = lua.pushString("lang");
653-
_ = lua.pushString("zig");
655+
lua.pushString("lang");
656+
lua.pushString("zig");
654657
lua.rawSetTable(1);
655658

656659
try expectError(Error.Fail, lua.getMetatable(1));
@@ -800,7 +803,7 @@ test "userdata and uservalues" {
800803
lua.pushNumber(1234.56);
801804
try lua.setIndexUserValue(1, 1);
802805

803-
_ = lua.pushString("test string");
806+
lua.pushString("test string");
804807
try lua.setIndexUserValue(1, 2);
805808

806809
try expectEqual(LuaType.number, try lua.getIndexUserValue(1, 1));
@@ -881,7 +884,7 @@ test "registry" {
881884
const key = "mykey";
882885

883886
// store a string in the registry
884-
_ = lua.pushString("hello there");
887+
lua.pushString("hello there");
885888
lua.rawSetPtr(ziglua.registry_index, key);
886889

887890
// get key from the registry
@@ -926,7 +929,7 @@ test "raise error" {
926929

927930
const makeError = struct {
928931
fn inner(l: *Lua) i32 {
929-
_ = l.pushString("makeError made an error");
932+
l.pushString("makeError made an error");
930933
l.raiseError();
931934
return 0;
932935
}
@@ -941,7 +944,7 @@ fn continuation(l: *Lua, status: ziglua.Status, ctx: isize) i32 {
941944
_ = status;
942945

943946
if (ctx == 5) {
944-
_ = l.pushString("done");
947+
l.pushString("done");
945948
return 1;
946949
} else {
947950
// yield the current context value
@@ -1168,7 +1171,7 @@ test "aux check functions" {
11681171
lua.pushFunction(function);
11691172
lua.pushNil();
11701173
lua.pushInteger(3);
1171-
_ = lua.pushBytes("hello world");
1174+
lua.pushBytes("hello world");
11721175
lua.protectedCall(3, 0, 0) catch {
11731176
try expectEqualStrings("bad argument #4 to '?' (number expected, got no value)", try lua.toBytes(-1));
11741177
lua.pop(-1);
@@ -1177,7 +1180,7 @@ test "aux check functions" {
11771180
lua.pushFunction(function);
11781181
lua.pushNil();
11791182
lua.pushInteger(3);
1180-
_ = lua.pushBytes("hello world");
1183+
lua.pushBytes("hello world");
11811184
lua.pushNumber(4);
11821185
lua.protectedCall(4, 0, 0) catch {
11831186
try expectEqualStrings("bad argument #5 to '?' (string expected, got no value)", try lua.toBytes(-1));
@@ -1187,9 +1190,9 @@ test "aux check functions" {
11871190
lua.pushFunction(function);
11881191
lua.pushNil();
11891192
lua.pushInteger(3);
1190-
_ = lua.pushBytes("hello world");
1193+
lua.pushBytes("hello world");
11911194
lua.pushNumber(4);
1192-
_ = lua.pushString("hello world");
1195+
lua.pushString("hello world");
11931196
lua.protectedCall(5, 0, 0) catch {
11941197
try expectEqualStrings("bad argument #6 to '?' (boolean expected, got no value)", try lua.toBytes(-1));
11951198
lua.pop(-1);
@@ -1198,9 +1201,9 @@ test "aux check functions" {
11981201
lua.pushFunction(function);
11991202
lua.pushNil();
12001203
lua.pushInteger(3);
1201-
_ = lua.pushBytes("hello world");
1204+
lua.pushBytes("hello world");
12021205
lua.pushNumber(4);
1203-
_ = lua.pushString("hello world");
1206+
lua.pushString("hello world");
12041207
lua.pushBoolean(true);
12051208
lua.protectedCall(6, 0, 0) catch {
12061209
try expectEqualStrings("bad argument #6 to '?' (boolean expected, got no value)", try lua.toBytes(-1));
@@ -1210,9 +1213,9 @@ test "aux check functions" {
12101213
lua.pushFunction(function);
12111214
lua.pushNil();
12121215
lua.pushInteger(3);
1213-
_ = lua.pushBytes("hello world");
1216+
lua.pushBytes("hello world");
12141217
lua.pushNumber(4);
1215-
_ = lua.pushString("hello world");
1218+
lua.pushString("hello world");
12161219
lua.pushBoolean(true);
12171220
try lua.protectedCall(6, 0, 0);
12181221
}

src/ziglua.zig

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -783,9 +783,14 @@ pub const Lua = struct {
783783
c.lua_pushlightuserdata(lua.state, ptr);
784784
}
785785

786+
/// Pushes a slice of bytes onto the stack
787+
pub fn pushBytes(lua: *Lua, bytes: []const u8) void {
788+
_ = lua.pushBytesEx(bytes);
789+
}
790+
786791
/// Pushes the bytes onto the stack. Returns a slice pointing to Lua's internal copy of the string
787-
pub fn pushBytes(lua: *Lua, str: []const u8) []const u8 {
788-
return c.lua_pushlstring(lua.state, str.ptr, str.len)[0..str.len];
792+
pub fn pushBytesEx(lua: *Lua, bytes: []const u8) []const u8 {
793+
return c.lua_pushlstring(lua.state, bytes.ptr, bytes.len)[0..bytes.len];
789794
}
790795

791796
/// Pushes a nil value onto the stack
@@ -798,11 +803,16 @@ pub const Lua = struct {
798803
c.lua_pushnumber(lua.state, n);
799804
}
800805

806+
/// Pushes a zero-terminated string on to the stack
807+
pub fn pushString(lua: *Lua, str: [:0]const u8) void {
808+
_ = lua.pushStringEx(str);
809+
}
810+
801811
/// Pushes a zero-terminated string onto the stack
802812
/// Lua makes a copy of the string so `str` may be freed immediately after return
803813
/// Returns a pointer to the internal Lua string
804-
pub fn pushString(lua: *Lua, str: []const u8) []const u8 {
805-
return c.lua_pushstring(lua.state, str.ptr).?[0..str.len];
814+
pub fn pushStringEx(lua: *Lua, str: [:0]const u8) [:0]const u8 {
815+
return c.lua_pushstring(lua.state, str.ptr).?[0..str.len :0];
806816
}
807817

808818
/// Pushes this thread onto the stack

0 commit comments

Comments
 (0)