zignapi 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  .{
2
2
  .name = .zignapi,
3
- .version = "0.2.0",
3
+ .version = "0.3.0",
4
4
  .fingerprint = 0x582024f92c57df77, // Changing this has security and trust implications.
5
5
  .minimum_zig_version = "0.16.0",
6
6
  .dependencies = .{},
@@ -16,6 +16,9 @@ comptime {
16
16
  _ = napi.getCallbackInfo;
17
17
  _ = napi.setNamedProperty;
18
18
  _ = napi.createFunction;
19
+ _ = napi.createString;
20
+ _ = napi.getUndefined;
21
+ _ = napi.createError;
19
22
  }
20
23
 
21
24
  fn add(a: i32, b: i32) i32 {
@@ -34,6 +37,21 @@ fn checked(x: i32) !i32 {
34
37
  return if (x < 0) error.MustBeNonNegative else x;
35
38
  }
36
39
 
40
+ // Async: runs on the thread pool, returns a Promise.
41
+ fn heavy(a: i32, b: i32) i32 {
42
+ return a * b;
43
+ }
44
+ fn heavyChecked(x: i32) !i32 {
45
+ return if (x < 0) error.MustBeNonNegative else x;
46
+ }
47
+
48
+ // Raw passthrough: `napi.Env` / `napi.Value` params + a threadsafe function.
49
+ fn onEvent(env: napi.Env, callback: napi.Value) void {
50
+ const tsfn = zignapi.ThreadsafeFunction(i32).create(env, callback) catch return;
51
+ tsfn.call(1) catch {};
52
+ tsfn.release();
53
+ }
54
+
37
55
  comptime {
38
56
  zignapi.register(.{
39
57
  .add = add,
@@ -41,5 +59,8 @@ comptime {
41
59
  .scale = scale,
42
60
  .echo = echo,
43
61
  .checked = checked,
62
+ .heavy = zignapi.asyncFn(heavy),
63
+ .heavyChecked = zignapi.asyncFn(heavyChecked),
64
+ .onEvent = onEvent,
44
65
  });
45
66
  }
@@ -1,15 +1,209 @@
1
- //! Async support stub.
1
+ //! Async support: run a Zig function on libuv's thread pool and resolve a JS
2
+ //! `Promise`, plus a threadsafe-function primitive for calling JS from any
3
+ //! thread.
2
4
  //!
3
- //! TODO: wrap `napi_create_async_work` / `napi_queue_async_work` so that Zig
4
- //! functions returning something awaitable (or explicitly marked async) run on
5
- //! libuv's thread pool and resolve a JS `Promise` via `napi_create_promise` /
6
- //! `napi_resolve_deferred`. Nothing here is wired into `register.zig` yet;
7
- //! today every exported function runs synchronously on the JS thread.
5
+ //! ## Async functions
6
+ //!
7
+ //! Wrap a function with `asyncFn` when registering it. Its arguments are
8
+ //! converted on the JS thread, the function body runs on a worker thread (so it
9
+ //! must NOT touch JS / N-API), and the result (or a thrown error) settles the
10
+ //! promise back on the JS thread:
11
+ //!
12
+ //! ```zig
13
+ //! fn heavy(a: i32, b: i32) i32 { return a * b; }
14
+ //! comptime { zignapi.register(.{ .heavy = zignapi.asyncFn(heavy) }); }
15
+ //! // JS: await addon.heavy(6, 7) === 42
16
+ //! ```
17
+ //!
18
+ //! ## Threadsafe functions
19
+ //!
20
+ //! `ThreadsafeFunction(T)` lets native code call a JS callback from any thread.
21
+ //! Create it on the JS thread from a callback the addon received as a raw
22
+ //! `napi.Value`, hand it to a worker thread, `call` it, then `release` it.
8
23
 
9
24
  const std = @import("std");
25
+ const napi = @import("napi.zig");
26
+ const convert = @import("convert.zig");
27
+ const c = napi.c;
28
+
29
+ /// Mark a function as async when registering it (runs on the thread pool and
30
+ /// returns a `Promise`). See the module docs.
31
+ pub fn asyncFn(comptime f: anytype) AsyncMarker(f) {
32
+ return .{};
33
+ }
34
+
35
+ fn AsyncMarker(comptime f: anytype) type {
36
+ return struct {
37
+ pub const zignapi_async_task = true;
38
+ pub const func = f;
39
+ };
40
+ }
41
+
42
+ /// Whether a `register` field value's type is an `asyncFn(...)` marker.
43
+ pub fn isAsyncMarker(comptime FieldT: type) bool {
44
+ return switch (@typeInfo(FieldT)) {
45
+ .@"struct" => @hasDecl(FieldT, "zignapi_async_task"),
46
+ else => false,
47
+ };
48
+ }
49
+
50
+ /// Build the `napi_callback` trampoline for an async function: it returns a
51
+ /// Promise immediately and does the work on a libuv worker thread.
52
+ pub fn AsyncWrap(comptime func: anytype) type {
53
+ const fn_info = @typeInfo(@TypeOf(func)).@"fn";
54
+ const RetType = fn_info.return_type.?;
55
+ const Payload = switch (@typeInfo(RetType)) {
56
+ .error_union => |eu| eu.payload,
57
+ else => RetType,
58
+ };
59
+
60
+ const Task = struct {
61
+ args: std.meta.ArgsTuple(@TypeOf(func)),
62
+ arena: std.heap.ArenaAllocator,
63
+ deferred: c.napi_deferred,
64
+ work: c.napi_async_work,
65
+ payload: Payload,
66
+ err_name: ?[:0]const u8,
67
+ };
68
+
69
+ return struct {
70
+ pub fn callback(env: napi.Env, info: napi.CallbackInfo) callconv(.c) napi.Value {
71
+ var argv: [fn_info.params.len]napi.Value = undefined;
72
+ _ = napi.getCallbackInfo(env, info, &argv) catch {
73
+ napi.throwError(env, "zignapi: failed to read arguments");
74
+ return null;
75
+ };
76
+
77
+ const task = std.heap.c_allocator.create(Task) catch {
78
+ napi.throwError(env, "zignapi: out of memory");
79
+ return null;
80
+ };
81
+ task.arena = std.heap.ArenaAllocator.init(std.heap.c_allocator);
82
+ task.err_name = null;
83
+
84
+ // Convert arguments on the JS thread (N-API access is valid here).
85
+ const allocator = task.arena.allocator();
86
+ inline for (fn_info.params, 0..) |param, i| {
87
+ task.args[i] = convert.fromJs(param.type.?, env, argv[i], allocator) catch
88
+ return abort(task, env, "zignapi: failed to convert argument");
89
+ }
90
+
91
+ var promise: napi.Value = undefined;
92
+ if (c.napi_create_promise(env, &task.deferred, &promise) != c.napi_ok)
93
+ return abort(task, env, "zignapi: failed to create promise");
94
+
95
+ const name = napi.createString(env, "zignapi_async_task") catch
96
+ return abort(task, env, "zignapi: failed to create async work");
97
+
98
+ _ = c.napi_create_async_work(env, null, name, execute, complete, task, &task.work);
99
+ _ = c.napi_queue_async_work(env, task.work);
100
+ return promise;
101
+ }
102
+
103
+ /// Free a partially-initialised task, throw, and return null (on the JS
104
+ /// thread, before the work is queued).
105
+ fn abort(task: *Task, env: napi.Env, msg: [:0]const u8) napi.Value {
106
+ task.arena.deinit();
107
+ std.heap.c_allocator.destroy(task);
108
+ napi.throwError(env, msg);
109
+ return null;
110
+ }
111
+
112
+ /// Worker thread — no N-API / JS access allowed.
113
+ fn execute(env: napi.Env, data: ?*anyopaque) callconv(.c) void {
114
+ _ = env;
115
+ const task: *Task = @ptrCast(@alignCast(data.?));
116
+ switch (@typeInfo(RetType)) {
117
+ .error_union => task.payload = @call(.auto, func, task.args) catch |err| {
118
+ task.err_name = @errorName(err);
119
+ return;
120
+ },
121
+ else => task.payload = @call(.auto, func, task.args),
122
+ }
123
+ }
124
+
125
+ /// JS thread — settle the promise with the result or the error.
126
+ fn complete(env: napi.Env, status: c.napi_status, data: ?*anyopaque) callconv(.c) void {
127
+ const task: *Task = @ptrCast(@alignCast(data.?));
128
+ defer {
129
+ task.arena.deinit();
130
+ _ = c.napi_delete_async_work(env, task.work);
131
+ std.heap.c_allocator.destroy(task);
132
+ }
133
+
134
+ if (status != c.napi_ok) return reject(env, task.deferred, "zignapi: async work cancelled");
135
+ if (task.err_name) |name| return reject(env, task.deferred, name);
136
+
137
+ const resolution = if (Payload == void)
138
+ (napi.getUndefined(env) catch return reject(env, task.deferred, "zignapi: internal error"))
139
+ else
140
+ (convert.toJs(Payload, env, task.payload) catch
141
+ return reject(env, task.deferred, "zignapi: failed to convert result"));
142
+ _ = c.napi_resolve_deferred(env, task.deferred, resolution);
143
+ }
144
+ };
145
+ }
146
+
147
+ fn reject(env: napi.Env, deferred: c.napi_deferred, msg: []const u8) void {
148
+ const err = napi.createError(env, msg) catch return;
149
+ _ = c.napi_reject_deferred(env, deferred, err);
150
+ }
151
+
152
+ /// A threadsafe function: a JS callback that native code can invoke from any
153
+ /// thread. `T` is the Zig type of the single argument passed to the callback
154
+ /// (converted to JS via `convert`). Create it on the JS thread, use it from
155
+ /// worker threads, and `release` it when done.
156
+ pub fn ThreadsafeFunction(comptime T: type) type {
157
+ return struct {
158
+ const Self = @This();
159
+
160
+ handle: c.napi_threadsafe_function,
161
+
162
+ /// Create from a JS callback value. Call on the JS thread.
163
+ pub fn create(env: napi.Env, js_callback: napi.Value) napi.Error!Self {
164
+ const name = try napi.createString(env, "zignapi_tsfn");
165
+ var handle: c.napi_threadsafe_function = undefined;
166
+ try napi.check(c.napi_create_threadsafe_function(
167
+ env,
168
+ js_callback,
169
+ null, // async_resource
170
+ name,
171
+ 0, // max_queue_size: unlimited
172
+ 1, // initial_thread_count
173
+ null, // thread_finalize_data
174
+ null, // thread_finalize_cb
175
+ null, // context
176
+ callJs,
177
+ &handle,
178
+ ));
179
+ return .{ .handle = handle };
180
+ }
181
+
182
+ /// Queue a call to the JS callback with `value`. Safe from any thread.
183
+ pub fn call(self: Self, value: T) napi.Error!void {
184
+ const boxed = std.heap.c_allocator.create(T) catch return napi.Error.NapiFailure;
185
+ boxed.* = value;
186
+ if (c.napi_call_threadsafe_function(self.handle, boxed, c.napi_tsfn_blocking) != c.napi_ok) {
187
+ std.heap.c_allocator.destroy(boxed);
188
+ return napi.Error.NapiFailure;
189
+ }
190
+ }
191
+
192
+ /// Release the reference so Node can tear the function down. Any thread.
193
+ pub fn release(self: Self) void {
194
+ _ = c.napi_release_threadsafe_function(self.handle, c.napi_tsfn_release);
195
+ }
10
196
 
11
- comptime {
12
- // Keep this file referenced so it participates in compilation checks even
13
- // while it is only a placeholder.
14
- std.debug.assert(true);
197
+ /// JS thread — convert the queued value and invoke the callback.
198
+ fn callJs(env: napi.Env, js_cb: napi.Value, context: ?*anyopaque, data: ?*anyopaque) callconv(.c) void {
199
+ _ = context;
200
+ const boxed: *T = @ptrCast(@alignCast(data.?));
201
+ defer std.heap.c_allocator.destroy(boxed);
202
+ if (env == null) return; // the environment is tearing down
203
+ const arg = convert.toJs(T, env, boxed.*) catch return;
204
+ const recv = napi.getUndefined(env) catch return;
205
+ var argv = [_]napi.Value{arg};
206
+ _ = c.napi_call_function(env, recv, js_cb, argv.len, &argv, null);
207
+ }
208
+ };
15
209
  }
@@ -67,3 +67,18 @@ pub fn createString(env: Env, s: []const u8) Error!Value {
67
67
  try check(c.napi_create_string_utf8(env, s.ptr, s.len, &result));
68
68
  return result;
69
69
  }
70
+
71
+ /// The JS `undefined` value.
72
+ pub fn getUndefined(env: Env) Error!Value {
73
+ var result: Value = undefined;
74
+ try check(c.napi_get_undefined(env, &result));
75
+ return result;
76
+ }
77
+
78
+ /// Create a JS `Error` object carrying `msg` (used to reject promises).
79
+ pub fn createError(env: Env, msg: []const u8) Error!Value {
80
+ const message = try createString(env, msg);
81
+ var result: Value = undefined;
82
+ try check(c.napi_create_error(env, null, message, &result));
83
+ return result;
84
+ }
@@ -13,6 +13,7 @@ const std = @import("std");
13
13
  const napi = @import("napi.zig");
14
14
  const convert = @import("convert.zig");
15
15
  const typedefs = @import("typedefs.zig");
16
+ const asyncwork = @import("async.zig");
16
17
  const c = napi.c;
17
18
 
18
19
  /// Export `napi_register_module_v1` for the given set of functions.
@@ -35,8 +36,7 @@ pub fn register(comptime defs: anytype) void {
35
36
  const Registrar = struct {
36
37
  fn entry(env: napi.Env, exports: napi.Value) callconv(.c) napi.Value {
37
38
  inline for (@typeInfo(Defs).@"struct".fields) |field| {
38
- const func = @field(defs, field.name);
39
- registerOne(env, exports, field.name, func) catch {
39
+ registerField(env, exports, field.name, @field(defs, field.name)) catch {
40
40
  napi.throwError(env, "zignapi: failed to register '" ++ field.name ++ "'");
41
41
  return null;
42
42
  };
@@ -53,17 +53,27 @@ pub fn register(comptime defs: anytype) void {
53
53
  @export(&Registrar.entry, .{ .name = "napi_register_module_v1", .linkage = .strong });
54
54
  }
55
55
 
56
- /// Bind a single function onto `exports` under `name`.
57
- fn registerOne(
56
+ /// Bind a single field onto `exports` under `name`. The field is either a plain
57
+ /// function (registered synchronously) or an `asyncFn(...)` marker (registered
58
+ /// as an async, promise-returning function).
59
+ fn registerField(
58
60
  env: napi.Env,
59
61
  exports: napi.Value,
60
62
  comptime name: [:0]const u8,
61
- comptime func: anytype,
63
+ comptime field_val: anytype,
62
64
  ) !void {
63
- const fn_value = try napi.createFunction(env, name, Wrap(func).callback);
65
+ const fn_value = try napi.createFunction(env, name, callbackFor(field_val));
64
66
  try napi.setNamedProperty(env, exports, name, fn_value);
65
67
  }
66
68
 
69
+ fn callbackFor(comptime field_val: anytype) napi.Callback {
70
+ const FieldT = @TypeOf(field_val);
71
+ if (comptime asyncwork.isAsyncMarker(FieldT)) {
72
+ return asyncwork.AsyncWrap(FieldT.func).callback;
73
+ }
74
+ return Wrap(field_val).callback;
75
+ }
76
+
67
77
  /// Build the `napi_callback` trampoline for a specific Zig function.
68
78
  ///
69
79
  /// Each distinct `func` produces its own type (and therefore its own callback),
@@ -87,13 +97,25 @@ fn Wrap(comptime func: anytype) type {
87
97
  defer arena.deinit();
88
98
  const allocator = arena.allocator();
89
99
 
90
- // Convert each argument from JS into its Zig type.
100
+ // Convert each argument from JS into its Zig type. `napi.Env` and
101
+ // `napi.Value` parameters are passed through raw and don't consume a
102
+ // JS argument (like napi-rs's `Env`), so track the JS index separately.
91
103
  var args: std.meta.ArgsTuple(@TypeOf(func)) = undefined;
104
+ comptime var js_arg: usize = 0;
92
105
  inline for (fn_info.params, 0..) |param, i| {
93
- args[i] = convert.fromJs(param.type.?, env, argv[i], allocator) catch {
94
- napi.throwError(env, "zignapi: failed to convert argument");
95
- return null;
96
- };
106
+ const P = param.type.?;
107
+ if (P == napi.Env) {
108
+ args[i] = env;
109
+ } else if (P == napi.Value) {
110
+ args[i] = argv[js_arg];
111
+ js_arg += 1;
112
+ } else {
113
+ args[i] = convert.fromJs(P, env, argv[js_arg], allocator) catch {
114
+ napi.throwError(env, "zignapi: failed to convert argument");
115
+ return null;
116
+ };
117
+ js_arg += 1;
118
+ }
97
119
  }
98
120
 
99
121
  const result = @call(.auto, func, args);
@@ -6,12 +6,15 @@
6
6
  //! and writes `index.d.ts` / `index.js`.
7
7
 
8
8
  const std = @import("std");
9
+ const napi = @import("napi.zig");
9
10
  const convert = @import("convert.zig");
11
+ const asyncwork = @import("async.zig");
10
12
 
11
- /// The TypeScript type string for a Zig type. Error unions map to their payload
12
- /// (Zig errors surface as thrown JS exceptions, which don't appear in the type);
13
- /// `void` maps to `void`.
13
+ /// The TypeScript type string for a Zig type. `napi.Value` (a raw JS handle,
14
+ /// e.g. a callback) maps to `any`; error unions map to their payload (Zig
15
+ /// errors surface as thrown JS exceptions); `void` maps to `void`.
14
16
  pub fn tsType(comptime T: type) []const u8 {
17
+ if (T == napi.Value) return "any";
15
18
  return switch (@typeInfo(T)) {
16
19
  .void => "void",
17
20
  .error_union => |eu| tsType(eu.payload),
@@ -27,26 +30,34 @@ pub fn tsType(comptime T: type) []const u8 {
27
30
  }
28
31
 
29
32
  /// Build the `.d.ts` body: one `export function` line per registered function.
30
- /// Parameter names aren't available from `@typeInfo`, so they're `arg0`, `arg1`, …
33
+ /// `napi.Env` parameters are skipped (they aren't JS arguments); `asyncFn`
34
+ /// functions return `Promise<T>`. Parameter names are `arg0`, `arg1`, …
31
35
  pub fn declarations(comptime defs: anytype) []const u8 {
32
36
  comptime var out: []const u8 = "";
33
37
  inline for (@typeInfo(@TypeOf(defs)).@"struct".fields) |field| {
34
- const fn_info = @typeInfo(@TypeOf(@field(defs, field.name))).@"fn";
38
+ const field_val = @field(defs, field.name);
39
+ const is_async = asyncwork.isAsyncMarker(@TypeOf(field_val));
40
+ const func = if (is_async) @TypeOf(field_val).func else field_val;
41
+ const fn_info = @typeInfo(@TypeOf(func)).@"fn";
42
+
35
43
  comptime var params: []const u8 = "";
36
- inline for (fn_info.params, 0..) |param, i| {
37
- const sep = if (i == 0) "" else ", ";
38
- params = params ++ sep ++ std.fmt.comptimePrint(
39
- "arg{d}: {s}",
40
- .{ i, tsType(param.type.?) },
41
- );
44
+ comptime var js_i: usize = 0;
45
+ inline for (fn_info.params) |param| {
46
+ const P = param.type.?;
47
+ if (P == napi.Env) continue; // not a JS argument
48
+ const sep = if (js_i == 0) "" else ", ";
49
+ params = params ++ sep ++ std.fmt.comptimePrint("arg{d}: {s}", .{ js_i, tsType(P) });
50
+ js_i += 1;
42
51
  }
43
- out = out ++ "export function " ++ field.name ++ "(" ++ params ++
44
- "): " ++ tsType(fn_info.return_type.?) ++ ";\n";
52
+
53
+ const ret = tsType(fn_info.return_type.?);
54
+ const ret_str = if (is_async) "Promise<" ++ ret ++ ">" else ret;
55
+ out = out ++ "export function " ++ field.name ++ "(" ++ params ++ "): " ++ ret_str ++ ";\n";
45
56
  }
46
57
  return out;
47
58
  }
48
59
 
49
- test "declarations render one export per function" {
60
+ test "declarations render sync, async and passthrough signatures" {
50
61
  const S = struct {
51
62
  fn add(a: i32, b: i32) i32 {
52
63
  return a + b;
@@ -54,24 +65,25 @@ test "declarations render one export per function" {
54
65
  fn greet(name: []const u8) []const u8 {
55
66
  return name;
56
67
  }
57
- fn toggle(x: bool) bool {
58
- return !x;
68
+ fn heavy(a: i32, b: i32) i32 {
69
+ return a * b;
59
70
  }
60
- fn checked(x: i32) !i32 {
61
- return x;
71
+ fn onEvent(env: napi.Env, cb: napi.Value) void {
72
+ _ = env;
73
+ _ = cb;
62
74
  }
63
75
  };
64
76
  const dts = comptime declarations(.{
65
77
  .add = S.add,
66
78
  .greet = S.greet,
67
- .toggle = S.toggle,
68
- .checked = S.checked,
79
+ .heavy = asyncwork.asyncFn(S.heavy),
80
+ .onEvent = S.onEvent,
69
81
  });
70
82
  try std.testing.expectEqualStrings(
71
83
  \\export function add(arg0: number, arg1: number): number;
72
84
  \\export function greet(arg0: string): string;
73
- \\export function toggle(arg0: boolean): boolean;
74
- \\export function checked(arg0: number): number;
85
+ \\export function heavy(arg0: number, arg1: number): Promise<number>;
86
+ \\export function onEvent(arg0: any): void;
75
87
  \\
76
88
  , dts);
77
89
  }
@@ -18,6 +18,12 @@ pub const async_ = @import("async.zig");
18
18
  /// Register Zig functions as a Node addon. See `register.zig`.
19
19
  pub const register = @import("register.zig").register;
20
20
 
21
+ /// Mark a function as async (runs on libuv's thread pool, returns a `Promise`).
22
+ pub const asyncFn = async_.asyncFn;
23
+
24
+ /// A JS callback callable from any thread. See `async.zig`.
25
+ pub const ThreadsafeFunction = async_.ThreadsafeFunction;
26
+
21
27
  /// The raw imported N-API C API (`napi_*` functions and types).
22
28
  pub const c = napi.c;
23
29
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zignapi",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "CLI to scaffold and build native Node.js addons written in Zig (the `zignapi` command)",
5
5
  "keywords": [
6
6
  "zig",
@@ -11,11 +11,18 @@ pub fn greet(name: []const u8) []const u8 {
11
11
  return "hello from Zig";
12
12
  }
13
13
 
14
+ /// Async — `zignapi.asyncFn` runs it on a worker thread and returns a
15
+ /// `Promise`. The body must not touch JS (it runs off the JS thread).
16
+ fn slowSquare(x: i32) i32 {
17
+ return x * x;
18
+ }
19
+
14
20
  // Emits `napi_register_module_v1`. Each field becomes a property on the addon's
15
21
  // `exports`. Add your own Zig functions here.
16
22
  comptime {
17
23
  zignapi.register(.{
18
24
  .add = add,
19
25
  .greet = greet,
26
+ .slowSquare = zignapi.asyncFn(slowSquare),
20
27
  });
21
28
  }
package/templates/test.js CHANGED
@@ -14,8 +14,13 @@ test("greet returns a string", () => {
14
14
  assert.strictEqual(addon.greet("world"), "hello from Zig");
15
15
  });
16
16
 
17
+ test("slowSquare resolves a Promise", async () => {
18
+ assert.strictEqual(await addon.slowSquare(8), 64);
19
+ });
20
+
17
21
  test("index.d.ts declares the exports", () => {
18
22
  const dts = fs.readFileSync("./index.d.ts", "utf8");
19
23
  assert.match(dts, /export function add\(arg0: number, arg1: number\): number;/);
20
24
  assert.match(dts, /export function greet\(arg0: string\): string;/);
25
+ assert.match(dts, /export function slowSquare\(arg0: number\): Promise<number>;/);
21
26
  });