Skip to content

feat: BigInt - #11

Open
rekram1-node wants to merge 1 commit into
devfrom
bigint
Open

rekram1-node wants to merge 1 commit into
devfrom
bigint

Conversation

@rekram1-node

Copy link
Copy Markdown
Member

Summary

Programs can use BigInt. Across ~7,000 Sol/Opus Code Mode trials, ~5–6% reached for it (64-bit hashing, LCGs mod 2^64, readBigInt64LE), and today they fail with ReferenceError: Unknown identifier 'BigInt'.

  • Values: host-native bigint is now a Value. Literals in every base, with separators, compile to a pooled constant (COMPILER_VERSION 4).
  • Operators (src/runtime/operations.ts): + - * / % **, & | ^ << >>, unary -/~, ++/-- and compound assignment, following BigInt::*. Mixing with a Number, unary + and >>> throw TypeError; a zero divisor or negative exponent throws RangeError; + with a string concatenates. == and relational operators compare with Numbers and strings by mathematical value (host semantics). Number fast paths are unchanged; BigInts take the slow paths.
  • ToNumber vs ToNumeric: coerceToNumber now throws TypeError on a BigInt, as spec ToNumber does, so Math.*, isNaN, Date, and index arguments all reject it. coerceToNumeric is the new ToNumeric. Number(bigint) converts.
  • Built-ins (src/stdlib/bigint.ts): BigInt() (ToPrimitive, NumberToBigInt, StringToBigInt; new BigInt throws), BigInt.asIntN/asUintN, BigInt.prototype.toString(radix)/toLocaleString (en-US, like Number's)/valueOf, and [object BigInt].
  • JSON / console: JSON.stringify throws TypeError: Do not know how to serialize a BigInt. unless toJSON (including BigInt.prototype.toJSON) or a replacer converts it. console.log prints 1n.
  • Buffer: readBigInt64LE/BE, readBigUInt64LE/BE, writeBigInt64LE/BE, writeBigUInt64LE/BE, plus the Uint aliases, delegated to the host Buffer like the other numeric methods.
  • Host boundary (still JSON-like, as interpreter-support.md said): a BigInt in the result, in a host function argument, or returned by a host function fails with a hint (convert it first, e.g. String(value), or Number(value) if it is small enough). The result and argument errors can be caught by the program.
  • Snapshots: new codec tag ["b", hex]. Hex rather than decimal because it converts in linear time. Snapshot FORMAT goes from 3 to 4, plus a round-trip program in test/snapshot.test.ts.
  • Docs: interpreter-support.md (new BigInt section, Buffer and boundary items), README (supported/not-supported list, test262 numbers), CHANGELOG (Unreleased).

test262

The manifest no longer excludes the BigInt feature.

files vendored pass skipped
before (dev) 22,253 19,522 2,731
after 22,435 19,708 2,727
  • 182 BigInt-feature files are now vendored; 179 pass. The other 1,288 BigInt-feature files are still excluded for other reasons: TypedArray (524), Symbol (242), ArrayBuffer/DataView/Atomics, Temporal, Reflect, propertyHelper.js, boxed primitives (Object(1n)).
  • The 3 failures are unrelated gaps, now in skipped.txt: JSON.rawJSON, Object.getOwnPropertySymbols, and generator methods in object literals.
  • 7 previously skipped Set.prototype.*/size-is-a-number.js files now pass.
  • skipped.txt also refreshes two stale reasons for files that already failed on dev.

bun run check passes: typecheck, 21,308 tests, round-trip mode, and pack. A separate comparison of ~220 expressions against native JS also matched.

Spec deviations

  • class X extends BigInt {} throws a TypeError at class definition, because extending built-ins other than Error is unsupported. JS throws only at super().
  • Primitive wrappers are still rejected: Object(1n) is not supported, matching Number, String and Boolean.
  • Object.prototype.hasOwnProperty.call(1n, "x") throws instead of returning false. This is the existing primitive-receiver gap and also affects Numbers.
  • BigInt.prototype[Symbol.toStringTag] does not exist, since there are no arbitrary symbols. Object.prototype.toString still reports BigInt.
  • No BigInt64Array/BigUint64Array or DataView BigInt methods, since those types don't exist.
  • Error messages are psychoscript's own (V8-like). Buffer argument errors carry the host's message, as other Buffer methods already do.
  • A host function returning a BigInt is still refused; see open questions.

Resource limits (no new limit added; needs a decision)

BigInt values are bounded only by the host engine's own limit (about 2^30 bits, roughly 128 MB, in JSC and V8). Going past it is now a clean RangeError: Maximum BigInt size exceeded.. Converting a BigInt to text checks the existing 2^24-character string limit before converting, so String(2n ** 60000000n) fails at once instead of grinding.

What remains, since one interpreter step can't be interrupted by the timeout (measured on Bun):

  • 2n ** 1000000000n takes about 165 ms and allocates about 125 MB in a single step. A program can keep a few of these alive.
  • Multiplying multi-million-bit values takes tens to hundreds of ms per op. A chain near the host cap took about 7 s before the host refused.
  • Decimal toString of a 4M-bit value takes about 0.3 s. Near the string limit (~55M bits) it would take many seconds in one step. BigInt(str) on a near-limit 16M-digit string is similar.

Proposal: cap a BigInt result at, say, 2^24 bits (about 5M decimal digits, in the spirit of MAX_STRING_LENGTH). Check it in bigintOperator/asUintN/BigInt(string) before computing where we can (<<, ** via bit-length estimates) and after otherwise. That keeps every single step well under a second and bounded in memory. Not done here because it's a new limit.

Open questions (for Aiden/James)

  1. Resource cap: add a BigInt bit-length limit as proposed above? What value?
  2. Snapshot format bump: I bumped FORMAT 3 to 4 for the new ["b", …] tag, and COMPILER_VERSION 3 to 4. Old snapshots were already refused by build id, so this is bookkeeping. Revert FORMAT if additive tags shouldn't bump it.
  3. Inbound BigInts: programs can hold BigInts now, so should a host function be allowed to return one (e.g. a DB driver's int8)? This PR keeps both directions JSON-like and refuses with a hint. Outbound stays refused regardless (results are JSON).
  4. Should the result boundary error be InvalidDataValue (as now, matching the Uint8Array case) or plain ExecutionFailure?

BigInt literals, arithmetic, comparison, BigInt(), asIntN/asUintN, BigInt.prototype, and Buffer's 64-bit BigInt methods, held as host-native bigints. They survive suspend and resume (snapshot format 4) and are refused, with a hint, at the host boundary. Enables test262's BigInt feature.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant