Truly independent web browser
LibJS: Watch that array prototypes have no @@isConcatSpreadable
Andreas Kling32de7270e316
diff --git a/Libraries/LibJS/Rust/src/runtime/array.rs b/Libraries/LibJS/Rust/src/runtime/array.rsindex 0439306c1b9..96670983151 100644--- a/Libraries/LibJS/Rust/src/runtime/array.rs+++ b/Libraries/LibJS/Rust/src/runtime/array.rs@@ -272,6 +272,11 @@ impl Array {Ok(true)}+ /// The realm whose intrinsics default_prototype_chain_intact() compares the prototype chain with.+ pub fn realm(&self) -> Gc<Realm> {+ self.realm.get()+ }+pub fn default_prototype_chain_intact(&self) -> bool {let realm = self.realm.get();let Some(array_prototype) = self.shape().prototype() else {diff --git a/Libraries/LibJS/Rust/src/runtime/array_prototype.rs b/Libraries/LibJS/Rust/src/runtime/array_prototype.rsindex 8481bf1342e..3f47aaa0707 100644--- a/Libraries/LibJS/Rust/src/runtime/array_prototype.rs+++ b/Libraries/LibJS/Rust/src/runtime/array_prototype.rs@@ -267,7 +267,7 @@ impl ArrayPrototype {// The result must not be one of the inputs, since we copy input storage into it.if let Some(array) = this_object.downcast::<Array>()&& can_use_packed_or_empty_array_fast_path(&array)- && !this_object.has_property(vm, &concat_spreadable_key)?+ && !has_is_concat_spreadable(vm, &array, &concat_spreadable_key)?&& let Some(result_array) = fast_array_species_result(new_array)&& result_array != array&& result_array.indexed_array_like_size() == 0@@ -292,7 +292,7 @@ impl ArrayPrototype {all_fast_path_arguments = false;break;};- if argument_array.has_property(vm, &concat_spreadable_key)? {+ if has_is_concat_spreadable(vm, &argument_array, &concat_spreadable_key)? {all_fast_path_arguments = false;break;}@@ -1876,6 +1876,20 @@ fn can_use_packed_array_fast_path(array: &Array) -> bool {array.is_simple_packed_array() && array.default_prototype_chain_intact()}+/// Whether `array`, which the fast paths of concat take, has an own or inherited @@isConcatSpreadable.+fn has_is_concat_spreadable(vm: &Vm, array: &Array, concat_spreadable_key: &PropertyKey) -> ThrowCompletionOr<bool> {+ // OPTIMIZATION: An array whose prototype is %Array.prototype% inherits none while the realm watches its prototypes+ // have none, so only its own properties need asking.+ let realm = array.realm();+ if array.prototype() == Some(realm.array_prototype())+ && realm_watchpoints::array_prototypes_have_no_is_concat_spreadable(vm, realm)+ {+ let shape = array.shape();+ return Ok(shape.property_count() != 0 && shape.lookup(concat_spreadable_key).is_some());+ }+ array.has_property(vm, concat_spreadable_key)+}+fn can_use_packed_or_empty_array_fast_path(array: &Array) -> bool {// An array that never had any elements has no indexed storage at all. Like a packed array, it has no holes that// could read through to the prototype chain.diff --git a/Libraries/LibJS/Rust/src/runtime/realm.rs b/Libraries/LibJS/Rust/src/runtime/realm.rsindex 72e3579ddbc..ea0cd836a17 100644--- a/Libraries/LibJS/Rust/src/runtime/realm.rs+++ b/Libraries/LibJS/Rust/src/runtime/realm.rs@@ -41,6 +41,9 @@ pub struct RealmStorage {pub(crate) array_species: WatchedCondition,/// Whether %Array.prototype%[@@iterator] is %Array.prototype.values% (see `runtime::realm_watchpoints`).pub(crate) array_iterator_method: WatchedCondition,+ /// Whether %Array.prototype% and %Object.prototype% have no @@isConcatSpreadable (see+ /// `runtime::realm_watchpoints`).+ pub(crate) array_prototypes_without_is_concat_spreadable: WatchedCondition,}define_cell!(Realm, Other);@@ -57,6 +60,9 @@ unsafe impl Trace for Realm {self.storage.regexp_primordial_properties.trace(visitor);self.storage.array_species.trace(visitor);self.storage.array_iterator_method.trace(visitor);+ self.storage+ .array_prototypes_without_is_concat_spreadable+ .trace(visitor);self.array_iterator_next_watchpoint_set.trace(visitor);self.array_iterator_return_watchpoint_set.trace(visitor);}diff --git a/Libraries/LibJS/Rust/src/runtime/realm_watchpoints.rs b/Libraries/LibJS/Rust/src/runtime/realm_watchpoints.rsindex d0b3a12120a..a381cb914d9 100644--- a/Libraries/LibJS/Rust/src/runtime/realm_watchpoints.rs+++ b/Libraries/LibJS/Rust/src/runtime/realm_watchpoints.rs@@ -23,6 +23,9 @@//! - `array_species`: %Array.prototype%.constructor is %Array%, whose @@species is the intrinsic getter, so that//! ArraySpeciesCreate makes a plain array for an array of the realm that has no own constructor. It depends on the//! sets that fire when either object changes, and when the values of those properties change.+//! - `array_prototypes_without_is_concat_spreadable`: %Array.prototype% and %Object.prototype% have no+//! @@isConcatSpreadable, and %Array.prototype% inherits from %Object.prototype%, so that IsConcatSpreadable of an+//! array of the realm without an own one is true. It depends on the sets that fire when either object changes.//!//! A change of an intrinsic fires the sets that depend on it, also one that leaves what they stand for in place, such//! as a new property of %Object.prototype%. The next query of a fired set checks whether what it stands for still@@ -292,3 +295,28 @@ pub fn array_is_iterated_unobservably(vm: &Vm, realm: Gc<Realm>, array: &Object).lookup(&PropertyKey::from(vm.well_known_symbols().iterator)).is_none()}++/// Whether %Array.prototype% and %Object.prototype% of the realm have no @@isConcatSpreadable.+pub fn array_prototypes_have_no_is_concat_spreadable(vm: &Vm, realm: Gc<Realm>) -> bool {+ realm.storage.array_prototypes_without_is_concat_spreadable.holds(+ vm,+ realm,+ watch_array_prototypes_without_is_concat_spreadable,+ )+}++/// The set that stays valid while %Array.prototype% and %Object.prototype% of the realm have no @@isConcatSpreadable,+/// if they have none.+fn watch_array_prototypes_without_is_concat_spreadable(vm: &Vm, realm: Gc<Realm>) -> Option<Gc<WatchpointSet>> {+ let array_prototype = realm.array_prototype();+ let object_prototype = realm.object_prototype();+ let key = PropertyKey::from(vm.well_known_symbols().is_concat_spreadable);+ // NB: %Object.prototype% has an immutable prototype.+ if array_prototype.shape().prototype() != Some(object_prototype)+ || array_prototype.shape().lookup(&key).is_some()+ || object_prototype.shape().lookup(&key).is_some()+ {+ return None;+ }+ watch_intrinsics(vm, &[array_prototype, object_prototype], &[])+}diff --git a/Tests/LibJS/Runtime/builtins/Array/Array.prototype.concat-is-concat-spreadable-changes.js b/Tests/LibJS/Runtime/builtins/Array/Array.prototype.concat-is-concat-spreadable-changes.jsnew file mode 100644index 00000000000..37cd7d47648--- /dev/null+++ b/Tests/LibJS/Runtime/builtins/Array/Array.prototype.concat-is-concat-spreadable-changes.js@@ -0,0 +1,56 @@+function runFastPath() {+ for (let i = 0; i < 10; ++i) expect([1].concat([2], 3)).toEqual([1, 2, 3]);+}++test("@@isConcatSpreadable of Array.prototype is read after the fast path ran", () => {+ runFastPath();+ Array.prototype[Symbol.isConcatSpreadable] = false;+ try {+ const result = [1].concat([2]);+ expect(result.length).toBe(2);+ expect(result[0]).toEqual([1]);+ expect(result[1]).toEqual([2]);+ } finally {+ delete Array.prototype[Symbol.isConcatSpreadable];+ }+ runFastPath();+});++test("@@isConcatSpreadable of Object.prototype is read after the fast path ran", () => {+ runFastPath();+ Object.prototype[Symbol.isConcatSpreadable] = false;+ try {+ expect([1].concat([2]).length).toBe(2);+ } finally {+ delete Object.prototype[Symbol.isConcatSpreadable];+ }+ runFastPath();+});++test("a getter for @@isConcatSpreadable is called after the fast path ran", () => {+ runFastPath();+ let calls = 0;+ Object.defineProperty(Array.prototype, Symbol.isConcatSpreadable, {+ get() {+ ++calls;+ return true;+ },+ configurable: true,+ });+ try {+ expect([1].concat([2])).toEqual([1, 2]);+ expect(calls).toBe(2);+ } finally {+ delete Array.prototype[Symbol.isConcatSpreadable];+ }+});++test("an own @@isConcatSpreadable of an array is read", () => {+ runFastPath();+ const array = [2];+ array[Symbol.isConcatSpreadable] = false;+ expect([1].concat(array)).toEqual([1, array]);+ const receiver = [1];+ receiver[Symbol.isConcatSpreadable] = false;+ expect(receiver.concat([2])).toEqual([receiver, 2]);+});