Skip to content

Commit 75f38db

Browse files
committed
feat: use type name as class name
1 parent 21afa13 commit 75f38db

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

src/Node.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub const NodeContext = struct {
3939
const lifetime = getLifetimeHandler(T);
4040
const props = getProps(T);
4141
var class: c.napi_value = undefined;
42-
s2e(c.napi_define_class(self.napi_env, "Foo", 3, lifetime.init, null, props.len, props.ptr, &class)) catch unreachable;
42+
s2e(c.napi_define_class(self.napi_env, @typeName(T), @typeName(T).len, lifetime.init, null, props.len, props.ptr, &class)) catch unreachable;
4343

4444
return NodeValue.init(self.napi_env, class);
4545
}
@@ -168,14 +168,18 @@ pub const NodeContext = struct {
168168
pub fn init(env: c.napi_env, cb: c.napi_callback_info) callconv(.c) c.napi_value {
169169
std.log.info("init {any} {any} {any}", .{ env, cb, T });
170170

171-
const init_fn = @field(T, "init");
172-
173171
var new_target: c.napi_value = null;
174172
if (c.napi_get_new_target(env, cb, &new_target) != c.napi_ok or new_target == null) {
175173
_ = c.napi_throw_error(env, null, "Constructor must be called with `new`.");
176174
return null;
177175
}
178176

177+
// TODO: if no init, use args object to set fields
178+
// TODO: if no fields -> treat as namespace
179+
// if (@hasField(T, "init")) {}
180+
181+
const init_fn = @field(T, "init");
182+
179183
const params = @typeInfo(@TypeOf(init_fn)).@"fn".params;
180184

181185
const node = NodeContext{ .napi_env = env };
@@ -254,7 +258,7 @@ pub const NodeContext = struct {
254258
const self: *T = @ptrCast(@alignCast(data.?));
255259
std.log.info(" Finalizer data {any} {any}", .{ data, self });
256260
freee(T, self);
257-
registry.wrapped_instances.remove(@intFromPtr(self));
261+
registry.untrack(self);
258262
}
259263
}
260264

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { describe, it, expect } from "bun:test";
2+
3+
import requireTestModule from "../";
4+
const TestClass = requireTestModule("export-class");
5+
6+
describe("node_api.register(type)", () => {
7+
it("should return class", () => {
8+
expect(typeof TestClass).toEqual("function");
9+
expect(TestClass.name).toEqual("TestClass");
10+
expect(TestClass.toString()).toEqual(
11+
"function TestClass() {\n [native code]\n}"
12+
);
13+
});
14+
15+
it("should define static memethods", () => {
16+
expect(typeof TestClass.static).toEqual("function");
17+
expect(typeof TestClass.static).toEqual("function");
18+
expect(TestClass.static()).toEqual("static");
19+
});
20+
21+
it("should be constructable", () => {
22+
expect(new TestClass()).toEqual({});
23+
});
24+
25+
it("should have methods", () => {
26+
const instance = new TestClass();
27+
expect(typeof instance.method).toEqual("function");
28+
expect(instance.method()).toEqual("method");
29+
});
30+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
pub fn init() @This() {
2+
return .{};
3+
}
4+
5+
pub fn deinit(_: @This()) !void {}
6+
7+
pub fn method(_: @This()) ![]const u8 {
8+
return "method";
9+
}
10+
11+
pub fn static() ![]const u8 {
12+
return "static";
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const node_api = @import("node-api");
2+
const TestClass = @import("TestClass.zig");
3+
4+
comptime {
5+
node_api.@"export"(TestClass);
6+
}

0 commit comments

Comments
 (0)