-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_values.zig
More file actions
277 lines (229 loc) · 8.67 KB
/
node_values.zig
File metadata and controls
277 lines (229 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
const std = @import("std");
const lib = @import("c.zig");
const c = lib.c;
const s2e = lib.statusToError;
const Convert = @import("Convert.zig");
// https://nodejs.org/api/n-api.html#napi_valuetype
pub const NodeValueType = enum {
Undefined,
Null,
Boolean,
Number,
String,
Symbol,
Object,
Function,
External,
BigInt,
};
// TODO: use napi_create_reference to support NodeValue.reference()?
// napi_int8_array,
// napi_uint8_array,
// napi_uint8_clamped_array,
// napi_int16_array,
// napi_uint16_array,
// napi_int32_array,
// napi_uint32_array,
// napi_float32_array,
// napi_float64_array,
// napi_bigint64_array,
// napi_biguint64_array,
/// Represents a JS value.
pub const NodeValue = struct {
const Self = @This();
napi_env: c.napi_env,
napi_value: c.napi_value,
pub fn init(env: c.napi_env, value: c.napi_value) Self {
return .{ .napi_env = env, .napi_value = value };
}
pub fn typeof(self: NodeValue.Self) !NodeValueType {
var result: c.napi_valuetype = undefined;
try s2e(c.napi_typeof(self.napi_env, self.napi_value, &result));
return switch (result) {
c.napi_undefined => NodeValueType.Undefined,
c.napi_null => NodeValueType.Null,
c.napi_boolean => NodeValueType.Boolean,
c.napi_number => NodeValueType.Number,
c.napi_string => NodeValueType.String,
c.napi_symbol => NodeValueType.Symbol,
c.napi_object => NodeValueType.Object,
c.napi_function => NodeValueType.Function,
c.napi_external => NodeValueType.External,
c.napi_bigint => NodeValueType.BigInt,
else => return error.UnknownType,
};
}
pub fn asDateValue(self: NodeValue.Self) !f64 {
var v: f64 = undefined;
s2e(c.napi_get_date_value(self.napi_env, self.napi_value, &v));
return v;
}
/// Casts the value to an NodeObject. Fails is the typeof != .Object.
pub fn asObject(self: NodeValue.Self) !NodeObject {
if (self.typeof() == .Object) {
return NodeObject{
.napi_env = self.napi_env,
.napi_value = self.napi_value,
};
}
return error.NodeValueIsNoObject;
}
/// Casts the value to an NodeArray. Fails is the typeof != .Array.
pub fn asArray(self: NodeValue.Self) !NodeArray {
if (self.typeof() == .Object) {
return NodeObject{
.napi_env = self.napi_env,
.napi_value = self.napi_value,
};
}
return error.NodeValueIsNoObject;
}
pub fn isArray(self: NodeValue.Self) !bool {
return self.is(c.napi_is_array);
}
pub fn isTypedArray(self: NodeValue.Self) !bool {
return self.is(c.napi_is_typedarray);
}
pub fn isArrayBuffer(self: NodeValue.Self) !bool {
return self.is(c.napi_is_arraybuffer);
}
pub fn isBuffer(self: NodeValue.Self) !bool {
return self.is(c.napi_is_buffer);
}
pub fn isDataView(self: NodeValue.Self) !bool {
return self.is(c.napi_is_dataview);
}
pub fn isDate(self: NodeValue.Self) !bool {
return self.is(c.napi_is_date);
}
pub fn isError(self: NodeValue.Self) !bool {
return self.is(c.napi_is_error);
}
pub fn isPromise(self: NodeValue.Self) !bool {
return self.is(c.napi_is_promise);
}
fn is(self: NodeValue.Self, is_fn: anytype) !bool {
var b: bool = undefined;
s2e(is_fn(self.napi_env, self.napi_value, &b));
return b;
}
};
/// Represents a JS Object, can be used to process JS objects by reference.
pub const NodeObject = struct {
const Self = @This();
napi_env: c.napi_env,
napi_value: c.napi_value,
pub fn asValue(self: NodeObject.Self) !NodeValue {
return NodeValue{
.napi_env = self.napi_env,
.napi_value = self.napi_value,
};
}
/// Gets the value of a property.
pub fn get(self: NodeObject.Self, property_name: []const u8) !NodeValue {
var v: c.napi_value = undefined;
try s2e(c.napi_get_named_property(self.napi_env, self.napi_value, property_name.ptr, &v));
return NodeValue{ .napi_env = self.napi_env, .napi_value = v };
}
/// Sets the value of a property.
pub fn set(self: NodeObject.Self, property_name: []const u8, value: NodeValue) !NodeValue {
try s2e(c.napi_set_named_property(self.napi_env, self.napi_value, property_name.ptr, value.napi_value));
return value;
}
/// Gets a NodeValue indicating wether the object has a property with the specified name.
pub fn has(self: NodeObject.Self, property_name: []const u8) !bool {
var v: bool = undefined;
try s2e(c.napi_has_named_property(self.napi_env, self.napi_value, property_name.ptr, &v));
return v;
}
/// Gets a NodeValue indicating wether the object has the named own property. key must be a string or a symbol, or an error will be thrown.
pub fn hasOwn(self: NodeObject.Self, property_name: []const u8) !bool {
var v: bool = undefined;
try s2e(c.napi_has_own_property(self.napi_env, self.napi_value, try Convert.nodeFromNative(self.napi_env, property_name), &v));
return v;
}
/// Gets the value of a property.
pub fn delete(self: NodeObject.Self, property_name: []const u8) !bool {
var v: bool = undefined;
try s2e(c.napi_delete_property(
self.napi_env,
self.napi_value,
try Convert.nodeFromNative(self.napi_env, property_name),
&v,
));
return v;
}
/// Returns the names of the enumerable properties as a NodeArray of strings. Symbol keys will not be included.
pub fn getPropertyNames(self: NodeObject.Self) !NodeArray {
var v: c.napi_value = undefined;
try s2e(c.napi_get_property_names(self.napi_env, self.napi_value, &v));
return NodeArray{ .napi_env = self.napi_env, .napi_value = v };
}
};
pub const NodeArray = struct {
const Self = @This();
napi_env: c.napi_env,
napi_value: c.napi_value,
pub fn asValue(self: NodeArray.Self) !NodeValue {
return NodeValue{
.napi_env = self.napi_env,
.napi_value = self.napi_value,
};
}
/// Gets the number of items in the array.
pub fn len(self: NodeArray.Self) !u32 {
var v: u32 = undefined;
try s2e(c.napi_get_array_length(self.napi_env, self.napi_value, &v));
return v;
}
/// Gets the NodeValue at the specified index.
pub fn get(self: NodeArray.Self, index: u32) !NodeValue {
var v: c.napi_value = undefined;
try s2e(c.napi_get_element(self.napi_env, self.napi_value, index, &v));
return NodeValue{ .napi_env = self.napi_env, .napi_value = v };
}
/// Sets the specified NodeValue at the specified index.
pub fn set(self: NodeArray.Self, index: u32, value: NodeValue) !NodeValue {
try s2e(c.napi_set_element(self.napi_env, self.napi_value, index, value.napi_value));
return value;
}
/// Gets whether the array has an element at the specified index.
pub fn has(self: NodeArray.Self, index: u32) !bool {
var v: bool = undefined;
try s2e(c.napi_has_element(self.napi_env, self.napi_value, index, &v));
return v;
}
};
/// Represents a JS function.
pub fn NodeFunction(comptime F: anytype) type {
const f = switch (@typeInfo(F)) {
.@"fn" => |info| info,
else => @compileError("F must be function"),
};
return struct {
const Self = @This();
pub const __is_node_function = true;
napi_env: c.napi_env,
napi_value: c.napi_value,
pub fn call(self: Self, args: TupleTypeOf(f.params)) !f.return_type.? {
var js_args: [f.params.len]c.napi_value = undefined;
inline for (0..args.len) |i| {
js_args[i] = try Convert.nodeFromNative(self.napi_env, args[i]);
}
var res: c.napi_value = undefined;
var global: c.napi_value = undefined;
_ = c.napi_get_global(self.napi_env, &global);
try s2e(c.napi_call_function(self.napi_env, global, self.napi_value, js_args.len, &js_args, &res));
var arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
defer arena.deinit();
return try Convert.nativeFromNode(self.napi_env, f.return_type.?, res, arena.allocator());
}
};
}
fn TupleTypeOf(params: []const std.builtin.Type.Fn.Param) type {
comptime var argTypes: [params.len]type = undefined;
inline for (0..params.len) |i| {
argTypes[i] = params[i].type.?;
}
return std.meta.Tuple(&argTypes);
}