diff --git a/.github/actions/setup-env/action.yml b/.github/actions/setup-env/action.yml index 27c04a1..59082e2 100644 --- a/.github/actions/setup-env/action.yml +++ b/.github/actions/setup-env/action.yml @@ -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 - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 with: node-version: 24 diff --git a/.gitignore b/.gitignore index 0d3a418..52706a9 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,6 @@ test/spec/static_tests.zig .yarn node_modules/ lib/ +zig-pkg/ docs/superpowers/ .claude/settings.local.json diff --git a/build.zig b/build.zig index 868b3c5..7965398 100644 --- a/build.zig +++ b/build.zig @@ -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"), .{}); } diff --git a/build.zig.zon b/build.zig.zon index 8533ce5..bd085e3 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -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", .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, + }, + }, } diff --git a/examples/hello_world/mod.zig b/examples/hello_world/mod.zig index 51ecb9c..fa4a4b3 100644 --- a/examples/hello_world/mod.zig +++ b/examples/hello_world/mod.zig @@ -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); @@ -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, @@ -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 @@ -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; } @@ -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 diff --git a/src/AsyncContext.zig b/src/AsyncContext.zig index 60aeccd..3abb4a1 100644 --- a/src/AsyncContext.zig +++ b/src/AsyncContext.zig @@ -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"); diff --git a/src/Deferred.zig b/src/Deferred.zig index 2c89248..9c6ef7e 100644 --- a/src/Deferred.zig +++ b/src/Deferred.zig @@ -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"); diff --git a/src/Env.zig b/src/Env.zig index 096fbef..3b6bf1a 100644 --- a/src/Env.zig +++ b/src/Env.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const c = @import("c.zig"); +const c = @import("c.zig").c; const status = @import("status.zig"); const NapiError = status.NapiError; const TypedarrayType = @import("value_types.zig").TypedarrayType; diff --git a/src/EscapableHandleScope.zig b/src/EscapableHandleScope.zig index 469f4a5..522d42e 100644 --- a/src/EscapableHandleScope.zig +++ b/src/EscapableHandleScope.zig @@ -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"); diff --git a/src/HandleScope.zig b/src/HandleScope.zig index 79efbd7..f5f09a8 100644 --- a/src/HandleScope.zig +++ b/src/HandleScope.zig @@ -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; diff --git a/src/NodeVersion.zig b/src/NodeVersion.zig index 9110009..7a04d4d 100644 --- a/src/NodeVersion.zig +++ b/src/NodeVersion.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const c = @import("c.zig"); +const c = @import("c.zig").c; version: c.napi_node_version, diff --git a/src/Ref.zig b/src/Ref.zig index 281e790..9ed19ac 100644 --- a/src/Ref.zig +++ b/src/Ref.zig @@ -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"); diff --git a/src/Value.zig b/src/Value.zig index 6a3f289..534b21b 100644 --- a/src/Value.zig +++ b/src/Value.zig @@ -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 TypedarrayType = @import("value_types.zig").TypedarrayType; diff --git a/src/args.zig b/src/args.zig index cd17e72..885d8f4 100644 --- a/src/args.zig +++ b/src/args.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const c = @import("c.zig"); +const c = @import("c.zig").c; const Value = @import("Value.zig"); pub fn tupleToRaw(args: anytype) [@typeInfo(@TypeOf(args)).@"struct".fields.len]c.napi_value { diff --git a/src/async_cleanup_hook.zig b/src/async_cleanup_hook.zig index 15f9cfe..b7964cd 100644 --- a/src/async_cleanup_hook.zig +++ b/src/async_cleanup_hook.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const c = @import("c.zig"); +const c = @import("c.zig").c; pub fn AsyncCleanupHookCallback(comptime Data: type) type { return *const fn (c.napi_async_cleanup_hook_handle, *Data) void; @@ -10,7 +10,7 @@ pub fn wrapAsyncCleanupHook( comptime cb: AsyncCleanupHookCallback(Data), ) c.napi_async_cleanup_hook { const wrapper = struct { - fn f(handle: c.napi_async_cleanup_hook_handle, arg: ?*anyopaque) callconv(.C) void { + fn f(handle: c.napi_async_cleanup_hook_handle, arg: ?*anyopaque) callconv(.c) void { if (arg == null) return; cb(handle, @ptrCast(@alignCast(arg))); } diff --git a/src/async_work.zig b/src/async_work.zig index ff76471..0a00bc7 100644 --- a/src/async_work.zig +++ b/src/async_work.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const c = @import("c.zig"); +const c = @import("c.zig").c; const status = @import("status.zig"); const NapiError = @import("status.zig").NapiError; const Env = @import("Env.zig"); @@ -18,7 +18,7 @@ pub fn wrapAsyncExecuteCallback( pub fn f( raw_env: c.napi_env, raw_data: ?*anyopaque, - ) callconv(.C) void { + ) callconv(.c) void { if (raw_data == null) return; const env = Env{ .env = raw_env }; const data: *Data = @ptrCast(@alignCast(raw_data)); @@ -41,7 +41,7 @@ pub fn wrapAsyncCompleteCallback( raw_env: c.napi_env, raw_status: c.napi_status, raw_data: ?*anyopaque, - ) callconv(.C) void { + ) callconv(.c) void { if (raw_data == null) return; const env = Env{ .env = raw_env }; const stat: Status = @enumFromInt(raw_status); diff --git a/src/c.zig b/src/c.zig index 452e16c..9c78edb 100644 --- a/src/c.zig +++ b/src/c.zig @@ -1,6 +1,6 @@ const napi_version = @import("build_options").napi_version; -pub usingnamespace @cImport({ +pub const c = @cImport({ @cDefine("NAPI_VERSION", napi_version); @cInclude("node_api.h"); }); diff --git a/src/callback.zig b/src/callback.zig index 12ad3ee..e5b2453 100644 --- a/src/callback.zig +++ b/src/callback.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const c = @import("c.zig"); +const c = @import("c.zig").c; const status = @import("status.zig"); const Env = @import("Env.zig"); const CallbackInfo = @import("callback_info.zig").CallbackInfo; @@ -18,7 +18,7 @@ pub fn wrapCallback( pub fn f( env: c.napi_env, info: c.napi_callback_info, - ) callconv(.C) c.napi_value { + ) callconv(.c) c.napi_value { const e = Env{ .env = env }; const cb_info = CallbackInfo(argc_cap).init(env, info) catch |err| { e.throwError(@errorName(err), "CallbackInfo initialization failed") catch {}; diff --git a/src/callback_info.zig b/src/callback_info.zig index 0b97ecf..6ccf996 100644 --- a/src/callback_info.zig +++ b/src/callback_info.zig @@ -1,5 +1,5 @@ const std = @import("std"); -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"); diff --git a/src/cleanup_hook.zig b/src/cleanup_hook.zig index 4696747..3960564 100644 --- a/src/cleanup_hook.zig +++ b/src/cleanup_hook.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const c = @import("c.zig"); +const c = @import("c.zig").c; pub fn CleanupHookCallback(comptime Data: type) type { return *const fn (*Data) void; @@ -8,9 +8,9 @@ pub fn CleanupHookCallback(comptime Data: type) type { pub fn wrapCleanupHook( comptime Data: type, comptime cb: CleanupHookCallback(Data), -) *const fn (?*anyopaque) callconv(.C) void { +) *const fn (?*anyopaque) callconv(.c) void { const wrapper = struct { - fn f(arg: ?*anyopaque) callconv(.C) void { + fn f(arg: ?*anyopaque) callconv(.c) void { if (arg == null) return; cb(@ptrCast(@alignCast(arg))); } diff --git a/src/finalize_callback.zig b/src/finalize_callback.zig index 47c7705..2bd6fee 100644 --- a/src/finalize_callback.zig +++ b/src/finalize_callback.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const c = @import("c.zig"); +const c = @import("c.zig").c; const Env = @import("Env.zig"); pub fn FinalizeCallback(comptime Data: type) type { @@ -15,7 +15,7 @@ pub fn wrapFinalizeCallback( env: c.napi_env, data: ?*anyopaque, hint: ?*anyopaque, - ) callconv(.C) void { + ) callconv(.c) void { if (data == null) return; return finalize_cb( Env{ .env = env }, diff --git a/src/js/wrap_class.zig b/src/js/wrap_class.zig index d65c1f2..7e246d0 100644 --- a/src/js/wrap_class.zig +++ b/src/js/wrap_class.zig @@ -344,7 +344,7 @@ pub fn getFactoryDescriptors(_: napi.c.napi_value) []const napi.c.napi_property_ const init_argc = init_params.len; const cb = struct { - pub fn callback(raw_env: napi.c.napi_env, cb_info: napi.c.napi_callback_info) callconv(.C) napi.c.napi_value { + pub fn callback(raw_env: napi.c.napi_env, cb_info: napi.c.napi_callback_info) callconv(.c) napi.c.napi_value { const e = napi.Env{ .env = raw_env }; const prev = context.setEnv(e); defer context.restoreEnv(prev); @@ -460,7 +460,7 @@ pub fn getFactoryDescriptors(_: napi.c.napi_value) []const napi.c.napi_property_ const prefers_receiver_ctor = ReturnClass != null and ReturnClass.? == Class; const method_cb = struct { - pub fn callback(raw_env: napi.c.napi_env, cb_info: napi.c.napi_callback_info) callconv(.C) napi.c.napi_value { + pub fn callback(raw_env: napi.c.napi_env, cb_info: napi.c.napi_callback_info) callconv(.c) napi.c.napi_value { const e = napi.Env{ .env = raw_env }; const prev_env = context.setEnv(e); defer context.restoreEnv(prev_env); @@ -522,7 +522,7 @@ pub fn getFactoryDescriptors(_: napi.c.napi_value) []const napi.c.napi_property_ const prefers_receiver_ctor = ReturnClass != null and ReturnClass.? == Class; const getter_cb = struct { - pub fn callback(raw_env: napi.c.napi_env, cb_info: napi.c.napi_callback_info) callconv(.C) napi.c.napi_value { + pub fn callback(raw_env: napi.c.napi_env, cb_info: napi.c.napi_callback_info) callconv(.c) napi.c.napi_value { const e = napi.Env{ .env = raw_env }; const prev_env = context.setEnv(e); defer context.restoreEnv(prev_env); @@ -573,7 +573,7 @@ pub fn getFactoryDescriptors(_: napi.c.napi_value) []const napi.c.napi_property_ const prefers_this_ctor = ReturnClass != null and ReturnClass.? == Class; const static_cb = struct { - pub fn callback(raw_env: napi.c.napi_env, cb_info: napi.c.napi_callback_info) callconv(.C) napi.c.napi_value { + pub fn callback(raw_env: napi.c.napi_env, cb_info: napi.c.napi_callback_info) callconv(.c) napi.c.napi_value { const e = napi.Env{ .env = raw_env }; const prev_env = context.setEnv(e); defer context.restoreEnv(prev_env); @@ -624,7 +624,7 @@ pub fn getFactoryDescriptors(_: napi.c.napi_value) []const napi.c.napi_property_ const ValueParamType = setter_params[1].type.?; const setter_cb = struct { - pub fn callback(raw_env: napi.c.napi_env, cb_info: napi.c.napi_callback_info) callconv(.C) napi.c.napi_value { + pub fn callback(raw_env: napi.c.napi_env, cb_info: napi.c.napi_callback_info) callconv(.c) napi.c.napi_value { const e = napi.Env{ .env = raw_env }; const prev_env = context.setEnv(e); defer context.restoreEnv(prev_env); diff --git a/src/js/wrap_function.zig b/src/js/wrap_function.zig index ca13872..5d02758 100644 --- a/src/js/wrap_function.zig +++ b/src/js/wrap_function.zig @@ -308,7 +308,7 @@ pub fn wrapFunction(comptime func: anytype) napi.c.napi_callback { const required_argc = comptime requiredArgCount(params); const wrapper = struct { - pub fn callback(raw_env: napi.c.napi_env, cb_info: napi.c.napi_callback_info) callconv(.C) napi.c.napi_value { + pub fn callback(raw_env: napi.c.napi_env, cb_info: napi.c.napi_callback_info) callconv(.c) napi.c.napi_value { const e = napi.Env{ .env = raw_env }; const prev = context.setEnv(e); defer context.restoreEnv(prev); diff --git a/src/module.zig b/src/module.zig index 16c862d..d834556 100644 --- a/src/module.zig +++ b/src/module.zig @@ -1,4 +1,4 @@ -const c = @import("c.zig"); +const c = @import("c.zig").c; const Env = @import("Env.zig"); const Value = @import("Value.zig"); @@ -6,10 +6,8 @@ extern fn napi_register_module_v1(env: c.napi_env, module: c.napi_value) c.napi_ pub fn register(comptime f: fn (Env, Value) anyerror!void) void { const wrapper = opaque { - fn napi_register_module_v1(env: c.napi_env, module: c.napi_value) callconv(.C) c.napi_value { - const e = Env{ - .env = env, - }; + fn napi_register_module_v1(env: c.napi_env, module: c.napi_value) callconv(.c) c.napi_value { + const e = Env{ .env = env }; const v = Value{ .env = env, .value = module, diff --git a/src/napi.zig b/src/napi.zig index 84d3856..aad1c2b 100644 --- a/src/napi.zig +++ b/src/napi.zig @@ -1,6 +1,6 @@ const std = @import("std"); -pub const c = @import("c.zig"); +pub const c = @import("c.zig").c; pub const AsyncContext = @import("AsyncContext.zig"); pub const Env = @import("Env.zig"); pub const Value = @import("Value.zig"); diff --git a/src/root.zig b/src/root.zig index 2525e67..af5e1e2 100644 --- a/src/root.zig +++ b/src/root.zig @@ -3,9 +3,29 @@ const std = @import("std"); pub const napi = @import("napi.zig"); pub const js = @import("js.zig"); -// Backwards-compatible flat exports: all existing napi symbols -// remain accessible at the top level. -pub usingnamespace @import("napi.zig"); +pub const c = napi.c; +pub const AsyncContext = napi.AsyncContext; +pub const Env = napi.Env; +pub const Value = napi.Value; +pub const Deferred = napi.Deferred; +pub const EscapableHandleScope = napi.EscapableHandleScope; +pub const HandleScope = napi.HandleScope; +pub const NodeVersion = napi.NodeVersion; +pub const status = napi.status; +pub const module = napi.module; +pub const Ref = napi.Ref; +pub const CallbackInfo = napi.CallbackInfo; +pub const Callback = napi.Callback; +pub const FinalizeCallback = napi.FinalizeCallback; +pub const value_types = napi.value_types; +pub const createCallback = napi.createCallback; +pub const registerDecls = napi.registerDecls; +pub const wrapFinalizeCallback = napi.wrapFinalizeCallback; +pub const wrapCallback = napi.wrapCallback; +pub const AsyncWork = napi.AsyncWork; +pub const ThreadSafeFunction = napi.ThreadSafeFunction; +pub const CallMode = napi.CallMode; +pub const ReleaseMode = napi.ReleaseMode; test { std.testing.refAllDecls(@This()); diff --git a/src/status.zig b/src/status.zig index 061d588..c20fc2c 100644 --- a/src/status.zig +++ b/src/status.zig @@ -1,4 +1,4 @@ -const c = @import("c.zig"); +const c = @import("c.zig").c; /// https://nodejs.org/api/n-api.html#napi_status pub const Status = enum(c.napi_status) { diff --git a/src/threadsafe_function.zig b/src/threadsafe_function.zig index 03bdbc0..6f21391 100644 --- a/src/threadsafe_function.zig +++ b/src/threadsafe_function.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const c = @import("c.zig"); +const c = @import("c.zig").c; const status = @import("status.zig"); const NapiError = status.NapiError; const Env = @import("Env.zig"); @@ -30,7 +30,7 @@ pub fn wrapCallJsCallback( raw_js_callback: c.napi_value, raw_context: ?*anyopaque, raw_data: ?*anyopaque, - ) callconv(.C) void { + ) callconv(.c) void { // If raw_env is null, the TSFN is being torn down. if (raw_env == null) return; // Context should be present if we customized it @@ -58,7 +58,7 @@ pub fn wrapFinalizeCallback( raw_env: c.napi_env, raw_data: ?*anyopaque, hint: ?*anyopaque, - ) callconv(.C) void { + ) callconv(.c) void { _ = hint; if (raw_data == null and Context != void) return; const env = Env{ .env = raw_env }; diff --git a/src/value_types.zig b/src/value_types.zig index acf113b..bd21a00 100644 --- a/src/value_types.zig +++ b/src/value_types.zig @@ -1,4 +1,4 @@ -const c = @import("c.zig"); +const c = @import("c.zig").c; /// https://nodejs.org/api/n-api.html#napi_key_collection_mode pub const KeyCollectionMode = enum(c.napi_key_collection_mode) { diff --git a/zbuild.zon b/zbuild.zon deleted file mode 100644 index 5016239..0000000 --- a/zbuild.zon +++ /dev/null @@ -1,61 +0,0 @@ -.{ - .name = .zapi, - .version = "1.0.1", // x-release-please-version - .fingerprint = 0x77829ef951b38aac, - .minimum_zig_version = "0.14.1", - .dependencies = .{}, - .paths = .{ "build.zig", "build.zig.zon", "src", "include" }, - .options_modules = .{ - .build_options = .{ - .napi_version = .{ - .type = "string", - .default = "10", - }, - }, - }, - .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, - }, - }, - .libraries = .{ - .example_hello_world = .{ - .root_module = .{ - .root_source_file = "examples/hello_world/mod.zig", - .imports = .{.zapi}, - .link_libc = true, - }, - .linkage = .dynamic, - .linker_allow_shlib_undefined = true, - .dest_sub_path = "example_hello_world.node", - }, - .example_type_tag = .{ - .root_module = .{ - .root_source_file = "examples/type_tag/mod.zig", - .imports = .{.zapi}, - .link_libc = true, - }, - .linkage = .dynamic, - .linker_allow_shlib_undefined = true, - .dest_sub_path = "example_type_tag.node", - }, - .example_js_dsl = .{ - .root_module = .{ - .root_source_file = "examples/js_dsl/mod.zig", - .imports = .{.zapi}, - .link_libc = true, - }, - .linkage = .dynamic, - .linker_allow_shlib_undefined = true, - .dest_sub_path = "example_js_dsl.node", - }, - }, -}