diff --git a/examples/interpreter.zig b/examples/interpreter.zig index 4b075e8..cd2f58e 100644 --- a/examples/interpreter.zig +++ b/examples/interpreter.zig @@ -50,7 +50,7 @@ pub fn main() anyerror!void { }; // Execute a line of Lua code - lua.protectedCall(0, 0, 0) catch { + lua.protectedCall(.{}) catch { // Error handling here is the same as above. try stdout.print("{s}\n", .{lua.toString(-1) catch unreachable}); lua.pop(1); diff --git a/examples/luau-bytecode.zig b/examples/luau-bytecode.zig index a6fe94d..b0f2d8a 100644 --- a/examples/luau-bytecode.zig +++ b/examples/luau-bytecode.zig @@ -31,5 +31,5 @@ pub fn main() anyerror!void { defer allocator.free(bc); try lua.loadBytecode("...", bc); - try lua.protectedCall(0, 0, 0); + try lua.protectedCall(.{}); } diff --git a/examples/zig-fn.zig b/examples/zig-fn.zig index 84ce122..17dfeaf 100644 --- a/examples/zig-fn.zig +++ b/examples/zig-fn.zig @@ -39,7 +39,7 @@ pub fn main() anyerror!void { // Call the function. It accepts 2 arguments and returns 1 value // We use catch unreachable because we can verify this function call will not fail - lua.protectedCall(2, 1, 0) catch unreachable; + lua.protectedCall(.{ .args = 2, .results = 1 }) catch unreachable; // The result of the function call is on the stack. // Use toInteger to read the integer at index 1 diff --git a/src/lib.zig b/src/lib.zig index 840bd7f..eb9487b 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -733,6 +733,13 @@ pub const Lua = opaque { return c.lua_atpanic(@ptrCast(lua), panic_fn); } + pub const CallArgs = struct { + /// The number of args passed to the function from the stack + args: i32 = 0, + /// The number of results the function pushes to the stack upon returning + results: i32 = 0, + }; + /// Calls a function /// /// Like regular Lua calls, `Lua.call()` respects the `__call` metamethod. So, here the word "function" means any callable value. @@ -741,49 +748,60 @@ pub const Lua = opaque { /// * First, the function to be called is pushed onto the stack /// * Then, the arguments to the call are pushed in direct order that is, the first argument is pushed first. /// * Finally you call `Lua.call()` - /// * `num_args` is the number of arguments that you pushed onto the stack. When the function returns, all arguments and + /// * `args.args` is the number of arguments that you pushed onto the stack. When the function returns, all arguments and /// the function value are popped and the call results are pushed onto the stack. - /// * The number of results is adjusted to `num_results`, unless `num_results` is `ziglua.mult_return`. In this case, all results from the function are pushed + /// * The number of results is adjusted to `args.results`, unless `args.results` is `ziglua.mult_return`. In this case, all results from the function are pushed /// * Lua takes care that the returned values fit into the stack space, but it does not ensure any extra space in the stack. The function results /// are pushed onto the stack in direct order (the first result is pushed first), so that after the call the last result is /// on the top of the stack. /// /// Any error while calling and running the function is propagated upwards (with a longjmp). /// - /// * Pops: `(num_args+1)` - /// * Pushes: `num_results` + /// * Pops: `(args.args+1)` + /// * Pushes: `args.results` /// * Errors: `other` /// /// See https://www.lua.org/manual/5.4/manual.html#lua_call - pub fn call(lua: *Lua, num_args: i32, num_results: i32) void { + pub fn call(lua: *Lua, args: CallArgs) void { switch (lang) { - .lua51, .luajit, .luau => c.lua_call(@ptrCast(lua), num_args, num_results), - else => lua.callCont(num_args, num_results, 0, null), + .lua51, .luajit, .luau => c.lua_call(@ptrCast(lua), args.args, args.results), + else => c.lua_callk(@ptrCast(lua), args.args, args.results, 0, null), } } - fn callCont52(lua: *Lua, num_args: i32, num_results: i32, ctx: i32, k: ?CFn) void { - c.lua_callk(@ptrCast(lua), num_args, num_results, ctx, k); - } - - fn callCont53(lua: *Lua, num_args: i32, num_results: i32, ctx: Context, k: ?CContFn) void { - c.lua_callk(@ptrCast(lua), num_args, num_results, ctx, k); - } + /// Continuations are supported in Lua 5.2, 5.3, and 5.4 + /// See https://www.lua.org/manual/5.4/manual.html#4.5 + pub const CallContArgs = struct { + /// The number of args passed to the function from the stack + args: i32 = 0, + /// The number of results the function pushes to the stack upon returning + results: i32 = 0, + /// Context value passed to the continuation function + ctx: switch (lang) { + .lua52 => i32, + .lua53, .lua54 => Context, + else => void, + }, + /// The continuation function + k: switch (lang) { + .lua52 => CFn, + .lua53, .lua54 => CContFn, + else => void, + }, + }; /// This function behaves exactly like `Lua.call()`, but allows the called function to yield /// /// Not implemented in Lua 5.1, LuaJIT, or Luau /// - /// * Pops: `(nargs + 1)` - /// * Pushes: `nresults` + /// * Pops: `(args.args + 1)` + /// * Pushes: `args.results` /// * Errors: `other` /// /// See https://www.lua.org/manual/5.4/manual.html#lua_callk - pub const callCont = switch (lang) { - .lua52 => callCont52, - .lua53, .lua54 => callCont53, - else => @compileError("callCont() not defined"), - }; + pub fn callCont(lua: *Lua, args: CallContArgs) void { + c.lua_callk(@ptrCast(lua), args.args, args.results, args.ctx, args.k); + } /// Ensures that the stack has space for at least `n` extra elements, that is, that you can safely push up to `n` values into /// it. It returns an error if it cannot fulfill the request, either because it would cause the stack to be greater than a @@ -1710,23 +1728,37 @@ pub const Lua = opaque { return c.lua_objlen(@ptrCast(lua), index); } - fn protectedCall51(lua: *Lua, num_args: i32, num_results: i32, err_func: i32) !void { - // The translate-c version of lua_pcall does not type-check so we must rewrite it - // (macros don't always translate well with translate-c) - const ret = c.lua_pcall(@ptrCast(lua), num_args, num_results, err_func); - switch (ret) { - StatusCode.ok => return, - StatusCode.err_runtime => return error.LuaRuntime, - StatusCode.err_memory => return error.OutOfMemory, - StatusCode.err_error => return error.LuaMsgHandler, - else => unreachable, - } - } + pub const ProtectedCallArgs = struct { + /// The number of args passed to the function from the stack + args: i32 = 0, + /// The number of results the function pushes to the stack upon returning + results: i32 = 0, + /// The stack index of a message handler (known as errfunc in some versions of Lua) + msg_handler: i32 = 0, + }; - fn protectedCall52(lua: *Lua, num_args: i32, num_results: i32, msg_handler: i32) !void { - // The translate-c version of lua_pcall does not type-check so we must rewrite it - // (macros don't always translate well with translate-c) - const ret = c.lua_pcallk(@ptrCast(lua), num_args, num_results, msg_handler, 0, null); + /// Calls a function (or a callable object) in protected mode. + /// Both `args.args` and `args.results` have the same meaning as in `Lua.call()`. If there are no errors during the call, `Lua.protectedCall()` behaves + /// exactly like `Lua.call()`. However, if there is any error, `Lua.protectedCall()` catches it, pushes a single value on the stack (the + /// error object), and returns an error code. Like `Lua.call()`, `Lua.protectedCall()` always removes the function and its arguments from + /// the stack. + /// + /// If `args.msg_handler` is 0, then the error object returned on the stack is exactly the original error object. Otherwise, `args.msg_handler` is the + /// stack index of a message handler. (This index cannot be a pseudo-index.) In case of runtime errors, this handler will + /// be called with the error object and its return value will be the object returned on the stack by `Lua.protectedCall()`. + /// Typically, the message handler is used to add more debug information to the error object, such as a stack traceback. + /// Such information cannot be gathered after the return of `Lua.protectedCall()`, since by then the stack has unwound. + /// + /// * Pops: `(args.args + 1)` + /// * Pushes: `(args.results|1)` + /// * Errors: `never` + /// + /// See https://www.lua.org/manual/5.4/manual.html#lua_pcallk + pub fn protectedCall(lua: *Lua, args: ProtectedCallArgs) !void { + const ret = switch (lang) { + .lua51, .luajit, .luau => c.lua_pcall(@ptrCast(lua), args.args, args.results, args.msg_handler), + else => c.lua_pcallk(@ptrCast(lua), args.args, args.results, args.msg_handler, 0, null), + }; return switch (lang) { .lua54 => switch (ret) { @@ -1736,7 +1768,7 @@ pub const Lua = opaque { StatusCode.err_error => return error.LuaMsgHandler, else => unreachable, }, - else => switch (ret) { + .lua52, .lua53 => switch (ret) { StatusCode.ok => return, StatusCode.err_runtime => return error.LuaRuntime, StatusCode.err_memory => return error.OutOfMemory, @@ -1744,30 +1776,49 @@ pub const Lua = opaque { StatusCode.err_gcmm => return error.LuaGCMetaMethod, else => unreachable, }, + else => switch (ret) { + StatusCode.ok => return, + StatusCode.err_runtime => return error.LuaRuntime, + StatusCode.err_memory => return error.OutOfMemory, + StatusCode.err_error => return error.LuaMsgHandler, + else => unreachable, + }, }; } - /// Calls a function (or callable object) in protected mode - /// See https://www.lua.org/manual/5.4/manual.html#lua_pcall - pub const protectedCall = switch (lang) { - .lua51, .luajit, .luau => protectedCall51, - else => protectedCall52, + pub const ProtectedCallContArgs = struct { + /// The number of args passed to the function from the stack + args: i32 = 0, + /// The number of results the function pushes to the stack upon returning + results: i32 = 0, + /// The stack index of a message handler (known as errfunc in some versions of Lua) + msg_handler: i32 = 0, + /// Context value passed to the continuation function + ctx: switch (lang) { + .lua52 => i32, + .lua53, .lua54 => Context, + else => void, + }, + /// The continuation function + k: switch (lang) { + .lua52 => CFn, + .lua53, .lua54 => CContFn, + else => void, + }, }; - fn protectedCallCont52(lua: *Lua, num_args: i32, num_results: i32, msg_handler: i32, ctx: i32, k: CFn) !void { - const ret = c.lua_pcallk(@ptrCast(lua), num_args, num_results, msg_handler, ctx, k); - switch (ret) { - StatusCode.ok => return, - StatusCode.err_runtime => return error.LuaRuntime, - StatusCode.err_memory => return error.OutOfMemory, - StatusCode.err_error => return error.LuaMsgHandler, - StatusCode.err_gcmm => return error.LuaGCMetaMethod, - else => unreachable, - } - } - - fn protectedCallCont53(lua: *Lua, num_args: i32, num_results: i32, msg_handler: i32, ctx: Context, k: CContFn) !void { - const ret = c.lua_pcallk(@ptrCast(lua), num_args, num_results, msg_handler, ctx, k); + /// This function behaves exactly like `Lua.protectedCall()`, except that it allows the called function to yield + /// + /// * Pops: `(nargs + 1)` + /// * Pushes: `(nresults|1)` + /// * Errors: `never` + /// + /// See https://www.lua.org/manual/5.4/manual.html#lua_pcallk + pub fn protectedCallCont(lua: *Lua, args: ProtectedCallContArgs) !void { + const ret = switch (lang) { + .lua51, .luajit, .luau => c.lua_pcall(@ptrCast(lua), args.args, args.results, args.msg_handler), + else => c.lua_pcallk(@ptrCast(lua), args.args, args.results, args.msg_handler, 0, null), + }; return switch (lang) { .lua54 => switch (ret) { @@ -1777,7 +1828,7 @@ pub const Lua = opaque { StatusCode.err_error => return error.LuaMsgHandler, else => unreachable, }, - else => switch (ret) { + .lua52, .lua53 => switch (ret) { StatusCode.ok => return, StatusCode.err_runtime => return error.LuaRuntime, StatusCode.err_memory => return error.OutOfMemory, @@ -1785,17 +1836,10 @@ pub const Lua = opaque { StatusCode.err_gcmm => return error.LuaGCMetaMethod, else => unreachable, }, + else => @compileError("Only implemented in Lua 5.2, 5.3, and 5.4"), }; } - /// Behaves exactly like `Lua.protectedCall()` except that it allows the called function to yield - /// See https://www.lua.org/manual/5.4/manual.html#lua_pcallk - pub const protectedCallCont = switch (lang) { - .lua52 => protectedCallCont52, - .lua53, .lua54 => protectedCallCont53, - else => @compileError("protectedCallCont() not implemented"), - }; - /// Pops `n` elements from the top of the stack /// /// * Pops: `n` @@ -3008,7 +3052,7 @@ pub const Lua = opaque { .luajit, .lua51 => try lua.loadFile(file_name), else => try lua.loadFile(file_name, .binary_text), } - try lua.protectedCall(0, mult_return, 0); + try lua.protectedCall(.{ .results = mult_return }); } /// Loads and runs the given string @@ -3016,7 +3060,7 @@ pub const Lua = opaque { pub fn doString(lua: *Lua, str: [:0]const u8) !void { // trnaslate-c failure try lua.loadString(str); - try lua.protectedCall(0, mult_return, 0); + try lua.protectedCall(.{ .results = mult_return }); } /// Raises an error @@ -3314,7 +3358,7 @@ pub const Lua = opaque { .lua51, .luajit, .luau => { lua.pushFunction(open_fn); _ = lua.pushStringZ(mod_name); - lua.call(1, 0); + lua.call(.{ .args = 1 }); }, else => c.luaL_requiref(@ptrCast(lua), mod_name.ptr, open_fn, @intFromBool(global)), } @@ -3665,7 +3709,7 @@ pub const Lua = opaque { if (lua.getMetaField(-1, "__index")) |_| { lua.pushValue(-2); lua.pushInteger(@intCast(i + 1)); - lua.call(2, 1); + lua.call(.{ .args = 1, .results = 1 }); } else |_| { _ = lua.rawGetIndex(-1, @intCast(i + 1)); } @@ -3828,7 +3872,7 @@ pub const Lua = opaque { } const num_results = if (ReturnType == void) 0 else 1; - try lua.protectedCall(args.len, num_results, 0); + try lua.protectedCall(.{ .args = args.len, .results = num_results }); } ///automatically calls a lua function with the given arguments diff --git a/src/tests.zig b/src/tests.zig index 94af63a..cb522b4 100644 --- a/src/tests.zig +++ b/src/tests.zig @@ -59,7 +59,7 @@ test "Zig allocator access" { lua.pushFunction(ziglua.wrap(inner)); lua.pushInteger(10); - try lua.protectedCall(1, 1, 0); + try lua.protectedCall(.{ .args = 1, .results = 1 }); try expectEqual(45, try lua.toInteger(-1)); } @@ -320,16 +320,16 @@ test "executing string contents" { lua.openLibs(); try lua.loadString("f = function(x) return x + 10 end"); - try lua.protectedCall(0, 0, 0); + try lua.protectedCall(.{}); try lua.loadString("a = f(2)"); - try lua.protectedCall(0, 0, 0); + try lua.protectedCall(.{}); try expectEqual(.number, try lua.getGlobal("a")); try expectEqual(12, try lua.toInteger(1)); try expectError(if (ziglua.lang == .luau) error.LuaError else error.LuaSyntax, lua.loadString("bad syntax")); try lua.loadString("a = g()"); - try expectError(error.LuaRuntime, lua.protectedCall(0, 0, 0)); + try expectError(error.LuaRuntime, lua.protectedCall(.{})); } test "filling and checking the stack" { @@ -441,7 +441,7 @@ test "calling a function" { lua.pushInteger(32); // protectedCall is preferred, but we might as well test call when we know it is safe - lua.call(2, 1); + lua.call(.{ .args = 2, .results = 1 }); try expectEqual(42, try lua.toInteger(1)); } @@ -595,7 +595,7 @@ test "function registration" { _ = lua.getField(-1, "add"); lua.pushInteger(1); lua.pushInteger(2); - try lua.protectedCall(2, 1, 0); + try lua.protectedCall(.{ .args = 2, .results = 1 }); try expectEqual(3, lua.toInteger(-1)); lua.setTop(0); @@ -607,7 +607,7 @@ test "function registration" { _ = lua.getField(-1, "add"); lua.pushInteger(1); lua.pushInteger(2); - try lua.protectedCall(2, 1, 0); + try lua.protectedCall(.{ .args = 2, .results = 1 }); try expectEqual(3, lua.toInteger(-1)); return; @@ -634,7 +634,7 @@ test "function registration" { // also try calling the sub function sub(42, 40) lua.pushInteger(42); lua.pushInteger(40); - try lua.protectedCall(2, 1, 0); + try lua.protectedCall(.{ .args = 2, .results = 1 }); try expectEqual(2, try lua.toInteger(-1)); // now test the newlib variation to build a library from functions @@ -912,11 +912,11 @@ test "dump and load" { // run the function (creating a new function) lua.pushInteger(5); - try lua.protectedCall(1, 1, 0); + try lua.protectedCall(.{ .args = 1, .results = 1 }); // now call the new function (which should return the value + 5) lua.pushInteger(6); - try lua.protectedCall(1, 1, 0); + try lua.protectedCall(.{ .args = 1, .results = 1 }); try expectEqual(11, try lua.toInteger(-1)); } @@ -1002,7 +1002,7 @@ test "upvalues" { var expected: i32 = 1; while (expected <= 10) : (expected += 1) { _ = try lua.getGlobal("counter"); - lua.call(0, 1); + lua.call(.{ .results = 1 }); try expectEqual(expected, try lua.toInteger(-1)); lua.pop(1); } @@ -1100,7 +1100,7 @@ test "raise error" { }.inner; lua.pushFunction(ziglua.wrap(makeError)); - try expectError(error.LuaRuntime, lua.protectedCall(0, 0, 0)); + try expectError(error.LuaRuntime, lua.protectedCall(.{})); try expectEqualStrings("makeError made an error", try lua.toString(-1)); } @@ -1274,14 +1274,14 @@ test "aux check functions" { }.inner); lua.pushFunction(function); - lua.protectedCall(0, 0, 0) catch { + lua.protectedCall(.{ .args = 0 }) catch { try expectStringContains("argument #1", try lua.toString(-1)); lua.pop(-1); }; lua.pushFunction(function); lua.pushNil(); - lua.protectedCall(1, 0, 0) catch { + lua.protectedCall(.{ .args = 1 }) catch { try expectStringContains("number expected", try lua.toString(-1)); lua.pop(-1); }; @@ -1289,7 +1289,7 @@ test "aux check functions" { lua.pushFunction(function); lua.pushNil(); lua.pushInteger(3); - lua.protectedCall(2, 0, 0) catch { + lua.protectedCall(.{ .args = 2 }) catch { try expectStringContains("string expected", try lua.toString(-1)); lua.pop(-1); }; @@ -1298,7 +1298,7 @@ test "aux check functions" { lua.pushNil(); lua.pushInteger(3); lua.pushNumber(4); - lua.protectedCall(3, 0, 0) catch { + lua.protectedCall(.{ .args = 3 }) catch { try expectStringContains("string expected", try lua.toString(-1)); lua.pop(-1); }; @@ -1308,7 +1308,7 @@ test "aux check functions" { lua.pushInteger(3); lua.pushNumber(4); _ = lua.pushString("hello world"); - lua.protectedCall(4, 0, 0) catch { + lua.protectedCall(.{ .args = 4 }) catch { try expectStringContains("boolean expected", try lua.toString(-1)); lua.pop(-1); }; @@ -1320,7 +1320,7 @@ test "aux check functions" { lua.pushNumber(4); _ = lua.pushString("hello world"); lua.pushBoolean(true); - lua.protectedCall(5, 0, 0) catch { + lua.protectedCall(.{ .args = 5 }) catch { try expectEqualStrings("bad argument #6 to '?' (number expected, got no value)", try lua.toString(-1)); lua.pop(-1); }; @@ -1335,8 +1335,8 @@ test "aux check functions" { lua.pushBoolean(true); if (ziglua.lang == .lua52) { lua.pushUnsigned(1); - try lua.protectedCall(6, 0, 0); - } else try lua.protectedCall(5, 0, 0); + try lua.protectedCall(.{ .args = 6 }); + } else try lua.protectedCall(.{ .args = 5 }); } test "aux opt functions" { @@ -1354,14 +1354,14 @@ test "aux opt functions" { }.inner); lua.pushFunction(function); - try lua.protectedCall(0, 0, 0); + try lua.protectedCall(.{}); lua.pushFunction(function); lua.pushInteger(10); _ = lua.pushString("zig"); lua.pushNumber(1.23); _ = lua.pushStringZ("lang"); - try lua.protectedCall(4, 0, 0); + try lua.protectedCall(.{ .args = 4 }); } test "checkOption" { @@ -1388,32 +1388,32 @@ test "checkOption" { lua.pushFunction(function); _ = lua.pushStringZ("one"); - try lua.protectedCall(1, 1, 0); + try lua.protectedCall(.{ .args = 1, .results = 1 }); try expectEqual(1, try lua.toInteger(-1)); lua.pop(1); lua.pushFunction(function); _ = lua.pushStringZ("two"); - try lua.protectedCall(1, 1, 0); + try lua.protectedCall(.{ .args = 1, .results = 1 }); try expectEqual(2, try lua.toInteger(-1)); lua.pop(1); lua.pushFunction(function); _ = lua.pushStringZ("three"); - try lua.protectedCall(1, 1, 0); + try lua.protectedCall(.{ .args = 1, .results = 1 }); try expectEqual(3, try lua.toInteger(-1)); lua.pop(1); // try the default now lua.pushFunction(function); - try lua.protectedCall(0, 1, 0); + try lua.protectedCall(.{ .results = 1 }); try expectEqual(1, try lua.toInteger(-1)); lua.pop(1); // check the raised error lua.pushFunction(function); _ = lua.pushStringZ("unknown"); - try expectError(error.LuaRuntime, lua.protectedCall(1, 1, 0)); + try expectError(error.LuaRuntime, lua.protectedCall(.{ .args = 1, .results = 1 })); try expectStringContains("(invalid option 'unknown')", try lua.toString(-1)); } @@ -1446,7 +1446,7 @@ test "loadBuffer" { _ = try lua.loadBuffer("global = 10", "chunkname"); } else _ = try lua.loadBuffer("global = 10", "chunkname", .text); - try lua.protectedCall(0, ziglua.mult_return, 0); + try lua.protectedCall(.{ .args = 0, .results = ziglua.mult_return }); _ = try lua.getGlobal("global"); try expectEqual(10, try lua.toInteger(-1)); } @@ -1556,7 +1556,7 @@ test "args and errors" { }.inner); lua.pushFunction(argCheck); - try expectError(error.LuaRuntime, lua.protectedCall(0, 0, 0)); + try expectError(error.LuaRuntime, lua.protectedCall(.{})); const raisesError = ziglua.wrap(struct { fn inner(l: *Lua) i32 { @@ -1566,7 +1566,7 @@ test "args and errors" { }.inner); lua.pushFunction(raisesError); - try expectError(error.LuaRuntime, lua.protectedCall(0, 0, 0)); + try expectError(error.LuaRuntime, lua.protectedCall(.{})); try expectEqualStrings("some error zig!", try lua.toString(-1)); if (ziglua.lang != .lua54) return; @@ -1579,7 +1579,7 @@ test "args and errors" { }.inner); lua.pushFunction(argExpected); - try expectError(error.LuaRuntime, lua.protectedCall(0, 0, 0)); + try expectError(error.LuaRuntime, lua.protectedCall(.{})); } test "traceback" { @@ -1662,7 +1662,7 @@ test "userdata" { // call checkUdata asserting that the udata passed in with the // correct metatable and values - try lua.protectedCall(1, 1, 0); + try lua.protectedCall(.{ .args = 1, .results = 1 }); } if (langIn(.{ .lua51, .luajit, .luau })) return; @@ -1692,7 +1692,7 @@ test "userdata" { // call checkUdata asserting that the udata passed in with the // correct metatable and values - try lua.protectedCall(1, 0, 0); + try lua.protectedCall(.{ .args = 1 }); } } @@ -1733,7 +1733,7 @@ test "userdata slices" { lua.pushFunction(ziglua.wrap(udataFn)); lua.pushValue(2); - try lua.protectedCall(1, 0, 0); + try lua.protectedCall(.{ .args = 1 }); } test "function environments" { @@ -1749,7 +1749,7 @@ test "function environments" { lua.setGlobal("x"); _ = try lua.getGlobal("test"); - try lua.protectedCall(0, 1, 0); + try lua.protectedCall(.{ .results = 1 }); try testing.expectEqual(10, lua.toInteger(1)); lua.pop(1); @@ -1760,7 +1760,7 @@ test "function environments" { lua.setField(2, "x"); try lua.setFnEnvironment(1); - try lua.protectedCall(0, 1, 0); + try lua.protectedCall(.{ .results = 1 }); try testing.expectEqual(20, lua.toInteger(1)); lua.pop(1); @@ -1856,7 +1856,7 @@ test "debug interface" { _ = try lua.getGlobal("f"); lua.pushNumber(3); - try lua.protectedCall(1, 1, 0); + try lua.protectedCall(.{ .args = 1, .results = 1 }); } test "debug interface Lua 5.1 and Luau" { @@ -1946,7 +1946,7 @@ test "debug interface Lua 5.1 and Luau" { _ = try lua.getGlobal("f"); lua.pushNumber(3); - try lua.protectedCall(1, 1, 0); + try lua.protectedCall(.{ .args = 1, .results = 1 }); } test "debug upvalues" { @@ -1980,7 +1980,7 @@ test "debug upvalues" { // call the new function (should return 7) lua.pushNumber(2); - try lua.protectedCall(1, 1, 0); + try lua.protectedCall(.{ .args = 1, .results = 1 }); try expectEqual(7, try lua.toNumber(-1)); if (langIn(.{ .lua51, .luajit, .luau })) return; @@ -2041,7 +2041,7 @@ test "compile and run bytecode" { defer testing.allocator.free(bc); try lua.loadBytecode("...", bc); - try lua.protectedCall(0, 1, 0); + try lua.protectedCall(.{ .results = 1 }); const v = try lua.toInteger(-1); try expectEqual(133, v); @@ -2155,7 +2155,7 @@ test "luau vectors" { \\end ); _ = try lua.getGlobal("test"); - try lua.protectedCall(0, 1, 0); + try lua.protectedCall(.{ .results = 1 }); var v = try lua.toVector(-1); try testing.expectEqualSlices(f32, &[3]f32{ 10, 14, 18 }, v[0..3]); @@ -2909,7 +2909,7 @@ test "error union for CFn" { // This will fail because there is no argument passed lua.pushFunction(ziglua.wrap(fails)); - lua.protectedCall(0, 0, 0) catch { + lua.protectedCall(.{}) catch { // Get the error string try expectEqualStrings("MissingInteger", try lua.toString(-1)); };