Skip to content

Commit 5be0cdf

Browse files
committed
Remove toBytes()
The toBytes function can always be replaced with toString now. Although it is useful to have two push functions (one for slices and one for zero-terminated slices), when returning a value from Lua, always having a zero-terminated Zig slice with a length is useful.
1 parent 220b10a commit 5be0cdf

File tree

2 files changed

+41
-53
lines changed

2 files changed

+41
-53
lines changed

src/lib.zig

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,19 +1526,17 @@ pub const Lua = struct {
15261526
c.lua_pushlightuserdata(lua.state, ptr);
15271527
}
15281528

1529-
const BytesResult = switch (lang) {
1529+
/// Pushes the string onto the stack. Returns a slice pointing to Lua's internal copy of the string
1530+
/// See https://www.lua.org/manual/5.4/manual.html#lua_pushlstring
1531+
pub fn pushString(lua: *Lua, str: []const u8) switch (lang) {
15301532
.lua51, .luajit, .luau => void,
15311533
else => []const u8,
1532-
};
1533-
1534-
/// Pushes the bytes onto the stack. Returns a slice pointing to Lua's internal copy of the string
1535-
/// See https://www.lua.org/manual/5.4/manual.html#lua_pushlstring
1536-
pub fn pushString(lua: *Lua, bytes: []const u8) BytesResult {
1534+
} {
15371535
switch (lang) {
15381536
.lua51, .luajit, .luau => {
1539-
c.lua_pushlstring(lua.state, bytes.ptr, bytes.len);
1537+
c.lua_pushlstring(lua.state, str.ptr, str.len);
15401538
},
1541-
else => return c.lua_pushlstring(lua.state, bytes.ptr, bytes.len)[0..bytes.len],
1539+
else => return c.lua_pushlstring(lua.state, str.ptr, str.len)[0..str.len],
15421540
}
15431541
}
15441542

@@ -1929,16 +1927,6 @@ pub const Lua = struct {
19291927
else => toInteger52,
19301928
};
19311929

1932-
/// Returns a slice of bytes at the given index
1933-
/// If the value is not a string or number, returns an error
1934-
/// If the value was a number the actual value in the stack will be changed to a string
1935-
/// See https://www.lua.org/manual/5.4/manual.html#lua_tolstring
1936-
pub fn toBytes(lua: *Lua, index: i32) ![:0]const u8 {
1937-
var length: usize = undefined;
1938-
if (c.lua_tolstring(lua.state, index, &length)) |ptr| return ptr[0..length :0];
1939-
return error.Fail;
1940-
}
1941-
19421930
fn toNumber51(lua: *Lua, index: i32) Number {
19431931
return c.lua_tonumber(lua.state, index);
19441932
}
@@ -1970,9 +1958,9 @@ pub const Lua = struct {
19701958
/// Returns an error if the conversion failed
19711959
/// If the value was a number the actual value in the stack will be changed to a string
19721960
/// See https://www.lua.org/manual/5.4/manual.html#lua_tostring
1973-
pub fn toString(lua: *Lua, index: i32) ![*:0]const u8 {
1961+
pub fn toString(lua: *Lua, index: i32) ![:0]const u8 {
19741962
var length: usize = undefined;
1975-
if (c.lua_tolstring(lua.state, index, &length)) |str| return str;
1963+
if (c.lua_tolstring(lua.state, index, &length)) |ptr| return ptr[0..length :0];
19761964
return error.Fail;
19771965
}
19781966

src/tests.zig

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ test "type of and getting values" {
337337
_ = lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) });
338338
try expectEqual(.string, lua.typeOf(-1));
339339
try expect(lua.isString(-1));
340-
try expectEqualStrings("hello world 10", std.mem.span(try lua.toString(-1)));
340+
try expectEqualStrings("hello world 10", try lua.toString(-1));
341341

342342
lua.pushValue(2);
343343
try expectEqual(.boolean, lua.typeOf(-1));
@@ -564,7 +564,7 @@ test "string buffers" {
564564
lua.pushNumber(5.1);
565565
buffer.addValue();
566566
buffer.pushResult();
567-
try expectEqualStrings("ziglua api 5.1", try lua.toBytes(-1));
567+
try expectEqualStrings("ziglua api 5.1", try lua.toString(-1));
568568

569569
// now test a small buffer
570570
buffer.init(lua);
@@ -578,7 +578,7 @@ test "string buffers" {
578578
@memcpy(b[0..23], "defghijklmnopqrstuvwxyz");
579579
buffer.addSize(23);
580580
buffer.pushResult();
581-
try expectEqualStrings("abcdefghijklmnopqrstuvwxyz", try lua.toBytes(-1));
581+
try expectEqualStrings("abcdefghijklmnopqrstuvwxyz", try lua.toString(-1));
582582
lua.pop(1);
583583

584584
if (ziglua.lang == .lua51 or ziglua.lang == .luajit) return;
@@ -587,7 +587,7 @@ test "string buffers" {
587587
b = buffer.prep();
588588
@memcpy(b[0..3], "abc");
589589
buffer.pushResultSize(3);
590-
try expectEqualStrings("abc", try lua.toBytes(-1));
590+
try expectEqualStrings("abc", try lua.toString(-1));
591591
lua.pop(1);
592592

593593
if (ziglua.lang == .luau) return;
@@ -758,9 +758,9 @@ test "concat" {
758758
lua.concat(3);
759759

760760
if (ziglua.lang == .lua53 or ziglua.lang == .lua54) {
761-
try expectEqualStrings("hello 10.0 wow!", try lua.toBytes(-1));
761+
try expectEqualStrings("hello 10.0 wow!", try lua.toString(-1));
762762
} else {
763-
try expectEqualStrings("hello 10 wow!", try lua.toBytes(-1));
763+
try expectEqualStrings("hello 10 wow!", try lua.toString(-1));
764764
}
765765
}
766766

@@ -821,18 +821,18 @@ test "table access" {
821821

822822
if (ziglua.lang == .lua53 or ziglua.lang == .lua54) {
823823
try expectEqual(.string, lua.rawGetIndex(1, 1));
824-
try expectEqualStrings("first", try lua.toBytes(-1));
824+
try expectEqualStrings("first", try lua.toString(-1));
825825
}
826826

827827
try expectEqual(.string, switch (ziglua.lang) {
828828
.lua53, .lua54 => lua.getIndex(1, 1),
829829
else => lua.rawGetIndex(1, 1),
830830
});
831-
try expectEqualStrings("first", try lua.toBytes(-1));
831+
try expectEqualStrings("first", try lua.toString(-1));
832832

833833
_ = lua.pushStringZ("key");
834834
try expectEqual(.string, lua.getTable(1));
835-
try expectEqualStrings("value", try lua.toBytes(-1));
835+
try expectEqualStrings("value", try lua.toString(-1));
836836

837837
_ = lua.pushStringZ("other one");
838838
try expectEqual(.number, lua.rawGetTable(1));
@@ -1037,7 +1037,7 @@ test "userdata and uservalues" {
10371037
try expectEqual(.number, try lua.getUserValue(1, 1));
10381038
try expectEqual(1234.56, try lua.toNumber(-1));
10391039
try expectEqual(.string, try lua.getUserValue(1, 2));
1040-
try expectEqualStrings("test string", try lua.toBytes(-1));
1040+
try expectEqualStrings("test string", try lua.toString(-1));
10411041

10421042
try expectError(error.Fail, lua.setUserValue(1, 3));
10431043
try expectError(error.Fail, lua.getUserValue(1, 3));
@@ -1087,15 +1087,15 @@ test "table traversal" {
10871087
while (lua.next(1)) {
10881088
switch (lua.typeOf(-1)) {
10891089
.string => {
1090-
try expectEqualStrings("key", try lua.toBytes(-2));
1091-
try expectEqualStrings("value", try lua.toBytes(-1));
1090+
try expectEqualStrings("key", try lua.toString(-2));
1091+
try expectEqualStrings("value", try lua.toString(-1));
10921092
},
10931093
.boolean => {
1094-
try expectEqualStrings("second", try lua.toBytes(-2));
1094+
try expectEqualStrings("second", try lua.toString(-2));
10951095
try expectEqual(true, lua.toBoolean(-1));
10961096
},
10971097
.number => {
1098-
try expectEqualStrings("third", try lua.toBytes(-2));
1098+
try expectEqualStrings("third", try lua.toString(-2));
10991099
try expectEqual(1, try toInteger(&lua, -1));
11001100
},
11011101
else => unreachable,
@@ -1118,7 +1118,7 @@ test "registry" {
11181118

11191119
// get key from the registry
11201120
_ = lua.rawGetPtr(ziglua.registry_index, key);
1121-
try expectEqualStrings("hello there", try lua.toBytes(-1));
1121+
try expectEqualStrings("hello there", try lua.toString(-1));
11221122
}
11231123

11241124
test "closing vars" {
@@ -1168,7 +1168,7 @@ test "raise error" {
11681168

11691169
lua.pushFunction(ziglua.wrap(makeError));
11701170
try expectError(error.Runtime, lua.protectedCall(0, 0, 0));
1171-
try expectEqualStrings("makeError made an error", try lua.toBytes(-1));
1171+
try expectEqualStrings("makeError made an error", try lua.toString(-1));
11721172
}
11731173

11741174
fn continuation(l: *Lua, status: ziglua.Status, ctx: isize) i32 {
@@ -1226,7 +1226,7 @@ test "yielding" {
12261226
try expectEqual(.ok, try thread.resumeThread(lua, 0));
12271227
}
12281228

1229-
try expectEqualStrings("done", try thread.toBytes(-1));
1229+
try expectEqualStrings("done", try thread.toString(-1));
12301230
}
12311231

12321232
fn continuation52(l: *Lua) i32 {
@@ -1266,7 +1266,7 @@ test "yielding Lua 5.2" {
12661266
lua.pop(lua.getTop());
12671267
}
12681268
try expectEqual(.ok, try thread.resumeThread(lua, 0));
1269-
try expectEqualStrings("done", try thread.toBytes(-1));
1269+
try expectEqualStrings("done", try thread.toString(-1));
12701270
}
12711271

12721272
test "yielding no continuation" {
@@ -1321,7 +1321,7 @@ test "resuming" {
13211321
lua.pop(lua.getTop());
13221322
}
13231323
try expectEqual(.ok, if (ziglua.lang == .lua51 or ziglua.lang == .luajit) try thread.resumeThread(0) else try thread.resumeThread(lua, 0));
1324-
try expectEqualStrings("done", try thread.toBytes(-1));
1324+
try expectEqualStrings("done", try thread.toString(-1));
13251325
}
13261326

13271327
test "aux check functions" {
@@ -1343,22 +1343,22 @@ test "aux check functions" {
13431343

13441344
lua.pushFunction(function);
13451345
lua.protectedCall(0, 0, 0) catch {
1346-
try expectStringContains("argument #1", try lua.toBytes(-1));
1346+
try expectStringContains("argument #1", try lua.toString(-1));
13471347
lua.pop(-1);
13481348
};
13491349

13501350
lua.pushFunction(function);
13511351
lua.pushNil();
13521352
lua.protectedCall(1, 0, 0) catch {
1353-
try expectStringContains("number expected", try lua.toBytes(-1));
1353+
try expectStringContains("number expected", try lua.toString(-1));
13541354
lua.pop(-1);
13551355
};
13561356

13571357
lua.pushFunction(function);
13581358
lua.pushNil();
13591359
lua.pushInteger(3);
13601360
lua.protectedCall(2, 0, 0) catch {
1361-
try expectStringContains("string expected", try lua.toBytes(-1));
1361+
try expectStringContains("string expected", try lua.toString(-1));
13621362
lua.pop(-1);
13631363
};
13641364

@@ -1367,7 +1367,7 @@ test "aux check functions" {
13671367
lua.pushInteger(3);
13681368
_ = lua.pushString("hello world");
13691369
lua.protectedCall(3, 0, 0) catch {
1370-
try expectStringContains("number expected", try lua.toBytes(-1));
1370+
try expectStringContains("number expected", try lua.toString(-1));
13711371
lua.pop(-1);
13721372
};
13731373

@@ -1377,7 +1377,7 @@ test "aux check functions" {
13771377
_ = lua.pushString("hello world");
13781378
lua.pushNumber(4);
13791379
lua.protectedCall(4, 0, 0) catch {
1380-
try expectStringContains("string expected", try lua.toBytes(-1));
1380+
try expectStringContains("string expected", try lua.toString(-1));
13811381
lua.pop(-1);
13821382
};
13831383

@@ -1388,7 +1388,7 @@ test "aux check functions" {
13881388
lua.pushNumber(4);
13891389
_ = lua.pushStringZ("hello world");
13901390
lua.protectedCall(5, 0, 0) catch {
1391-
try expectStringContains("boolean expected", try lua.toBytes(-1));
1391+
try expectStringContains("boolean expected", try lua.toString(-1));
13921392
lua.pop(-1);
13931393
};
13941394

@@ -1401,7 +1401,7 @@ test "aux check functions" {
14011401
_ = lua.pushStringZ("hello world");
14021402
lua.pushBoolean(true);
14031403
lua.protectedCall(6, 0, 0) catch {
1404-
try expectEqualStrings("bad argument #7 to '?' (number expected, got no value)", try lua.toBytes(-1));
1404+
try expectEqualStrings("bad argument #7 to '?' (number expected, got no value)", try lua.toString(-1));
14051405
lua.pop(-1);
14061406
};
14071407
}
@@ -1495,7 +1495,7 @@ test "checkOption" {
14951495
lua.pushFunction(function);
14961496
_ = lua.pushStringZ("unknown");
14971497
try expectError(error.Runtime, lua.protectedCall(1, 1, 0));
1498-
try expectStringContains("(invalid option 'unknown')", try lua.toBytes(-1));
1498+
try expectStringContains("(invalid option 'unknown')", try lua.toString(-1));
14991499
}
15001500

15011501
test "get global fail" {
@@ -1514,7 +1514,7 @@ test "globalSub" {
15141514
defer lua.deinit();
15151515

15161516
_ = lua.globalSub("-gity -!", "-", "zig");
1517-
try expectEqualStrings("ziggity zig!", try lua.toBytes(-1));
1517+
try expectEqualStrings("ziggity zig!", try lua.toString(-1));
15181518
}
15191519

15201520
test "loadBuffer" {
@@ -1552,7 +1552,7 @@ test "where" {
15521552
);
15531553

15541554
_ = try lua.getGlobal("ret");
1555-
try expectEqualStrings("[string \"...\"]:2: ", try lua.toBytes(-1));
1555+
try expectEqualStrings("[string \"...\"]:2: ", try lua.toString(-1));
15561556
}
15571557

15581558
test "ref" {
@@ -1569,7 +1569,7 @@ test "ref" {
15691569
const ref = try lua.ref(ziglua.registry_index);
15701570

15711571
_ = lua.rawGetIndex(ziglua.registry_index, ref);
1572-
try expectEqualStrings("Hello there", try lua.toBytes(-1));
1572+
try expectEqualStrings("Hello there", try lua.toString(-1));
15731573

15741574
lua.unref(ziglua.registry_index, ref);
15751575
}
@@ -1590,7 +1590,7 @@ test "ref luau" {
15901590
const ref = try lua.ref(2);
15911591

15921592
_ = lua.rawGetIndex(ziglua.registry_index, ref);
1593-
try expectEqualStrings("Hello there", try lua.toBytes(-1));
1593+
try expectEqualStrings("Hello there", try lua.toString(-1));
15941594

15951595
lua.unref(ref);
15961596
}
@@ -1648,7 +1648,7 @@ test "args and errors" {
16481648

16491649
lua.pushFunction(raisesError);
16501650
try expectError(error.Runtime, lua.protectedCall(0, 0, 0));
1651-
try expectEqualStrings("some error zig!", try lua.toBytes(-1));
1651+
try expectEqualStrings("some error zig!", try lua.toString(-1));
16521652

16531653
if (ziglua.lang != .lua54) return;
16541654

@@ -1681,7 +1681,7 @@ test "traceback" {
16811681
try lua.doString("res = tracebackFn()");
16821682

16831683
_ = try lua.getGlobal("res");
1684-
try expectEqualStrings("\nstack traceback:\n\t[string \"res = tracebackFn()\"]:1: in main chunk", try lua.toBytes(-1));
1684+
try expectEqualStrings("\nstack traceback:\n\t[string \"res = tracebackFn()\"]:1: in main chunk", try lua.toString(-1));
16851685
}
16861686

16871687
test "getSubtable" {

0 commit comments

Comments
 (0)