Skip to main content

luaur_repl_cli/functions/
sigint_handler_repl.rs

1use core::ffi::c_int;
2use core::sync::atomic::Ordering;
3
4use luaur_vm::functions::lua_callbacks::lua_callbacks;
5
6use crate::functions::sigint_callback::{sigint_callback, REPL_STATE};
7
8// SIGINT value on POSIX systems.
9const SIGINT: c_int = 2;
10
11// Unix variant of Repl.cpp's `sigintHandler`:
12//
13//     static void sigintHandler(int signum)
14//     {
15//         if (signum == SIGINT && replState)
16//             lua_callbacks(replState)->interrupt = &sigintCallback;
17//     }
18//
19// Installed with `signal(SIGINT, sigintHandler)`; it merely arms the interrupt
20// callback so the VM raises "Execution interrupted" at the next safe point.
21pub unsafe extern "C" fn sigint_handler(signum: c_int) {
22    let repl_state = REPL_STATE.load(Ordering::SeqCst);
23    if signum == SIGINT && !repl_state.is_null() {
24        (*lua_callbacks(repl_state)).interrupt = Some(sigint_callback);
25    }
26}