@@ -11,7 +11,7 @@ const NodeFunction = node_values.NodeFunction;
1111const registry = @import ("references.zig" ).Registry ;
1212
1313/// Converts a Zig value to a Node-API value. Memory for the node value is allocated by V8.
14- pub fn serialize (env : c.napi_env , value : anytype ) ! c.napi_value {
14+ pub fn nodeFromNative (env : c.napi_env , value : anytype ) ! c.napi_value {
1515 const T = @TypeOf (value );
1616
1717 const node = NodeContext .init (env );
@@ -29,28 +29,28 @@ pub fn serialize(env: c.napi_env, value: anytype) !c.napi_value {
2929 switch (@bitSizeOf (T )) {
3030 0... 32 = > try s2e (c .napi_create_int32 (env , value , & res )),
3131 33... 64 = > try s2e (c .napi_create_int64 (env , value , & res )),
32- else = > | s | @compileError (std .fmt .comptimePrint ("Cannot serialize value of type {s}, unsupported bitsize {d}" , .{ @typeName (T ), s })),
32+ else = > | s | @compileError (std .fmt .comptimePrint ("Cannot convert value of type ' {s}' , unsupported bitsize {d}" , .{ @typeName (T ), s })),
3333 }
3434 } else {
3535 switch (@bitSizeOf (T )) {
3636 0... 32 = > try s2e (c .napi_create_uint32 (env , value , & res )),
3737 33... 64 = > try s2e (c .napi_create_bigint_uint64 (env , value , & res )),
38- else = > | s | @compileError (std .fmt .comptimePrint ("Cannot serialize value of type {s}, unsupported bitsize {d}" , .{ @typeName (T ), s })),
38+ else = > | s | @compileError (std .fmt .comptimePrint ("Cannot convert value of type ' {s}' , unsupported bitsize {d}" , .{ @typeName (T ), s })),
3939 }
4040 }
4141 },
4242 .int = > | i | {
4343 if (i .signedness == .signed ) {
4444 switch (i .bits ) {
45- 0 ... 32 = > try s2e (c .napi_create_int32 (env , value , & res )),
46- 33 ... 64 = > try s2e (c .napi_create_int64 (env , value , & res )),
47- else = > | s | @compileError (std .fmt .comptimePrint ("Cannot serialize value of type {s}, unsupported bitsize {d}" , .{ @typeName (T ), s })),
45+ 32 = > try s2e (c .napi_create_int32 (env , value , & res )),
46+ 64 = > try s2e (c .napi_create_int64 (env , value , & res )),
47+ else = > | s | @compileError (std .fmt .comptimePrint ("Cannot convert value of type ' {s}' , unsupported bitsize {d}" , .{ @typeName (T ), s })),
4848 }
4949 } else {
5050 switch (i .bits ) {
51- 0 ... 32 = > try s2e (c .napi_create_uint32 (env , value , & res )),
52- 33 ... 64 = > try s2e (c .napi_create_bigint_uint64 (env , value , & res )),
53- else = > | s | @compileError (std .fmt .comptimePrint ("Cannot serialize value of type {s}, unsupported bitsize {d}" , .{ @typeName (T ), s })),
51+ 32 = > try s2e (c .napi_create_uint32 (env , value , & res )),
52+ 64 = > try s2e (c .napi_create_bigint_uint64 (env , value , & res )),
53+ else = > | s | @compileError (std .fmt .comptimePrint ("Cannot convert value of type ' {s}' , unsupported bitsize {d}" , .{ @typeName (T ), s })),
5454 }
5555 }
5656 },
@@ -70,7 +70,7 @@ pub fn serialize(env: c.napi_env, value: anytype) !c.napi_value {
7070 },
7171 .optional = > {
7272 if (value ) | val | {
73- return serialize (env , val );
73+ return nodeFromNative (env , val );
7474 } else {
7575 try s2e (c .napi_get_null (env , & res ));
7676 }
@@ -82,11 +82,7 @@ pub fn serialize(env: c.napi_env, value: anytype) !c.napi_value {
8282 return v ;
8383 }
8484
85- // TODO: could be wrapped value, do we need this?
86- // how do we know if the instance was already wrapped before?
87- // hashset?
88-
89- return serialize (env , value .* );
85+ return nodeFromNative (env , value .* );
9086 },
9187 .slice = > {
9288 std .log .debug ("serializing {s} of type {s}" , .{ value , @typeName (T ) });
@@ -96,13 +92,13 @@ pub fn serialize(env: c.napi_env, value: anytype) !c.napi_value {
9692 try s2e (c .napi_create_array_with_length (env , value .len , & res ));
9793
9894 for (value , 0.. ) | item , i | {
99- const el = try serialize (env , item );
95+ const el = try nodeFromNative (env , item );
10096 try s2e (c .napi_set_element (env , res , i , el ));
10197 }
10298 }
10399 },
104100 .many , .c = > {
105- @compileError ("Cannot serialize c-style pointer values, they are not supported." );
101+ @compileError ("Cannot convert c-style pointer values, they are not supported." );
106102 },
107103 }
108104 },
@@ -114,21 +110,19 @@ pub fn serialize(env: c.napi_env, value: anytype) !c.napi_value {
114110 if (! s .is_tuple ) {
115111 try s2e (c .napi_create_object (env , & res ));
116112 inline for (s .fields ) | field | {
117- // std.log.debug("tuple field: {any}", .{field});
118113 if (field .type == void ) continue ;
119114
120115 const field_val = @field (value , field .name );
121116
122- try s2e (c .napi_set_property (env , res , try serialize (env , field .name ), try serialize (env , field_val )));
117+ try s2e (c .napi_set_property (env , res , try nodeFromNative (env , field .name ), try nodeFromNative (env , field_val )));
123118 }
124119 } else {
125120 try s2e (c .napi_create_array_with_length (env , s .fields .len , & res ));
126121 inline for (s .fields , 0.. ) | field , i | {
127- // std.log.debug("field: {any}", .{field});
128122 if (field .type == void ) continue ;
129123
130124 const field_val = @field (value , field .name );
131- try s2e (c .napi_set_element (env , res , i , try serialize (env , field_val )));
125+ try s2e (c .napi_set_element (env , res , i , try nodeFromNative (env , field_val )));
132126 }
133127 }
134128 },
@@ -140,7 +134,7 @@ pub fn serialize(env: c.napi_env, value: anytype) !c.napi_value {
140134 try s2e (c .napi_create_array_with_length (env , value .len , & res ));
141135
142136 for (value , 0.. ) | item , i | {
143- const el = try serialize (env , item );
137+ const el = try nodeFromNative (env , item );
144138 try s2e (c .napi_set_element (env , res , i , el ));
145139 }
146140 }
@@ -151,62 +145,47 @@ pub fn serialize(env: c.napi_env, value: anytype) !c.napi_value {
151145
152146 const tag = @as (Tag , value );
153147 const tag_name = @tagName (tag );
154- try s2e (c .napi_set_property (env , res , try serialize (env , "type" ), try serialize (env , tag_name )));
148+ try s2e (c .napi_set_property (env , res , try nodeFromNative (env , "type" ), try nodeFromNative (env , tag_name )));
155149
156150 inline for (u .fields ) | f | {
157151 if (std .mem .eql (u8 , f .name , tag_name )) {
158152 if (f .type == void ) break ;
159- try s2e (c .napi_set_property (env , res , try serialize (env , "value" ), try serialize (env , @field (value , f .name ))));
153+ try s2e (c .napi_set_property (env , res , try nodeFromNative (env , "value" ), try nodeFromNative (env , @field (value , f .name ))));
160154 break ;
161155 }
162156 }
163157 } else {
164- @compileError (std .fmt .comptimePrint ("Cannot serialize value of type {s}, untagged unions are not supported." , .{@typeName (T )}));
158+ @compileError (std .fmt .comptimePrint ("Cannot convert value of type ' {s}' , untagged unions are not supported." , .{@typeName (T )}));
165159 }
166160 },
167161
168- else = > @compileError (std .fmt .comptimePrint ("Cannot serialize value of type {s}" , .{@typeName (T )})),
162+ else = > @compileError (std .fmt .comptimePrint ("Cannot convert value of type ' {s}' " , .{@typeName (T )})),
169163 }
170164
171165 return res ;
172166}
173167
174168/// Converts a Node-API value to a native Zig value.
175- pub fn deserialize (env : c.napi_env , comptime T : type , js_value : c.napi_value , allocator : std.mem.Allocator ) ! T {
169+ pub fn nativeFromNode (env : c.napi_env , comptime T : type , js_value : c.napi_value , allocator : std.mem.Allocator ) ! T {
176170 var res : T = undefined ;
177171
178172 switch (@typeInfo (T )) {
179173 .bool = > try s2e (c .napi_get_value_bool (env , js_value , & res )),
180174 .int = > | i | {
181175 if (i .signedness == .signed ) {
182176 switch (i .bits ) {
183- 32 = > {
184- var tmp : i32 = undefined ;
185- try s2e (c .napi_get_value_int32 (env , js_value , & tmp ));
186- // TODO: handle failing int casts
187- return @intCast (tmp );
188- },
189- 64 = > {
190- var tmp : i64 = undefined ;
191- try s2e (c .napi_get_value_int64 (env , js_value , & tmp ));
192- return @intCast (tmp );
193- },
194- else = > @compileError (std .fmt .comptimePrint ("Cannot deserialize value of type {s}" , .{@typeName (T )})),
177+ 32 = > try s2e (c .napi_get_value_int32 (env , js_value , & res )),
178+ 64 = > try s2e (c .napi_get_value_int64 (env , js_value , & res )),
179+ else = > @compileError (std .fmt .comptimePrint ("Cannot convert node value to '{s}'" , .{@typeName (T )})),
195180 }
196181 } else {
197182 switch (i .bits ) {
198- 0... 32 = > {
199- var tmp : u32 = undefined ;
200- try s2e (c .napi_get_value_uint32 (env , js_value , & tmp ));
201- return @intCast (tmp );
202- },
203- 33... 64 = > {
204- var tmp : u64 = undefined ;
183+ 32 = > try s2e (c .napi_get_value_uint32 (env , js_value , & res )),
184+ 64 = > {
205185 var b : bool = undefined ;
206- try s2e (c .napi_get_value_bigint_uint64 (env , js_value , & tmp , & b ));
207- return @intCast (tmp );
186+ try s2e (c .napi_get_value_bigint_uint64 (env , js_value , & res , & b ));
208187 },
209- else = > @compileError (std .fmt .comptimePrint ("Cannot deserialize value of type {s}" , .{@typeName (T )})),
188+ else = > @compileError (std .fmt .comptimePrint ("Cannot convert node value to ' {s}' " , .{@typeName (T )})),
210189 }
211190 }
212191 },
@@ -234,7 +213,7 @@ pub fn deserialize(env: c.napi_env, comptime T: type, js_value: c.napi_value, al
234213 try s2e (c .napi_typeof (env , js_value , & t ));
235214 switch (t ) {
236215 c .napi_undefined , c .napi_null = > return null ,
237- else = > return try deserialize (env , o .child , js_value , allocator ),
216+ else = > return try nativeFromNode (env , o .child , js_value , allocator ),
238217 }
239218 },
240219 .pointer = > | p | {
@@ -261,7 +240,7 @@ pub fn deserialize(env: c.napi_env, comptime T: type, js_value: c.napi_value, al
261240 for (0.. len ) | i | {
262241 var elem : c.napi_value = undefined ;
263242 try s2e (c .napi_get_element (env , js_value , i , & elem ));
264- buf [i ] = try deserialize (env , p .child , elem , allocator );
243+ buf [i ] = try nativeFromNode (env , p .child , elem , allocator );
265244 }
266245
267246 return buf ;
@@ -272,7 +251,7 @@ pub fn deserialize(env: c.napi_env, comptime T: type, js_value: c.napi_value, al
272251 }
273252 },
274253 else = > {
275- @compileError ("Cannot serialize c-style pointer values, they are not supported." );
254+ @compileError ("Cannot convert c-style pointer values, they are not supported." );
276255 },
277256 }
278257 },
@@ -288,13 +267,13 @@ pub fn deserialize(env: c.napi_env, comptime T: type, js_value: c.napi_value, al
288267 inline for (s .fields ) | field | {
289268 var v : c.napi_value = undefined ;
290269 try s2e (c .napi_get_named_property (env , js_value , field .name .ptr , & v ));
291- @field (instance , field .name ) = try deserialize (env , field .type , v , allocator );
270+ @field (instance , field .name ) = try nativeFromNode (env , field .type , v , allocator );
292271 }
293272
294273 return instance ;
295274 },
296275
297- else = > @compileError (std .fmt .comptimePrint ("Cannot deserialize value of type {s}" , .{@typeName (T )})),
276+ else = > @compileError (std .fmt .comptimePrint ("Cannot convert node value to ' {s}' " , .{@typeName (T )})),
298277 }
299278
300279 return res ;
0 commit comments