Skip to content

fix: serialize DataView subviews with correct byte offset and length - #166

Merged
Rich-Harris merged 1 commit into
mainfrom
fix/dataview-subview
Jul 16, 2026
Merged

Rich-Harris merged 1 commit into
mainfrom
fix/dataview-subview

Conversation

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

Copy link
Copy Markdown
Contributor

Problem

Serializing a DataView that is a subview of its buffer (created with new DataView(buffer, byteOffset, byteLength)) produced incorrect output in two separate code paths:

  • uneval referenced thing.startOffset — a property that does not exist on DataView. It evaluates to undefined, so the output was new DataView(..., undefined, 4). On revival, undefined coerces to 0, silently placing the view at the wrong offset with no error.
  • stringify reused the shared typed-array branch and emitted typedArray.length. DataView has no .length property, so the output was ["DataView", 1, 2, undefined] — invalid JSON that throws on parse.

Full-buffer DataViews were unaffected (the offset/length arguments are only emitted for subviews), which is why this went unnoticed.

Fix

Both paths now emit byteOffset, byteLength, matching the new DataView(buffer, byteOffset, byteLength) constructor signature. In stringify, the shared typed-array/DataView branch now selects byteLength for DataViews (a byte count) and length for typed arrays (an element count).

Tests

Adds regression fixtures for both an inline and a named (deduplicated) DataView subview. Verified that each fixture fails against the unfixed code for the right reason:

  • Reverting the uneval fix → uneval: basics "DataView subview" fails with ...,undefined,4) vs expected ...,2,4).
  • Reverting the stringify fix → 6 fixtures across stringify / stringifyAsync / stringifyAsync round-trip fail with ["DataView",1,2,undefined] vs expected ["DataView",1,2,4].

With both fixes in place, the full suite passes.

@changeset-bot

changeset-bot Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b50f85c

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

A DataView subview (created with a byteOffset/byteLength) was serialized incorrectly in two code paths:

- uneval used a nonexistent `startOffset` property, which is `undefined` and coerces to 0 on revival, silently placing the view at the wrong offset.
- stringify reused the typed-array branch and emitted `.length`, which is `undefined` for a DataView, producing invalid JSON that fails to parse.

Both now emit `byteOffset,byteLength`, matching the `new DataView(buffer, byteOffset, byteLength)` constructor. Adds regression fixtures for both an inline and a named (deduplicated) DataView subview.

@Rich-Harris Rich-Harris left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"startOffset"? wow

@Rich-Harris
Rich-Harris merged commit 48cc81f into main Jul 16, 2026
5 checks passed
@Rich-Harris
Rich-Harris deleted the fix/dataview-subview branch July 16, 2026 01:36
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