Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/posix-node/src/wait.zig
1067 views
1
const c = @import("c.zig");
2
const node = @import("node.zig");
3
const std = @import("std");
4
const wait = @cImport({
5
@cDefine("struct__OSUnalignedU16", "uint16_t");
6
@cDefine("struct__OSUnalignedU32", "uint32_t");
7
@cDefine("struct__OSUnalignedU64", "uint64_t");
8
@cInclude("sys/wait.h");
9
});
10
11
pub const constants = .{
12
.c_import = wait,
13
.names = [_][:0]const u8{ "WNOHANG", "WUNTRACED", "WCONTINUED" },
14
};
15
16
pub fn register(env: c.napi_env, exports: c.napi_value) !void {
17
try node.registerFunction(env, exports, "wait", wait_impl);
18
try node.registerFunction(env, exports, "wait3", wait3_impl);
19
try node.registerFunction(env, exports, "waitpid", waitpid);
20
}
21
22
fn wait_impl(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
23
_ = info;
24
var wstatus: c_int = undefined;
25
const ret = wait.wait(&wstatus);
26
if (ret == -1) {
27
node.throwErrno(env, "error calling wait.wait");
28
return null;
29
}
30
var object = node.createObject(env, "return status and value") catch return null;
31
node.setNamedProperty(env, object, "wstatus", node.create_i32(env, wstatus, "wstatus") catch return null, "wstatus") catch return null;
32
node.setNamedProperty(env, object, "ret", node.create_i32(env, ret, "return value") catch return null, "return value") catch return null;
33
return object;
34
}
35
36
// wait3: (options: number) => {ret:number; wstatus: number; /* rusage not implemented yet */ }
37
// pid_t wait3(int *stat_loc, int options, struct rusage *rusage);
38
fn wait3_impl(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
39
const argv = node.getArgv(env, info, 1) catch return null;
40
const options = node.i32FromValue(env, argv[0], "options") catch return null;
41
var wstatus: c_int = undefined;
42
const ret = wait.wait3(&wstatus, options, null);
43
if (ret == -1) {
44
node.throwErrno(env, "error calling wait.wait3");
45
return null;
46
}
47
var object = node.createObject(env, "return pid and wstatus") catch return null;
48
node.setNamedProperty(env, object, "wstatus", node.create_i32(env, wstatus, "wstatus") catch return null, "wstatus") catch return null;
49
node.setNamedProperty(env, object, "ret", node.create_i32(env, ret, "return value") catch return null, "return value") catch return null;
50
return object;
51
}
52
53
// pid_t waitpid(pid_t pid, int *wstatus, int options);
54
55
// waitpid(pid: number, options : number) => {wstatus: number, ret:number}
56
57
fn waitpid(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
58
const argv = node.getArgv(env, info, 2) catch return null;
59
const pid = node.i32FromValue(env, argv[0], "pid") catch return null;
60
const options = node.i32FromValue(env, argv[1], "options") catch return null;
61
62
var wstatus: c_int = undefined;
63
const ret = wait.waitpid(pid, &wstatus, options);
64
if (ret == -1) {
65
node.throwErrno(env, "error calling wait.waitpid");
66
return null;
67
}
68
var object = node.createObject(env, "return status and value") catch return null;
69
node.setNamedProperty(env, object, "wstatus", node.create_i32(env, wstatus, "wstatus") catch return null, "wstatus") catch return null;
70
node.setNamedProperty(env, object, "ret", node.create_i32(env, ret, "return value") catch return null, "return value") catch return null;
71
return object;
72
}
73
74