-
-
Notifications
You must be signed in to change notification settings - Fork 161
fix(runtime): resolve computed string properties through their prototype #9818
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
proggeramlug
wants to merge
2
commits into
PerryTS:main
from
proggeramlug:fix/9815-primitive-computed-properties
Closed
Changes from all commits
Commits
Show all changes
2 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,10 @@ | ||
| Fix primitive string property reads with computed keys. Named properties now | ||
| consult `String.prototype` and preserve the original method value, so reflective | ||
| read-then-call code and method identity checks work. Inherited accessors receive | ||
| the primitive string as `this`; object and symbol keys follow `ToPropertyKey`. | ||
| Character indices and `length` keep precedence over prototype properties, and | ||
| boxed strings retain their own-property lookup before their custom prototype. | ||
|
|
||
| Cover typed and untyped receivers, short strings, borrowed methods, inherited | ||
| accessors, symbol keys, key coercion, and prototype mutation in runtime unit | ||
| tests and a Node parity fixture. Direct method-call lowering is unchanged. | ||
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
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
70 changes: 70 additions & 0 deletions
70
crates/perry-runtime/src/string/char_ops/computed_property_tests.rs
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,70 @@ | ||
| use super::*; | ||
| use crate::value::{js_dyn_index_get, js_nanbox_string, JSValue}; | ||
|
|
||
| fn string(value: &str) -> f64 { | ||
| js_nanbox_string(js_string_from_str(value) as i64) | ||
| } | ||
|
|
||
| #[test] | ||
| fn computed_string_method_is_the_prototype_function() { | ||
| let scope = crate::gc::RuntimeHandleScope::new(); | ||
| let proto = scope.root_nanbox_f64(crate::object::builtin_prototype_value("String")); | ||
| for receiver in [ | ||
| string("abcdef"), | ||
| f64::from_bits(JSValue::try_short_string(b"abc").unwrap().bits()), | ||
| ] { | ||
| let receiver = scope.root_nanbox_f64(receiver); | ||
| for name in ["charAt", "trim", "toUpperCase", "toString", "constructor"] { | ||
| let key = scope.root_nanbox_f64(string(name)); | ||
| let expected = scope.root_nanbox_f64(crate::proxy::js_reflect_get( | ||
| proto.get_nanbox_f64(), | ||
| key.get_nanbox_f64(), | ||
| proto.get_nanbox_f64(), | ||
| )); | ||
| assert_ne!( | ||
| expected.get_nanbox_f64().to_bits(), | ||
| crate::value::TAG_UNDEFINED, | ||
| "prototype has {name}" | ||
| ); | ||
| for get in [js_string_index_get_boxed, js_dyn_index_get] { | ||
| let actual = get(receiver.get_nanbox_f64(), key.get_nanbox_f64()); | ||
| assert_eq!( | ||
| actual.to_bits(), | ||
| expected.get_nanbox_f64().to_bits(), | ||
| "{name} must preserve method identity" | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn computed_string_own_properties_keep_index_semantics() { | ||
| let scope = crate::gc::RuntimeHandleScope::new(); | ||
| let receiver = scope.root_nanbox_f64(string("abcdef")); | ||
| for get in [js_string_index_get_boxed, js_dyn_index_get] { | ||
| assert_eq!(get(receiver.get_nanbox_f64(), string("length")), 6.0); | ||
| for key in [1.0, string("1")] { | ||
| let value = get(receiver.get_nanbox_f64(), key); | ||
| assert_eq!( | ||
| crate::builtins::jsvalue_string_content(value).as_deref(), | ||
| Some("b") | ||
| ); | ||
| } | ||
| for key in [ | ||
| -1.0, | ||
| 1.5, | ||
| 6.0, | ||
| f64::NAN, | ||
| f64::INFINITY, | ||
| string("01"), | ||
| string("1.0"), | ||
| string("missing"), | ||
| ] { | ||
| assert_eq!( | ||
| get(receiver.get_nanbox_f64(), key).to_bits(), | ||
| crate::value::TAG_UNDEFINED | ||
| ); | ||
| } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Dynamic value reads must return the original prototype method (#9815). | ||
| function typed(s: string, key: any): any { return s[key]; } | ||
| function unknown(s: any, key: any): any { return s[key]; } | ||
| function named(s: any, key: string): any { return s[key]; } | ||
|
|
||
| for (const s of ["abcde", " abcdef ", "a" + "bc"]) { | ||
| for (const key of ["charAt", "trim", "toUpperCase", "toString", "constructor"]) { | ||
| const expected = String.prototype[key]; | ||
| console.log(key, typeof typed(s, key), typed(s, key) === expected, | ||
| unknown(s, key) === expected, named(s, key) === expected); | ||
| } | ||
| const charAt = typed(s, "charAt"); | ||
| const trim = unknown(s, "trim"); | ||
| const upper = named(s, "toUpperCase"); | ||
| console.log("borrowed", charAt.call(s, 1), trim.apply(s, []), upper.bind(s)()); | ||
| console.log("length", typed(s, "length"), unknown(s, "length"), named(s, "length")); | ||
| for (const key of [0, -0, 1, "1", -1, 1.5, 99, NaN, Infinity, "01", "1.0", "missing"]) { | ||
| console.log("index", String(key), typed(s, key), unknown(s, key)); | ||
| } | ||
| } | ||
|
|
||
| const proto: any = String.prototype; | ||
| const sym = Symbol("computed"); | ||
| proto[sym] = 73; | ||
| proto.custom9815 = function () { "use strict"; return this; }; | ||
| proto["01"] = 81; | ||
| proto["99"] = 99; | ||
| proto["1"] = "shadowed"; | ||
| Object.defineProperty(proto, "get9815", { | ||
| configurable: true, | ||
| get: function () { "use strict"; return typeof this + ":" + this; } | ||
| }); | ||
| Object.defineProperty(Object.prototype, "inherited9815", { | ||
| configurable: true, | ||
| get: function () { "use strict"; return typeof this + ":" + this; } | ||
| }); | ||
|
|
||
| for (const key of ["custom9815", "get9815", "inherited9815", "01", "99", "1", sym]) { | ||
| const value = typed("abc", key); | ||
| console.log("custom", typeof value, value === unknown("abc", key)); | ||
| if (typeof value === "function") console.log("receiver", value.call("xyz")); | ||
| else console.log("value", value); | ||
| } | ||
| let coercions = 0; | ||
| const indexKey = { toString() { coercions++; return "1"; } }; | ||
| const nameKey = { [Symbol.toPrimitive](hint) { coercions++; return "get9815"; } }; | ||
| const symbolKey = { [Symbol.toPrimitive](hint) { coercions++; return sym; } }; | ||
| console.log("coercion", typed("abc", indexKey), unknown("abc", nameKey), typed("abc", symbolKey), coercions); | ||
| console.log("bigint index", typed("abc", 1n)); | ||
| console.log("symbol identity", typed("abc", Symbol.iterator) === String.prototype[Symbol.iterator]); | ||
| const boxed: any = new String("abc"); | ||
| Object.setPrototypeOf(boxed, { custom9815: 42, "1": "shadowed" }); | ||
| console.log("boxed", unknown(boxed, "custom9815"), unknown(boxed, "1"), unknown(boxed, "length")); | ||
| const savedConstructor = proto.constructor; | ||
| proto.constructor = 91; | ||
| console.log("constructor override", typed("abcdef", "constructor"), named("abcdef", "constructor")); | ||
| proto.constructor = savedConstructor; | ||
| const callKey = "charAt"; | ||
| console.log("direct call", "abc"[callKey](1), "abc"["charAt"](1)); | ||
| delete proto[sym]; | ||
| delete proto.custom9815; | ||
| delete proto["01"]; | ||
| delete proto["99"]; | ||
| delete proto["1"]; | ||
| delete proto.get9815; | ||
| delete Object.prototype.inherited9815; | ||
| console.log("deleted", typed("abc", "custom9815"), typed("abc", "99")); |
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Keep the changelog fragment release-facing.
Remove the test inventory in Lines 8-10. It describes validation work, not shipped behavior. Keep one coherent entry that describes the user-visible computed-property fix.
Based on learnings, changelog fragments must describe final shipped behavior as one coherent release-note entry.
🤖 Prompt for AI Agents
Source: Learnings