Repository navigation
feat: BigInt - #11
Open
rekram1-node wants to merge 1 commit into
Open
feat: BigInt#11rekram1-node wants to merge 1 commit into
rekram1-node wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 withReferenceError: Unknown identifier 'BigInt'.bigintis now aValue. Literals in every base, with separators, compile to a pooled constant (COMPILER_VERSION4).src/runtime/operations.ts):+ - * / % **,& | ^ << >>, unary-/~,++/--and compound assignment, following BigInt::*. Mixing with a Number, unary+and>>>throwTypeError; a zero divisor or negative exponent throwsRangeError;+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.coerceToNumbernow throwsTypeErroron a BigInt, as spec ToNumber does, soMath.*,isNaN, Date, and index arguments all reject it.coerceToNumericis the new ToNumeric.Number(bigint)converts.src/stdlib/bigint.ts):BigInt()(ToPrimitive, NumberToBigInt, StringToBigInt;new BigIntthrows),BigInt.asIntN/asUintN,BigInt.prototype.toString(radix)/toLocaleString(en-US, like Number's)/valueOf, and[object BigInt].JSON.stringifythrowsTypeError: Do not know how to serialize a BigInt.unlesstoJSON(includingBigInt.prototype.toJSON) or a replacer converts it.console.logprints1n.readBigInt64LE/BE,readBigUInt64LE/BE,writeBigInt64LE/BE,writeBigUInt64LE/BE, plus theUintaliases, delegated to the host Buffer like the other numeric methods.interpreter-support.mdsaid): 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.["b", hex]. Hex rather than decimal because it converts in linear time. SnapshotFORMATgoes from 3 to 4, plus a round-trip program intest/snapshot.test.ts.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
BigIntfeature.dev)propertyHelper.js, boxed primitives (Object(1n)).skipped.txt:JSON.rawJSON,Object.getOwnPropertySymbols, and generator methods in object literals.Set.prototype.*/size-is-a-number.jsfiles now pass.skipped.txtalso refreshes two stale reasons for files that already failed ondev.bun run checkpasses: 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 aTypeErrorat class definition, because extending built-ins other than Error is unsupported. JS throws only atsuper().Object(1n)is not supported, matching Number, String and Boolean.Object.prototype.hasOwnProperty.call(1n, "x")throws instead of returningfalse. 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.toStringstill reportsBigInt.BigInt64Array/BigUint64Arrayor DataView BigInt methods, since those types don't exist.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, soString(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 ** 1000000000ntakes about 165 ms and allocates about 125 MB in a single step. A program can keep a few of these alive.toStringof 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 inbigintOperator/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)
FORMAT3 to 4 for the new["b", …]tag, andCOMPILER_VERSION3 to 4. Old snapshots were already refused by build id, so this is bookkeeping. RevertFORMATif additive tags shouldn't bump it.int8)? This PR keeps both directions JSON-like and refuses with a hint. Outbound stays refused regardless (results are JSON).InvalidDataValue(as now, matching the Uint8Array case) or plainExecutionFailure?