Skip to content

fix: preserve -0 in float typed arrays in uneval - #178

Merged
Rich-Harris merged 1 commit into
sveltejs:mainfrom
maximilliangrand:fix/uneval-float-negative-zero
Aug 20, 2026
Merged

fix: preserve -0 in float typed arrays in uneval#178
Rich-Harris merged 1 commit into
sveltejs:mainfrom
maximilliangrand:fix/uneval-float-negative-zero

Conversation

@maximilliangrand

Copy link
Copy Markdown
Contributor

Bug — uneval loses the sign of -0 in float typed arrays:

uneval(new Float64Array([-0])); // "new Float64Array([0])" -> round-trips to +0
stringify(new Float64Array([-0])); // preserves -0

Cause: stringify_typed_array_elements serializes non-BigInt typed-array elements with Array.prototype.toString, which renders -0 as "0". stringify is unaffected because it base64-encodes the raw buffer, so the two serializers disagree and only uneval drops the sign bit.

Fix: for Float32Array/Float64Array/Float16Array, map elements and emit -0 explicitly (via Object.is); integer arrays keep the fast toString path. Float16Array is typeof-guarded so nothing throws on Node without it.

Evidence: added a Float64Array([-0, 1.5]) fixture (fails before, passes after); full suite 797/797, build + publint clean.

uneval serialized Float32Array/Float64Array/Float16Array elements via
Array.prototype.toString, which renders -0 as "0", so a negative zero
silently became +0 on round-trip. stringify is unaffected (it encodes the
raw buffer as base64). Emit "-0" explicitly for those elements.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@changeset-bot

changeset-bot Bot commented Aug 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 706b2b1

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 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.

thank you!

@Rich-Harris
Rich-Harris merged commit 376b65c into sveltejs:main Aug 20, 2026
5 checks passed
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