Skip to content

Commit 94b581b

Browse files
luchaknatecraddock
authored andcommitted
Add a helper to make throwing string errors from interrupts easier
1 parent 918e5db commit 94b581b

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

src/lib.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2555,6 +2555,15 @@ pub const Lua = opaque {
25552555
unreachable;
25562556
}
25572557

2558+
/// Raises an error from inside a Luau interrupt
2559+
/// See https://github.com/luau-lang/luau/blob/ce8495a69e7a4e774a5402f99e1fc282a92ced91/CLI/Repl.cpp#L59
2560+
pub fn raiseInterruptErrorStr(lua: *Lua, fmt: [:0]const u8, args: anytype) noreturn {
2561+
if (lang != .luau) return;
2562+
c.lua_rawcheckstack(@ptrCast(lua), 1);
2563+
lua.raiseErrorStr(fmt, args);
2564+
unreachable;
2565+
}
2566+
25582567
/// This function produces the return values for process-related functions in the standard library
25592568
/// See https://www.lua.org/manual/5.4/manual.html#luaL_execresult
25602569
pub fn execResult(lua: *Lua, stat: i32) i32 {

src/tests.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2895,6 +2895,7 @@ test "interrupt" {
28952895
pub fn inner(l: *Lua, _: i32) void {
28962896
times_called += 1;
28972897
l.setInterruptCallbackFn(null);
2898+
l.raiseInterruptErrorStr("interrupted", .{});
28982899
}
28992900
};
29002901

@@ -2906,9 +2907,10 @@ test "interrupt" {
29062907
);
29072908
lua.setInterruptCallbackFn(ziglua.wrap(interrupt_handler.inner));
29082909

2909-
try lua.doString(
2910+
const expected_err = lua.doString(
29102911
\\c = add(1, 2)
29112912
);
2913+
try testing.expectError(ziglua.Error.Runtime, expected_err);
29122914
try testing.expectEqual(1, interrupt_handler.times_called);
29132915

29142916
// Handler should have removed itself

0 commit comments

Comments
 (0)