Skip to content

fix: emit valid JS for BigInt64Array and BigUint64Array in uneval - #171

Merged
Rich-Harris merged 1 commit into
mainfrom
fix/uneval-bigint-typed-array
Jul 18, 2026
Merged

fix: emit valid JS for BigInt64Array and BigUint64Array in uneval#171
Rich-Harris merged 1 commit into
mainfrom
fix/uneval-bigint-typed-array

Conversation

@elliott-with-the-longest-name-on-github

Copy link
Copy Markdown
Contributor

Problem

uneval renders typed-array elements by interpolating the array directly into a template string, which invokes Array#toString and produces bare numbers:

uneval(new BigInt64Array([1n, 2n, 3n]))
// → 'new BigInt64Array([1,2,3])'

But the BigInt64Array/BigUint64Array constructors require bigint elements, so the emitted code throws when evaluated:

Cannot convert 1 to a BigInt

This affects any BigInt64Array/BigUint64Array whose buffer isn't separately shared (the common case), including subarrays. There was previously no test coverage for these two types.

Fix

Emit bigint elements with an n suffix (e.g. new BigInt64Array([1n,2n,3n])) via a small stringify_typed_array_elements helper used by both the inline and the deduplicated (hoisted) code paths. Non-bigint typed arrays are unchanged.

Scope

  • stringify/parse were not affected — they encode the underlying ArrayBuffer as base64 and rebuild the view from bytes, so elements are never stringified individually.
  • The uneval shared-buffer path was also already correct (it reconstructs from the buffer); only the inline-elements path was broken.

Tests

Adds fixtures for BigInt64Array (with a negative value) and BigUint64Array, plus a repetition fixture, which run through the existing uneval / stringify / parse / round-trip harnesses.

uneval rendered typed-array elements by interpolating the array directly, which calls Array#toString and produces bare numbers (e.g. new BigInt64Array([1,2,3])). BigInt64Array/BigUint64Array constructors require bigint elements, so the emitted code threw 'Cannot convert 1 to a BigInt' when evaluated.

Emit bigint elements with an 'n' suffix. stringify/parse were unaffected (they encode the underlying buffer as base64).
@changeset-bot

changeset-bot Bot commented Jul 18, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8bc2da9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
devalue Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Rich-Harris
Rich-Harris merged commit 3770846 into main Jul 18, 2026
5 checks passed
@Rich-Harris
Rich-Harris deleted the fix/uneval-bigint-typed-array branch July 18, 2026 13:31
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.

2 participants