Skip to content

Truly independent web browser

LibJS: Watch the iterator method of arrays for compiled array iteration

Andreas Klingdb84b07925cd
diff --git a/Libraries/LibJS/Rust/src/bytecode/executable.rs b/Libraries/LibJS/Rust/src/bytecode/executable.rs
index e2c0e9f5df6..7af1c4059f2 100644
--- a/Libraries/LibJS/Rust/src/bytecode/executable.rs
+++ b/Libraries/LibJS/Rust/src/bytecode/executable.rs
@@ -735,8 +735,6 @@ define_static_property_lookup_cache_sites! {
IteratorValueValue,
IteratorCloseReturn,
ArrayAppendIteratorMethod,
- OpenArrayIterationOwnIteratorMethod,
- OpenArrayIterationIteratorMethod,
ObjectPrototypeToStringToStringTag,
ArrayFromIteratorMethod,
ArraySpeciesCreateConstructor,
diff --git a/Libraries/LibJS/Rust/src/jit/intrinsics.rs b/Libraries/LibJS/Rust/src/jit/intrinsics.rs
index 4a8168dd880..5afea2829b8 100644
--- a/Libraries/LibJS/Rust/src/jit/intrinsics.rs
+++ b/Libraries/LibJS/Rust/src/jit/intrinsics.rs
@@ -10,10 +10,7 @@
use ak::Utf16FlyString;
-use crate::bytecode::executable::{
- KeyedPropertyLookup, KeyedPropertyLookupCache, PropertyLookupCacheEntryType, StaticPropertyLookupCacheSite,
-};
-use crate::bytecode::property_access::get_own_property_without_side_effects;
+use crate::bytecode::executable::{KeyedPropertyLookup, KeyedPropertyLookupCache, PropertyLookupCacheEntryType};
use crate::gc::class::GcCell;
use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
@@ -221,31 +218,23 @@ pub extern "C" fn libjs_jit_open_array_iteration(vm: &Vm, iterable: u64) -> u64
if array.downcast::<Array>().is_none() {
return Value::EMPTY.0;
}
- // NB: The array inherits the iterator method from %Array.prototype%, which holds it as a data property. The cached
- // lookups of own properties run no code.
+ // NB: The array inherits the iterator method from %Array.prototype%, which holds it as a data property, unless it
+ // has an own @@iterator. Arrays without named properties have none.
let realm = vm.current_realm().expect("JIT code runs in a realm");
if !realm_watchpoints::array_iterators_have_builtin_next(vm, realm) {
return Value::EMPTY.0;
}
- let array_prototype = realm.array_prototype();
- let iterator_key = PropertyKey::from(vm.well_known_symbols().iterator);
- let cache = |site| vm.static_property_lookup_cache(site);
- if array.prototype() != Some(array_prototype)
- || get_own_property_without_side_effects(
- &array,
- &iterator_key,
- cache(StaticPropertyLookupCacheSite::OpenArrayIterationOwnIteratorMethod),
- ) != Value::EMPTY
+ if array.prototype() != Some(realm.array_prototype())
+ || !realm_watchpoints::arrays_have_builtin_iterator_method(vm, realm)
{
return Value::EMPTY.0;
}
- let values = Value::from_object(realm.array_prototype_values_function().upcast::<Object>());
- let iterator_method = get_own_property_without_side_effects(
- &array_prototype,
- &iterator_key,
- cache(StaticPropertyLookupCacheSite::OpenArrayIterationIteratorMethod),
- );
- if iterator_method != values {
+ let shape = array.shape();
+ if shape.property_count() != 0
+ && shape
+ .lookup(&PropertyKey::from(vm.well_known_symbols().iterator))
+ .is_some()
+ {
return Value::EMPTY.0;
}
iterable.0
diff --git a/Libraries/LibJS/Rust/src/runtime/realm.rs b/Libraries/LibJS/Rust/src/runtime/realm.rs
index 3405a587c96..72e3579ddbc 100644
--- a/Libraries/LibJS/Rust/src/runtime/realm.rs
+++ b/Libraries/LibJS/Rust/src/runtime/realm.rs
@@ -39,6 +39,8 @@ pub struct RealmStorage {
pub(crate) regexp_primordial_properties: WatchedCondition,
/// Whether ArraySpeciesCreate makes plain arrays (see `runtime::realm_watchpoints`).
pub(crate) array_species: WatchedCondition,
+ /// Whether %Array.prototype%[@@iterator] is %Array.prototype.values% (see `runtime::realm_watchpoints`).
+ pub(crate) array_iterator_method: WatchedCondition,
}
define_cell!(Realm, Other);
@@ -54,6 +56,7 @@ unsafe impl Trace for Realm {
self.storage.embedder_data.trace(visitor);
self.storage.regexp_primordial_properties.trace(visitor);
self.storage.array_species.trace(visitor);
+ self.storage.array_iterator_method.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.rs
index 1957496deca..29a83a3f68e 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_iterator_method`: %Array.prototype%[@@iterator] is %Array.prototype.values% of the realm, so that arrays
+//! of the realm without an own @@iterator are iterated with the array iterators of the realm. It depends on the sets
+//! that fire when %Array.prototype% changes, and when the value of that property changes.
//! - `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.
@@ -248,3 +251,25 @@ fn watch_array_species(vm: &Vm, realm: Gc<Realm>) -> Option<Gc<WatchpointSet>> {
],
)
}
+
+/// Whether %Array.prototype%[@@iterator] of the realm is its %Array.prototype.values%.
+pub fn arrays_have_builtin_iterator_method(vm: &Vm, realm: Gc<Realm>) -> bool {
+ realm
+ .storage
+ .array_iterator_method
+ .holds(vm, realm, watch_array_iterator_method)
+}
+
+/// The set that stays valid while %Array.prototype%[@@iterator] of the realm is its %Array.prototype.values%, if it is.
+fn watch_array_iterator_method(vm: &Vm, realm: Gc<Realm>) -> Option<Gc<WatchpointSet>> {
+ let array_prototype = realm.array_prototype();
+ let iterator_key = PropertyKey::from(vm.well_known_symbols().iterator);
+ let metadata = array_prototype.shape().lookup(&iterator_key)?;
+ let iterator_method = array_prototype.get_direct(metadata.offset);
+ if !iterator_method.is_object()
+ || iterator_method.as_object() != realm.array_prototype_values_function().upcast::<Object>()
+ {
+ return None;
+ }
+ watch_intrinsics(vm, &[array_prototype], &[(array_prototype, iterator_key)])
+}
diff --git a/Tests/LibJS/Runtime/iterators/watchpoints/array-iterator-method.js b/Tests/LibJS/Runtime/iterators/watchpoints/array-iterator-method.js
new file mode 100644
index 00000000000..5a093b3163e
--- /dev/null
+++ b/Tests/LibJS/Runtime/iterators/watchpoints/array-iterator-method.js
@@ -0,0 +1,56 @@
+const originalIteratorMethod = Array.prototype[Symbol.iterator];
+
+function collect(array) {
+ const values = [];
+ for (const value of array) values.push(value);
+ return values;
+}
+
+function compileIteration() {
+ if (typeof jit !== "undefined") jit.prepare(collect);
+ for (let i = 0; i < 10; ++i) expect(collect([1, 2, 3])).toEqual([1, 2, 3]);
+ if (typeof jit !== "undefined") expect(jit.compile(collect)).toBe(jit.enabled);
+}
+
+function* reversed() {
+ for (let i = this.length - 1; i >= 0; --i) yield this[i];
+}
+
+test("array iteration calls an iterator method assigned to Array.prototype", () => {
+ compileIteration();
+ Array.prototype[Symbol.iterator] = reversed;
+ try {
+ expect(collect([1, 2, 3])).toEqual([3, 2, 1]);
+ } finally {
+ Array.prototype[Symbol.iterator] = originalIteratorMethod;
+ }
+ expect(collect([1, 2, 3])).toEqual([1, 2, 3]);
+});
+
+test("array iteration calls an own iterator method of the array", () => {
+ compileIteration();
+ const array = [1, 2, 3];
+ array[Symbol.iterator] = reversed;
+ expect(collect(array)).toEqual([3, 2, 1]);
+ const arrayWithNamedProperty = [1, 2, 3];
+ arrayWithNamedProperty.name = "array";
+ expect(collect(arrayWithNamedProperty)).toEqual([1, 2, 3]);
+});
+
+test("array iteration calls an iterator method assigned after unrelated properties changed", () => {
+ compileIteration();
+ Array.prototype.unrelated = 1;
+ expect(collect([1, 2, 3])).toEqual([1, 2, 3]);
+ delete Array.prototype.unrelated;
+ compileIteration();
+ Object.defineProperty(Array.prototype, Symbol.iterator, {
+ value: reversed,
+ configurable: true,
+ writable: true,
+ });
+ try {
+ expect(collect([1, 2, 3])).toEqual([3, 2, 1]);
+ } finally {
+ Array.prototype[Symbol.iterator] = originalIteratorMethod;
+ }
+});