Visitar URL original
feat(js): add exact BigInt conversions, toI128LowBits and fromWords by nazarhussain · Pull Request #91 · ChainSafe/zapi · GitHub
Skip to content
Merged
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
Next Next commit
feat(js): add BigInt.fromWords for word-level construction
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
nazarhussain and claude committed Nov 6, 2026
commit 67b6d8051f5d98c38455cb96c24283d00e4a36b7
20 changes: 18 additions & 2 deletions src/js/bigint.zig
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ pub const BigInt = struct {
/// Creates a JavaScript `BigInt` from a Zig integer value.
///
/// Accepts `i64`, `u64`, and `comptime_int` values within this range.
/// For `i128`/`u128` values, use `fromWords` (not currently exposed but
/// available at the `napi.Env` level).
/// For `i128`/`u128` values, use `fromWords`.
///
/// Panics if N-API operations fail (e.g., invalid environment) or for Zig
/// integer types larger than `u64` that cannot be converted to `i64`/`u64`.
Expand Down Expand Up @@ -118,6 +117,23 @@ pub const BigInt = struct {
return .{ .val = val };
}

/// Creates a JavaScript `BigInt` from a sign bit and word array.
///
/// `sign_bit` is 0 for non-negative, 1 for negative. `words` is the
/// little-endian unsigned magnitude. The resulting BigInt equals
/// `(sign_bit == 1 ? -1 : 1) * sum(words[i] << (64 * i))`.
///
/// Use this for constructing BigInts whose magnitude exceeds `u64` (e.g.,
/// i128 or larger). For `i64`/`u64` values, prefer `BigInt.from`.
///
/// Panics if N-API operations fail (e.g., invalid environment).
pub fn fromWords(sign_bit: u1, words: []const u64) BigInt {
const e = context.env();
const val = e.createBigintWords(sign_bit, words) catch
@panic("BigInt.fromWords: createBigintWords failed");
return .{ .val = val };
}

/// Returns the underlying `napi.Value` representation of this JavaScript BigInt.
pub fn toValue(self: BigInt) napi.Value {
return self.val;
Expand Down