Skip to content

Commit 88ea152

Browse files
committed
auto-convert types by convention
1 parent dfde62b commit 88ea152

File tree

5 files changed

+22
-28
lines changed

5 files changed

+22
-28
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ The `node-api` Zig package provides [Node-API](https://nodejs.org/api/n-api.html
22

33
TODO:
44

5+
56
- [ ] Add GH pipeline (incl. test report)
7+
- [ ] auto-register class for members of type `type`.
68
- [ ] Make `NodeFunction` thread safe by convention when it is used in an async function/method.
79
- [ ] Add `NodeObject` for "by reference" access to JS objects from Zig
810
- [ ] Add `NodeArray` for "by reference" access to JS objects from Zig

src/Serializer.zig

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@ const lib = @import("c.zig");
33
const c = lib.c;
44
const s2e = lib.statusToError;
55
const node_values = @import("node_values.zig");
6-
6+
const NodeContext = @import("Node.zig").NodeContext;
77
const NodeValue = node_values.NodeValue;
88
const NodeObject = node_values.NodeObject;
99
const NodeArray = node_values.NodeArray;
1010
const NodeFunction = node_values.NodeFunction;
1111
const registry = @import("references.zig").Registry;
12+
1213
/// Converts a Zig value to a Node-API value. Memory for the node value is allocated by V8.
1314
pub fn serialize(env: c.napi_env, value: anytype) !c.napi_value {
1415
const T = @TypeOf(value);
1516

17+
const node = NodeContext.init(env);
1618
var res: c.napi_value = undefined;
1719

1820
switch (@typeInfo(T)) {
21+
.type => return (try node.defineClass(value)).napi_value,
22+
.@"fn" => return (try node.defineFunction(value)).napi_value,
1923
.null => try s2e(c.napi_get_null(env, &res)),
2024
.void, .undefined => try s2e(c.napi_get_undefined(env, &res)),
2125
.bool => try s2e(c.napi_get_boolean(env, value, &res)),
624 Bytes
Binary file not shown.

tests/wrap.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ describe("wrapInstance", () => {
55
it("should wrap instance", () => {
66
expect(addon.wrappedInstance).toBeObject();
77
});
8+
it("should wrap by convention", () => {
9+
expect(addon.wrappedByConvention).toBeObject();
10+
addon.wrappedByConvention.foo = 456;
11+
expect(addon.wrappedByConvention.foo).toEqual(456);
12+
});
813
it("should wrap fields", () => {
914
console.log(addon.wrappedInstance);
1015
expect(addon.wrappedInstance.foo).toEqual(123);

tests/zig/test-module.zig

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,26 @@ const TestClass = @import("TestClass.zig");
55

66
const WrapTarget = @import("WrapTarget.zig");
77
const Serialization = @import("Serialization.zig");
8+
89
comptime {
910
node_api.register(init);
1011
}
1112

1213
fn init(node: node_api.NodeContext) !?node_api.NodeValue {
13-
// const i = getInt();
14-
// const ui = getUInt();
15-
// const b = try node.deserialize(bool, try node.serialize(true));
16-
17-
// const ;
14+
const ptr = try std.heap.c_allocator.create(WrapTarget);
15+
ptr.* = .{ .foo = 123, .bar = "hopla" };
1816

19-
const v = try node.serialize(.{
20-
.serialization = try node.defineClass(Serialization),
21-
.TestClass = try node.defineClass(TestClass),
17+
return try node.serialize(.{
18+
.serialization = Serialization,
19+
.TestClass = TestClass,
2220
.wrappedInstance = try node.wrapInstance(WrapTarget, .{ .foo = 123, .bar = "hopla" }),
21+
.wrappedByConvention = ptr,
2322
.functions = .{
24-
.fnWithJsNewedNativeInstance = try node.defineFunction(fnWithJsNewedNativeInstance),
25-
.fnWithSerializedParams = try node.defineFunction(fnWithSerializedParams),
23+
.fnWithJsNewedNativeInstance = fnWithJsNewedNativeInstance,
24+
.fnWithSerializedParams = fnWithSerializedParams,
2625
.fnWithAllocatorParam = try node.defineFunction(fnWithAllocatorParam),
2726
.fnCallback = try node.defineFunction(fnCallback),
27+
// async must still be done explicitly
2828
.fnCallbackAsync = try node.defineAsyncFunction(fnCallbackAsync),
2929
.asyncFunction = try node.defineAsyncFunction(sleep),
3030
},
@@ -35,24 +35,7 @@ fn init(node: node_api.NodeContext) !?node_api.NodeValue {
3535
.comptime_int = try node.deserialize(i32, try node.serialize(1234)),
3636
.float = try node.deserialize(f32, try node.serialize(12.34)),
3737
},
38-
// .s = s,
39-
// .x = x,
40-
// .b = b,
41-
// .foo = "foo",
42-
// .bar = "bar",
43-
// .int = 123,
44-
// .f = 12.34,
45-
// .i = i,
46-
// .ui = ui,
47-
// .nested = .{ .more = "foo" },
48-
// .callMet = try node.createFunction(),
4938
});
50-
51-
// const v = try node.serialize(.{
52-
// .fun = try node.createFunc(testFuncNative2),
53-
// });
54-
55-
return v;
5639
}
5740

5841
fn getInt() i16 {

0 commit comments

Comments
 (0)