Skip to main content

luaur_repl_cli/functions/
sigint_callback.rs

1use core::ffi::c_int;
2use core::sync::atomic::AtomicPtr;
3
4use luaur_vm::functions::lua_callbacks::lua_callbacks;
5use luaur_vm::functions::lua_rawcheckstack::lua_rawcheckstack;
6use luaur_vm::macros::lua_l_error::luaL_error;
7use luaur_vm::type_aliases::lua_state::lua_State;
8
9// `replState` from Repl.cpp: the REPL's lua_State, used by the OS signal handler
10// to arm the interrupt callback. Stored atomically so the async-signal handler
11// (sigintHandler) can read it safely.
12pub static REPL_STATE: AtomicPtr<lua_State> = AtomicPtr::new(core::ptr::null_mut());
13
14// Ctrl-C handling. Matches the `interrupt` callback ABI on lua_Callbacks.
15pub unsafe extern "C-unwind" fn sigint_callback(l: *mut lua_State, gc: c_int) {
16    if gc >= 0 {
17        return;
18    }
19
20    (*lua_callbacks(l)).interrupt = None;
21
22    lua_rawcheckstack(l, 1); // reserve space for error string
23    luaL_error!(l, "Execution interrupted");
24}