Skip to content

Commit ec16700

Browse files
committed
Rename pushString to pushStringZ
This is more conventional in the Zig standard library, and it allows us to now rename the pushBytes function to pushString to make it more discoverable
1 parent 1938997 commit ec16700

File tree

2 files changed

+42
-44
lines changed

2 files changed

+42
-44
lines changed

src/lib.zig

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,16 +1554,14 @@ pub const Lua = struct {
15541554
c.lua_pushnumber(lua.state, n);
15551555
}
15561556

1557-
const StringResult = switch (lang) {
1558-
.lua51, .luajit, .luau => void,
1559-
else => [:0]const u8,
1560-
};
1561-
15621557
/// Pushes a zero-terminated string onto the stack
15631558
/// Lua makes a copy of the string so `str` may be freed immediately after return
15641559
/// Returns a pointer to the internal Lua string
15651560
/// See https://www.lua.org/manual/5.4/manual.html#lua_pushstring
1566-
pub fn pushString(lua: *Lua, str: [:0]const u8) StringResult {
1561+
pub fn pushStringZ(lua: *Lua, str: [:0]const u8) switch (lang) {
1562+
.lua51, .luajit, .luau => void,
1563+
else => [:0]const u8,
1564+
} {
15671565
switch (lang) {
15681566
.lua51, .luajit, .luau => {
15691567
c.lua_pushstring(lua.state, str.ptr);
@@ -2853,7 +2851,7 @@ pub const Lua = struct {
28532851
switch (lang) {
28542852
.lua51, .luajit, .luau => {
28552853
lua.pushFunction(open_fn);
2856-
_ = lua.pushString(mod_name);
2854+
_ = lua.pushStringZ(mod_name);
28572855
lua.call(1, 0);
28582856
},
28592857
else => c.luaL_requiref(lua.state, mod_name.ptr, open_fn, @intFromBool(global)),
@@ -3034,7 +3032,7 @@ pub const Lua = struct {
30343032
if (casted.* != 0) {
30353033
@compileError("Sentinel of slice must be a null terminator");
30363034
}
3037-
_ = lua.pushString(value);
3035+
_ = lua.pushStringZ(value);
30383036
},
30393037
.C, .Many, .Slice => {
30403038
std.debug.assert(info.child == u8);
@@ -3043,11 +3041,11 @@ pub const Lua = struct {
30433041
if (casted.* != 0) {
30443042
@compileError("Sentinel of slice must be a null terminator");
30453043
}
3046-
_ = lua.pushString(value);
3044+
_ = lua.pushStringZ(value);
30473045
} else {
30483046
const null_terminated = try lua.allocator().dupeZ(u8, value);
30493047
defer lua.allocator().free(null_terminated);
3050-
_ = lua.pushString(null_terminated);
3048+
_ = lua.pushStringZ(null_terminated);
30513049
}
30523050
},
30533051
}
@@ -3101,7 +3099,7 @@ pub const Lua = struct {
31013099
lua.pushBoolean(value);
31023100
},
31033101
.Enum => {
3104-
_ = lua.pushString(@tagName(value));
3102+
_ = lua.pushStringZ(@tagName(value));
31053103
},
31063104
.Optional, .Null => {
31073105
if (value == null) {
@@ -3345,7 +3343,7 @@ pub const Lua = struct {
33453343

33463344
inline for (@typeInfo(T).Struct.fields) |field| {
33473345
const field_name = comptime field.name ++ "";
3348-
_ = lua.pushString(field_name);
3346+
_ = lua.pushStringZ(field_name);
33493347

33503348
const lua_field_type = lua.getTable(index);
33513349
defer lua.pop(1);

src/tests.zig

Lines changed: 32 additions & 32 deletions
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.pushString("all your codebase are belong to us");
323+
_ = lua.pushStringZ("all your codebase are belong to us");
324324
try expectEqual(.string, lua.typeOf(-1));
325325
try expect(lua.isString(-1));
326326

@@ -619,7 +619,7 @@ test "global table" {
619619
lua.pushGlobalTable();
620620

621621
// find the print function
622-
_ = lua.pushString("print");
622+
_ = lua.pushStringZ("print");
623623
try expectEqual(.function, lua.getTable(-2));
624624

625625
// index the global table in the global table
@@ -752,9 +752,9 @@ test "concat" {
752752
var lua = try Lua.init(&testing.allocator);
753753
defer lua.deinit();
754754

755-
_ = lua.pushString("hello ");
755+
_ = lua.pushStringZ("hello ");
756756
lua.pushNumber(10);
757-
_ = lua.pushString(" wow!");
757+
_ = lua.pushStringZ(" wow!");
758758
lua.concat(3);
759759

760760
if (ziglua.lang == .lua53 or ziglua.lang == .lua54) {
@@ -830,22 +830,22 @@ test "table access" {
830830
});
831831
try expectEqualStrings("first", try lua.toBytes(-1));
832832

833-
_ = lua.pushString("key");
833+
_ = lua.pushStringZ("key");
834834
try expectEqual(.string, lua.getTable(1));
835835
try expectEqualStrings("value", try lua.toBytes(-1));
836836

837-
_ = lua.pushString("other one");
837+
_ = lua.pushStringZ("other one");
838838
try expectEqual(.number, lua.rawGetTable(1));
839839
try expectEqual(1234, try toInteger(&lua, -1));
840840

841841
// a.name = "ziglua"
842-
_ = lua.pushString("name");
843-
_ = lua.pushString("ziglua");
842+
_ = lua.pushStringZ("name");
843+
_ = lua.pushStringZ("ziglua");
844844
lua.setTable(1);
845845

846846
// a.lang = "zig"
847-
_ = lua.pushString("lang");
848-
_ = lua.pushString("zig");
847+
_ = lua.pushStringZ("lang");
848+
_ = lua.pushStringZ("zig");
849849
lua.rawSetTable(1);
850850

851851
try expectError(error.Fail, lua.getMetatable(1));
@@ -1031,7 +1031,7 @@ test "userdata and uservalues" {
10311031
lua.pushNumber(1234.56);
10321032
try lua.setUserValue(1, 1);
10331033

1034-
_ = lua.pushString("test string");
1034+
_ = lua.pushStringZ("test string");
10351035
try lua.setUserValue(1, 2);
10361036

10371037
try expectEqual(.number, try lua.getUserValue(1, 1));
@@ -1113,7 +1113,7 @@ test "registry" {
11131113
const key = "mykey";
11141114

11151115
// store a string in the registry
1116-
_ = lua.pushString("hello there");
1116+
_ = lua.pushStringZ("hello there");
11171117
lua.rawSetPtr(ziglua.registry_index, key);
11181118

11191119
// get key from the registry
@@ -1160,7 +1160,7 @@ test "raise error" {
11601160

11611161
const makeError = struct {
11621162
fn inner(l: *Lua) i32 {
1163-
_ = l.pushString("makeError made an error");
1163+
_ = l.pushStringZ("makeError made an error");
11641164
l.raiseError();
11651165
return 0;
11661166
}
@@ -1175,7 +1175,7 @@ fn continuation(l: *Lua, status: ziglua.Status, ctx: isize) i32 {
11751175
_ = status;
11761176

11771177
if (ctx == 5) {
1178-
_ = l.pushString("done");
1178+
_ = l.pushStringZ("done");
11791179
return 1;
11801180
} else {
11811181
// yield the current context value
@@ -1233,7 +1233,7 @@ fn continuation52(l: *Lua) i32 {
12331233
const ctxOrNull = l.getContext() catch unreachable;
12341234
const ctx = ctxOrNull orelse 0;
12351235
if (ctx == 5) {
1236-
_ = l.pushString("done");
1236+
_ = l.pushStringZ("done");
12371237
return 1;
12381238
} else {
12391239
// yield the current context value
@@ -1386,7 +1386,7 @@ test "aux check functions" {
13861386
lua.pushInteger(3);
13871387
_ = lua.pushBytes("hello world");
13881388
lua.pushNumber(4);
1389-
_ = lua.pushString("hello world");
1389+
_ = lua.pushStringZ("hello world");
13901390
lua.protectedCall(5, 0, 0) catch {
13911391
try expectStringContains("boolean expected", try lua.toBytes(-1));
13921392
lua.pop(-1);
@@ -1398,7 +1398,7 @@ test "aux check functions" {
13981398
lua.pushInteger(3);
13991399
_ = lua.pushBytes("hello world");
14001400
lua.pushNumber(4);
1401-
_ = lua.pushString("hello world");
1401+
_ = lua.pushStringZ("hello world");
14021402
lua.pushBoolean(true);
14031403
lua.protectedCall(6, 0, 0) catch {
14041404
try expectEqualStrings("bad argument #7 to '?' (number expected, got no value)", try lua.toBytes(-1));
@@ -1412,7 +1412,7 @@ test "aux check functions" {
14121412
lua.pushInteger(3);
14131413
_ = lua.pushBytes("hello world");
14141414
lua.pushNumber(4);
1415-
_ = lua.pushString("hello world");
1415+
_ = lua.pushStringZ("hello world");
14161416
lua.pushBoolean(true);
14171417
if (ziglua.lang == .lua52) {
14181418
lua.pushUnsigned(1);
@@ -1441,7 +1441,7 @@ test "aux opt functions" {
14411441
lua.pushInteger(10);
14421442
_ = lua.pushBytes("zig");
14431443
lua.pushNumber(1.23);
1444-
_ = lua.pushString("lang");
1444+
_ = lua.pushStringZ("lang");
14451445
try lua.protectedCall(4, 0, 0);
14461446
}
14471447

@@ -1468,19 +1468,19 @@ test "checkOption" {
14681468
}.inner);
14691469

14701470
lua.pushFunction(function);
1471-
_ = lua.pushString("one");
1471+
_ = lua.pushStringZ("one");
14721472
try lua.protectedCall(1, 1, 0);
14731473
try expectEqual(1, try toInteger(&lua, -1));
14741474
lua.pop(1);
14751475

14761476
lua.pushFunction(function);
1477-
_ = lua.pushString("two");
1477+
_ = lua.pushStringZ("two");
14781478
try lua.protectedCall(1, 1, 0);
14791479
try expectEqual(2, try toInteger(&lua, -1));
14801480
lua.pop(1);
14811481

14821482
lua.pushFunction(function);
1483-
_ = lua.pushString("three");
1483+
_ = lua.pushStringZ("three");
14841484
try lua.protectedCall(1, 1, 0);
14851485
try expectEqual(3, try toInteger(&lua, -1));
14861486
lua.pop(1);
@@ -1493,7 +1493,7 @@ test "checkOption" {
14931493

14941494
// check the raised error
14951495
lua.pushFunction(function);
1496-
_ = lua.pushString("unknown");
1496+
_ = lua.pushStringZ("unknown");
14971497
try expectError(error.Runtime, lua.protectedCall(1, 1, 0));
14981498
try expectStringContains("(invalid option 'unknown')", try lua.toBytes(-1));
14991499
}
@@ -1860,7 +1860,7 @@ test "objectLen" {
18601860
var lua = try Lua.init(&testing.allocator);
18611861
defer lua.deinit();
18621862

1863-
lua.pushString("lua");
1863+
lua.pushStringZ("lua");
18641864
try testing.expectEqual(3, lua.objectLen(-1));
18651865
}
18661866

@@ -2292,9 +2292,9 @@ test "useratom" {
22922292
defer lua.deinit();
22932293
lua.setUserAtomCallbackFn(ziglua.wrap(useratomCb));
22942294

2295-
_ = lua.pushString("unknownatom");
2296-
_ = lua.pushString("method_one");
2297-
_ = lua.pushString("another_method");
2295+
_ = lua.pushStringZ("unknownatom");
2296+
_ = lua.pushStringZ("method_one");
2297+
_ = lua.pushStringZ("another_method");
22982298

22992299
const atom_idx0, const str0 = try lua.toStringAtom(-2);
23002300
const atom_idx1, const str1 = try lua.toStringAtom(-1);
@@ -2360,7 +2360,7 @@ test "namecall" {
23602360
lua.pushVector(0, 0, 0);
23612361

23622362
try lua.newMetatable("vector");
2363-
lua.pushString("__namecall");
2363+
lua.pushStringZ("__namecall");
23642364
lua.pushFunctionNamed(ziglua.wrap(funcs.vectorNamecall), "vector_namecall");
23652365
lua.setTable(-3);
23662366

@@ -2406,17 +2406,17 @@ test "toAny" {
24062406
try testing.expect(my_float == 100.0);
24072407

24082408
//[]const u8
2409-
_ = lua.pushString("hello world");
2409+
_ = lua.pushStringZ("hello world");
24102410
const my_string_1 = try lua.toAny([]const u8, -1);
24112411
try testing.expect(std.mem.eql(u8, my_string_1, "hello world"));
24122412

24132413
//[:0]const u8
2414-
_ = lua.pushString("hello world");
2414+
_ = lua.pushStringZ("hello world");
24152415
const my_string_2 = try lua.toAny([:0]const u8, -1);
24162416
try testing.expect(std.mem.eql(u8, my_string_2, "hello world"));
24172417

24182418
//[*:0]const u8
2419-
_ = lua.pushString("hello world");
2419+
_ = lua.pushStringZ("hello world");
24202420
const my_string_3 = try lua.toAny([*:0]const u8, -1);
24212421
const end = std.mem.indexOfSentinel(u8, 0, my_string_3);
24222422
try testing.expect(std.mem.eql(u8, my_string_3[0..end], "hello world"));
@@ -2434,7 +2434,7 @@ test "toAny" {
24342434

24352435
//enum
24362436
const MyEnumType = enum { hello, goodbye };
2437-
_ = lua.pushString("hello");
2437+
_ = lua.pushStringZ("hello");
24382438
const my_enum = try lua.toAny(MyEnumType, -1);
24392439
try testing.expect(my_enum == MyEnumType.hello);
24402440

0 commit comments

Comments
 (0)