Skip to content

Commit 0773d1c

Browse files
nazarhussainclaude
andcommitted
refactor(js)!: report inexact Number and BigInt conversions as InexactConversion
Exact conversions now share one error. BigInt previously reused InvalidArg, which collides with N-API's own invalid-argument status. BREAKING CHANGE: Number.toU32Exact and Number.toSafeInteger return error.InexactConversion instead of error.InvalidUnsignedInteger. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 17fba3f commit 0773d1c

3 files changed

Lines changed: 19 additions & 19 deletions

File tree

‎examples/js_dsl/mod.test.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe("primitive types", () => {
6767

6868
it("exactU32 rejects values outside the u32 range", () => {
6969
for (const value of [-1, 1.5, 2 ** 32, Number.MAX_SAFE_INTEGER, Infinity, -Infinity, NaN]) {
70-
expect(() => mod.exactU32(value), `value ${value}`).toThrow("InvalidUnsignedInteger");
70+
expect(() => mod.exactU32(value), `value ${value}`).toThrow("InexactConversion");
7171
}
7272
});
7373

@@ -79,14 +79,14 @@ describe("primitive types", () => {
7979

8080
it("safeInteger rejects values that are not non-negative safe integers", () => {
8181
for (const value of [-1, 1.5, Number.MAX_SAFE_INTEGER + 1, Infinity, -Infinity, NaN]) {
82-
expect(() => mod.safeInteger(value), `value ${value}`).toThrow("InvalidUnsignedInteger");
82+
expect(() => mod.safeInteger(value), `value ${value}`).toThrow("InexactConversion");
8383
}
8484
});
8585

8686
// 2^32 separates the two: in range for a safe integer, out of range for a u32.
8787
it("safeInteger and exactU32 differ exactly at the u32 boundary", () => {
8888
expect(mod.safeInteger(2 ** 32)).toEqual(2 ** 32);
89-
expect(() => mod.exactU32(2 ** 32)).toThrow("InvalidUnsignedInteger");
89+
expect(() => mod.exactU32(2 ** 32)).toThrow("InexactConversion");
9090
});
9191

9292
it("toggleBool", () => {
@@ -156,11 +156,11 @@ describe("primitive types", () => {
156156
});
157157

158158
it("rejects BigInts outside the i128 range instead of crashing", () => {
159-
expect(() => mod.bigIntToI128String(2n ** 127n)).toThrow();
160-
expect(() => mod.bigIntToI128String(-(2n ** 127n) - 1n)).toThrow();
161-
expect(() => mod.bigIntToI128String(2n ** 128n)).toThrow();
162-
expect(() => mod.bigIntToI128String(2n ** 200n)).toThrow();
163-
expect(() => mod.bigIntToI128String(-(2n ** 200n))).toThrow();
159+
expect(() => mod.bigIntToI128String(2n ** 127n)).toThrow("InexactConversion");
160+
expect(() => mod.bigIntToI128String(-(2n ** 127n) - 1n)).toThrow("InexactConversion");
161+
expect(() => mod.bigIntToI128String(2n ** 128n)).toThrow("InexactConversion");
162+
expect(() => mod.bigIntToI128String(2n ** 200n)).toThrow("InexactConversion");
163+
expect(() => mod.bigIntToI128String(-(2n ** 200n))).toThrow("InexactConversion");
164164
});
165165
});
166166

‎src/js/bigint.zig‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ pub const BigInt = struct {
1616

1717
/// Attempts to convert the JavaScript BigInt exactly to a Zig `i64`.
1818
///
19-
/// Returns `error.InvalidArg` if the value is outside the `i64` range. Use
19+
/// Returns `error.InexactConversion` if the value is outside the `i64` range. Use
2020
/// `toI64LowBits` when low-64-bit conversion is intended.
2121
pub fn toI64(self: BigInt) !i64 {
2222
var lossless = false;
2323
const value = try self.toI64LowBits(&lossless);
24-
if (!lossless) return error.InvalidArg;
24+
if (!lossless) return error.InexactConversion;
2525
return value;
2626
}
2727

@@ -37,12 +37,12 @@ pub const BigInt = struct {
3737

3838
/// Attempts to convert the JavaScript BigInt exactly to a Zig `u64`.
3939
///
40-
/// Returns `error.InvalidArg` if the value is negative or outside the `u64`
40+
/// Returns `error.InexactConversion` if the value is negative or outside the `u64`
4141
/// range. Use `toU64LowBits` when low-64-bit conversion is intended.
4242
pub fn toU64(self: BigInt) !u64 {
4343
var lossless = false;
4444
const value = try self.toU64LowBits(&lossless);
45-
if (!lossless) return error.InvalidArg;
45+
if (!lossless) return error.InexactConversion;
4646
return value;
4747
}
4848

@@ -58,7 +58,7 @@ pub const BigInt = struct {
5858

5959
/// Attempts to convert the JavaScript BigInt exactly to a Zig `i128`.
6060
///
61-
/// Returns `error.InvalidArg` if the value is outside `[-2^127, 2^127)`.
61+
/// Returns `error.InexactConversion` if the value is outside `[-2^127, 2^127)`.
6262
/// Use `toI128LowBits` when low-128-bit conversion is intended.
6363
///
6464
/// Returns an error if N-API operations fail.
@@ -68,19 +68,19 @@ pub const BigInt = struct {
6868
// BigInts needing more than two words are always outside the i128
6969
// range; getValueBigintWords reports them as error.Overflow.
7070
_ = self.val.getValueBigintWords(&sign_bit, &words) catch |err| switch (err) {
71-
error.Overflow => return error.InvalidArg,
71+
error.Overflow => return error.InexactConversion,
7272
else => |e| return e,
7373
};
7474
const lo: u128 = words[0];
7575
const hi: u128 = words[1];
7676
const magnitude: u128 = (hi << 64) | lo;
7777
const max_positive: u128 = std.math.maxInt(i128);
7878
if (sign_bit == 1) {
79-
if (magnitude > max_positive + 1) return error.InvalidArg;
79+
if (magnitude > max_positive + 1) return error.InexactConversion;
8080
if (magnitude == max_positive + 1) return std.math.minInt(i128);
8181
return -@as(i128, @intCast(magnitude));
8282
}
83-
if (magnitude > max_positive) return error.InvalidArg;
83+
if (magnitude > max_positive) return error.InexactConversion;
8484
return @intCast(magnitude);
8585
}
8686

‎src/js/number.zig‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub const Number = struct {
6565
value > max or
6666
@trunc(value) != value)
6767
{
68-
return error.InvalidUnsignedInteger;
68+
return error.InexactConversion;
6969
}
7070
return @intFromFloat(value);
7171
}
@@ -74,7 +74,7 @@ pub const Number = struct {
7474
///
7575
/// Exact across the whole `u32` range.
7676
///
77-
/// Returns `error.InvalidUnsignedInteger` if the number is negative,
77+
/// Returns `error.InexactConversion` if the number is negative,
7878
/// fractional, non-finite, or greater than `std.math.maxInt(u32)`.
7979
pub fn toU32Exact(self: Number) !u32 {
8080
return self.toUnsignedExact(u32, std.math.maxInt(u32));
@@ -83,7 +83,7 @@ pub const Number = struct {
8383
/// Attempts to convert the JavaScript number to a non-negative JS safe
8484
/// integer, returned as a `u64`.
8585
///
86-
/// Returns `error.InvalidUnsignedInteger` if the number is negative,
86+
/// Returns `error.InexactConversion` if the number is negative,
8787
/// fractional, non-finite, or greater than `Number.MAX_SAFE_INTEGER`
8888
/// (2^53 - 1).
8989
///

0 commit comments

Comments
 (0)