-
-
Notifications
You must be signed in to change notification settings - Fork 163
fix(runtime): reflect ClassBody accessors on per-evaluation class prototypes #11113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| fix(runtime): a capture-carrying class's prototype now reflects its ClassBody accessors (part of #11043) | ||
|
|
||
| A class whose members close over a local of the enclosing function lowers to | ||
| `ClassExprFresh`, and each evaluation materializes its own prototype object | ||
| (`class_evaluation_prototype_value`). Like a declared class's prototype, that | ||
| object carries physical `constructor` + method keys while `get`/`set` | ||
| accessors live only in the template vtable — but it was never recognized by | ||
| `class_id_for_decl_prototype_object`, the lookup every reflection site uses to | ||
| surface those accessors. So `Object.getOwnPropertyNames(C.prototype)` omitted | ||
| them, and `Object.defineProperties(C.prototype, { x: { enumerable: true } })` | ||
| (whatwg-url's generated `URL` wrapper) installed a read-only `undefined` data | ||
| property over `get x`/`set x`. mongodb 7.5.0 compiled from real source then | ||
| threw `Cannot assign to read only property 'pathname'` from | ||
| `mongodb-connection-string-url`'s `ConnectionString` constructor. | ||
|
|
||
| `class_id_for_decl_prototype_object` now falls back to | ||
| `class_evaluation_prototype_class_id`, which recognizes a per-evaluation | ||
| prototype structurally (own `constructor` is a heap class object of the same | ||
| template id whose hidden evaluation-prototype slot points back at it) — no | ||
| side table to root or rekey, no allocation, and gated on an `AtomicBool` so | ||
| programs without such classes pay one relaxed load on the miss path. | ||
|
|
||
| Gap test: `test_gap_11043_class_eval_proto_accessors`. The next mongodb blocker | ||
| is #11111 (`net.Socket#write` returns `undefined`); #11112 tracks the `in` | ||
| operator on the same class shape. |
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
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
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
138 changes: 138 additions & 0 deletions
138
test-files/test_gap_11043_class_eval_proto_accessors.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| // #11043: a capture-carrying class (one whose members close over a local of | ||
| // the enclosing function) gets a distinct prototype object per evaluation. | ||
| // Its ClassBody accessors must be own properties of that prototype, exactly | ||
| // like a top-level class's. whatwg-url's generated `URL` wrapper is the shape | ||
| // that broke mongodb: the class is declared inside `install(globalObject)`, | ||
| // its accessors close over `globalObject`, and the module then runs | ||
| // `Object.defineProperties(URL.prototype, { pathname: { enumerable: true } })`. | ||
| // A generic descriptor must only flip `enumerable` — before the fix it | ||
| // replaced the accessor with a read-only `undefined` data property and | ||
| // `url.pathname = "/"` threw "Cannot assign to read only property". | ||
|
|
||
| function describe(label: string, proto: object, key: string): void { | ||
| const d = Object.getOwnPropertyDescriptor(proto, key); | ||
| if (d === undefined) { | ||
| console.log(label, key, "missing"); | ||
| return; | ||
| } | ||
| console.log( | ||
| label, | ||
| key, | ||
| "get:" + typeof d.get, | ||
| "set:" + typeof d.set, | ||
| "enumerable:" + d.enumerable, | ||
| "configurable:" + d.configurable, | ||
| "value" in d ? "has-value" : "no-value", | ||
| ); | ||
| } | ||
|
|
||
| // 1. The whatwg-url shape. | ||
| const implSymbol = Symbol("impl"); | ||
| function install(globalObject: any): void { | ||
| class URL { | ||
| constructor(href: string) { | ||
| const wrapper = Object.create(new.target.prototype); | ||
| Object.defineProperty(wrapper, implSymbol, { | ||
| value: { href, pathname: "" }, | ||
| configurable: true, | ||
| }); | ||
| return wrapper; | ||
| } | ||
| get href(): string { | ||
| const esValue = this !== null && this !== undefined ? this : globalObject; | ||
| return esValue[implSymbol].href; | ||
| } | ||
| get pathname(): string { | ||
| const esValue = this !== null && this !== undefined ? this : globalObject; | ||
| return esValue[implSymbol].pathname; | ||
| } | ||
| set pathname(v: string) { | ||
| const esValue = this !== null && this !== undefined ? this : globalObject; | ||
| esValue[implSymbol].pathname = String(v); | ||
| } | ||
| toJSON(): string { | ||
| return (this as any)[implSymbol].href; | ||
| } | ||
| } | ||
| console.log("own before", Object.getOwnPropertyNames(URL.prototype).join(",")); | ||
| describe("before", URL.prototype, "pathname"); | ||
| Object.defineProperties(URL.prototype, { | ||
| toJSON: { enumerable: true }, | ||
| href: { enumerable: true }, | ||
| pathname: { enumerable: true }, | ||
| [Symbol.toStringTag]: { value: "URL", configurable: true }, | ||
| }); | ||
| describe("after", URL.prototype, "pathname"); | ||
| describe("after", URL.prototype, "href"); | ||
| describe("after", URL.prototype, "toJSON"); | ||
| console.log("keys after", Object.keys(URL.prototype).join(",")); | ||
| Object.defineProperty(globalObject, "URL", { configurable: true, writable: true, value: URL }); | ||
| } | ||
| const sharedGlobalObject: any = {}; | ||
| install(sharedGlobalObject); | ||
| const WURL = sharedGlobalObject.URL; | ||
|
|
||
| const u = new WURL("mongodb://h/x"); | ||
| u.pathname = "/db"; | ||
| console.log("set on instance", u.pathname, Object.prototype.toString.call(u)); | ||
|
|
||
| // 2. Reflection on a plain capturing accessor, per evaluation. | ||
| function makeCounter(start: number) { | ||
| let n = start; | ||
| class Counter { | ||
| get value(): number { | ||
| return n; | ||
| } | ||
| set value(v: number) { | ||
| n = v; | ||
| } | ||
| get readOnly(): number { | ||
| return n * 2; | ||
| } | ||
| bump(): number { | ||
| return ++n; | ||
| } | ||
| } | ||
| return Counter; | ||
| } | ||
| const A = makeCounter(1); | ||
| const B = makeCounter(10); | ||
| console.log("distinct prototypes", A.prototype !== B.prototype); | ||
| describe("A", A.prototype, "value"); | ||
| describe("A", A.prototype, "readOnly"); | ||
| describe("A", A.prototype, "bump"); | ||
| console.log( | ||
| "hasOwn", | ||
| Object.prototype.hasOwnProperty.call(A.prototype, "value"), | ||
| Object.prototype.hasOwnProperty.call(B.prototype, "readOnly"), | ||
| Object.prototype.hasOwnProperty.call(new A(), "value"), | ||
| ); | ||
| console.log("names A", Object.getOwnPropertyNames(A.prototype).join(",")); | ||
| console.log("names B", Object.getOwnPropertyNames(B.prototype).join(",")); | ||
| Object.defineProperty(B.prototype, "value", { enumerable: true }); | ||
| describe("B redefined", B.prototype, "value"); | ||
| const b = new B(); | ||
| b.value = 42; | ||
| console.log("B setter after redefine", b.value, b.bump(), b.readOnly); | ||
| const a = new A(); | ||
| a.value = 7; | ||
| console.log("A untouched", a.value, a.bump()); | ||
|
|
||
| // A reflected getter still reads through the receiver. | ||
| const getter = Object.getOwnPropertyDescriptor(A.prototype, "readOnly")!.get!; | ||
| console.log("reflected getter", getter.call(a)); | ||
|
|
||
| // 3. Getter-only accessor stays getter-only after a generic redefine. | ||
| function makeFrozen(tag: string) { | ||
| class Frozen { | ||
| get tag(): string { | ||
| return tag; | ||
| } | ||
| } | ||
| Object.defineProperties(Frozen.prototype, { tag: { enumerable: true } }); | ||
| return Frozen; | ||
| } | ||
| const F = makeFrozen("t1"); | ||
| describe("F", F.prototype, "tag"); | ||
| const f: any = new F(); | ||
| console.log("F reads", f.tag, Object.keys(F.prototype).join(",")); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 11753
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 30457
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 31412
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 42343
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 42393
🏁 Script executed:
Repository: PerryTS/perry
Length of output: 29721
Preserve evaluation-prototype recognition after
constructorchanges.class_evaluation_prototype_class_idrequires the prototype's ownconstructorto be a class object with a matching back-edge. AssigningC.prototype.constructor = nulltherefore makes the lookup returnNone.ClassBody accessors remain in the class registry. The descriptor and generic define-property paths use this lookup before they update those accessors. When recognition fails, a descriptor-only
Object.definePropertiescall can follow the ordinary absent-property path and create a read-only data property instead.Record a role-specific evaluation-prototype marker in GC-traced
ObjectMetastate, and use it during lookup. The existingClassEvaluationlink flag is not sufficient because that link kind is also used for evaluated instances.🤖 Prompt for AI Agents