@@ -3,50 +3,10 @@ pub const c = @cImport({
33 @cInclude ("node_api.h" );
44});
55
6- // pub const cc = wrapCLib(c);
7-
8- // fn wrapCLib(comptime T: type) type {
9- // const decls = @typeInfo(T).@"struct".decls;
10-
11- // const N = decls.len;
12-
13- // var fields: [N]std.builtin.Type.StructField = undefined;
14-
15- // inline for (decls, 0..) |d, i| {
16- // const wrapper = opaque {
17- // pub fn f() NodeApiError!void {}
18- // }.f;
19- // const f : std.builtin.Type.Fn = .{
20- // .
21- // };
22- // fields[i] = .{
23- // .name = d.name,
24- // .type = d.ty,
25- // .default_value = null, // keep it simple; you can wire a default if you want
26- // .is_comptime = false,
27- // .alignment = @alignOf(d.ty),
28- // };
29- // }
30-
31- // return @Type(.{
32- // .Struct = .{
33- // .layout = .Auto,
34- // .fields = &fields,
35- // .decls = &.{}, // no nested declarations (constants/functions) added here
36- // .is_tuple = false,
37- // },
38- // });
39- // }
40-
416/// Maps a node_api status codes to Zig errors.
427pub fn statusToError (status : c_uint ) NodeApiError ! void {
43- const v : NapiStatusValue = @enumFromInt (status );
44-
45- if (v == NapiStatusValue .ok ) {
46- return ;
47- }
48-
49- return switch (v ) {
8+ return switch (@as (NapiStatusValue , @enumFromInt (status ))) {
9+ NapiStatusValue .ok = > return ,
5010 NapiStatusValue .invalid_arg = > NodeApiError .InvalidArg ,
5111 NapiStatusValue .object_expected = > NodeApiError .ObjectExpected ,
5212 NapiStatusValue .string_expected = > NodeApiError .StringExpected ,
@@ -132,15 +92,12 @@ pub const NodeApiError = error{
13292
13393// https://nodejs.org/api/n-api.html#error-handling
13494pub fn handleError (env : c.napi_env , err : anyerror ) void {
135- std .log .debug ("handling error for {any}" , .{err });
95+ std .log .debug ("handling error ' {any}' " , .{err });
13696
13797 var err_info : [* c ]const c.napi_extended_error_info = undefined ;
13898 if (c .napi_get_last_error_info (env , & err_info ) != c .napi_ok ) {
13999 @panic ("failed to call `napi_get_last_error_info`." );
140100 }
141- // if (err_info != null) {
142- // std.log.debug("err_infooo {s}", .{err_info.*.error_message});
143- // }
144101
145102 // In many cases when a Node-API function is called and an exception is already pending, the function will return immediately with a napi_status of napi_pending_exception.
146103 // However, this is not the case for all functions. Node-API allows a subset of the functions to be called to allow for some minimal cleanup before returning to JavaScript.
@@ -154,8 +111,6 @@ pub fn handleError(env: c.napi_env, err: anyerror) void {
154111 pending_exception = true ;
155112 }
156113
157- std .log .debug ("pending_expeption {any}" , .{pending_exception });
158-
159114 if (pending_exception ) {
160115 // the pending exception will be thrown in JS
161116 return ;
0 commit comments