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
11 changes: 11 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_register_decls = .{
.root_source_file = "examples/register_decls/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_register_decls = .{
.root_module = .example_register_decls,
.linkage = .dynamic,
.linker_allow_shlib_undefined = true,
.dest_sub_path = "example_register_decls.node",
},
},
.tests = .{
.napi = .{ .root_module = .napi },
Expand Down
12 changes: 12 additions & 0 deletions examples/register_decls/mod.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { createRequire } from "node:module";
import { describe, expect, it } from "vitest";

const require = createRequire(import.meta.url);
const mod = require("../../zig-out/lib/example_register_decls.node");

describe("registerDecls", () => {
it("registers functions and strings", () => {
expect(mod.add(1, 2)).toEqual(3);
expect(mod.greeting).toEqual("hello");
});
});
14 changes: 14 additions & 0 deletions examples/register_decls/mod.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const zapi = @import("zapi");

const greeting: []const u8 = "hello";

comptime {
zapi.registerDecls(.{
.add = .{ .value = add },
.greeting = .{ .value = greeting },
}, .{});
}

fn add(a: i32, b: i32) i32 {
return a + b;
}
28 changes: 13 additions & 15 deletions src/register_decls.zig
Original file line number Diff line number Diff line change
@@ -1,36 +1,34 @@
const std = @import("std");
const Env = @import("Env.zig");
const Value = @import("Value.zig");
const createCallback = @import("create_callback.zig").createCallback;
const register = @import("module.zig").register;

pub fn registerDecls(comptime decls: anytype, comptime options: anytype) type {
pub fn registerDecls(comptime decls: anytype, comptime options: anytype) void {
_ = options;

const mod = (struct {
pub fn mod(env: Env, module: Value) anyerror!void {
inline for (@typeInfo(@TypeOf(decls)).@"struct".fields) |field| {
const decl = @field(decls, field.name);
const value = switch (@typeInfo(@TypeOf(decl.value))) {
.Fn => try env.createFunction(
.@"fn" => try env.createFunction(
field.name,
@typeInfo(@TypeOf(decl.value)).Fn.params.len,
void,
@typeInfo(@TypeOf(decl.value)).@"fn".params.len,
createCallback(
void,
@typeInfo(@TypeOf(decl.value)).@"fn".params.len,
decl.value,
.{},
),
@constCast(&{}),
null,
),
else => |T| switch (T) {
.Pointer => |p| if (p.size == .Slice) {
if (p.child == u8) {
return try env.createStringUtf8(decl.value);
} else @compileError("unsupported slice type");
} else @compileError("unsupported pointer type"),
else => @compileError("unsupported value type"),
},
.pointer => |p| if (p.size == .slice)
if (p.child == u8)
try env.createStringUtf8(decl.value)
else
@compileError("unsupported slice type")
else
@compileError("unsupported pointer type"),
else => @compileError("unsupported value type"),
};

try module.setNamedProperty(field.name, value);
Expand Down
Loading