Registry Mirror
Skip to content

Conversation

@proggeramlug

Copy link
Copy Markdown
Contributor

Summary

Fixes #2374. [Symbol.toPrimitive] was not invoked by unary + (numeric coercion) or template-literal / String() (string coercion) on class instances — they returned NaN and [object Object] respectively.

Root cause

[Symbol.toPrimitive] defined as a computed method on a class was dropped entirely during HIR lowering. In crates/perry-hir/src/lower_decl/class_decl.rs, the well-known-symbol method handler explicitly handled iterator / asyncIterator / hasInstance / toStringTag / dispose / asyncDispose and fell through to a continue (silent drop, with a "not yet implemented" comment) for toPrimitive. The method was therefore never registered in the class vtable.

The runtime side already supports @@toPrimitive: js_to_primitive — consulted by js_number_coerce (unary +) and js_jsvalue_to_string (template / String()) — resolves instance[Symbol.toPrimitive] through js_object_get_symbol_property, which maps the well-known symbol to the synthetic @@toPrimitive vtable key via well_known_symbol_method_key and 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 / @@asyncIterator path. One additional else if branch in class lowering.

Verification

class Temperature {
  celsius: number;
  constructor(c: number) { this.celsius = c; }
  [Symbol.toPrimitive](hint: string) {
    if (hint === "number") return this.celsius;
    if (hint === "string") return `${this.celsius}°C`;
    return this.celsius;
  }
}
const t = new Temperature(42);
console.log(+t);        // 42   (was NaN)
console.log(`${t}`);    // 42°C (was [object Object])
console.log(String(t)); // 42°C

A class-instance case was added to test-files/test_gap_symbols.ts; the full file output is byte-identical to node --experimental-strip-types.

Scope

Targeted at the issue (unary + and string coercion via Symbol.toPrimitive on class instances). Two unrelated, pre-existing ToPrimitive gaps observed nearby are intentionally left out of this PR: binary + uses a "number" hint where spec says "default", and valueOf-only objects don't fall back to valueOf for numeric coercion (returns NaN).

Ralph Küpper 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
proggeramlug force-pushed the worktree-fix-2374-toprimitive branch from cd80d78 to 2c6507d Compare May 29, 2026 06:19
proggeramlug and others added 3 commits May 29, 2026 09:02
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).
@proggeramlug
proggeramlug merged commit 1b1f960 into main May 29, 2026
11 checks passed
@proggeramlug
proggeramlug deleted the worktree-fix-2374-toprimitive branch May 29, 2026 07:50
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.

Symbol.toPrimitive not invoked by + / template-literal coercion on class instances

1 participant