Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/core/posix-node/src/constants.zig
1067 views
1
// Important; make sure to compile on all supported architectures to make sure these are
2
// all available.
3
// To add new constants, define constants in a module (see how it is defined in netdb or unistd),
4
// then update setting NAMES and VALUES below.
5
6
const c = @import("c.zig");
7
const node = @import("node.zig");
8
const netdb = @import("netdb.zig");
9
const signal = @import("signal.zig");
10
const socket = @import("socket.zig");
11
const termios = @import("termios.zig");
12
const unistd = @import("unistd.zig");
13
const util = @import("util.zig");
14
const wait = @import("wait.zig");
15
16
const NAMES = netdb.constants.names ++ unistd.constants.names ++ wait.constants.names ++ signal.constants.names ++ socket.constants.names ++ termios.constants.names ++ util.constants.names;
17
18
const VALUES = getValues(netdb.constants) ++ getValues(unistd.constants) ++ getValues(wait.constants) ++ getValues(signal.constants) ++ getValues(socket.constants) ++ getValues(termios.constants) ++ getValues(util.constants);
19
20
// You shouldn't have to change anything below.
21
22
pub fn register(env: c.napi_env, exports: c.napi_value) !void {
23
try node.registerFunction(env, exports, "getConstants", getConstants);
24
}
25
26
fn getConstants(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
27
_ = info;
28
var obj = node.createObject(env, "") catch return null;
29
var i: usize = 0;
30
for (NAMES) |name| {
31
setConstant(env, obj, name, VALUES[i]) catch return null;
32
i += 1;
33
}
34
return obj;
35
}
36
37
fn getValues(comptime constants: anytype) [constants.names.len]u32 {
38
return _getValues(constants.c_import, constants.names.len, constants.names);
39
}
40
41
fn _getValues(comptime c_import: anytype, comptime len: usize, comptime names: [len][:0]const u8) [len]u32 {
42
var x: [names.len]u32 = undefined;
43
var i = 0;
44
for (names) |constant| {
45
x[i] = @field(c_import, constant);
46
i += 1;
47
}
48
return x;
49
}
50
51
fn setConstant(env: c.napi_env, object: c.napi_value, key: [:0]const u8, value: u32) !void {
52
var result: c.napi_value = undefined;
53
if (c.napi_create_uint32(env, value, &result) != c.napi_ok) {
54
return node.throw(env, "error creating u32 constant");
55
}
56
if (c.napi_set_named_property(env, object, @ptrCast([*c]const u8, key), result) != c.napi_ok) {
57
return node.throw(env, "error setting constant");
58
}
59
}
60
61