Truly independent web browser
LibJS: Watch the properties ArraySpeciesCreate reads for plain arrays
Andreas Kling00961b465604
diff --git a/Libraries/LibJS/Rust/src/runtime/array_prototype.rs b/Libraries/LibJS/Rust/src/runtime/array_prototype.rsindex 45bf6002834..8481bf1342e 100644--- a/Libraries/LibJS/Rust/src/runtime/array_prototype.rs+++ b/Libraries/LibJS/Rust/src/runtime/array_prototype.rs@@ -32,6 +32,7 @@ use crate::runtime::primitive_string::PrimitiveString;use crate::runtime::property_attributes::{Attribute, DEFAULT_ATTRIBUTES, PropertyAttributes};use crate::runtime::property_key::PropertyKey;use crate::runtime::realm::Realm;+use crate::runtime::realm_watchpoints;use crate::runtime::value::{PreferredType, append_number_to_string, is_strictly_equal, same_value_zero};use crate::runtime::value_conversions::MAX_ARRAY_LIKE_INDEX;use crate::utf16::{Utf16StringBuilder, Utf16View};@@ -1799,6 +1800,17 @@ impl ArrayPrototype {pub(crate) fn array_species_create(vm: &Vm, original_array: &Object, length: u64) -> ThrowCompletionOr<Gc<Object>> {let realm = vm.current_realm().expect("a builtin runs in a realm");+ // OPTIMIZATION: For an array of this realm without an own constructor, the steps below Get %Array% as the+ // constructor and as its @@species, as long as the realm watches that they are, and Construct(%Array%,+ // « length ») is ArrayCreate(length).+ if original_array.is::<Array>()+ && original_array.shape().prototype() == Some(realm.array_prototype())+ && original_array.shape().lookup(&vm.names.constructor).is_none()+ && realm_watchpoints::array_species_is_unchanged(vm, realm)+ {+ return Ok(Array::create(vm, realm, length, None)?.upcast());+ }+let is_array = Value::from_object(original_array.as_gc()).is_array(vm)?;if !is_array {diff --git a/Libraries/LibJS/Rust/src/runtime/realm.rs b/Libraries/LibJS/Rust/src/runtime/realm.rsindex cdaef99e99d..3405a587c96 100644--- a/Libraries/LibJS/Rust/src/runtime/realm.rs+++ b/Libraries/LibJS/Rust/src/runtime/realm.rs@@ -37,6 +37,8 @@ pub struct RealmStorage {/// Whether %RegExp.prototype% and %RegExp% have the properties the fast paths of RegExp skip reading as the realm/// made them (see `runtime::realm_watchpoints`).pub(crate) regexp_primordial_properties: WatchedCondition,+ /// Whether ArraySpeciesCreate makes plain arrays (see `runtime::realm_watchpoints`).+ pub(crate) array_species: WatchedCondition,}define_cell!(Realm, Other);@@ -51,6 +53,7 @@ unsafe impl Trace for Realm {self.host_defined.trace(visitor);self.storage.embedder_data.trace(visitor);self.storage.regexp_primordial_properties.trace(visitor);+ self.storage.array_species.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 6b1deac62e6..1957496deca 100644--- a/Libraries/LibJS/Rust/src/runtime/realm_watchpoints.rs+++ b/Libraries/LibJS/Rust/src/runtime/realm_watchpoints.rs@@ -17,6 +17,9 @@//! paths of RegExp skip reading as the realm made them (see `RegExpPrototype::primordial_properties_are_unchanged()`).//! It depends on the sets that fire when either object changes, and when the values of those properties change. The//! realm starts watching them when a fast path first asks, since it creates the RegExp intrinsics on demand.+//! - `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.//!//! 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@@ -213,3 +216,35 @@ fn watch_regexp_primordial_properties(vm: &Vm, realm: Gc<Realm>) -> Option<Gc<Wa.collect();watch_intrinsics(vm, &[regexp_prototype, regexp_constructor], &slots)}++/// Whether ArraySpeciesCreate makes a plain array for an array of the realm that has no own constructor:+/// %Array.prototype%.constructor is %Array%, whose @@species is the intrinsic getter.+pub fn array_species_is_unchanged(vm: &Vm, realm: Gc<Realm>) -> bool {+ realm.storage.array_species.holds(vm, realm, watch_array_species)+}++/// The set that stays valid while ArraySpeciesCreate makes a plain array for an array of the realm that has no own+/// constructor, if it does.+fn watch_array_species(vm: &Vm, realm: Gc<Realm>) -> Option<Gc<WatchpointSet>> {+ let array_prototype = realm.array_prototype();+ let array_constructor = realm.intrinsics().array_constructor(vm);+ let constructor = array_prototype+ .shape()+ .lookup(&vm.names.constructor)+ .map(|metadata| array_prototype.get_direct(metadata.offset));+ if constructor.is_none_or(|constructor| {+ !constructor.is_object() || constructor.as_object() != array_constructor.upcast::<Object>()+ }) || !array_constructor.has_intrinsic_symbol_species_getter(vm)+ {+ return None;+ }+ let array_constructor: Gc<Object> = array_constructor.upcast();+ watch_intrinsics(+ vm,+ &[array_prototype, array_constructor],+ &[+ (array_prototype, vm.names.constructor.clone()),+ (array_constructor, PropertyKey::from(vm.well_known_symbols().species)),+ ],+ )+}diff --git a/Tests/LibJS/Runtime/builtins/Array/Array.species-changes.js b/Tests/LibJS/Runtime/builtins/Array/Array.species-changes.jsnew file mode 100644index 00000000000..61586c97380--- /dev/null+++ b/Tests/LibJS/Runtime/builtins/Array/Array.species-changes.js@@ -0,0 +1,72 @@+// ArraySpeciesCreate skips reading constructor and @@species for arrays of the realm while they are unchanged. These+// tests change them after methods that create arrays ran.++class Derived extends Array {}++function createArrays() {+ const array = [1, 2, 3];+ for (let i = 0; i < 10; ++i) {+ expect(array.map(x => x)).toEqual([1, 2, 3]);+ expect(array.filter(() => true)).toEqual([1, 2, 3]);+ expect(array.slice(1)).toEqual([2, 3]);+ expect(array.concat([4])).toEqual([1, 2, 3, 4]);+ expect(array.splice(0, 0)).toEqual([]);+ }+ return array;+}++function expectDerived(array) {+ expect(array.map(x => x)).toBeInstanceOf(Derived);+ expect(array.filter(() => true)).toBeInstanceOf(Derived);+ expect(array.slice(1)).toBeInstanceOf(Derived);+ expect(array.concat([4])).toBeInstanceOf(Derived);+ expect(array.splice(0, 0)).toBeInstanceOf(Derived);+}++test("a replaced Array.prototype.constructor is used", () => {+ const array = createArrays();+ const original = Array.prototype.constructor;+ Array.prototype.constructor = Derived;+ try {+ expectDerived(array);+ } finally {+ Array.prototype.constructor = original;+ }+ expect(array.slice(1)).not.toBeInstanceOf(Derived);+});++test("a replaced Array[@@species] getter is used", () => {+ const array = createArrays();+ const original = Object.getOwnPropertyDescriptor(Array, Symbol.species);+ Object.defineProperty(Array, Symbol.species, { get: () => Derived, configurable: true });+ try {+ expectDerived(array);+ } finally {+ Object.defineProperty(Array, Symbol.species, original);+ }+ expect(array.slice(1)).not.toBeInstanceOf(Derived);+});++test("an own constructor of the array is used", () => {+ const array = createArrays();+ array.constructor = Derived;+ expectDerived(array);+});++test("a replaced Array.prototype.constructor is used after unrelated properties changed", () => {+ createArrays();+ for (const object of [Array.prototype, Array, Object.prototype]) {+ object.unrelated = 1;+ createArrays();+ delete object.unrelated;+ createArrays();+ }+ const array = createArrays();+ const original = Array.prototype.constructor;+ Array.prototype.constructor = Derived;+ try {+ expectDerived(array);+ } finally {+ Array.prototype.constructor = original;+ }+});