Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/posix-node/src/signal.zig
1067 views
1
const c = @import("c.zig");
2
const node = @import("node.zig");
3
const std = @import("std");
4
const signal = @cImport({
5
@cInclude("signal.h");
6
});
7
8
pub const constants = .{
9
.c_import = signal,
10
.names = [_][:0]const u8{"SIGINT"},
11
};
12
13
pub fn register(env: c.napi_env, exports: c.napi_value) !void {
14
try node.registerFunction(env, exports, "watchForSignal", watchForSignal);
15
try node.registerFunction(env, exports, "getSignalState", getSignalState);
16
}
17
18
var sigintState: bool = false;
19
fn handleSigint(sig: c_int) callconv(.C) void {
20
_ = sig;
21
// std.debug.print("Caught signal {}\n", .{sig});
22
sigintState = true;
23
}
24
25
fn watchForSignal(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
26
const argv = node.getArgv(env, info, 1) catch return null;
27
const whichSignal = node.i32FromValue(env, argv[0], "signal") catch return null;
28
if (whichSignal != signal.SIGINT) {
29
node.throwErrno(env, "only SIGINT is currently supported");
30
return null;
31
}
32
33
const r = signal.signal(signal.SIGINT, handleSigint);
34
_ = r;
35
// clib returns -1 via some scary casts into a pointer to indicate an error, and
36
// zig is not so happy with that.
37
// if (r ==-1) {
38
// node.throwErrno(env, "error setting SIGINT handler");
39
// return null;
40
// }
41
42
return null;
43
}
44
45
fn getSignalState(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
46
_ = info;
47
const state = node.create_bool(env, sigintState, "signal state") catch return null;
48
sigintState = false;
49
return state;
50
}
51
52