Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f69fb19
chore: upgrade to Zig 0.16.0-dev (master)
lodekeeper-z Mar 19, 2026
a236788
chore: merge upstream main (type-tag-aware unwrap, safer integer conv…
lodekeeper-z Mar 23, 2026
650da13
Merge remote-tracking branch 'upstream/main' into chore/zig-master
lodekeeper-z Mar 25, 2026
880afd6
chore: merge upstream main (Ref refactor, raw variant functions, safe…
lodekeeper-z Mar 25, 2026
0f6d617
chore: merge upstream main (napi→zapi rename, CI, hello_world example)
lodekeeper-z Mar 26, 2026
a5e62b7
fix: restore hello_world example with Zig 0.16 Timer migration
GrapeBaBa Apr 17, 2026
a5f5711
refactor(examples): use std.Io instead of libc for timer and sleep
GrapeBaBa Apr 17, 2026
24bb6d5
refactor(examples): use Io.Threaded.global_single_threaded
GrapeBaBa Apr 17, 2026
3272eb2
refactor(examples): use explicit Io.Threaded init instead of global
GrapeBaBa Apr 17, 2026
6face6a
refactor(examples): remove Timer wrapper, use Io.Timestamp directly
GrapeBaBa Apr 17, 2026
b614ae2
fix(build): exclude example tests from global test step
GrapeBaBa Apr 17, 2026
91fca03
fix: address PR review comments
GrapeBaBa Apr 17, 2026
05b18eb
fix: use threadlocal for Io.Threaded to avoid cross-thread sharing
GrapeBaBa Apr 17, 2026
f18aab2
fix: restore zbuild generated-file headers
GrapeBaBa Apr 17, 2026
ef82c4c
fix: remove unnecessary link_libc from zapi module
GrapeBaBa Apr 17, 2026
6d86a6a
example: use stdlib global_single_threaded Io in hello_world
Apr 20, 2026
a0312a4
Merge remote-tracking branch 'upstream/main' into chore/zig-master
Apr 20, 2026
76dae72
chore: port main's newly-merged code to Zig 0.16
Apr 20, 2026
014aed6
chore: migrate to zbuild library mode
GrapeBaBa Apr 25, 2026
4a372c6
chore: declare test linker_allow_shlib_undefined in manifest
GrapeBaBa Apr 26, 2026
0c62a9f
fix: remove obsolete zbuild manifest
GrapeBaBa Apr 27, 2026
064f283
Merge remote-tracking branch 'origin/main' into chore/zig-master
GrapeBaBa Apr 28, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/actions/setup-env/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ runs:
- uses: pnpm/action-setup@fc06bc1257f339d1d5d8b3a19a8cae5388b55320 # v4
- uses: mlugg/setup-zig@d1434d08867e3ee9daa34448df10607b98908d29 # v2
with:
version: 0.14.1
version: 0.16.0
Comment thread
GrapeBaBa marked this conversation as resolved.
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
with:
node-version: 24
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ test/spec/static_tests.zig
.yarn
node_modules/
lib/
zig-pkg/
docs/superpowers/
.claude/settings.local.json
182 changes: 4 additions & 178 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,181 +1,7 @@
// This file is generated by zbuild. Do not edit manually.

const std = @import("std");
const zbuild = @import("zbuild");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const options_build_options = b.addOptions();
const option_napi_version = b.option([]const u8, "napi_version", "") orelse "10";
options_build_options.addOption([]const u8, "napi_version", option_napi_version);
const options_module_build_options = options_build_options.createModule();

const module_napi = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
module_napi.addIncludePath(b.path("include"));
b.modules.put(b.dupe("napi"), module_napi) catch @panic("OOM");

const module_zapi = b.createModule(.{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
module_zapi.addIncludePath(b.path("include"));
b.modules.put(b.dupe("zapi"), module_zapi) catch @panic("OOM");

const module_example_hello_world = b.createModule(.{
.root_source_file = b.path("examples/hello_world/mod.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
b.modules.put(b.dupe("example_hello_world"), module_example_hello_world) catch @panic("OOM");

const lib_example_hello_world = b.addLibrary(.{
.name = "example_hello_world",
.root_module = module_example_hello_world,
.linkage = .dynamic,
});

lib_example_hello_world.linker_allow_shlib_undefined = true;
const install_lib_example_hello_world = b.addInstallArtifact(lib_example_hello_world, .{
.dest_sub_path = "example_hello_world.node",
});

const tls_install_lib_example_hello_world = b.step("build-lib:example_hello_world", "Install the example_hello_world library");
tls_install_lib_example_hello_world.dependOn(&install_lib_example_hello_world.step);
b.getInstallStep().dependOn(&install_lib_example_hello_world.step);

const module_example_type_tag = b.createModule(.{
.root_source_file = b.path("examples/type_tag/mod.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
b.modules.put(b.dupe("example_type_tag"), module_example_type_tag) catch @panic("OOM");

const lib_example_type_tag = b.addLibrary(.{
.name = "example_type_tag",
.root_module = module_example_type_tag,
.linkage = .dynamic,
});

lib_example_type_tag.linker_allow_shlib_undefined = true;
const install_lib_example_type_tag = b.addInstallArtifact(lib_example_type_tag, .{
.dest_sub_path = "example_type_tag.node",
});

const tls_install_lib_example_type_tag = b.step("build-lib:example_type_tag", "Install the example_type_tag library");
tls_install_lib_example_type_tag.dependOn(&install_lib_example_type_tag.step);
b.getInstallStep().dependOn(&install_lib_example_type_tag.step);

const module_example_js_dsl = b.createModule(.{
.root_source_file = b.path("examples/js_dsl/mod.zig"),
.target = target,
.optimize = optimize,
.link_libc = true,
});
b.modules.put(b.dupe("example_js_dsl"), module_example_js_dsl) catch @panic("OOM");

const lib_example_js_dsl = b.addLibrary(.{
.name = "example_js_dsl",
.root_module = module_example_js_dsl,
.linkage = .dynamic,
});

lib_example_js_dsl.linker_allow_shlib_undefined = true;
const install_lib_example_js_dsl = b.addInstallArtifact(lib_example_js_dsl, .{
.dest_sub_path = "example_js_dsl.node",
});

const tls_install_lib_example_js_dsl = b.step("build-lib:example_js_dsl", "Install the example_js_dsl library");
tls_install_lib_example_js_dsl.dependOn(&install_lib_example_js_dsl.step);
b.getInstallStep().dependOn(&install_lib_example_js_dsl.step);

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

const test_napi = b.addTest(.{
.name = "napi",
.root_module = module_napi,
.filters = b.option([][]const u8, "napi.filters", "napi test filters") orelse &[_][]const u8{},
});
const install_test_napi = b.addInstallArtifact(test_napi, .{});
const tls_install_test_napi = b.step("build-test:napi", "Install the napi test");
tls_install_test_napi.dependOn(&install_test_napi.step);

const run_test_napi = b.addRunArtifact(test_napi);
const tls_run_test_napi = b.step("test:napi", "Run the napi test");
tls_run_test_napi.dependOn(&run_test_napi.step);
tls_run_test.dependOn(&run_test_napi.step);

const test_zapi = b.addTest(.{
.name = "zapi",
.root_module = module_zapi,
.filters = b.option([][]const u8, "zapi.filters", "zapi test filters") orelse &[_][]const u8{},
});
const install_test_zapi = b.addInstallArtifact(test_zapi, .{});
const tls_install_test_zapi = b.step("build-test:zapi", "Install the zapi test");
tls_install_test_zapi.dependOn(&install_test_zapi.step);

const run_test_zapi = b.addRunArtifact(test_zapi);
const tls_run_test_zapi = b.step("test:zapi", "Run the zapi test");
tls_run_test_zapi.dependOn(&run_test_zapi.step);
tls_run_test.dependOn(&run_test_zapi.step);

const test_example_hello_world = b.addTest(.{
.name = "example_hello_world",
.root_module = module_example_hello_world,
.filters = b.option([][]const u8, "example_hello_world.filters", "example_hello_world test filters") orelse &[_][]const u8{},
});
const install_test_example_hello_world = b.addInstallArtifact(test_example_hello_world, .{});
const tls_install_test_example_hello_world = b.step("build-test:example_hello_world", "Install the example_hello_world test");
tls_install_test_example_hello_world.dependOn(&install_test_example_hello_world.step);

const run_test_example_hello_world = b.addRunArtifact(test_example_hello_world);
const tls_run_test_example_hello_world = b.step("test:example_hello_world", "Run the example_hello_world test");
tls_run_test_example_hello_world.dependOn(&run_test_example_hello_world.step);
tls_run_test.dependOn(&run_test_example_hello_world.step);

const test_example_type_tag = b.addTest(.{
.name = "example_type_tag",
.root_module = module_example_type_tag,
.filters = b.option([][]const u8, "example_type_tag.filters", "example_type_tag test filters") orelse &[_][]const u8{},
});
const install_test_example_type_tag = b.addInstallArtifact(test_example_type_tag, .{});
const tls_install_test_example_type_tag = b.step("build-test:example_type_tag", "Install the example_type_tag test");
tls_install_test_example_type_tag.dependOn(&install_test_example_type_tag.step);

const run_test_example_type_tag = b.addRunArtifact(test_example_type_tag);
const tls_run_test_example_type_tag = b.step("test:example_type_tag", "Run the example_type_tag test");
tls_run_test_example_type_tag.dependOn(&run_test_example_type_tag.step);
tls_run_test.dependOn(&run_test_example_type_tag.step);

const test_example_js_dsl = b.addTest(.{
.name = "example_js_dsl",
.root_module = module_example_js_dsl,
.filters = b.option([][]const u8, "example_js_dsl.filters", "example_js_dsl test filters") orelse &[_][]const u8{},
});
const install_test_example_js_dsl = b.addInstallArtifact(test_example_js_dsl, .{});
const tls_install_test_example_js_dsl = b.step("build-test:example_js_dsl", "Install the example_js_dsl test");
tls_install_test_example_js_dsl.dependOn(&install_test_example_js_dsl.step);

const run_test_example_js_dsl = b.addRunArtifact(test_example_js_dsl);
const tls_run_test_example_js_dsl = b.step("test:example_js_dsl", "Run the example_js_dsl test");
tls_run_test_example_js_dsl.dependOn(&run_test_example_js_dsl.step);
tls_run_test.dependOn(&run_test_example_js_dsl.step);

module_napi.addImport("build_options", options_module_build_options);

module_zapi.addImport("build_options", options_module_build_options);

module_example_hello_world.addImport("zapi", module_zapi);

module_example_type_tag.addImport("zapi", module_zapi);

module_example_js_dsl.addImport("zapi", module_zapi);
pub fn build(b: *std.Build) !void {
@setEvalBranchQuota(200_000);
_ = try zbuild.configureBuild(b, @import("build.zig.zon"), .{});
}
90 changes: 86 additions & 4 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,10 +1,92 @@
// This file is generated by zbuild. Do not edit manually.

.{
.name = .zapi,
.version = "1.0.1", // x-release-please-version
.fingerprint = 0x77829ef951b38aac,
.minimum_zig_version = "0.14.1",
.dependencies = .{},
.minimum_zig_version = "0.16.0",
Comment thread
GrapeBaBa marked this conversation as resolved.
.paths = .{ "build.zig", "build.zig.zon", "src", "include" },
.dependencies = .{
.zbuild = .{
.url = "git+https://github.com/ChainSafe/zbuild?ref=refactor/comptime-library-rewrite#7ec852f1c6495b62d964e311bd210b6f9a3015be",
.hash = "zbuild-0.4.0-XJFav9lXAgCtcFwxSb5qEkex3kijECLtqo7z9FzrkUaf",
},
},
.options_modules = .{
.build_options = .{
.napi_version = .{
.type = .string,
.default = "10",
.description = "Node.js NAPI version (default: 10).",
},
},
},
.modules = .{
// `napi` and `zapi` share the same source file but expose different
// imports — `napi` is used by `napi.zig`-only consumers, while `zapi`
// links libc and is the canonical entry point for example modules.
.napi = .{
.root_source_file = "src/root.zig",
.include_paths = .{"include"},
.imports = .{.build_options},
},
.zapi = .{
.root_source_file = "src/root.zig",
.include_paths = .{"include"},
.imports = .{.build_options},
.link_libc = true,
},
.example_hello_world = .{
.root_source_file = "examples/hello_world/mod.zig",
.imports = .{.zapi},
.link_libc = true,
},
.example_type_tag = .{
.root_source_file = "examples/type_tag/mod.zig",
.imports = .{.zapi},
.link_libc = true,
},
.example_js_dsl = .{
.root_source_file = "examples/js_dsl/mod.zig",
.imports = .{.zapi},
.link_libc = true,
},
},
.libraries = .{
.example_hello_world = .{
.root_module = .example_hello_world,
.linkage = .dynamic,
.linker_allow_shlib_undefined = true,
.dest_sub_path = "example_hello_world.node",
},
.example_type_tag = .{
.root_module = .example_type_tag,
.linkage = .dynamic,
.linker_allow_shlib_undefined = true,
.dest_sub_path = "example_type_tag.node",
},
.example_js_dsl = .{
.root_module = .example_js_dsl,
.linkage = .dynamic,
.linker_allow_shlib_undefined = true,
.dest_sub_path = "example_js_dsl.node",
},
},
.tests = .{
.napi = .{ .root_module = .napi },
.zapi = .{ .root_module = .zapi },
// Example tests reference napi C symbols (`napi_wrap`, `napi_typeof`, …)
// which Node provides at dlopen time. Standalone zig test binaries don't
// have Node around, so allow undefined shared-library symbols.
.example_hello_world = .{
.root_module = .example_hello_world,
.linker_allow_shlib_undefined = true,
},
.example_type_tag = .{
.root_module = .example_type_tag,
.linker_allow_shlib_undefined = true,
},
.example_js_dsl = .{
.root_module = .example_js_dsl,
.linker_allow_shlib_undefined = true,
},
},
}
43 changes: 27 additions & 16 deletions examples/hello_world/mod.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
///! This is an example napi module that exercises various napi features.
const std = @import("std");
const Io = std.Io;
const zapi = @import("zapi");
const allocator = std.heap.page_allocator;

fn io() Io {
return Io.Threaded.global_single_threaded.io();
}

comptime {
// The module must be registered with napi via `register`
zapi.module.register(exampleMod);
Expand Down Expand Up @@ -151,20 +156,20 @@ var s: S = S{
.b = 2,
};

// Wrapped class example (std.time.Timer)
// Wrapped class example using Io.Timestamp directly

fn Timer_finalize(_: zapi.Env, timer: *std.time.Timer, _: ?*anyopaque) void {
std.debug.print("Destroying timer {any}\n", .{timer});
allocator.destroy(timer);
fn Timer_finalize(_: zapi.Env, ts: *Io.Timestamp, _: ?*anyopaque) void {
std.debug.print("Destroying timer {any}\n", .{ts});
allocator.destroy(ts);
}

fn Timer_ctor(env: zapi.Env, cb: zapi.CallbackInfo(0)) !zapi.Value {
const timer = try allocator.create(std.time.Timer);
timer.* = try std.time.Timer.start();
const ts = try allocator.create(Io.Timestamp);
ts.* = Io.Timestamp.now(io(), .awake);
_ = try env.wrap(
cb.this(),
std.time.Timer,
timer,
Io.Timestamp,
ts,
Timer_finalize,
null,
null,
Expand All @@ -173,19 +178,23 @@ fn Timer_ctor(env: zapi.Env, cb: zapi.CallbackInfo(0)) !zapi.Value {
}

fn Timer_reset(env: zapi.Env, cb: zapi.CallbackInfo(0)) !zapi.Value {
const timer = try env.unwrap(std.time.Timer, cb.this());
timer.reset();
const ts = try env.unwrap(Io.Timestamp, cb.this());
ts.* = Io.Timestamp.now(io(), .awake);
return try env.getUndefined();
}

fn Timer_read(env: zapi.Env, cb: zapi.CallbackInfo(0)) !zapi.Value {
const timer = try env.unwrap(std.time.Timer, cb.this());
return try env.createInt64(@intCast(timer.read()));
const ts = try env.unwrap(Io.Timestamp, cb.this());
const elapsed = ts.durationTo(Io.Timestamp.now(io(), .awake));
return try env.createInt64(@intCast(elapsed.nanoseconds));
}

fn Timer_lap(env: zapi.Env, cb: zapi.CallbackInfo(0)) !zapi.Value {
const timer = try env.unwrap(std.time.Timer, cb.this());
return try env.createInt64(@intCast(timer.lap()));
const ts = try env.unwrap(Io.Timestamp, cb.this());
const now_ts = Io.Timestamp.now(io(), .awake);
const elapsed = ts.durationTo(now_ts);
ts.* = now_ts;
return try env.createInt64(@intCast(elapsed.nanoseconds));
}

// Async work example
Expand All @@ -199,7 +208,8 @@ const AsyncAddData = struct {
};

fn asyncAddExecute(_: zapi.Env, data: *AsyncAddData) void {
std.time.sleep(1_000_000_000); // 1 second
// sleep 1 second using std.Io
io().sleep(Io.Duration.fromSeconds(1), .awake) catch {};
data.result = data.a + data.b;
}

Expand Down Expand Up @@ -288,7 +298,8 @@ fn threadMain(tsfn: zapi.ThreadSafeFunction(TsfnContext, TsfnData)) void {
// Call into JS
tsfn.call(data, .blocking) catch {};

std.time.sleep(100 * std.time.ns_per_ms);
// sleep 100ms using std.Io
io().sleep(Io.Duration.fromMilliseconds(100), .awake) catch {};
}

// Release the thread-safe function
Expand Down
2 changes: 1 addition & 1 deletion src/AsyncContext.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const c = @import("c.zig");
const c = @import("c.zig").c;
const status = @import("status.zig");
const NapiError = @import("status.zig").NapiError;
const Value = @import("Value.zig");
Expand Down
Loading
Loading