Skip to content

Commit 8398b38

Browse files
committed
refactor: accept a slice in pushString
Typically the strings pushed are from Zig so the lengths will be known
1 parent 566d775 commit 8398b38

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

src/tests.zig

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ test "type of and getting values" {
252252
lua.pushNumber(0.1);
253253
_ = lua.pushThread();
254254
_ = lua.pushString("all your codebase are belong to us");
255-
try expectEqual(@as(?[*]const u8, null), lua.pushString(null));
256255
lua.pushCFunction(ziglua.wrap(add));
257256
_ = lua.pushBytes("hello world");
258257
_ = lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) });
@@ -267,11 +266,10 @@ test "type of and getting values" {
267266
try expectEqual(LuaType.number, lua.typeOf(6));
268267
try expectEqual(LuaType.thread, lua.typeOf(7));
269268
try expectEqual(LuaType.string, lua.typeOf(8));
270-
try expectEqual(LuaType.nil, lua.typeOf(9));
271-
try expectEqual(LuaType.function, lua.typeOf(10));
269+
try expectEqual(LuaType.function, lua.typeOf(9));
270+
try expectEqual(LuaType.string, lua.typeOf(10));
272271
try expectEqual(LuaType.string, lua.typeOf(11));
273-
try expectEqual(LuaType.string, lua.typeOf(12));
274-
try expectEqual(LuaType.boolean, lua.typeOf(13));
272+
try expectEqual(LuaType.boolean, lua.typeOf(12));
275273

276274
try expect(lua.isBoolean(1));
277275
try expect(lua.isTable(2));
@@ -282,18 +280,17 @@ test "type of and getting values" {
282280
try expect(lua.isNumber(6));
283281
try expect(lua.isThread(7));
284282
try expect(lua.isString(8));
285-
try expect(lua.isNil(9));
286-
try expect(lua.isCFunction(10));
287-
try expect(lua.isFunction(10));
283+
try expect(lua.isCFunction(9));
284+
try expect(lua.isFunction(9));
285+
try expect(lua.isString(10));
288286
try expect(lua.isString(11));
289-
try expect(lua.isString(12));
290-
try expect(lua.isBoolean(13));
287+
try expect(lua.isBoolean(12));
291288

292-
try expectEqualStrings("hello world 10", std.mem.span(try lua.toString(12)));
289+
try expectEqualStrings("hello world 10", std.mem.span(try lua.toString(11)));
293290

294291
// the created thread should equal the main thread (but created thread has no allocator ref)
295292
try expectEqual(lua.state, (try lua.toThread(7)).state);
296-
try expectEqual(@as(ziglua.CFn, ziglua.wrap(add)), try lua.toCFunction(10));
293+
try expectEqual(@as(ziglua.CFn, ziglua.wrap(add)), try lua.toCFunction(9));
297294

298295
try expectEqual(@as(Number, 0.1), try lua.toNumberX(6));
299296
try expectEqual(@as(Integer, 1), try lua.toIntegerX(3));

src/ziglua.zig

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,9 +735,8 @@ pub const Lua = struct {
735735
/// Returns a pointer to the internal Lua string
736736
/// If `str` is null pushes nil and returns null
737737
/// TODO: is it useful to return null?
738-
pub fn pushString(lua: *Lua, str: ?[*:0]const u8) ?[*]const u8 {
739-
const ptr = c.lua_pushstring(lua.state, str);
740-
return @ptrCast(?[*]const u8, ptr);
738+
pub fn pushString(lua: *Lua, str: []const u8) []const u8 {
739+
return c.lua_pushstring(lua.state, str.ptr).?[0..str.len];
741740
}
742741

743742
/// Pushes this thread onto the stack

0 commit comments

Comments
 (0)