Skip to content

Commit

Permalink
Add a helper to make throwing string errors from interrupts easier
Browse files Browse the repository at this point in the history
  • Loading branch information
luchak committed Sep 22, 2024
1 parent dd465a5 commit ef8203b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2552,6 +2552,15 @@ pub const Lua = opaque {
unreachable;
}

/// Raises an error from inside a Luau interrupt
/// See https://github.com/luau-lang/luau/blob/ce8495a69e7a4e774a5402f99e1fc282a92ced91/CLI/Repl.cpp#L59
pub fn raiseInterruptErrorStr(lua: *Lua, fmt: [:0]const u8, args: anytype) noreturn {
if (lang != .luau) return;
c.lua_rawcheckstack(@ptrCast(lua), 1);
lua.raiseErrorStr(fmt, args);
unreachable;
}

/// This function produces the return values for process-related functions in the standard library
/// See https://www.lua.org/manual/5.4/manual.html#luaL_execresult
pub fn execResult(lua: *Lua, stat: i32) i32 {
Expand Down
4 changes: 3 additions & 1 deletion src/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2829,6 +2829,7 @@ test "interrupt" {
pub fn inner(l: *Lua, _: i32) void {
times_called += 1;
l.setInterruptCallbackFn(null);
l.raiseInterruptErrorStr("interrupted", .{});
}
};

Expand All @@ -2840,9 +2841,10 @@ test "interrupt" {
);
lua.setInterruptCallbackFn(ziglua.wrap(interrupt_handler.inner));

try lua.doString(
const expected_err = lua.doString(
\\c = add(1, 2)
);
try testing.expectError(ziglua.Error.Runtime, expected_err);
try testing.expectEqual(1, interrupt_handler.times_called);

// Handler should have removed itself
Expand Down

0 comments on commit ef8203b

Please sign in to comment.