Conversation
added 2 commits
May 29, 2026 08:19
…string coercion (#2374) `[Symbol.toPrimitive]` defined as a computed method on a class (`class T { [Symbol.toPrimitive](hint) {...} }`) was dropped entirely during HIR lowering — the well-known-symbol method handler in `lower_decl/class_decl.rs` fell through to a `continue` for any well-known symbol other than iterator/asyncIterator/hasInstance/ toStringTag/dispose. As a result the method was never registered in the class vtable, so unary `+` (numeric coercion) returned `NaN` and template-literal / `String()` (string coercion) returned `[object Object]` for class instances. The runtime side already handles `@@toPrimitive`: `js_to_primitive` (consulted by `js_number_coerce` and `js_jsvalue_to_string`) resolves `instance[Symbol.toPrimitive]` via `js_object_get_symbol_property`, which maps the well-known symbol to the synthetic `@@toPrimitive` vtable key (`well_known_symbol_method_key`) and binds it to the instance. The only gap was lowering dropping the method. Register `[Symbol.toPrimitive]` instance methods under `@@toPrimitive`, mirroring the existing `@@iterator` / `@@asyncIterator` path. Object literals already worked (own symbol property); this closes the class-instance case. Added a class-instance case to test_gap_symbols.ts; output is byte-identical to `node --experimental-strip-types`.
…age on main) The import block at buffer/mod.rs:40 landed unformatted via #2271, which fails the required `cargo fmt --all -- --check` lint on every PR. Apply the formatting rustfmt emits so this branch's lint check goes green.
proggeramlug
force-pushed
the
worktree-fix-2374-toprimitive
branch
from
May 29, 2026 06:19
cd80d78 to
2c6507d
Compare
The util/types isFloat32Array/isInt8Array/… predicates were added to the API manifest without regenerating docs, leaving docs/ at 1491 entries vs the manifest's 1496 — failing the required api-docs-drift check. Run ./scripts/regen_api_docs.sh to resync. Unrelated to the toPrimitive fix but required to land this branch (pre-existing drift on main).
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
Fixes #2374.
[Symbol.toPrimitive]was not invoked by unary+(numeric coercion) or template-literal /String()(string coercion) on class instances — they returnedNaNand[object Object]respectively.Root cause
[Symbol.toPrimitive]defined as a computed method on a class was dropped entirely during HIR lowering. Incrates/perry-hir/src/lower_decl/class_decl.rs, the well-known-symbol method handler explicitly handlediterator/asyncIterator/hasInstance/toStringTag/dispose/asyncDisposeand fell through to acontinue(silent drop, with a "not yet implemented" comment) fortoPrimitive. The method was therefore never registered in the class vtable.The runtime side already supports
@@toPrimitive:js_to_primitive— consulted byjs_number_coerce(unary+) andjs_jsvalue_to_string(template /String()) — resolvesinstance[Symbol.toPrimitive]throughjs_object_get_symbol_property, which maps the well-known symbol to the synthetic@@toPrimitivevtable key viawell_known_symbol_method_keyand binds it to the instance. So the only gap was the lowering dropping the method. Object literals already worked because their method is stored as an own symbol property.Fix
Register
[Symbol.toPrimitive]instance methods under@@toPrimitive, mirroring the existing@@iterator/@@asyncIteratorpath. One additionalelse ifbranch in class lowering.Verification
A class-instance case was added to
test-files/test_gap_symbols.ts; the full file output is byte-identical tonode --experimental-strip-types.Scope
Targeted at the issue (unary
+and string coercion viaSymbol.toPrimitiveon class instances). Two unrelated, pre-existingToPrimitivegaps observed nearby are intentionally left out of this PR: binary+uses a"number"hint where spec says"default", andvalueOf-only objects don't fall back tovalueOffor numeric coercion (returnsNaN).