Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
15 changes: 15 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
.imports = .{.zapi},
.link_libc = true,
},
.example_non_dsl_warning = .{
.root_source_file = "examples/non_dsl_warning/mod.zig",
.imports = .{.zapi},
.link_libc = true,
},
},
.libraries = .{
.example_hello_world = .{
Expand All @@ -69,6 +74,12 @@
.linker_allow_shlib_undefined = true,
.dest_sub_path = "example_js_dsl.node",
},
.example_non_dsl_warning = .{
.root_module = .example_non_dsl_warning,
.linkage = .dynamic,
.linker_allow_shlib_undefined = true,
.dest_sub_path = "example_non_dsl_warning.node",
},
},
.tests = .{
.napi = .{ .root_module = .napi },
Expand All @@ -88,5 +99,9 @@
.root_module = .example_js_dsl,
.linker_allow_shlib_undefined = true,
},
.example_non_dsl_warning = .{
.root_module = .example_non_dsl_warning,
.linker_allow_shlib_undefined = true,
},
},
}
32 changes: 32 additions & 0 deletions examples/non_dsl_warning/mod.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expect, it } from "vitest";
import { spawnSync } from "node:child_process";
const non_dsl_mod = require("../../zig-out/lib/example_non_dsl_warning.node");

const cwd = new URL("../..", import.meta.url);

function runNode(source: string) {
return spawnSync(process.execPath, ["-e", source], {
cwd,
encoding: "utf8",
});
}

describe("non-DSL fn should warn user", () => {
it("exports DSL functions and skips non-DSL functions", () => {
const result = runNode(`
const mod = require("./zig-out/lib/example_non_dsl_warning.node");
process.stdout.write(JSON.stringify({
exported: mod.exported(41),
skipped: typeof mod.skipped,
}));
`);

expect(result.status).toEqual(0);
expect(JSON.parse(result.stdout)).toEqual({
exported: 42,
skipped: "undefined",
});

expect(result.stderr).toContain("zapi: skipping non-DSL function mod.skipped, this will not be exported");
});
});
29 changes: 29 additions & 0 deletions examples/non_dsl_warning/mod.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! This example module demonstrates a public function with non-DSL parameters
//! being skipped by `zapi`'s `exportModule` functionality.
//!
//! `zapi` will only export zig functions as bindings if they:
//!
//! 1) are public functions (`pub fn`),
//! 2) use DSL-compatible parameters (eg. `js.Number`).
//!
//! In this scenario, even though `skipped` is public, it does not use
//! a DSL-compatible type for its `value` parameter and is therefore
//! not exported.
//!
//! If the user should want to export any other functions manually,
//! they would need to pass a custom `.register` to `js.exportModule`.
const js = @import("zapi").js;

const Number = js.Number;

pub fn exported(value: Number) Number {
return Number.from(value.assertI32() + 1);
}

pub fn skipped(value: u32) u32 {
return value + 1;
}

comptime {
js.exportModule(@This(), .{});
}
5 changes: 4 additions & 1 deletion src/js/export_module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ fn registerDecls(comptime Module: type, env: napi.Env, module: napi.Value, compt
}
break :blk true;
};
if (!is_dsl_fn) continue;
if (!is_dsl_fn) {
std.log.warn("zapi: skipping non-DSL function {s}.{s}, this will not be exported", .{ @typeName(Module), decl.name });
continue;
}

// DSL function — wrap and register
const cb = wrap_function.wrapFunction(field);
Expand Down
Loading