Skip to content
Merged
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
53 changes: 26 additions & 27 deletions src/to_from_value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ pub fn fromValue(
return try value.getValueBool();
},
.int => |i| {
if (T == i32) {
return try value.getValueInt32();
if (i.signedness == .signed) {
const n: i64 = if (i.bits <= 32)
try value.getValueInt32()
else
try value.getValueInt64();
return std.math.cast(T, n) orelse error.InvalidArg;
}
if (T == u32) {
return try value.getValueUint32();
}
if (i.bits < 32) {
if (i.signedness == .signed) {
return @intCast(try value.getValueInt32());
} else {
return @intCast(try value.getValueUint32());
}
}
return @intCast(try value.getValueInt64());

const n: i64 = if (i.bits <= 32)
@as(i64, try value.getValueUint32())
else
try value.getValueInt64();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clarifying question: i know this was the same code previously, but how come value.getValueInt64() works here for >u32? does the napi api require an i64 result?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

napi doesn't provide a napi_get_value_uint64 — the only 64-bit integer API is napi_get_value_int64 which returns i64. So for unsigned types >32 bits we're forced to read as i64 first, then std.math.cast handles the range check (rejecting negative values or values that overflow the target type).

return std.math.cast(T, n) orelse error.InvalidArg;
},
.float => {
if (T == f64) {
Expand Down Expand Up @@ -61,20 +60,17 @@ pub fn toValue(
return try env.getBoolean(v);
},
.int => |i| {
if (T == i32) {
return try env.createInt32(v);
}
if (T == u32) {
return try env.createUint32(v);
}
if (i.bits < 32) {
if (i.signedness == .signed) {
return try env.createInt32(v);
} else {
return try env.createUint32(v);
if (i.signedness == .signed) {
if (i.bits <= 32) {
return try env.createInt32(std.math.cast(i32, v) orelse return error.InvalidArg);
}
return try env.createInt64(std.math.cast(i64, v) orelse return error.InvalidArg);
}

if (i.bits <= 32) {
return try env.createUint32(std.math.cast(u32, v) orelse return error.InvalidArg);
}
return try env.createInt64(@intCast(v));
return try env.createInt64(std.math.cast(i64, v) orelse return error.InvalidArg);
},
.float => {
if (T == f64) {
Expand All @@ -86,10 +82,13 @@ pub fn toValue(
.pointer => |p| {
const h = hint;
if (p.child == u8 and p.size == .slice) {
const bytes: []const u8 = @ptrCast(v);
if (h == .string) {
return try env.createStringUtf8(@ptrCast(v));
return try env.createStringUtf8(bytes);
} else if (h == .external_buffer) {
return try env.createExternalBuffer(bytes, null, null);
} else if (h == .buffer or h == .auto) {
return try env.createBufferCopy(@ptrCast(v), null);
return try env.createBufferCopy(bytes, null);
}
}
const child_type_info = @typeInfo(p.child);
Expand Down