Skip to content

Commit a7dd328

Browse files
proggeramlugRalphclaude
authored
fix(runtime): per-evaluation inherited static wins over last-wins registry props (#6552) (#6556)
* fix(runtime): per-evaluation inherited static wins over last-wins registry props (#6552) A subclass of a class-EXPRESSION value evaluated more than once (`function make(a){return class{static ast=a}}`, then `class Number$ extends make(x) {}` / `class Widget$ extends make(y) {}`) records THIS evaluation's parent object as its static prototype (`class_prototype_object`, #1788). But #6443's static-DATA-field inheritance walk reads the parent's `CLASS_DYNAMIC_PROPS`, which are keyed by the class-expression TEMPLATE id — shared, last-wins across every evaluation — and it ran BEFORE the per-evaluation prototype-object walk. So every renamed effect-Schema class read via `import * as M` (`M.Number.ast`/`M.Widget.ast`) collapsed to the LAST `make(...)`'s static `ast` (the direct export's `DirectKeyword`), re-breaking effect Schema decode (test_gap_renamed_class_export_namespace, the #1758/#1812 guard). Interleave the two walks: at each level of the class-object proto chain, consult the class's pinned per-evaluation parent object BEFORE the parent's registry props. The pinned object is authoritative (it carries this evaluation's own edge); the registry read stays the fallback for a plain declaration parent (#6443: Auth.js `SignInError.kind`), which has no pinned object. Depth order is preserved, so a closer registry static still shadows a deeper per-evaluation one. test_gap_renamed_class_export_namespace is byte-identical to node again across renamed (global-colliding + not) and direct exports; #6443 static-field inheritance + deleted-key fallthrough, #6438 per-eval statics, effect factory static dispatch, and the rest of the class-expr/static suite (18/18) stay green. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(runtime): return a present `null` inherited static, don't fall through (#6552 review) CodeRabbit review follow-up. The interleaved per-evaluation proto-object read guarded on `!is_undefined() && !is_null()`, so a static explicitly set to `null` on one evaluation (`class NullAst$ extends make(null) {}`) was treated as "absent" and fell through to the parent's last-wins `CLASS_DYNAMIC_PROPS`, returning a SIBLING evaluation's `ast` instead of `null` (verified: perry read `{_tag:'OBJ'}` where node reads `null`). `null` is a present, authoritative value — only `undefined` means "absent here". Guard on `!is_undefined()` alone so a present `null` is returned and only a true miss continues to the registry / a higher ancestor. Extends the gap test + helper with a renamed null-static export (`M.NullAst.ast === null`), placed before the last non-null `make(...)` so `null` is a genuine discriminator; byte-identical to node. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Ralph <ralph@skelpo.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 95aed4f commit a7dd328

3 files changed

Lines changed: 66 additions & 23 deletions

File tree

‎crates/perry-runtime/src/object/field_get_set/get_field_by_name.rs‎

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,33 +1033,66 @@ pub extern "C" fn js_object_get_field_by_name(
10331033
// chain walk so the #2059 own-name synthesis below answers
10341034
// with THIS class's registered name instead of an ancestor's.
10351035
if name != "name" {
1036-
let mut cid = class_id;
1036+
// Walk the class-object proto chain for an inherited static
1037+
// DATA field. At EACH level the class's pinned
1038+
// per-evaluation parent OBJECT is consulted BEFORE the
1039+
// parent's registry props (`CLASS_DYNAMIC_PROPS`).
1040+
//
1041+
// #6552: a subclass of a class-EXPRESSION value evaluated
1042+
// more than once (`function make(a){return class{static
1043+
// ast=a}}`, then `class Number$ extends make(x) {}` /
1044+
// `class Widget$ extends make(y) {}`) records THIS
1045+
// evaluation's parent object as its static prototype
1046+
// (`class_prototype_object`, #1788), but the parent's
1047+
// `CLASS_DYNAMIC_PROPS` are keyed by the class-expression
1048+
// TEMPLATE id — shared, last-wins across every evaluation.
1049+
// Reading the registry entry for such a parent collapses
1050+
// sibling subclasses to the LAST `make(...)` (effect Schema:
1051+
// `Number$.ast`/`Widget$.ast` both read the last parent's
1052+
// `ast`). The pinned object carries this evaluation's own
1053+
// edge, so it is authoritative; the registry read remains
1054+
// the fallback for a plain declaration parent (#6443:
1055+
// Auth.js `SignInError.kind`), which has no pinned object.
1056+
let mut child = class_id;
10371057
let mut depth = 0usize;
10381058
while depth < 32 {
1039-
match get_parent_class_id(cid) {
1040-
Some(p) if p != 0 && p != cid => {
1041-
cid = p;
1042-
depth += 1;
1059+
let proto =
1060+
super::super::class_registry::class_prototype_object(child);
1061+
if !proto.is_null() {
1062+
let v = js_object_get_field_by_name(proto as *const _, key);
1063+
// Return a value present on the pinned object even
1064+
// when it is `null` — a static explicitly set to
1065+
// `null` on THIS evaluation is authoritative and
1066+
// must not fall through to the last-wins registry
1067+
// entry (a sibling evaluation's value). Only
1068+
// `undefined` means "absent here", which continues
1069+
// the walk to the parent's registry props / a higher
1070+
// ancestor.
1071+
if !v.is_undefined() {
1072+
return v;
10431073
}
1044-
_ => break,
1045-
}
1046-
if super::super::class_registry::class_is_key_deleted(cid, name) {
1047-
// A key deleted on THIS ancestor is not provided by
1048-
// it, but a higher ancestor may still define it —
1049-
// `delete Mid.foo` must let `Sub.foo` inherit
1050-
// `Base.foo`, not resolve to undefined. Skip this
1051-
// level and keep walking up (safe: `cid`/`depth`
1052-
// advance at the top of every iteration).
1053-
continue;
10541074
}
1055-
let inherited = CLASS_DYNAMIC_PROPS.with(|m| {
1056-
m.borrow()
1057-
.get(&cid)
1058-
.and_then(|props| props.get(name).copied())
1059-
});
1060-
if let Some(v) = inherited {
1061-
return JSValue::from_bits(v.to_bits());
1075+
let p = match get_parent_class_id(child) {
1076+
Some(p) if p != 0 && p != child => p,
1077+
_ => break,
1078+
};
1079+
// A key deleted on THIS ancestor is not provided by it,
1080+
// but a higher ancestor may still define it — `delete
1081+
// Mid.foo` must let `Sub.foo` inherit `Base.foo`, not
1082+
// resolve to undefined. Skip the registry read for the
1083+
// deleted level and keep walking up.
1084+
if !super::super::class_registry::class_is_key_deleted(p, name) {
1085+
let inherited = CLASS_DYNAMIC_PROPS.with(|m| {
1086+
m.borrow()
1087+
.get(&p)
1088+
.and_then(|props| props.get(name).copied())
1089+
});
1090+
if let Some(v) = inherited {
1091+
return JSValue::from_bits(v.to_bits());
1092+
}
10621093
}
1094+
child = p;
1095+
depth += 1;
10631096
}
10641097
}
10651098
if super::super::class_registry::lookup_static_method_in_chain(class_id, name)

‎test-files/_helpers/renamed_class_export.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ export function make(a: any) {
1212

1313
class Number$ extends make({ _tag: "NumberKeyword" }) {}
1414
class Widget$ extends make({ _tag: "WidgetKeyword" }) {}
15+
// A per-evaluation static explicitly set to `null` — its own value must win
16+
// over any sibling's (last-wins) registry `ast`. Declared before `DirectCls`
17+
// so the last `make(...)` to register is a NON-null value, making `null` a real
18+
// discriminator (regression guard for the #6552 fix's proto-vs-registry order).
19+
class NullAst$ extends make(null) {}
1520
export class DirectCls extends make({ _tag: "DirectKeyword" }) {}
1621

1722
// Renamed exports. `Number` deliberately collides with the JS global
1823
// `Number` — pre-fix, `M.Number` resolved to the global constructor.
19-
export { Number$ as Number, Widget$ as Widget };
24+
export { Number$ as Number, Widget$ as Widget, NullAst$ as NullAst };

‎test-files/test_gap_renamed_class_export_namespace.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ console.log("(3) M.DirectCls.ast._tag:", (M as any).DirectCls.ast?._tag);
2929

3030
// (4) the global `Number` is still itself (not shadowed by the import).
3131
console.log("(4) global Number(42):", Number("42"));
32+
33+
// (5) a renamed export whose per-evaluation static is explicitly `null` reads
34+
// back `null` — it must not fall through to a sibling evaluation's (last-wins)
35+
// `ast`. Guards the proto-object-before-registry ordering of the #6552 fix.
36+
console.log("(5) M.NullAst.ast:", (M as any).NullAst.ast);

0 commit comments

Comments
 (0)