From 8cc749bc1ab9da47862ade1aab0294bb3ede62e1 Mon Sep 17 00:00:00 2001 From: Nathan Craddock Date: Sat, 18 Feb 2023 17:43:54 -0700 Subject: [PATCH] feat: remove all Ex function variations While these functions can sometimes be convenient, it makes the API more clean to not have separate functions just to avoid using _ = at the call site. Closes #8 --- src/ziglua-5.1/lib.zig | 52 +++-------------- src/ziglua-5.1/tests.zig | 14 ++--- src/ziglua-5.2/lib.zig | 52 +++-------------- src/ziglua-5.2/tests.zig | 82 +++++++++++++------------- src/ziglua-5.3/lib.zig | 61 +++----------------- src/ziglua-5.3/tests.zig | 112 ++++++++++++++++++------------------ src/ziglua-5.4/lib.zig | 61 +++----------------- src/ziglua-5.4/tests.zig | 120 +++++++++++++++++++-------------------- 8 files changed, 197 insertions(+), 357 deletions(-) diff --git a/src/ziglua-5.1/lib.zig b/src/ziglua-5.1/lib.zig index 711de95..42203de 100644 --- a/src/ziglua-5.1/lib.zig +++ b/src/ziglua-5.1/lib.zig @@ -695,15 +695,9 @@ pub const Lua = struct { lua.pushClosure(c_fn, 0); } - /// Push a formatted string onto the stack - /// See https://www.lua.org/manual/5.1/manual.html#lua_pushfstring - pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) void { - _ = lua.pushFStringEx(fmt, args); - } - /// Push a formatted string onto the stack and return a pointer to the string /// See https://www.lua.org/manual/5.1/manual.html#lua_pushfstring - pub fn pushFStringEx(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 { + pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 { return @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args); } @@ -737,30 +731,17 @@ pub const Lua = struct { c.lua_pushnumber(lua.state, n); } - /// Pushes a zero-terminated string on to the stack - /// See https://www.lua.org/manual/5.1/manual.html#lua_pushstring - pub fn pushString(lua: *Lua, str: [:0]const u8) void { - _ = lua.pushStringEx(str); - } - /// Pushes a zero-terminated string onto the stack /// Lua makes a copy of the string so `str` may be freed immediately after return - /// Returns a pointer to the internal Lua string /// See https://www.lua.org/manual/5.1/manual.html#lua_pushstring - pub fn pushStringEx(lua: *Lua, str: [:0]const u8) void { + pub fn pushString(lua: *Lua, str: [:0]const u8) void { c.lua_pushstring(lua.state, str.ptr); } - /// Pushes this thread onto the stack - /// See https://www.lua.org/manual/5.1/manual.html#lua_pushthread - pub fn pushThread(lua: *Lua) void { - _ = lua.pushThreadEx(); - } - /// Pushes this thread onto the stack /// Returns true if this thread is the main thread of its state /// See https://www.lua.org/manual/5.1/manual.html#lua_pushthread - pub fn pushThreadEx(lua: *Lua) bool { + pub fn pushThread(lua: *Lua) bool { return c.lua_pushthread(lua.state) != 0; } @@ -1064,16 +1045,10 @@ pub const Lua = struct { } } - /// Gets information about a local variable - /// See https://www.lua.org/manual/5.1/manual.html#lua_getlocal - pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) !void { - _ = try lua.getLocalEx(info, n); - } - /// Gets information about a local variable /// Returns the name of the local variable /// See https://www.lua.org/manual/5.1/manual.html#lua_getlocal - pub fn getLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { + pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { var ar: Debug = undefined; ar.i_ci = info.private; if (c.lua_getlocal(lua.state, &ar, n)) |name| { @@ -1106,17 +1081,11 @@ pub const Lua = struct { _ = c.lua_sethook(lua.state, hook_fn, hook_mask, count); } - /// Sets the value of a local variable - /// See https://www.lua.org/manual/5.1/manual.html#lua_setlocal - pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) !void { - _ = try lua.setLocalEx(info, n); - } - /// Sets the value of a local variable /// Returns an error when the index is greater than the number of active locals /// Returns the name of the local variable /// See https://www.lua.org/manual/5.1/manual.html#lua_setlocal - pub fn setLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { + pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { var ar: Debug = undefined; ar.i_ci = info.private; if (c.lua_setlocal(lua.state, &ar, n)) |name| { @@ -1125,17 +1094,10 @@ pub const Lua = struct { return error.Fail; } - /// Sets the value of a closure's upvalue - /// Returns an error if the upvalu does not exist - /// See https://www.lua.org/manual/5.1/manual.html#lua_setupvalue - pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) !void { - _ = try lua.setUpvalueEx(func_index, n); - } - /// Sets the value of a closure's upvalue /// Returns the name of the upvalue or an error if the upvalue does not exist /// See https://www.lua.org/manual/5.1/manual.html#lua_setupvalue - pub fn setUpvalueEx(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 { + pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 { if (c.lua_setupvalue(lua.state, func_index, n)) |name| { return std.mem.span(name); } @@ -1221,7 +1183,7 @@ pub const Lua = struct { } } - return lua.argError(arg, lua.pushFStringEx("invalid option '%s'", .{name.ptr})); + return lua.argError(arg, lua.pushFString("invalid option '%s'", .{name.ptr})); } /// Grows the stack size to top + `size` elements, raising an error if the stack cannot grow to that size diff --git a/src/ziglua-5.1/tests.zig b/src/ziglua-5.1/tests.zig index 40fe427..2a0dd96 100644 --- a/src/ziglua-5.1/tests.zig +++ b/src/ziglua-5.1/tests.zig @@ -166,11 +166,11 @@ test "type of and getting values" { lua.pushLightUserdata(&value); lua.pushNil(); lua.pushNumber(0.1); - lua.pushThread(); - lua.pushStringEx("all your codebase are belong to us"); + _ = lua.pushThread(); + lua.pushString("all your codebase are belong to us"); lua.pushFunction(ziglua.wrap(add)); lua.pushBytes("hello world"); - lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) }); + _ = lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) }); lua.pushValue(1); // test both typeof and is functions @@ -736,18 +736,18 @@ test "debug interface" { .call => { l.getInfo(.{ .l = true }, i); if (i.current_line.? != 2) panic("Expected line to be 2", .{}); - l.getLocal(i, 1) catch unreachable; + _ = l.getLocal(i, 1) catch unreachable; if ((l.toNumber(-1)) != 3) panic("Expected x to equal 3", .{}); }, .line => if (i.current_line.? == 4) { // modify the value of y to be 0 right before returning l.pushNumber(0); - l.setLocal(i, 2) catch unreachable; + _ = l.setLocal(i, 2) catch unreachable; }, .ret => { l.getInfo(.{ .l = true }, i); if (i.current_line.? != 4) panic("Expected line to be 4", .{}); - l.getLocal(i, 1) catch unreachable; + _ = l.getLocal(i, 1) catch unreachable; if ((l.toNumber(-1)) != 3) panic("Expected result to equal 3", .{}); }, else => unreachable, @@ -793,7 +793,7 @@ test "debug upvalues" { // now make the function an "add five" function lua.pushNumber(5); - try lua.setUpvalue(-2, 1); + _ = try lua.setUpvalue(-2, 1); // call the new function (should return 7) lua.pushNumber(2); diff --git a/src/ziglua-5.2/lib.zig b/src/ziglua-5.2/lib.zig index fa7dda3..3a48b00 100644 --- a/src/ziglua-5.2/lib.zig +++ b/src/ziglua-5.2/lib.zig @@ -766,13 +766,8 @@ pub const Lua = struct { lua.pushClosure(c_fn, 0); } - /// Push a formatted string onto the stack - pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) void { - _ = lua.pushFStringEx(fmt, args); - } - /// Push a formatted string onto the stack and return a pointer to the string - pub fn pushFStringEx(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 { + pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 { return @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args); } @@ -794,13 +789,8 @@ pub const Lua = struct { c.lua_pushlightuserdata(lua.state, ptr); } - /// Pushes a slice of bytes onto the stack - pub fn pushBytes(lua: *Lua, bytes: []const u8) void { - _ = lua.pushBytesEx(bytes); - } - /// Pushes the bytes onto the stack. Returns a slice pointing to Lua's internal copy of the string - pub fn pushBytesEx(lua: *Lua, bytes: []const u8) []const u8 { + pub fn pushBytes(lua: *Lua, bytes: []const u8) []const u8 { return c.lua_pushlstring(lua.state, bytes.ptr, bytes.len)[0..bytes.len]; } @@ -814,26 +804,16 @@ pub const Lua = struct { c.lua_pushnumber(lua.state, n); } - /// Pushes a zero-terminated string on to the stack - pub fn pushString(lua: *Lua, str: [:0]const u8) void { - _ = lua.pushStringEx(str); - } - /// Pushes a zero-terminated string onto the stack /// Lua makes a copy of the string so `str` may be freed immediately after return /// Returns a pointer to the internal Lua string - pub fn pushStringEx(lua: *Lua, str: [:0]const u8) [:0]const u8 { + pub fn pushString(lua: *Lua, str: [:0]const u8) [:0]const u8 { return c.lua_pushstring(lua.state, str.ptr)[0..str.len :0]; } - /// Pushes this thread onto the stack - pub fn pushThread(lua: *Lua) void { - _ = lua.pushThreadEx(); - } - /// Pushes this thread onto the stack /// Returns true if this thread is the main thread of its state - pub fn pushThreadEx(lua: *Lua) bool { + pub fn pushThread(lua: *Lua) bool { return c.lua_pushthread(lua.state) != 0; } @@ -1169,14 +1149,9 @@ pub const Lua = struct { } } - /// Gets information about a local variable - pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) !void { - _ = try lua.getLocalEx(info, n); - } - /// Gets information about a local variable /// Returns the name of the local variable - pub fn getLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { + pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { var ar: Debug = undefined; ar.i_ci = @ptrCast(*c.struct_CallInfo, info.private); if (c.lua_getlocal(lua.state, &ar, n)) |name| { @@ -1206,15 +1181,10 @@ pub const Lua = struct { _ = c.lua_sethook(lua.state, hook_fn, hook_mask, count); } - /// Sets the value of a local variable - pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) !void { - _ = try lua.setLocalEx(info, n); - } - /// Sets the value of a local variable /// Returns an error when the index is greater than the number of active locals /// Returns the name of the local variable - pub fn setLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { + pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { var ar: Debug = undefined; ar.i_ci = @ptrCast(*c.struct_CallInfo, info.private); if (c.lua_setlocal(lua.state, &ar, n)) |name| { @@ -1223,15 +1193,9 @@ pub const Lua = struct { return error.Fail; } - /// Sets the value of a closure's upvalue - /// Returns an error if the upvalu does not exist - pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) !void { - _ = try lua.setUpvalueEx(func_index, n); - } - /// Sets the value of a closure's upvalue /// Returns the name of the upvalue or an error if the upvalue does not exist - pub fn setUpvalueEx(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 { + pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 { if (c.lua_setupvalue(lua.state, func_index, n)) |name| { return std.mem.span(name); } @@ -1315,7 +1279,7 @@ pub const Lua = struct { } } - return lua.argError(arg, lua.pushFStringEx("invalid option '%s'", .{name.ptr})); + return lua.argError(arg, lua.pushFString("invalid option '%s'", .{name.ptr})); } /// Grows the stack size to top + `size` elements, raising an error if the stack cannot grow to that size diff --git a/src/ziglua-5.2/tests.zig b/src/ziglua-5.2/tests.zig index 697aa6f..5acbc60 100644 --- a/src/ziglua-5.2/tests.zig +++ b/src/ziglua-5.2/tests.zig @@ -213,7 +213,7 @@ test "unsigned" { lua.pushUnsigned(123456); try expectEqual(@as(Unsigned, 123456), try lua.toUnsigned(-1)); - lua.pushBytes("hello"); + _ = lua.pushBytes("hello"); try expectError(error.Fail, lua.toUnsigned(-1)); } @@ -236,14 +236,14 @@ test "type of and getting values" { lua.pushLightUserdata(&value); lua.pushNil(); lua.pushNumber(0.1); - lua.pushThread(); + _ = lua.pushThread(); try expectEqualStrings( "all your codebase are belong to us", - lua.pushStringEx("all your codebase are belong to us"), + lua.pushString("all your codebase are belong to us"), ); lua.pushFunction(ziglua.wrap(add)); - try expectEqualStrings("hello world", lua.pushBytesEx("hello world")); - lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) }); + try expectEqualStrings("hello world", lua.pushBytes("hello world")); + _ = lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) }); lua.pushValue(1); // test both typeof and is functions @@ -459,7 +459,7 @@ test "global table" { lua.pushGlobalTable(); // find the print function - lua.pushString("print"); + _ = lua.pushString("print"); lua.getTable(-2); try expectEqual(LuaType.function, lua.typeOf(-1)); @@ -538,9 +538,9 @@ test "concat" { var lua = try Lua.init(testing.allocator); defer lua.deinit(); - lua.pushString("hello "); + _ = lua.pushString("hello "); lua.pushNumber(10); - lua.pushString(" wow!"); + _ = lua.pushString(" wow!"); lua.concat(3); try expectEqualStrings("hello 10 wow!", try lua.toBytes(-1)); @@ -578,24 +578,24 @@ test "table access" { try expectEqual(LuaType.string, lua.typeOf(-1)); try expectEqualStrings("first", try lua.toBytes(-1)); - lua.pushString("key"); + _ = lua.pushString("key"); lua.getTable(1); try expectEqual(LuaType.string, lua.typeOf(-1)); try expectEqualStrings("value", try lua.toBytes(-1)); - lua.pushString("other one"); + _ = lua.pushString("other one"); lua.rawGetTable(1); try expectEqual(LuaType.number, lua.typeOf(-1)); try expectEqual(@as(Integer, 1234), try lua.toInteger(-1)); // a.name = "ziglua" - lua.pushString("name"); - lua.pushString("ziglua"); + _ = lua.pushString("name"); + _ = lua.pushString("ziglua"); lua.setTable(1); // a.lang = "zig" - lua.pushString("lang"); - lua.pushString("zig"); + _ = lua.pushString("lang"); + _ = lua.pushString("zig"); lua.rawSetTable(1); try expectError(error.Fail, lua.getMetatable(1)); @@ -801,7 +801,7 @@ test "registry" { const key = "mykey"; // store a string in the registry - lua.pushString("hello there"); + _ = lua.pushString("hello there"); lua.rawSetPtr(ziglua.registry_index, key); // get key from the registry @@ -815,7 +815,7 @@ test "raise error" { const makeError = struct { fn inner(l: *Lua) i32 { - l.pushString("makeError made an error"); + _ = l.pushString("makeError made an error"); l.raiseError(); return 0; } @@ -830,7 +830,7 @@ fn continuation(l: *Lua) i32 { const ctxOrNull = l.getCtx() catch unreachable; const ctx = ctxOrNull orelse 0; if (ctx == 5) { - l.pushString("done"); + _ = l.pushString("done"); return 1; } else { // yield the current context value @@ -906,18 +906,18 @@ test "debug interface" { .call => { l.getInfo(.{ .l = true }, i); if (i.current_line.? != 2) panic("Expected line to be 2", .{}); - l.getLocal(i, 1) catch unreachable; + _ = l.getLocal(i, 1) catch unreachable; if ((l.toNumber(-1) catch unreachable) != 3) panic("Expected x to equal 3", .{}); }, .line => if (i.current_line.? == 4) { // modify the value of y to be 0 right before returning l.pushNumber(0); - l.setLocal(i, 2) catch unreachable; + _ = l.setLocal(i, 2) catch unreachable; }, .ret => { l.getInfo(.{ .l = true }, i); if (i.current_line.? != 4) panic("Expected line to be 4", .{}); - l.getLocal(i, 1) catch unreachable; + _ = l.getLocal(i, 1) catch unreachable; if ((l.toNumber(-1) catch unreachable) != 3) panic("Expected result to equal 3", .{}); }, else => unreachable, @@ -963,7 +963,7 @@ test "debug upvalues" { // now make the function an "add five" function lua.pushNumber(5); - try lua.setUpvalue(-2, 1); + _ = try lua.setUpvalue(-2, 1); // call the new function (should return 7) lua.pushNumber(2); @@ -1051,7 +1051,7 @@ test "aux check functions" { lua.pushFunction(function); lua.pushNil(); lua.pushInteger(3); - lua.pushBytes("hello world"); + _ = lua.pushBytes("hello world"); lua.protectedCall(3, 0, 0) catch { try expectEqualStrings("bad argument #4 to '?' (number expected, got no value)", try lua.toBytes(-1)); lua.pop(-1); @@ -1060,7 +1060,7 @@ test "aux check functions" { lua.pushFunction(function); lua.pushNil(); lua.pushInteger(3); - lua.pushBytes("hello world"); + _ = lua.pushBytes("hello world"); lua.pushNumber(4); lua.protectedCall(4, 0, 0) catch { try expectEqualStrings("bad argument #5 to '?' (string expected, got no value)", try lua.toBytes(-1)); @@ -1070,9 +1070,9 @@ test "aux check functions" { lua.pushFunction(function); lua.pushNil(); lua.pushInteger(3); - lua.pushBytes("hello world"); + _ = lua.pushBytes("hello world"); lua.pushNumber(4); - lua.pushString("hello world"); + _ = lua.pushString("hello world"); lua.protectedCall(5, 0, 0) catch { try expectEqualStrings("bad argument #6 to '?' (boolean expected, got no value)", try lua.toBytes(-1)); lua.pop(-1); @@ -1081,9 +1081,9 @@ test "aux check functions" { lua.pushFunction(function); lua.pushNil(); lua.pushInteger(3); - lua.pushBytes("hello world"); + _ = lua.pushBytes("hello world"); lua.pushNumber(4); - lua.pushString("hello world"); + _ = lua.pushString("hello world"); lua.pushBoolean(true); lua.protectedCall(6, 0, 0) catch { try expectEqualStrings("bad argument #7 to '?' (number expected, got no value)", try lua.toBytes(-1)); @@ -1093,9 +1093,9 @@ test "aux check functions" { lua.pushFunction(function); lua.pushNil(); lua.pushInteger(3); - lua.pushBytes("hello world"); + _ = lua.pushBytes("hello world"); lua.pushNumber(4); - lua.pushString("hello world"); + _ = lua.pushString("hello world"); lua.pushBoolean(true); lua.pushUnsigned(1); try lua.protectedCall(7, 0, 0); @@ -1151,9 +1151,9 @@ test "aux opt functions" { lua.pushFunction(function); lua.pushInteger(10); - lua.pushBytes("zig"); + _ = lua.pushBytes("zig"); lua.pushNumber(1.23); - lua.pushString("lang"); + _ = lua.pushString("lang"); lua.pushUnsigned(1); try lua.protectedCall(5, 0, 0); } @@ -1181,19 +1181,19 @@ test "checkOption" { }.inner); lua.pushFunction(function); - lua.pushString("one"); + _ = lua.pushString("one"); try lua.protectedCall(1, 1, 0); try expectEqual(@as(Integer, 1), try lua.toInteger(-1)); lua.pop(1); lua.pushFunction(function); - lua.pushString("two"); + _ = lua.pushString("two"); try lua.protectedCall(1, 1, 0); try expectEqual(@as(Integer, 2), try lua.toInteger(-1)); lua.pop(1); lua.pushFunction(function); - lua.pushString("three"); + _ = lua.pushString("three"); try lua.protectedCall(1, 1, 0); try expectEqual(@as(Integer, 3), try lua.toInteger(-1)); lua.pop(1); @@ -1206,7 +1206,7 @@ test "checkOption" { // check the raised error lua.pushFunction(function); - lua.pushString("unknown"); + _ = lua.pushString("unknown"); try expectError(error.Runtime, lua.protectedCall(1, 1, 0)); try expectEqualStrings("bad argument #1 to '?' (invalid option 'unknown')", try lua.toBytes(-1)); } @@ -1260,7 +1260,7 @@ test "ref" { try expectError(error.Fail, lua.ref(ziglua.registry_index)); try expectEqual(@as(Integer, 0), lua.getTop()); - lua.pushBytes("Hello there"); + _ = lua.pushBytes("Hello there"); const ref = try lua.ref(ziglua.registry_index); _ = lua.rawGetIndex(ziglua.registry_index, ref); @@ -1346,11 +1346,11 @@ test "userdata" { fn inner(l: *Lua) i32 { const ptr = l.checkUserdata(Type, 1); if (ptr.a != 1234) { - l.pushBytes("error!"); + _ = l.pushBytes("error!"); l.raiseError(); } if (ptr.b != 3.14) { - l.pushBytes("error!"); + _ = l.pushBytes("error!"); l.raiseError(); } return 1; @@ -1373,15 +1373,15 @@ test "userdata" { const testUdata = ziglua.wrap(struct { fn inner(l: *Lua) i32 { const ptr = l.testUserdata(Type, 1) catch { - l.pushBytes("error!"); + _ = l.pushBytes("error!"); l.raiseError(); }; if (ptr.a != 1234) { - l.pushBytes("error!"); + _ = l.pushBytes("error!"); l.raiseError(); } if (ptr.b != 3.14) { - l.pushBytes("error!"); + _ = l.pushBytes("error!"); l.raiseError(); } return 0; diff --git a/src/ziglua-5.3/lib.zig b/src/ziglua-5.3/lib.zig index 6f05ed4..0c3d782 100644 --- a/src/ziglua-5.3/lib.zig +++ b/src/ziglua-5.3/lib.zig @@ -547,17 +547,10 @@ pub const Lua = struct { return @intToEnum(LuaType, c.lua_getfield(lua.state, index, key.ptr)); } - /// Pushes onto the stack the value of the global name - /// Returns an error if the global does not exist (is nil) - /// See https://www.lua.org/manual/5.4/manual.html#lua_getglobal - pub fn getGlobal(lua: *Lua, name: [:0]const u8) !void { - _ = try lua.getGlobalEx(name); - } - /// Pushes onto the stack the value of the global name and returns the type of that value /// Returns an error if the global does not exist (is nil) /// See https://www.lua.org/manual/5.4/manual.html#lua_getglobal - pub fn getGlobalEx(lua: *Lua, name: [:0]const u8) !LuaType { + pub fn getGlobal(lua: *Lua, name: [:0]const u8) !LuaType { const lua_type = @intToEnum(LuaType, c.lua_getglobal(lua.state, name.ptr)); if (lua_type == .nil) return error.Fail; return lua_type; @@ -819,13 +812,8 @@ pub const Lua = struct { lua.pushClosure(c_fn, 0); } - /// Push a formatted string onto the stack - pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) void { - _ = lua.pushFStringEx(fmt, args); - } - /// Push a formatted string onto the stack and return a pointer to the string - pub fn pushFStringEx(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 { + pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 { return @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args); } @@ -847,13 +835,8 @@ pub const Lua = struct { c.lua_pushlightuserdata(lua.state, ptr); } - /// Pushes a slice of bytes onto the stack - pub fn pushBytes(lua: *Lua, bytes: []const u8) void { - _ = lua.pushBytesEx(bytes); - } - /// Pushes the bytes onto the stack. Returns a slice pointing to Lua's internal copy of the string - pub fn pushBytesEx(lua: *Lua, bytes: []const u8) []const u8 { + pub fn pushBytes(lua: *Lua, bytes: []const u8) []const u8 { return c.lua_pushlstring(lua.state, bytes.ptr, bytes.len)[0..bytes.len]; } @@ -867,26 +850,16 @@ pub const Lua = struct { c.lua_pushnumber(lua.state, n); } - /// Pushes a zero-terminated string on to the stack - pub fn pushString(lua: *Lua, str: [:0]const u8) void { - _ = lua.pushStringEx(str); - } - /// Pushes a zero-terminated string onto the stack /// Lua makes a copy of the string so `str` may be freed immediately after return /// Returns a pointer to the internal Lua string - pub fn pushStringEx(lua: *Lua, str: [:0]const u8) [:0]const u8 { + pub fn pushString(lua: *Lua, str: [:0]const u8) [:0]const u8 { return c.lua_pushstring(lua.state, str.ptr)[0..str.len :0]; } - /// Pushes this thread onto the stack - pub fn pushThread(lua: *Lua) void { - _ = lua.pushThreadEx(); - } - /// Pushes this thread onto the stack /// Returns true if this thread is the main thread of its state - pub fn pushThreadEx(lua: *Lua) bool { + pub fn pushThread(lua: *Lua) bool { return c.lua_pushthread(lua.state) != 0; } @@ -1231,14 +1204,9 @@ pub const Lua = struct { } } - /// Gets information about a local variable - pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) !void { - _ = try lua.getLocalEx(info, n); - } - /// Gets information about a local variable /// Returns the name of the local variable - pub fn getLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { + pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { var ar: Debug = undefined; ar.i_ci = @ptrCast(*c.struct_CallInfo, info.private); if (c.lua_getlocal(lua.state, &ar, n)) |name| { @@ -1268,15 +1236,10 @@ pub const Lua = struct { c.lua_sethook(lua.state, hook_fn, hook_mask, count); } - /// Sets the value of a local variable - pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) !void { - _ = try lua.setLocalEx(info, n); - } - /// Sets the value of a local variable /// Returns an error when the index is greater than the number of active locals /// Returns the name of the local variable - pub fn setLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { + pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { var ar: Debug = undefined; ar.i_ci = @ptrCast(*c.struct_CallInfo, info.private); if (c.lua_setlocal(lua.state, &ar, n)) |name| { @@ -1285,15 +1248,9 @@ pub const Lua = struct { return error.Fail; } - /// Sets the value of a closure's upvalue - /// Returns an error if the upvalu does not exist - pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) !void { - _ = try lua.setUpvalueEx(func_index, n); - } - /// Sets the value of a closure's upvalue /// Returns the name of the upvalue or an error if the upvalue does not exist - pub fn setUpvalueEx(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 { + pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 { if (c.lua_setupvalue(lua.state, func_index, n)) |name| { return std.mem.span(name); } @@ -1377,7 +1334,7 @@ pub const Lua = struct { } } - return lua.argError(arg, lua.pushFStringEx("invalid option '%s'", .{name.ptr})); + return lua.argError(arg, lua.pushFString("invalid option '%s'", .{name.ptr})); } /// Grows the stack size to top + `size` elements, raising an error if the stack cannot grow to that size diff --git a/src/ziglua-5.3/tests.zig b/src/ziglua-5.3/tests.zig index 112e945..0172452 100644 --- a/src/ziglua-5.3/tests.zig +++ b/src/ziglua-5.3/tests.zig @@ -252,14 +252,14 @@ test "type of and getting values" { lua.pushLightUserdata(&value); lua.pushNil(); lua.pushNumber(0.1); - lua.pushThread(); + _ = lua.pushThread(); try expectEqualStrings( "all your codebase are belong to us", - lua.pushStringEx("all your codebase are belong to us"), + lua.pushString("all your codebase are belong to us"), ); lua.pushFunction(ziglua.wrap(add)); - try expectEqualStrings("hello world", lua.pushBytesEx("hello world")); - lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) }); + try expectEqualStrings("hello world", lua.pushBytes("hello world")); + _ = lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) }); lua.pushValue(1); // test both typeof and is functions @@ -329,9 +329,9 @@ test "executing string contents" { try lua.loadString("a = f(2)"); try lua.protectedCall(0, 0, 0); - try expectEqual(LuaType.function, try lua.getGlobalEx("f")); + try expectEqual(LuaType.function, try lua.getGlobal("f")); lua.pop(1); - try expectEqual(LuaType.number, try lua.getGlobalEx("a")); + try expectEqual(LuaType.number, try lua.getGlobal("a")); try expectEqual(@as(i64, 12), try lua.toInteger(1)); try expectError(error.Syntax, lua.loadString("bad syntax")); @@ -415,7 +415,7 @@ test "calling a function" { defer lua.deinit(); lua.register("zigadd", ziglua.wrap(add)); - try lua.getGlobal("zigadd"); + _ = try lua.getGlobal("zigadd"); lua.pushInteger(10); lua.pushInteger(32); @@ -486,7 +486,7 @@ test "global table" { lua.pushGlobalTable(); // find the print function - lua.pushString("print"); + _ = lua.pushString("print"); try expectEqual(LuaType.function, lua.getTable(-2)); // index the global table in the global table @@ -557,9 +557,9 @@ test "concat" { var lua = try Lua.init(testing.allocator); defer lua.deinit(); - lua.pushString("hello "); + _ = lua.pushString("hello "); lua.pushNumber(10); - lua.pushString(" wow!"); + _ = lua.pushString(" wow!"); lua.concat(3); try expectEqualStrings("hello 10.0 wow!", try lua.toBytes(-1)); @@ -598,7 +598,7 @@ test "table access" { defer lua.deinit(); try lua.doString("a = { [1] = 'first', key = 'value', ['other one'] = 1234 }"); - try lua.getGlobal("a"); + _ = try lua.getGlobal("a"); try expectEqual(LuaType.string, lua.getIndex(1, 1)); try expectEqualStrings("first", try lua.toBytes(-1)); @@ -606,22 +606,22 @@ test "table access" { try expectEqual(LuaType.string, lua.rawGetIndex(1, 1)); try expectEqualStrings("first", try lua.toBytes(-1)); - lua.pushString("key"); + _ = lua.pushString("key"); try expectEqual(LuaType.string, lua.getTable(1)); try expectEqualStrings("value", try lua.toBytes(-1)); - lua.pushString("other one"); + _ = lua.pushString("other one"); try expectEqual(LuaType.number, lua.rawGetTable(1)); try expectEqual(@as(Integer, 1234), try lua.toInteger(-1)); // a.name = "ziglua" - lua.pushString("name"); - lua.pushString("ziglua"); + _ = lua.pushString("name"); + _ = lua.pushString("ziglua"); lua.setTable(1); // a.lang = "zig" - lua.pushString("lang"); - lua.pushString("zig"); + _ = lua.pushString("lang"); + _ = lua.pushString("zig"); lua.rawSetTable(1); try expectError(error.Fail, lua.getMetatable(1)); @@ -640,7 +640,7 @@ test "table access" { lua.setField(1, "bool"); try lua.doString("b = a.bool"); - try expectEqual(LuaType.boolean, try lua.getGlobalEx("b")); + try expectEqual(LuaType.boolean, try lua.getGlobal("b")); try expect(lua.toBoolean(-1)); // create array [1, 2, 3, 4, 5] @@ -696,7 +696,7 @@ test "dump and load" { // store a function in a global try lua.doString("f = function(x) return function(n) return n + x end end"); // put the function on the stack - try lua.getGlobal("f"); + _ = try lua.getGlobal("f"); const writer = struct { fn inner(l: *Lua, buf: []const u8, data: *anyopaque) bool { @@ -801,7 +801,7 @@ test "upvalues" { // call the function repeatedly, each time ensuring the result increases by one var expected: Integer = 1; while (expected <= 10) : (expected += 1) { - try lua.getGlobal("counter"); + _ = try lua.getGlobal("counter"); lua.call(0, 1); try expectEqual(expected, try lua.toInteger(-1)); lua.pop(1); @@ -813,7 +813,7 @@ test "table traversal" { defer lua.deinit(); try lua.doString("t = { key = 'value', second = true, third = 1 }"); - try lua.getGlobal("t"); + _ = try lua.getGlobal("t"); lua.pushNil(); @@ -844,7 +844,7 @@ test "registry" { const key = "mykey"; // store a string in the registry - lua.pushString("hello there"); + _ = lua.pushString("hello there"); lua.rawSetPtr(ziglua.registry_index, key); // get key from the registry @@ -858,7 +858,7 @@ test "raise error" { const makeError = struct { fn inner(l: *Lua) i32 { - l.pushString("makeError made an error"); + _ = l.pushString("makeError made an error"); l.raiseError(); return 0; } @@ -873,7 +873,7 @@ fn continuation(l: *Lua, status: ziglua.Status, ctx: isize) i32 { _ = status; if (ctx == 5) { - l.pushString("done"); + _ = l.pushString("done"); return 1; } else { // yield the current context value @@ -921,7 +921,7 @@ test "debug interface" { \\ return x + y \\end ); - try lua.getGlobal("f"); + _ = try lua.getGlobal("f"); var info: DebugInfo = undefined; lua.getInfo(.{ @@ -952,18 +952,18 @@ test "debug interface" { .call => { l.getInfo(.{ .l = true }, i); if (i.current_line.? != 2) panic("Expected line to be 2", .{}); - l.getLocal(i, 1) catch unreachable; + _ = l.getLocal(i, 1) catch unreachable; if ((l.toNumber(-1) catch unreachable) != 3) panic("Expected x to equal 3", .{}); }, .line => if (i.current_line.? == 4) { // modify the value of y to be 0 right before returning l.pushNumber(0); - l.setLocal(i, 2) catch unreachable; + _ = l.setLocal(i, 2) catch unreachable; }, .ret => { l.getInfo(.{ .l = true }, i); if (i.current_line.? != 4) panic("Expected line to be 4", .{}); - l.getLocal(i, 1) catch unreachable; + _ = l.getLocal(i, 1) catch unreachable; if ((l.toNumber(-1) catch unreachable) != 3) panic("Expected result to equal 3", .{}); }, else => unreachable, @@ -980,7 +980,7 @@ test "debug interface" { try expectEqual(@as(?ziglua.CHookFn, ziglua.wrap(hook)), lua.getHook()); try expectEqual(ziglua.HookMask{ .call = true, .line = true, .ret = true }, lua.getHookMask()); - try lua.getGlobal("f"); + _ = try lua.getGlobal("f"); lua.pushNumber(3); try lua.protectedCall(1, 1, 0); } @@ -997,7 +997,7 @@ test "debug upvalues" { \\end \\addone = f(1) ); - try lua.getGlobal("addone"); + _ = try lua.getGlobal("addone"); // index doesn't exist try expectError(error.Fail, lua.getUpvalue(1, 2)); @@ -1009,7 +1009,7 @@ test "debug upvalues" { // now make the function an "add five" function lua.pushNumber(5); - try lua.setUpvalue(-2, 1); + _ = try lua.setUpvalue(-2, 1); // call the new function (should return 7) lua.pushNumber(2); @@ -1021,8 +1021,8 @@ test "debug upvalues" { \\addthree = f(3) ); - try lua.getGlobal("addone"); - try lua.getGlobal("addthree"); + _ = try lua.getGlobal("addone"); + _ = try lua.getGlobal("addthree"); // now addone and addthree share the same upvalue lua.upvalueJoin(-2, 1, -1, 1); @@ -1096,7 +1096,7 @@ test "aux check functions" { lua.pushFunction(function); lua.pushNil(); lua.pushInteger(3); - lua.pushBytes("hello world"); + _ = lua.pushBytes("hello world"); lua.protectedCall(3, 0, 0) catch { try expectEqualStrings("bad argument #4 to '?' (number expected, got no value)", try lua.toBytes(-1)); lua.pop(-1); @@ -1105,7 +1105,7 @@ test "aux check functions" { lua.pushFunction(function); lua.pushNil(); lua.pushInteger(3); - lua.pushBytes("hello world"); + _ = lua.pushBytes("hello world"); lua.pushNumber(4); lua.protectedCall(4, 0, 0) catch { try expectEqualStrings("bad argument #5 to '?' (string expected, got no value)", try lua.toBytes(-1)); @@ -1115,9 +1115,9 @@ test "aux check functions" { lua.pushFunction(function); lua.pushNil(); lua.pushInteger(3); - lua.pushBytes("hello world"); + _ = lua.pushBytes("hello world"); lua.pushNumber(4); - lua.pushString("hello world"); + _ = lua.pushString("hello world"); lua.protectedCall(5, 0, 0) catch { try expectEqualStrings("bad argument #6 to '?' (boolean expected, got no value)", try lua.toBytes(-1)); lua.pop(-1); @@ -1126,9 +1126,9 @@ test "aux check functions" { lua.pushFunction(function); lua.pushNil(); lua.pushInteger(3); - lua.pushBytes("hello world"); + _ = lua.pushBytes("hello world"); lua.pushNumber(4); - lua.pushString("hello world"); + _ = lua.pushString("hello world"); lua.pushBoolean(true); try lua.protectedCall(6, 0, 0); } @@ -1152,7 +1152,7 @@ test "metatables" { lua.pop(1); // set the len metamethod to the function f - try lua.getGlobal("f"); + _ = try lua.getGlobal("f"); lua.setField(1, "__len"); lua.newTable(); @@ -1181,9 +1181,9 @@ test "aux opt functions" { lua.pushFunction(function); lua.pushInteger(10); - lua.pushBytes("zig"); + _ = lua.pushBytes("zig"); lua.pushNumber(1.23); - lua.pushString("lang"); + _ = lua.pushString("lang"); try lua.protectedCall(4, 0, 0); } @@ -1210,19 +1210,19 @@ test "checkOption" { }.inner); lua.pushFunction(function); - lua.pushString("one"); + _ = lua.pushString("one"); try lua.protectedCall(1, 1, 0); try expectEqual(@as(Integer, 1), try lua.toInteger(-1)); lua.pop(1); lua.pushFunction(function); - lua.pushString("two"); + _ = lua.pushString("two"); try lua.protectedCall(1, 1, 0); try expectEqual(@as(Integer, 2), try lua.toInteger(-1)); lua.pop(1); lua.pushFunction(function); - lua.pushString("three"); + _ = lua.pushString("three"); try lua.protectedCall(1, 1, 0); try expectEqual(@as(Integer, 3), try lua.toInteger(-1)); lua.pop(1); @@ -1235,7 +1235,7 @@ test "checkOption" { // check the raised error lua.pushFunction(function); - lua.pushString("unknown"); + _ = lua.pushString("unknown"); try expectError(error.Runtime, lua.protectedCall(1, 1, 0)); try expectEqualStrings("bad argument #1 to '?' (invalid option 'unknown')", try lua.toBytes(-1)); } @@ -1254,7 +1254,7 @@ test "loadBuffer" { _ = try lua.loadBuffer("global = 10", "chunkname"); try lua.protectedCall(0, ziglua.mult_return, 0); - try lua.getGlobal("global"); + _ = try lua.getGlobal("global"); try expectEqual(@as(Integer, 10), try lua.toInteger(-1)); } @@ -1277,7 +1277,7 @@ test "where" { \\ret = whereFn() ); - try lua.getGlobal("ret"); + _ = try lua.getGlobal("ret"); try expectEqualStrings("[string \"...\"]:2: ", try lua.toBytes(-1)); } @@ -1289,7 +1289,7 @@ test "ref" { try expectError(error.Fail, lua.ref(ziglua.registry_index)); try expectEqual(@as(Integer, 0), lua.getTop()); - lua.pushBytes("Hello there"); + _ = lua.pushBytes("Hello there"); const ref = try lua.ref(ziglua.registry_index); _ = lua.rawGetIndex(ziglua.registry_index, ref); @@ -1339,7 +1339,7 @@ test "traceback" { lua.setGlobal("tracebackFn"); try lua.doString("res = tracebackFn()"); - try lua.getGlobal("res"); + _ = try lua.getGlobal("res"); try expectEqualStrings("\nstack traceback:\n\t[string \"res = tracebackFn()\"]:1: in main chunk", try lua.toBytes(-1)); } @@ -1352,7 +1352,7 @@ test "getSubtable" { \\ b = {}, \\} ); - try lua.getGlobal("a"); + _ = try lua.getGlobal("a"); // get the subtable a.b try lua.getSubtable(-1, "b"); @@ -1380,11 +1380,11 @@ test "userdata" { fn inner(l: *Lua) i32 { const ptr = l.checkUserdata(Type, 1); if (ptr.a != 1234) { - l.pushBytes("error!"); + _ = l.pushBytes("error!"); l.raiseError(); } if (ptr.b != 3.14) { - l.pushBytes("error!"); + _ = l.pushBytes("error!"); l.raiseError(); } return 1; @@ -1401,15 +1401,15 @@ test "userdata" { const testUdata = ziglua.wrap(struct { fn inner(l: *Lua) i32 { const ptr = l.testUserdata(Type, 1) catch { - l.pushBytes("error!"); + _ = l.pushBytes("error!"); l.raiseError(); }; if (ptr.a != 1234) { - l.pushBytes("error!"); + _ = l.pushBytes("error!"); l.raiseError(); } if (ptr.b != 3.14) { - l.pushBytes("error!"); + _ = l.pushBytes("error!"); l.raiseError(); } return 0; diff --git a/src/ziglua-5.4/lib.zig b/src/ziglua-5.4/lib.zig index 66f2c94..7314f61 100644 --- a/src/ziglua-5.4/lib.zig +++ b/src/ziglua-5.4/lib.zig @@ -559,17 +559,10 @@ pub const Lua = struct { return @intToEnum(LuaType, c.lua_getfield(lua.state, index, key.ptr)); } - /// Pushes onto the stack the value of the global name - /// Returns an error if the global does not exist (is nil) - /// See https://www.lua.org/manual/5.4/manual.html#lua_getglobal - pub fn getGlobal(lua: *Lua, name: [:0]const u8) !void { - _ = try lua.getGlobalEx(name); - } - /// Pushes onto the stack the value of the global name and returns the type of that value /// Returns an error if the global does not exist (is nil) /// See https://www.lua.org/manual/5.4/manual.html#lua_getglobal - pub fn getGlobalEx(lua: *Lua, name: [:0]const u8) !LuaType { + pub fn getGlobal(lua: *Lua, name: [:0]const u8) !LuaType { const lua_type = @intToEnum(LuaType, c.lua_getglobal(lua.state, name.ptr)); if (lua_type == .nil) return error.Fail; return lua_type; @@ -831,13 +824,8 @@ pub const Lua = struct { lua.pushClosure(c_fn, 0); } - /// Push a formatted string onto the stack - pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) void { - _ = lua.pushFStringEx(fmt, args); - } - /// Push a formatted string onto the stack and return a pointer to the string - pub fn pushFStringEx(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 { + pub fn pushFString(lua: *Lua, fmt: [:0]const u8, args: anytype) [*:0]const u8 { return @call(.auto, c.lua_pushfstring, .{ lua.state, fmt.ptr } ++ args); } @@ -859,13 +847,8 @@ pub const Lua = struct { c.lua_pushlightuserdata(lua.state, ptr); } - /// Pushes a slice of bytes onto the stack - pub fn pushBytes(lua: *Lua, bytes: []const u8) void { - _ = lua.pushBytesEx(bytes); - } - /// Pushes the bytes onto the stack. Returns a slice pointing to Lua's internal copy of the string - pub fn pushBytesEx(lua: *Lua, bytes: []const u8) []const u8 { + pub fn pushBytes(lua: *Lua, bytes: []const u8) []const u8 { return c.lua_pushlstring(lua.state, bytes.ptr, bytes.len)[0..bytes.len]; } @@ -879,26 +862,16 @@ pub const Lua = struct { c.lua_pushnumber(lua.state, n); } - /// Pushes a zero-terminated string on to the stack - pub fn pushString(lua: *Lua, str: [:0]const u8) void { - _ = lua.pushStringEx(str); - } - /// Pushes a zero-terminated string onto the stack /// Lua makes a copy of the string so `str` may be freed immediately after return /// Returns a pointer to the internal Lua string - pub fn pushStringEx(lua: *Lua, str: [:0]const u8) [:0]const u8 { + pub fn pushString(lua: *Lua, str: [:0]const u8) [:0]const u8 { return c.lua_pushstring(lua.state, str.ptr)[0..str.len :0]; } - /// Pushes this thread onto the stack - pub fn pushThread(lua: *Lua) void { - _ = lua.pushThreadEx(); - } - /// Pushes this thread onto the stack /// Returns true if this thread is the main thread of its state - pub fn pushThreadEx(lua: *Lua) bool { + pub fn pushThread(lua: *Lua) bool { return c.lua_pushthread(lua.state) != 0; } @@ -1268,14 +1241,9 @@ pub const Lua = struct { } } - /// Gets information about a local variable - pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) !void { - _ = try lua.getLocalEx(info, n); - } - /// Gets information about a local variable /// Returns the name of the local variable - pub fn getLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { + pub fn getLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { var ar: Debug = undefined; ar.i_ci = @ptrCast(*c.struct_CallInfo, info.private); if (c.lua_getlocal(lua.state, &ar, n)) |name| { @@ -1305,15 +1273,10 @@ pub const Lua = struct { c.lua_sethook(lua.state, hook_fn, hook_mask, count); } - /// Sets the value of a local variable - pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) !void { - _ = try lua.setLocalEx(info, n); - } - /// Sets the value of a local variable /// Returns an error when the index is greater than the number of active locals /// Returns the name of the local variable - pub fn setLocalEx(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { + pub fn setLocal(lua: *Lua, info: *DebugInfo, n: i32) ![:0]const u8 { var ar: Debug = undefined; ar.i_ci = @ptrCast(*c.struct_CallInfo, info.private); if (c.lua_setlocal(lua.state, &ar, n)) |name| { @@ -1322,15 +1285,9 @@ pub const Lua = struct { return error.Fail; } - /// Sets the value of a closure's upvalue - /// Returns an error if the upvalu does not exist - pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) !void { - _ = try lua.setUpvalueEx(func_index, n); - } - /// Sets the value of a closure's upvalue /// Returns the name of the upvalue or an error if the upvalue does not exist - pub fn setUpvalueEx(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 { + pub fn setUpvalue(lua: *Lua, func_index: i32, n: i32) ![:0]const u8 { if (c.lua_setupvalue(lua.state, func_index, n)) |name| { return std.mem.span(name); } @@ -1421,7 +1378,7 @@ pub const Lua = struct { } } - return lua.argError(arg, lua.pushFStringEx("invalid option '%s'", .{name.ptr})); + return lua.argError(arg, lua.pushFString("invalid option '%s'", .{name.ptr})); } /// Grows the stack size to top + `size` elements, raising an error if the stack cannot grow to that size diff --git a/src/ziglua-5.4/tests.zig b/src/ziglua-5.4/tests.zig index 62f7c30..a02e5cb 100644 --- a/src/ziglua-5.4/tests.zig +++ b/src/ziglua-5.4/tests.zig @@ -252,14 +252,14 @@ test "type of and getting values" { lua.pushLightUserdata(&value); lua.pushNil(); lua.pushNumber(0.1); - lua.pushThread(); + _ = lua.pushThread(); try expectEqualStrings( "all your codebase are belong to us", - lua.pushStringEx("all your codebase are belong to us"), + lua.pushString("all your codebase are belong to us"), ); lua.pushFunction(ziglua.wrap(add)); - try expectEqualStrings("hello world", lua.pushBytesEx("hello world")); - lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) }); + try expectEqualStrings("hello world", lua.pushBytes("hello world")); + _ = lua.pushFString("%s %s %d", .{ "hello", "world", @as(i32, 10) }); lua.pushValue(1); // test both typeof and is functions @@ -329,9 +329,9 @@ test "executing string contents" { try lua.loadString("a = f(2)"); try lua.protectedCall(0, 0, 0); - try expectEqual(LuaType.function, try lua.getGlobalEx("f")); + try expectEqual(LuaType.function, try lua.getGlobal("f")); lua.pop(1); - try expectEqual(LuaType.number, try lua.getGlobalEx("a")); + try expectEqual(LuaType.number, try lua.getGlobal("a")); try expectEqual(@as(i64, 12), try lua.toInteger(1)); try expectError(error.Syntax, lua.loadString("bad syntax")); @@ -415,7 +415,7 @@ test "calling a function" { defer lua.deinit(); lua.register("zigadd", ziglua.wrap(add)); - try lua.getGlobal("zigadd"); + _ = try lua.getGlobal("zigadd"); lua.pushInteger(10); lua.pushInteger(32); @@ -503,7 +503,7 @@ test "global table" { lua.pushGlobalTable(); // find the print function - lua.pushString("print"); + _ = lua.pushString("print"); try expectEqual(LuaType.function, lua.getTable(-2)); // index the global table in the global table @@ -592,9 +592,9 @@ test "concat" { var lua = try Lua.init(testing.allocator); defer lua.deinit(); - lua.pushString("hello "); + _ = lua.pushString("hello "); lua.pushNumber(10); - lua.pushString(" wow!"); + _ = lua.pushString(" wow!"); lua.concat(3); try expectEqualStrings("hello 10.0 wow!", try lua.toBytes(-1)); @@ -635,7 +635,7 @@ test "table access" { defer lua.deinit(); try lua.doString("a = { [1] = 'first', key = 'value', ['other one'] = 1234 }"); - try lua.getGlobal("a"); + _ = try lua.getGlobal("a"); try expectEqual(LuaType.string, lua.getIndex(1, 1)); try expectEqualStrings("first", try lua.toBytes(-1)); @@ -643,22 +643,22 @@ test "table access" { try expectEqual(LuaType.string, lua.rawGetIndex(1, 1)); try expectEqualStrings("first", try lua.toBytes(-1)); - lua.pushString("key"); + _ = lua.pushString("key"); try expectEqual(LuaType.string, lua.getTable(1)); try expectEqualStrings("value", try lua.toBytes(-1)); - lua.pushString("other one"); + _ = lua.pushString("other one"); try expectEqual(LuaType.number, lua.rawGetTable(1)); try expectEqual(@as(Integer, 1234), try lua.toInteger(-1)); // a.name = "ziglua" - lua.pushString("name"); - lua.pushString("ziglua"); + _ = lua.pushString("name"); + _ = lua.pushString("ziglua"); lua.setTable(1); // a.lang = "zig" - lua.pushString("lang"); - lua.pushString("zig"); + _ = lua.pushString("lang"); + _ = lua.pushString("zig"); lua.rawSetTable(1); try expectError(error.Fail, lua.getMetatable(1)); @@ -677,7 +677,7 @@ test "table access" { lua.setField(1, "bool"); try lua.doString("b = a.bool"); - try expectEqual(LuaType.boolean, try lua.getGlobalEx("b")); + try expectEqual(LuaType.boolean, try lua.getGlobal("b")); try expect(lua.toBoolean(-1)); // create array [1, 2, 3, 4, 5] @@ -733,7 +733,7 @@ test "dump and load" { // store a function in a global try lua.doString("f = function(x) return function(n) return n + x end end"); // put the function on the stack - try lua.getGlobal("f"); + _ = try lua.getGlobal("f"); const writer = struct { fn inner(l: *Lua, buf: []const u8, data: *anyopaque) bool { @@ -808,7 +808,7 @@ test "userdata and uservalues" { lua.pushNumber(1234.56); try lua.setIndexUserValue(1, 1); - lua.pushString("test string"); + _ = lua.pushString("test string"); try lua.setIndexUserValue(1, 2); try expectEqual(LuaType.number, try lua.getIndexUserValue(1, 1)); @@ -846,7 +846,7 @@ test "upvalues" { // call the function repeatedly, each time ensuring the result increases by one var expected: Integer = 1; while (expected <= 10) : (expected += 1) { - try lua.getGlobal("counter"); + _ = try lua.getGlobal("counter"); lua.call(0, 1); try expectEqual(expected, try lua.toInteger(-1)); lua.pop(1); @@ -858,7 +858,7 @@ test "table traversal" { defer lua.deinit(); try lua.doString("t = { key = 'value', second = true, third = 1 }"); - try lua.getGlobal("t"); + _ = try lua.getGlobal("t"); lua.pushNil(); @@ -889,7 +889,7 @@ test "registry" { const key = "mykey"; // store a string in the registry - lua.pushString("hello there"); + _ = lua.pushString("hello there"); lua.rawSetPtr(ziglua.registry_index, key); // get key from the registry @@ -910,21 +910,21 @@ test "closing vars" { ); lua.newTable(); - try lua.getGlobal("mt"); + _ = try lua.getGlobal("mt"); lua.setMetatable(-2); lua.toClose(-1); lua.closeSlot(-1); lua.pop(1); lua.newTable(); - try lua.getGlobal("mt"); + _ = try lua.getGlobal("mt"); lua.setMetatable(-2); lua.toClose(-1); lua.closeSlot(-1); lua.pop(1); // this should have incremented "closed_vars" to 2 - try lua.getGlobal("closed_vars"); + _ = try lua.getGlobal("closed_vars"); try expectEqual(@as(Number, 2), try lua.toNumber(-1)); } @@ -934,7 +934,7 @@ test "raise error" { const makeError = struct { fn inner(l: *Lua) i32 { - l.pushString("makeError made an error"); + _ = l.pushString("makeError made an error"); l.raiseError(); return 0; } @@ -949,7 +949,7 @@ fn continuation(l: *Lua, status: ziglua.Status, ctx: isize) i32 { _ = status; if (ctx == 5) { - l.pushString("done"); + _ = l.pushString("done"); return 1; } else { // yield the current context value @@ -998,7 +998,7 @@ test "debug interface" { \\ return x + y \\end ); - try lua.getGlobal("f"); + _ = try lua.getGlobal("f"); var info: DebugInfo = undefined; lua.getInfo(.{ @@ -1029,18 +1029,18 @@ test "debug interface" { .call => { l.getInfo(.{ .l = true, .r = true }, i); if (i.current_line.? != 2) panic("Expected line to be 2", .{}); - l.getLocal(i, i.first_transfer) catch unreachable; + _ = l.getLocal(i, i.first_transfer) catch unreachable; if ((l.toNumber(-1) catch unreachable) != 3) panic("Expected x to equal 3", .{}); }, .line => if (i.current_line.? == 4) { // modify the value of y to be 0 right before returning l.pushNumber(0); - l.setLocal(i, 2) catch unreachable; + _ = l.setLocal(i, 2) catch unreachable; }, .ret => { l.getInfo(.{ .l = true, .r = true }, i); if (i.current_line.? != 4) panic("Expected line to be 4", .{}); - l.getLocal(i, i.first_transfer) catch unreachable; + _ = l.getLocal(i, i.first_transfer) catch unreachable; if ((l.toNumber(-1) catch unreachable) != 3) panic("Expected result to equal 3", .{}); }, else => unreachable, @@ -1057,7 +1057,7 @@ test "debug interface" { try expectEqual(@as(?ziglua.CHookFn, ziglua.wrap(hook)), lua.getHook()); try expectEqual(ziglua.HookMask{ .call = true, .line = true, .ret = true }, lua.getHookMask()); - try lua.getGlobal("f"); + _ = try lua.getGlobal("f"); lua.pushNumber(3); try lua.protectedCall(1, 1, 0); } @@ -1074,7 +1074,7 @@ test "debug upvalues" { \\end \\addone = f(1) ); - try lua.getGlobal("addone"); + _ = try lua.getGlobal("addone"); // index doesn't exist try expectError(error.Fail, lua.getUpvalue(1, 2)); @@ -1086,7 +1086,7 @@ test "debug upvalues" { // now make the function an "add five" function lua.pushNumber(5); - try lua.setUpvalue(-2, 1); + _ = try lua.setUpvalue(-2, 1); // test a bad index (the valid one's result is unpredicable) try expectError(error.Fail, lua.upvalueId(-1, 2)); @@ -1101,8 +1101,8 @@ test "debug upvalues" { \\addthree = f(3) ); - try lua.getGlobal("addone"); - try lua.getGlobal("addthree"); + _ = try lua.getGlobal("addone"); + _ = try lua.getGlobal("addthree"); // now addone and addthree share the same upvalue lua.upvalueJoin(-2, 1, -1, 1); @@ -1176,7 +1176,7 @@ test "aux check functions" { lua.pushFunction(function); lua.pushNil(); lua.pushInteger(3); - lua.pushBytes("hello world"); + _ = lua.pushBytes("hello world"); lua.protectedCall(3, 0, 0) catch { try expectEqualStrings("bad argument #4 to '?' (number expected, got no value)", try lua.toBytes(-1)); lua.pop(-1); @@ -1185,7 +1185,7 @@ test "aux check functions" { lua.pushFunction(function); lua.pushNil(); lua.pushInteger(3); - lua.pushBytes("hello world"); + _ = lua.pushBytes("hello world"); lua.pushNumber(4); lua.protectedCall(4, 0, 0) catch { try expectEqualStrings("bad argument #5 to '?' (string expected, got no value)", try lua.toBytes(-1)); @@ -1195,9 +1195,9 @@ test "aux check functions" { lua.pushFunction(function); lua.pushNil(); lua.pushInteger(3); - lua.pushBytes("hello world"); + _ = lua.pushBytes("hello world"); lua.pushNumber(4); - lua.pushString("hello world"); + _ = lua.pushString("hello world"); lua.protectedCall(5, 0, 0) catch { try expectEqualStrings("bad argument #6 to '?' (boolean expected, got no value)", try lua.toBytes(-1)); lua.pop(-1); @@ -1207,9 +1207,9 @@ test "aux check functions" { // test pushFail here (currently acts the same as pushNil) lua.pushFail(); lua.pushInteger(3); - lua.pushBytes("hello world"); + _ = lua.pushBytes("hello world"); lua.pushNumber(4); - lua.pushString("hello world"); + _ = lua.pushString("hello world"); lua.pushBoolean(true); try lua.protectedCall(6, 0, 0); } @@ -1233,7 +1233,7 @@ test "metatables" { lua.pop(1); // set the len metamethod to the function f - try lua.getGlobal("f"); + _ = try lua.getGlobal("f"); lua.setField(1, "__len"); lua.newTable(); @@ -1262,9 +1262,9 @@ test "aux opt functions" { lua.pushFunction(function); lua.pushInteger(10); - lua.pushBytes("zig"); + _ = lua.pushBytes("zig"); lua.pushNumber(1.23); - lua.pushString("lang"); + _ = lua.pushString("lang"); try lua.protectedCall(4, 0, 0); } @@ -1291,19 +1291,19 @@ test "checkOption" { }.inner); lua.pushFunction(function); - lua.pushString("one"); + _ = lua.pushString("one"); try lua.protectedCall(1, 1, 0); try expectEqual(@as(Integer, 1), try lua.toInteger(-1)); lua.pop(1); lua.pushFunction(function); - lua.pushString("two"); + _ = lua.pushString("two"); try lua.protectedCall(1, 1, 0); try expectEqual(@as(Integer, 2), try lua.toInteger(-1)); lua.pop(1); lua.pushFunction(function); - lua.pushString("three"); + _ = lua.pushString("three"); try lua.protectedCall(1, 1, 0); try expectEqual(@as(Integer, 3), try lua.toInteger(-1)); lua.pop(1); @@ -1316,7 +1316,7 @@ test "checkOption" { // check the raised error lua.pushFunction(function); - lua.pushString("unknown"); + _ = lua.pushString("unknown"); try expectError(error.Runtime, lua.protectedCall(1, 1, 0)); try expectEqualStrings("bad argument #1 to '?' (invalid option 'unknown')", try lua.toBytes(-1)); } @@ -1335,7 +1335,7 @@ test "loadBuffer" { _ = try lua.loadBuffer("global = 10", "chunkname"); try lua.protectedCall(0, ziglua.mult_return, 0); - try lua.getGlobal("global"); + _ = try lua.getGlobal("global"); try expectEqual(@as(Integer, 10), try lua.toInteger(-1)); } @@ -1358,7 +1358,7 @@ test "where" { \\ret = whereFn() ); - try lua.getGlobal("ret"); + _ = try lua.getGlobal("ret"); try expectEqualStrings("[string \"...\"]:2: ", try lua.toBytes(-1)); } @@ -1370,7 +1370,7 @@ test "ref" { try expectError(error.Fail, lua.ref(ziglua.registry_index)); try expectEqual(@as(Integer, 0), lua.getTop()); - lua.pushBytes("Hello there"); + _ = lua.pushBytes("Hello there"); const ref = try lua.ref(ziglua.registry_index); _ = lua.rawGetIndex(ziglua.registry_index, ref); @@ -1430,7 +1430,7 @@ test "traceback" { lua.setGlobal("tracebackFn"); try lua.doString("res = tracebackFn()"); - try lua.getGlobal("res"); + _ = try lua.getGlobal("res"); try expectEqualStrings("\nstack traceback:\n\t[string \"res = tracebackFn()\"]:1: in main chunk", try lua.toBytes(-1)); } @@ -1443,7 +1443,7 @@ test "getSubtable" { \\ b = {}, \\} ); - try lua.getGlobal("a"); + _ = try lua.getGlobal("a"); // get the subtable a.b try lua.getSubtable(-1, "b"); @@ -1471,11 +1471,11 @@ test "userdata" { fn inner(l: *Lua) i32 { const ptr = l.checkUserdata(Type, 1); if (ptr.a != 1234) { - l.pushBytes("error!"); + _ = l.pushBytes("error!"); l.raiseError(); } if (ptr.b != 3.14) { - l.pushBytes("error!"); + _ = l.pushBytes("error!"); l.raiseError(); } return 1; @@ -1492,15 +1492,15 @@ test "userdata" { const testUdata = ziglua.wrap(struct { fn inner(l: *Lua) i32 { const ptr = l.testUserdata(Type, 1) catch { - l.pushBytes("error!"); + _ = l.pushBytes("error!"); l.raiseError(); }; if (ptr.a != 1234) { - l.pushBytes("error!"); + _ = l.pushBytes("error!"); l.raiseError(); } if (ptr.b != 3.14) { - l.pushBytes("error!"); + _ = l.pushBytes("error!"); l.raiseError(); } return 0;