Truly independent web browser
Meta: Support pair iteration over values that hold cells
Andreas Kling5698dad9d497
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/iterable.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/iterable.rsindex 4541f612b4d..82fc7deed30 100644--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/iterable.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/iterable.rs@@ -18,7 +18,7 @@ use libjs_rust::api::{use crate::bindings::CallContext;use crate::bindings::conversions::ToJs;-use crate::bindings::interface::{Interface, Ref, define_operation, implementation_of, this_object};+use crate::bindings::interface::{Interface, define_operation, this_object};use crate::bindings::services::web_bindings_ensure_web_iterator_prototype;// https://webidl.spec.whatwg.org/#default-iterator-object-kind@@ -33,8 +33,8 @@ pub(crate) enum IterationKind {/// a host object of the iterator class of its interface.#[derive(Trace)]#[gc(crate = libjs_rust)]-pub(crate) struct DefaultIterator<T: Interface> {- target: Ref<T>,+pub(crate) struct DefaultIterator<T: PairIterable> {+ target: T::Target,#[gc(untraced)]kind: IterationKind,#[gc(untraced)]@@ -42,13 +42,20 @@ pub(crate) struct DefaultIterator<T: Interface> {}/// What the implementation of an interface with a pair iterator implements: its value pairs to iterate over, with-/// the Rust types of the key and value types of the iterable declaration.+/// Rust types that convert to the key and value types of the iterable declaration.pub(crate) trait PairIterable: Interface {type Key: ToJs + Clone;type Value: ToJs + Clone;+ /// What an iteration reads its value pairs from: normally the object, or what the object looked like when the+ /// iteration started, for an interface that does not follow the changes of the object during an iteration.+ type Target: Trace + 'static;++ /// The target of an iteration of the kind over the object that the wrapper wraps.+ fn iteration_target(cx: &CallContext, wrapper: Gc<Object>, kind: IterationKind) -> Self::Target;+// https://webidl.spec.whatwg.org/#dfn-value-pairs-to-iterate-over- fn value_pairs_to_iterate_over(&self) -> impl Deref<Target = [(Self::Key, Self::Value)]> + '_;+ fn value_pairs_to_iterate_over(target: &Self::Target) -> impl Deref<Target = [(Self::Key, Self::Value)]> + '_;}/// What the generated bindings of an interface with a pair iterator define: the classes of its default iterator@@ -94,11 +101,12 @@ pub(crate) fn define_pair_iterator_properties(/// to 0, in the realm of the call context.pub(crate) fn create_default_iterator<T: DefaultIteratorClasses>(cx: &CallContext,- target: Ref<T>,+ wrapper: Gc<Object>,kind: IterationKind,) -> Value {let vm = cx.vm();let realm = cx.realm();+ let target = T::iteration_target(cx, wrapper, kind);let iterator = T::ITERATOR_CELL_CLASS.allocate(vm, |_| DefaultIterator {target,kind,@@ -150,7 +158,7 @@ pub(crate) fn iterator_next<T: DefaultIteratorClasses>(vm: &Vm) -> ThrowCompletilet pair = {// 8. Let values be object's target's value pairs to iterate over.- let values = iterator.target.value_pairs_to_iterate_over();+ let values = T::value_pairs_to_iterate_over(&iterator.target);// 9. Let len be the length of values.let len = values.len();@@ -237,9 +245,9 @@ pub(crate) fn entries<T: DefaultIteratorClasses>(vm: &Vm) -> ThrowCompletionOr<V// 4. Return a newly created default iterator object for definition, with jsValue as its target, "key+value" as its// kind, and index set to 0.- Ok(create_default_iterator(+ Ok(create_default_iterator::<T>(&CallContext::new(vm, realm),- implementation_of::<T>(js_value),+ js_value,IterationKind::KeyAndValue,))}@@ -254,9 +262,9 @@ pub(crate) fn keys<T: DefaultIteratorClasses>(vm: &Vm) -> ThrowCompletionOr<Valu// 4. Return a newly created default iterator object for definition, with jsValue as its target, "key" as its kind,// and index set to 0.- Ok(create_default_iterator(+ Ok(create_default_iterator::<T>(&CallContext::new(vm, realm),- implementation_of::<T>(js_value),+ js_value,IterationKind::Key,))}@@ -271,9 +279,9 @@ pub(crate) fn values<T: DefaultIteratorClasses>(vm: &Vm) -> ThrowCompletionOr<Va// 4. Return a newly created default iterator object for definition, with jsValue as its target, "value" as its// kind, and index set to 0.- Ok(create_default_iterator(+ Ok(create_default_iterator::<T>(&CallContext::new(vm, realm),- implementation_of::<T>(js_value),+ js_value,IterationKind::Value,))}@@ -296,16 +304,16 @@ pub(crate) fn for_each<T: DefaultIteratorClasses>(vm: &Vm) -> ThrowCompletionOr<}// 5. Let idlObject be the IDL interface type value that represents a reference to jsValue.- let idl_object = implementation_of::<T>(js_value);+ let idl_object = T::iteration_target(&cx, js_value, IterationKind::KeyAndValue);// 6. Let pairs be idlObject's list of value pairs to iterate over.// 7. Let i be 0.let mut i = 0;// 8. While i < pairs's size:- while i < idl_object.value_pairs_to_iterate_over().len() {+ while i < T::value_pairs_to_iterate_over(&idl_object).len() {// 1. Let pair be pairs[i].- let (key, value) = idl_object.value_pairs_to_iterate_over()[i].clone();+ let (key, value) = T::value_pairs_to_iterate_over(&idl_object)[i].clone();// 2. Invoke idlCallback with « pair's value, pair's key, idlObject » and with thisArg as the callback this// value.diff --git a/Libraries/LibWeb/Bindings/Rust/src/domurl/url_search_params.rs b/Libraries/LibWeb/Bindings/Rust/src/domurl/url_search_params.rsindex 1ea0e7219ab..df5a9b8191e 100644--- a/Libraries/LibWeb/Bindings/Rust/src/domurl/url_search_params.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/domurl/url_search_params.rs@@ -11,14 +11,14 @@ use core::ops::Deref;use ak::{Utf16String, Utf16StringUnits};use encoding_rs::UTF_8;-use libjs_rust::api::Trace;+use libjs_rust::api::{Gc, Object, Trace};use liburl_rust::url::form_urlencoded;use super::url::URL;use super::url_input;use crate::bindings::conversions::UsvString;-use crate::bindings::interface::{Ref, Wrappable};-use crate::bindings::iterable::PairIterable;+use crate::bindings::interface::{Ref, Wrappable, implementation_of};+use crate::bindings::iterable::{IterationKind, PairIterable};use crate::bindings::{CallContext, Exception, WebIdlResult};/// The union (sequence<sequence<USVString>> or record<USVString, USVString> or USVString) of@@ -274,11 +274,16 @@ impl URLSearchParams {impl PairIterable for URLSearchParams {type Key = UsvString;type Value = UsvString;+ type Target = Ref<URLSearchParams>;++ fn iteration_target(_: &CallContext, wrapper: Gc<Object>, _: IterationKind) -> Self::Target {+ implementation_of(wrapper)+ }// https://url.spec.whatwg.org/#ref-for-dfn-value-pairs-to-iterate-over- fn value_pairs_to_iterate_over(&self) -> impl Deref<Target = [(UsvString, UsvString)]> + '_ {+ fn value_pairs_to_iterate_over(target: &Self::Target) -> impl Deref<Target = [(UsvString, UsvString)]> + '_ {// The value pairs to iterate over are this's list's tuples with the key being the name and the value being the// value.- cell::Ref::map(self.list.borrow(), Vec::as_slice)+ cell::Ref::map(target.list.borrow(), Vec::as_slice)}}diff --git a/Libraries/LibWeb/Bindings/Rust/src/xhr/form_data.rs b/Libraries/LibWeb/Bindings/Rust/src/xhr/form_data.rsindex 6dd4d3270ba..defee9d9179 100644--- a/Libraries/LibWeb/Bindings/Rust/src/xhr/form_data.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/xhr/form_data.rs@@ -290,10 +290,21 @@ impl FormData {impl crate::bindings::iterable::PairIterable for FormData {type Key = UsvString;type Value = FormDataEntryValue;+ type Target = Ref<FormData>;++ fn iteration_target(+ _: &CallContext,+ wrapper: libjs_rust::api::Gc<libjs_rust::api::Object>,+ _: crate::bindings::iterable::IterationKind,+ ) -> Self::Target {+ crate::bindings::interface::implementation_of(wrapper)+ }- fn value_pairs_to_iterate_over(&self) -> impl core::ops::Deref<Target = [(UsvString, FormDataEntryValue)]> + '_ {+ fn value_pairs_to_iterate_over(+ target: &Self::Target,+ ) -> impl core::ops::Deref<Target = [(UsvString, FormDataEntryValue)]> + '_ {// The value pairs to iterate over are this’s entry list’s entries with the key being the name and the value// being the value.- cell::Ref::map(self.entry_list.borrow(), Vec::as_slice)+ cell::Ref::map(target.entry_list.borrow(), Vec::as_slice)}}diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.pyindex f733281dbe0..7b16c78157d 100644--- a/Meta/Generators/libweb_bindings/rust_bindings.py+++ b/Meta/Generators/libweb_bindings/rust_bindings.py@@ -670,7 +670,7 @@ def implementation_method_signatures(key_type, value_type = pair_iterator_types(context, interface, members.pair_iterator)signatures.append(f"impl PairIterable with Key = {key_type} and Value = {value_type}: "- "fn value_pairs_to_iterate_over(&self) -> impl Deref<Target = [(Self::Key, Self::Value)]> + '_"+ "fn value_pairs_to_iterate_over(target: &Self::Target) -> impl Deref<Target = [(Self::Key, Self::Value)]> + '_")if members.async_iterator is not None:what = f"the asynchronously iterable declaration of {interface.name}"