Skip to content

Commit 9b7159f

Browse files
committed
fix: fix init crash
1 parent fe3b1f7 commit 9b7159f

3 files changed

Lines changed: 21 additions & 43 deletions

File tree

examples/init/src/hello.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ fn add(left: f64, right: f64) f64 {
2727
return result;
2828
}
2929

30-
fn hello(name: []u8) []u8 {
30+
fn hello(env: napi.Env, name: []u8) napi.String {
3131
const allocator = std.heap.page_allocator;
3232

3333
const message = std.fmt.allocPrint(allocator, "Hello, {s}!", .{name}) catch @panic("OOM");
3434
defer allocator.free(message);
3535

36-
return message;
36+
return napi.String.New(env, message);
3737
}
3838

3939
fn fib(env: napi.Env, n: f64) void {

src/build/napi-tsgen.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,6 +1634,11 @@ fn emitSourceTypeExpr(state: *State, file_path: []const u8, type_expr: []const u
16341634

16351635
if (std.mem.eql(u8, trimmed, "void")) return "void";
16361636
if (std.mem.eql(u8, trimmed, "bool")) return "boolean";
1637+
if (std.mem.eql(u8, trimmed, "napi.Bool")) return "boolean";
1638+
if (std.mem.eql(u8, trimmed, "napi.String")) return "string";
1639+
if (std.mem.eql(u8, trimmed, "napi.Number")) return "number";
1640+
if (std.mem.eql(u8, trimmed, "napi.Null")) return "null";
1641+
if (std.mem.eql(u8, trimmed, "napi.Undefined")) return "undefined";
16371642
if (isSourceNumericType(trimmed)) return "number";
16381643
if (isSourceStringType(trimmed)) return "string";
16391644
if (std.mem.eql(u8, trimmed, "AbortSignal") or

src/napi/value/object.zig

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -79,48 +79,21 @@ pub const Object = struct {
7979
}
8080

8181
pub fn Set(self: Object, comptime key: []const u8, value: anytype) !void {
82-
const value_type = @TypeOf(value);
83-
const infos = @typeInfo(value_type);
84-
85-
switch (infos) {
86-
.@"fn" => {
87-
const args_type = comptime helper.collectFunctionArgs(value_type);
88-
const return_type = infos.@"fn".return_type.?;
89-
const fn_impl = try Function(args_type, return_type).New(Env.from_raw(self.env), key, value);
90-
const napi_desc = [_]napi.napi_property_descriptor{
91-
.{
92-
.utf8name = @ptrCast(key.ptr),
93-
.method = fn_impl.inner_fn,
94-
.getter = null,
95-
.setter = null,
96-
.value = null,
97-
.attributes = napi.napi_default,
98-
.data = null,
99-
},
100-
};
101-
const status = napi.napi_define_properties(self.env, self.raw, 1, &napi_desc);
102-
if (status != napi.napi_ok) {
103-
return NapiError.Error.fromStatus(NapiError.Status.New(status));
104-
}
105-
},
106-
else => {
107-
const n_value = try Napi.to_napi_value(self.env, value, null);
108-
const napi_desc = [_]napi.napi_property_descriptor{
109-
.{
110-
.utf8name = @ptrCast(key.ptr),
111-
.method = null,
112-
.getter = null,
113-
.setter = null,
114-
.value = n_value,
115-
.attributes = napi.napi_default,
116-
.data = null,
117-
},
118-
};
119-
const status = napi.napi_define_properties(self.env, self.raw, 1, &napi_desc);
120-
if (status != napi.napi_ok) {
121-
return NapiError.Error.fromStatus(NapiError.Status.New(status));
122-
}
82+
const n_value = try Napi.to_napi_value(self.env, value, key);
83+
const napi_desc = [_]napi.napi_property_descriptor{
84+
.{
85+
.utf8name = @ptrCast(key.ptr),
86+
.method = null,
87+
.getter = null,
88+
.setter = null,
89+
.value = n_value,
90+
.attributes = napi.napi_default,
91+
.data = null,
12392
},
93+
};
94+
const status = napi.napi_define_properties(self.env, self.raw, 1, &napi_desc);
95+
if (status != napi.napi_ok) {
96+
return NapiError.Error.fromStatus(NapiError.Status.New(status));
12497
}
12598
}
12699

0 commit comments

Comments
 (0)