const std = @import("std");1const python = @cImport(@cInclude("Python.h"));23// wasmGetSignalState has to be defined at the typescript level, since it potentially4// reads from a small SharedArrayBuffer that WASM does not have access to. It returns5// any pending signal.6extern fn wasmGetSignalState() i32;78export fn _Py_CheckEmscriptenSignals() void {9const signal = wasmGetSignalState();10// std.debug.print("python-wasm: _Py_CheckEmscriptenSignals: signal={}\n", .{signal});11if (signal != 0) {12// std.debug.print("_Py_CheckEmscriptenSignals: got a signal! {}\n", .{signal});13if (python.PyErr_SetInterruptEx(signal) != 0) {14std.debug.print("_Py_CheckEmscriptenSignals: ERROR -- invalid signal = {}\n", .{signal});15}16}17}1819// Upstream uses 50 for analogue of SIGNAL_INTERVAL (see Python/emscripten_signal.c in the cpython sources).20// However, this may or may not be called frequently, so not checking more could be a problem. It would21// make a lot more sense to space these out with a high performance system clock... but of course access to22// that is at least as expensive as wasmGetSignalState, so there's no point.23const SIGNAL_INTERVAL: i32 = 50;24var signal_counter: i32 = SIGNAL_INTERVAL;25export fn _Py_CheckEmscriptenSignalsPeriodically() void {26// std.debug.print("python-wasm: _Py_CheckEmscriptenSignalsPeriodically\n", .{});27signal_counter -= 1;28if (signal_counter <= 0) {29signal_counter = SIGNAL_INTERVAL;30_Py_CheckEmscriptenSignals();31}32}3334pub fn keepalive() void {}353637