You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
codegen: scalar replacement drops runtime-installed own properties — class X extends EventEmitter { a = 1 } reads typeof x.emit as undefined #6343
Escape analysis scalar-replaces a class instance that "never escapes", but the promotion is blind to own properties installed at runtime by the native base's subclass-init. Reading a declared field alongside a native method makes the method read undefined.
Confirmed on clean origin/main. No override, no indirection — a directextends EventEmitter with one field is enough.
Repro
import{EventEmitter}from"node:events";classXextendsEventEmitter{a=1;// <- a declared field is load-bearing}constx=newX();console.log("field:",x.a,"typeof emit:",typeofx.emit,"typeof on:",typeofx.on);
output
node 26
field: 1 typeof emit: function typeof on: function
perry main
field: 1 typeof emit: undefined typeof on: undefined
Drop the field (class X extends EventEmitter {}) and the methods reappear — the instance then escapes / isn't promoted.
Mechanism
EventEmitter (and the node:stream classes) install their method surface as own properties on the instance at subclass-init time (install_methods_on_existing_object -> js_object_set_field_by_name(obj, "emit", <native closure>)), rather than on a prototype.
When the instance is proven non-escaping, scalar replacement promotes its declared fields to scalars — and a runtime-installed own property has no declared slot, so it simply isn't in the promoted set. The subsequent x.emit read resolves against the scalarized object and finds nothing.
The escape/scalar-replacement decision must treat "this class's chain reaches a native base that installs an instance surface" as a disqualifier (or the promoted set must include runtime-installed own keys). Note PR #6324's guard is precise rather than a blanket disable — follow that shape; do not simply switch scalar replacement off, which would give back a real perf win.
The scripts/run_issue_945_scalar_method_ir_guard.sh IR guard and test-files/test_issue_945_scalar_method_guards.ts are the existing harness for this analysis.
Activity
fix(codegen): don't scalar-replace an instance whose chain reaches an…