Skip to content
Merged
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
13 changes: 13 additions & 0 deletions examples/js_dsl/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,19 @@ describe("Counter class", () => {
expect(c.getCount()).toEqual(2);
});

it("passes class pointers to exported functions", () => {
const c = new mod.Counter(3);
mod.incrementCounter(c);
expect(c.getCount()).toEqual(4);
});

it("rejects the wrong class for pointer arguments", () => {
expectTypeErrorWithMessage(
() => mod.incrementCounter(new mod.Buffer(4)),
"Argument 1 must be an instance of mod.Counter",
);
});

it("isAbove returns boolean", () => {
const c = new mod.Counter(10);
expect(c.isAbove(5)).toBe(true);
Expand Down
5 changes: 5 additions & 0 deletions examples/js_dsl/mod.zig
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ pub const Counter = struct {
}
};

/// Increment a counter passed as a class pointer argument.
pub fn incrementCounter(counter: *Counter) void {
counter.count += 1;
}

/// A resource-owning buffer class demonstrating deinit.
pub const Buffer = struct {
pub const js_meta = js.class(.{});
Expand Down
2 changes: 1 addition & 1 deletion src/js/wrap_function.zig
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub fn convertArg(comptime T: type, raw: napi.c.napi_value, env: napi.c.napi_env
}
switch (@typeInfo(T)) {
.pointer => |ptr| {
if (ptr.size == .one and class_meta.isClassType(ptr.child)) {
if (comptime ptr.size == .one and class_meta.isClassType(ptr.child)) {
const e = napi.Env{ .env = env };
return e.unwrapChecked(ptr.child, value, class_runtime.typeTag(ptr.child)) catch error.TypeMismatch;
}
Expand Down
Loading