Skip to content

Truly independent web browser

LibJS: Share the check for unobservable packed array iteration

Andreas Klinge8f5d31f3dcf
diff --git a/Libraries/LibJS/Rust/src/interpreter/slow_paths/control.rs b/Libraries/LibJS/Rust/src/interpreter/slow_paths/control.rs
index 0d243cb4acf..9d9232b9efd 100644
--- a/Libraries/LibJS/Rust/src/interpreter/slow_paths/control.rs
+++ b/Libraries/LibJS/Rust/src/interpreter/slow_paths/control.rs
@@ -19,7 +19,9 @@ use crate::layout::function_object::FunctionObject;
use crate::layout::object::Object;
use crate::layout::value::Value;
use crate::runtime::array::Array;
-use crate::runtime::array_iterator::{ArrayIterator, iterated_array_like_element, iterated_array_like_length};
+use crate::runtime::array_iterator::{
+ ArrayIterator, iterated_array_like_element, iterated_array_like_length, packed_array_iteration_is_unobservable,
+};
use crate::runtime::async_from_sync_iterator_prototype::create_async_from_sync_iterator;
use crate::runtime::async_generator::AsyncGenerator;
use crate::runtime::completion::{Completion, Must, ThrowCompletionOr, completion_type_from_bytecode};
@@ -219,9 +221,7 @@ pub fn array_append(
// array can be appended in bulk if its next method is also unchanged.
let realm = current_realm(vm);
if let Some(rhs_array) = rhs_array
- && iterator_method == realm.array_prototype_values_function()
- && rhs_array.is_simple_packed_array()
- && realm_watchpoints::array_iterators_have_builtin_next(vm, realm)
+ && packed_array_iteration_is_unobservable(vm, realm, &rhs_array, iterator_method)
&& rhs_array.indexed_packed_element_count() <= u32::MAX - lhs_size
{
lhs_array.indexed_append_packed_elements_of(&rhs_array);
diff --git a/Libraries/LibJS/Rust/src/runtime/array_constructor.rs b/Libraries/LibJS/Rust/src/runtime/array_constructor.rs
index 62e9b030b27..74dfa5e03de 100644
--- a/Libraries/LibJS/Rust/src/runtime/array_constructor.rs
+++ b/Libraries/LibJS/Rust/src/runtime/array_constructor.rs
@@ -17,6 +17,7 @@ use crate::runtime::abstract_operations::{
call_function_object, construct, get_prototype_from_constructor, length_of_array_like,
};
use crate::runtime::array::Array;
+use crate::runtime::array_iterator::packed_array_iteration_is_unobservable;
use crate::runtime::completion::{Must, ThrowCompletionOr};
use crate::runtime::error::ErrorKind;
use crate::runtime::error_types::ErrorType;
@@ -32,7 +33,6 @@ use crate::runtime::object::ShouldThrowExceptions;
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::regexp_constructor::is_raw_native_function_running;
use crate::runtime::set::Set;
use crate::runtime::set_iterator::set_iteration_is_unobservable;
@@ -49,10 +49,7 @@ fn array_from_array_set_or_map(
using_iterator: Gc<FunctionObject>,
) -> Option<Gc<Array>> {
if let Some(source) = items.downcast::<Array>() {
- if using_iterator != realm.array_prototype_values_function()
- || !source.is_simple_packed_array()
- || !realm_watchpoints::array_iterators_have_builtin_next(vm, realm)
- {
+ if !packed_array_iteration_is_unobservable(vm, realm, &source, using_iterator) {
return None;
}
diff --git a/Libraries/LibJS/Rust/src/runtime/array_iterator.rs b/Libraries/LibJS/Rust/src/runtime/array_iterator.rs
index f4daebefd68..c2179c42269 100644
--- a/Libraries/LibJS/Rust/src/runtime/array_iterator.rs
+++ b/Libraries/LibJS/Rust/src/runtime/array_iterator.rs
@@ -19,18 +19,34 @@ use crate::runtime::array_buffer::Order;
use crate::runtime::completion::ThrowCompletionOr;
use crate::runtime::error::ErrorKind;
use crate::runtime::error_types::ErrorType;
+use crate::runtime::function_object::FunctionObject;
use crate::runtime::iterator::{BuiltinIteratorNext, BuiltinIteratorStep};
use crate::runtime::object::{
MayInterfereWithIndexedPropertyAccess, ORDINARY_OBJECT_METHODS, PropertyKind, define_object_class,
};
use crate::runtime::property_key::PropertyKey;
use crate::runtime::realm::Realm;
+use crate::runtime::realm_watchpoints;
use crate::runtime::typed_array::{
is_typed_array_out_of_bounds, make_typed_array_with_buffer_witness_record, typed_array_length,
typed_array_of_object,
};
/// An Array Iterator instance, the iterator CreateArrayIterator creates.
+/// Returns true if iterating `array` with `iterator_method` cannot be observed: `iterator_method` is this realm's
+/// original Array.prototype.values, array iterators of the realm have the builtin next method, and the array is a
+/// simple packed array. Its packed elements are then what the iterator protocol produces.
+pub fn packed_array_iteration_is_unobservable(
+ vm: &Vm,
+ realm: Gc<Realm>,
+ array: &Array,
+ iterator_method: Gc<FunctionObject>,
+) -> bool {
+ iterator_method == realm.array_prototype_values_function()
+ && array.is_simple_packed_array()
+ && realm_watchpoints::array_iterators_have_builtin_next(vm, realm)
+}
+
#[repr(C)]
#[derive(Trace)]
pub struct ArrayIterator {
diff --git a/Libraries/LibJS/Rust/src/runtime/typed_array.rs b/Libraries/LibJS/Rust/src/runtime/typed_array.rs
index ab891e3b31a..928cf00b4d0 100644
--- a/Libraries/LibJS/Rust/src/runtime/typed_array.rs
+++ b/Libraries/LibJS/Rust/src/runtime/typed_array.rs
@@ -26,6 +26,7 @@ use crate::runtime::array_buffer::{
ArrayBuffer, ElementType, INVALID_DATA_OFFSET, Order, ReadWriteModifyOperation, allocate_array_buffer,
array_buffer_byte_length, clone_array_buffer, f64_to_binary16_bits,
};
+use crate::runtime::array_iterator::packed_array_iteration_is_unobservable;
use crate::runtime::byte_length::{ByteLength, ByteLengthSlot};
use crate::runtime::canonical_index::{CanonicalIndex, CanonicalIndexType};
use crate::runtime::completion::ThrowCompletionOr;
@@ -44,7 +45,6 @@ use crate::runtime::property_attributes::{Attribute, PropertyAttributes};
use crate::runtime::property_descriptor::PropertyDescriptor;
use crate::runtime::property_key::PropertyKey;
use crate::runtime::realm::Realm;
-use crate::runtime::realm_watchpoints;
use crate::runtime::uint8_array::{Uint8ArrayConstructorHelpers, Uint8ArrayPrototypeHelpers};
use crate::runtime::value::same_value;
use crate::runtime::value_conversions;
@@ -1345,9 +1345,7 @@ pub(crate) fn iterable_to_list(
let realm = vm.current_realm().expect("iterables are iterated in a realm");
if source.is_object()
&& let Some(array) = source.as_object().downcast::<Array>()
- && using_iterator == realm.array_prototype_values_function()
- && array.is_simple_packed_array()
- && realm_watchpoints::array_iterators_have_builtin_next(vm, realm)
+ && packed_array_iteration_is_unobservable(vm, realm, &array, using_iterator)
{
return Ok(array.indexed_packed_elements(vm));
}