Skip to content

Commit 5a51cf2

Browse files
authored
feat!: use zapi namespace (#12)
* feature: use zapi namespace * chore: update package script for zapi
1 parent 6b260fc commit 5a51cf2

File tree

6 files changed

+53
-53
lines changed

6 files changed

+53
-53
lines changed

build.zig

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ pub fn build(b: *std.Build) void {
1111
options_build_options.addOption([]const u8, "napi_version", option_napi_version);
1212
const options_module_build_options = options_build_options.createModule();
1313

14-
const module_napi = b.createModule(.{
14+
const module_zapi = b.createModule(.{
1515
.root_source_file = b.path("src/root.zig"),
1616
.target = target,
1717
.optimize = optimize,
1818
});
19-
module_napi.addIncludePath(b.path("include"));
20-
b.modules.put(b.dupe("napi"), module_napi) catch @panic("OOM");
19+
module_zapi.addIncludePath(b.path("include"));
20+
b.modules.put(b.dupe("zapi"), module_zapi) catch @panic("OOM");
2121

2222
const module_example_hello_world = b.createModule(.{
2323
.root_source_file = b.path("examples/hello_world/mod.zig"),
@@ -67,19 +67,19 @@ pub fn build(b: *std.Build) void {
6767

6868
const tls_run_test = b.step("test", "Run all tests");
6969

70-
const test_napi = b.addTest(.{
71-
.name = "napi",
72-
.root_module = module_napi,
73-
.filters = b.option([][]const u8, "napi.filters", "napi test filters") orelse &[_][]const u8{},
70+
const test_zapi = b.addTest(.{
71+
.name = "zapi",
72+
.root_module = module_zapi,
73+
.filters = b.option([][]const u8, "zapi.filters", "zapi test filters") orelse &[_][]const u8{},
7474
});
75-
const install_test_napi = b.addInstallArtifact(test_napi, .{});
76-
const tls_install_test_napi = b.step("build-test:napi", "Install the napi test");
77-
tls_install_test_napi.dependOn(&install_test_napi.step);
75+
const install_test_zapi = b.addInstallArtifact(test_zapi, .{});
76+
const tls_install_test_zapi = b.step("build-test:zapi", "Install the zapi test");
77+
tls_install_test_zapi.dependOn(&install_test_zapi.step);
7878

79-
const run_test_napi = b.addRunArtifact(test_napi);
80-
const tls_run_test_napi = b.step("test:napi", "Run the napi test");
81-
tls_run_test_napi.dependOn(&run_test_napi.step);
82-
tls_run_test.dependOn(&run_test_napi.step);
79+
const run_test_zapi = b.addRunArtifact(test_zapi);
80+
const tls_run_test_zapi = b.step("test:zapi", "Run the zapi test");
81+
tls_run_test_zapi.dependOn(&run_test_zapi.step);
82+
tls_run_test.dependOn(&run_test_zapi.step);
8383

8484
const test_example_hello_world = b.addTest(.{
8585
.name = "example_hello_world",
@@ -109,9 +109,9 @@ pub fn build(b: *std.Build) void {
109109
tls_run_test_example_type_tag.dependOn(&run_test_example_type_tag.step);
110110
tls_run_test.dependOn(&run_test_example_type_tag.step);
111111

112-
module_napi.addImport("build_options", options_module_build_options);
112+
module_zapi.addImport("build_options", options_module_build_options);
113113

114-
module_example_hello_world.addImport("napi", module_napi);
114+
module_example_hello_world.addImport("zapi", module_zapi);
115115

116-
module_example_type_tag.addImport("napi", module_napi);
116+
module_example_type_tag.addImport("zapi", module_zapi);
117117
}

build.zig.zon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// This file is generated by zbuild. Do not edit manually.
22

33
.{
4-
.name = .napi_z,
4+
.name = .zapi,
55
.version = "0.1.0",
6-
.fingerprint = 0x86a418feef5ba28a,
6+
.fingerprint = 0x77829ef951b38aac,
77
.minimum_zig_version = "0.14.1",
88
.dependencies = .{},
99
.paths = .{ "build.zig", "build.zig.zon", "src", "include" },

examples/hello_world/mod.zig

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
///! This is an example napi module that exercises various napi features.
22
const std = @import("std");
3-
const napi = @import("napi");
3+
const zapi = @import("zapi");
44
const allocator = std.heap.page_allocator;
55

66
comptime {
77
// The module must be registered with napi via `register`
8-
napi.module.register(exampleMod);
8+
zapi.module.register(exampleMod);
99
}
1010

1111
// This is the top-level module registration function for this module.
1212
// It is called by napi when the module is loaded.
13-
fn exampleMod(env: napi.Env, module: napi.Value) anyerror!void {
13+
fn exampleMod(env: zapi.Env, module: zapi.Value) anyerror!void {
1414
// Example of a string property
1515
try module.setNamedProperty("helloWorld", try env.createStringUtf8(hello_world));
1616

@@ -29,7 +29,7 @@ fn exampleMod(env: napi.Env, module: napi.Value) anyerror!void {
2929
try module.setNamedProperty("add", try env.createFunction(
3030
"add",
3131
2,
32-
napi.createCallback(2, add, .{}),
32+
zapi.createCallback(2, add, .{}),
3333
null,
3434
));
3535

@@ -40,7 +40,7 @@ fn exampleMod(env: napi.Env, module: napi.Value) anyerror!void {
4040
try module.setNamedProperty("add_semimanual", try env.createFunction(
4141
"add_semimanual",
4242
2,
43-
napi.createCallback(2, add_semimanual, .{
43+
zapi.createCallback(2, add_semimanual, .{
4444
.args = .{ .env, .auto, .value },
4545
.returns = .value,
4646
}),
@@ -51,7 +51,7 @@ fn exampleMod(env: napi.Env, module: napi.Value) anyerror!void {
5151
try module.setNamedProperty("surprise", try env.createFunction(
5252
"surprise",
5353
0,
54-
napi.createCallback(0, surprise, .{
54+
zapi.createCallback(0, surprise, .{
5555
.returns = .string,
5656
}),
5757
null,
@@ -60,7 +60,7 @@ fn exampleMod(env: napi.Env, module: napi.Value) anyerror!void {
6060
try module.setNamedProperty("update", try env.createFunction(
6161
"update",
6262
1,
63-
napi.createCallback(1, S.update, .{
63+
zapi.createCallback(1, S.update, .{
6464
.args = .{ .data, .auto },
6565
}),
6666
&s,
@@ -74,15 +74,15 @@ fn exampleMod(env: napi.Env, module: napi.Value) anyerror!void {
7474
0,
7575
Timer_ctor,
7676
null,
77-
&[_]napi.c.napi_property_descriptor{ .{
77+
&[_]zapi.c.napi_property_descriptor{ .{
7878
.utf8name = "reset",
79-
.method = napi.wrapCallback(0, Timer_reset),
79+
.method = zapi.wrapCallback(0, Timer_reset),
8080
}, .{
8181
.utf8name = "read",
82-
.method = napi.wrapCallback(0, Timer_read),
82+
.method = zapi.wrapCallback(0, Timer_read),
8383
}, .{
8484
.utf8name = "lap",
85-
.method = napi.wrapCallback(0, Timer_lap),
85+
.method = zapi.wrapCallback(0, Timer_lap),
8686
} },
8787
),
8888
);
@@ -110,7 +110,7 @@ comptime {
110110
// std.debug.assert(@TypeOf(&add_manual) == napi.Callback(2));
111111
}
112112

113-
fn add_manual(env: napi.Env, cb: napi.CallbackInfo(2)) !napi.Value {
113+
fn add_manual(env: zapi.Env, cb: zapi.CallbackInfo(2)) !zapi.Value {
114114
const a = try cb.arg(0).getValueInt32();
115115
const b = try cb.arg(1).getValueInt32();
116116

@@ -123,7 +123,7 @@ fn add(a: i32, b: i32) !i32 {
123123
return a + b;
124124
}
125125

126-
fn add_semimanual(env: napi.Env, a: i32, b: napi.Value) !napi.Value {
126+
fn add_semimanual(env: zapi.Env, a: i32, b: zapi.Value) !zapi.Value {
127127
const b_int = try b.getValueInt32();
128128

129129
const result = a + b_int;
@@ -153,12 +153,12 @@ var s: S = S{
153153

154154
// Wrapped class example (std.time.Timer)
155155

156-
fn Timer_finalize(_: napi.Env, timer: *std.time.Timer, _: ?*anyopaque) void {
156+
fn Timer_finalize(_: zapi.Env, timer: *std.time.Timer, _: ?*anyopaque) void {
157157
std.debug.print("Destroying timer {any}\n", .{timer});
158158
allocator.destroy(timer);
159159
}
160160

161-
fn Timer_ctor(env: napi.Env, cb: napi.CallbackInfo(0)) !napi.Value {
161+
fn Timer_ctor(env: zapi.Env, cb: zapi.CallbackInfo(0)) !zapi.Value {
162162
const timer = try allocator.create(std.time.Timer);
163163
timer.* = try std.time.Timer.start();
164164
_ = try env.wrap(
@@ -172,18 +172,18 @@ fn Timer_ctor(env: napi.Env, cb: napi.CallbackInfo(0)) !napi.Value {
172172
return cb.this();
173173
}
174174

175-
fn Timer_reset(env: napi.Env, cb: napi.CallbackInfo(0)) !napi.Value {
175+
fn Timer_reset(env: zapi.Env, cb: zapi.CallbackInfo(0)) !zapi.Value {
176176
const timer = try env.unwrap(std.time.Timer, cb.this());
177177
timer.reset();
178178
return try env.getUndefined();
179179
}
180180

181-
fn Timer_read(env: napi.Env, cb: napi.CallbackInfo(0)) !napi.Value {
181+
fn Timer_read(env: zapi.Env, cb: zapi.CallbackInfo(0)) !zapi.Value {
182182
const timer = try env.unwrap(std.time.Timer, cb.this());
183183
return try env.createInt64(@intCast(timer.read()));
184184
}
185185

186-
fn Timer_lap(env: napi.Env, cb: napi.CallbackInfo(0)) !napi.Value {
186+
fn Timer_lap(env: zapi.Env, cb: zapi.CallbackInfo(0)) !zapi.Value {
187187
const timer = try env.unwrap(std.time.Timer, cb.this());
188188
return try env.createInt64(@intCast(timer.lap()));
189189
}
@@ -194,16 +194,16 @@ const AsyncAddData = struct {
194194
a: i32,
195195
b: i32,
196196
result: i32,
197-
deferred: napi.Deferred,
198-
work: napi.AsyncWork(AsyncAddData),
197+
deferred: zapi.Deferred,
198+
work: zapi.AsyncWork(AsyncAddData),
199199
};
200200

201-
fn asyncAddExecute(_: napi.Env, data: *AsyncAddData) void {
201+
fn asyncAddExecute(_: zapi.Env, data: *AsyncAddData) void {
202202
std.time.sleep(1_000_000_000); // 1 second
203203
data.result = data.a + data.b;
204204
}
205205

206-
fn asyncAddComplete(env: napi.Env, status: napi.status.Status, data: *AsyncAddData) void {
206+
fn asyncAddComplete(env: zapi.Env, status: zapi.status.Status, data: *AsyncAddData) void {
207207
defer {
208208
data.work.delete() catch undefined;
209209
allocator.destroy(data);
@@ -220,7 +220,7 @@ fn asyncAddComplete(env: napi.Env, status: napi.status.Status, data: *AsyncAddDa
220220
data.deferred.resolve(env.createInt32(data.result) catch unreachable) catch unreachable;
221221
}
222222

223-
fn asyncAdd(env: napi.Env, cb: napi.CallbackInfo(2)) !napi.Value {
223+
fn asyncAdd(env: zapi.Env, cb: zapi.CallbackInfo(2)) !zapi.Value {
224224
const a = try cb.arg(0).getValueInt32();
225225
const b = try cb.arg(1).getValueInt32();
226226

@@ -256,7 +256,7 @@ const TsfnData = struct {
256256
count: i32,
257257
};
258258

259-
fn startThread(env: napi.Env, cb: napi.CallbackInfo(1)) !napi.Value {
259+
fn startThread(env: zapi.Env, cb: zapi.CallbackInfo(1)) !zapi.Value {
260260
const context = try allocator.create(TsfnContext);
261261

262262
// Create the thread-safe function
@@ -279,7 +279,7 @@ fn startThread(env: napi.Env, cb: napi.CallbackInfo(1)) !napi.Value {
279279
return try env.getUndefined();
280280
}
281281

282-
fn threadMain(tsfn: napi.ThreadSafeFunction(TsfnContext, TsfnData)) void {
282+
fn threadMain(tsfn: zapi.ThreadSafeFunction(TsfnContext, TsfnData)) void {
283283
var i: i32 = 0;
284284
while (i < 5) : (i += 1) {
285285
const data = allocator.create(TsfnData) catch return;
@@ -295,7 +295,7 @@ fn threadMain(tsfn: napi.ThreadSafeFunction(TsfnContext, TsfnData)) void {
295295
tsfn.release(.release) catch {};
296296
}
297297

298-
fn callJs(env: napi.Env, cb: napi.Value, _: *TsfnContext, data: *TsfnData) void {
298+
fn callJs(env: zapi.Env, cb: zapi.Value, _: *TsfnContext, data: *TsfnData) void {
299299
defer allocator.destroy(data);
300300

301301
_ = env.callFunction(
@@ -305,7 +305,7 @@ fn callJs(env: napi.Env, cb: napi.Value, _: *TsfnContext, data: *TsfnData) void
305305
) catch {};
306306
}
307307

308-
fn finalizeTsfn(_: napi.Env, context: *TsfnContext) void {
308+
fn finalizeTsfn(_: zapi.Env, context: *TsfnContext) void {
309309
defer {
310310
context.thread.join();
311311
allocator.destroy(context);

examples/type_tag/mod.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
///! method) silently reinterprets memory. With unwrapChecked the mismatch is
66
///! caught and returns error.InvalidArg instead.
77
const std = @import("std");
8-
const napi = @import("napi");
8+
const napi = @import("zapi");
99
const allocator = std.heap.page_allocator;
1010

1111
comptime {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build": "pnpm build:js && pnpm build:zig",
99
"build:js": "tsc --outDir lib",
1010
"build:zig": "zig build",
11-
"test:zig": "zig build test:napi",
11+
"test:zig": "zig build test:zapi",
1212
"test:js": "vitest run examples/**/*.test.ts",
1313
"test": "pnpm test:zig && pnpm test:js",
1414
"lint:zig": "zig fmt src",

zbuild.zon

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.{
2-
.name = .napi_z,
2+
.name = .zapi,
33
.version = "0.1.0",
4-
.fingerprint = 0x86a418feef5ba28a,
4+
.fingerprint = 0x77829ef951b38aac,
55
.minimum_zig_version = "0.14.1",
66
.dependencies = .{},
77
.paths = .{ "build.zig", "build.zig.zon", "src", "include" },
@@ -14,7 +14,7 @@
1414
},
1515
},
1616
.modules = .{
17-
.napi = .{
17+
.zapi = .{
1818
.root_source_file = "src/root.zig",
1919
.include_paths = .{"include"},
2020
.imports = .{.build_options},
@@ -24,7 +24,7 @@
2424
.example_hello_world = .{
2525
.root_module = .{
2626
.root_source_file = "examples/hello_world/mod.zig",
27-
.imports = .{.napi},
27+
.imports = .{.zapi},
2828
.link_libc = true,
2929
},
3030
.linkage = .dynamic,
@@ -34,7 +34,7 @@
3434
.example_type_tag = .{
3535
.root_module = .{
3636
.root_source_file = "examples/type_tag/mod.zig",
37-
.imports = .{.napi},
37+
.imports = .{.zapi},
3838
.link_libc = true,
3939
},
4040
.linkage = .dynamic,

0 commit comments

Comments
 (0)