Visitar URL original
feat(js): add exact BigInt conversions, toI128LowBits and fromWords by nazarhussain · Pull Request #91 · ChainSafe/zapi · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
feat(js): add exact BigInt i64/u64 conversions and toI128LowBits
BigInt.toI64Exact and BigInt.toU64Exact return error.Overflow when the
value does not fit, instead of the low 64 bits toI64/toU64 return.
BigInt.toI128LowBits returns BigInt.asIntN(128) semantics. Existing
methods are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
nazarhussain and claude committed Nov 6, 2026
commit 226ade0921fa8c430c1573c1f8f8c4a04299aaad
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ Code paths that cannot accept or return DSL classes pass
| `Number` | `number` | `toI32()`, `toF64()`, `assertI32()`, `from(anytype)` |
| `String` | `string` | `toSlice(buf)`, `toOwnedSlice(alloc)`, `len()`, `from([]const u8)` |
| `Boolean` | `boolean` | `toBool()`, `assertBool()`, `from(bool)` |
| `BigInt` | `bigint` | `toI64()`, `toU64()`, `toI128()`, `from(anytype)` |
| `BigInt` | `bigint` | `toI64()`, `toU64()`, `toI64Exact()`, `toU64Exact()`, `toI128()`, `toI128LowBits()`, `from(anytype)`, `fromWords()` |
| `Date` | `Date` | `toTimestamp()`, `from(f64)` |
| `Array` | `Array` | `get(i)`, `getNumber(i)`, `length()`, `set(i, val)` |
| `Object(T)` | `object` | `get()`, `set(value)` — `T` fields must be DSL types |
Expand Down
5 changes: 5 additions & 0 deletions examples/js_dsl/mod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ describe("primitive types", () => {
expect(mod.doubleBigInt(50n)).toEqual(100n);
});

it("doubleBigInt rejects BigInts outside the i64 range", () => {
expect(() => mod.doubleBigInt(2n ** 63n)).toThrow("Overflow");
expect(() => mod.doubleBigInt(-(2n ** 63n) - 1n)).toThrow("Overflow");
});

it("doubleBigInt rejects non-bigint arguments at the JS boundary", () => {
expectTypeErrorWithMessage(() => mod.doubleBigInt(50), "Argument 1 must be a bigint");
expectTypeErrorWithMessage(() => mod.doubleBigInt("50"), "Argument 1 must be a bigint");
Expand Down
3 changes: 1 addition & 2 deletions examples/js_dsl/mod.zig
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ pub fn reverseString(s: String) !String {

/// Double a BigInt value.
pub fn doubleBigInt(n: BigInt) !BigInt {
var lossless: bool = false;
const val = try n.toI64(&lossless);
const val = try n.toI64Exact();
return BigInt.from(val * 2);
}

Expand Down
40 changes: 40 additions & 0 deletions src/js/bigint.zig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ pub const BigInt = struct {
return self.val.getValueBigintUint64(lossless);
}

/// Converts the JavaScript BigInt exactly to a Zig `i64`.
///
/// Returns `error.Overflow` if the value is outside the `i64` range,
/// instead of the low 64 bits `toI64` returns.
pub fn toI64Exact(self: BigInt) !i64 {
var lossless = false;
const value = try self.toI64(&lossless);
if (!lossless) return error.Overflow;
return value;
}

/// Converts the JavaScript BigInt exactly to a Zig `u64`.
///
/// Returns `error.Overflow` if the value is negative or outside the `u64`
/// range, instead of the low 64 bits `toU64` returns.
pub fn toU64Exact(self: BigInt) !u64 {
var lossless = false;
const value = try self.toU64(&lossless);
if (!lossless) return error.Overflow;
return value;
}

/// Attempts to convert the JavaScript BigInt to a Zig `i128`.
///
/// This function reads the BigInt as two 64-bit words and reconstructs it
Expand All @@ -57,6 +79,24 @@ pub const BigInt = struct {
return @intCast(magnitude);
}

/// Converts the JavaScript BigInt to the low 128 bits as a Zig `i128`.
///
/// Returns `BigInt.asIntN(128, b)`
/// (https://tc39.es/ecma262/#sec-bigint.asintn), so out-of-range values
/// wrap instead of failing. Use `toI128` for an exact conversion.
pub fn toI128LowBits(self: BigInt) !i128 {
var sign_bit: u1 = 0;
var words: [2]u64 = .{ 0, 0 };
// On overflow N-API still fills the two low words and the sign.
_ = self.val.getValueBigintWords(&sign_bit, &words) catch |err| switch (err) {
error.Overflow => {},
else => |e| return e,
};
const magnitude: u128 = (@as(u128, words[1]) << 64) | words[0];
if (sign_bit == 1) return @bitCast(0 -% magnitude);
return @bitCast(magnitude);
}

/// Converts the JavaScript BigInt to a Zig `i64`, panicking on failure.
///
/// This is a convenience method for cases where the BigInt is guaranteed
Expand Down
Loading