Skip to content

Truly independent web browser

LibWeb: Borrow rooted dictionaries during sequence conversion

Shannon Booth3197e79aa44d
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
index 93431471519..e28808fdb94 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
@@ -1297,11 +1297,11 @@ pub(crate) fn frozen_array_to_js(cx: &CallContext, array: ThrowCompletionOr<Valu
pub(crate) fn rooted_sequence_to_js<T: Trace + Clone + 'static>(
cx: &CallContext,
sequence: MarkedVec<'_, T>,
- mut element_to_js: impl FnMut(T) -> ThrowCompletionOr<Value>,
+ mut element_to_js: impl FnMut(&T) -> ThrowCompletionOr<Value>,
) -> ThrowCompletionOr<Value> {
let array = Array::create(cx.vm(), cx.realm(), sequence.len() as u64, None)?;
- for index in 0..sequence.len() {
- let element = element_to_js(sequence.get(index).expect("the index is in the sequence"))?;
+ for (index, element) in sequence.values().iter().enumerate() {
+ let element = element_to_js(element)?;
let index = u32::try_from(index).expect("an array of the length of the sequence was created");
array.create_data_property(cx.vm(), &PropertyKey::from(index), element, None, None)?;
}
diff --git a/Meta/Generators/libweb_bindings/rust_to_js_value.py b/Meta/Generators/libweb_bindings/rust_to_js_value.py
index cce5f655952..cbe0328defa 100644
--- a/Meta/Generators/libweb_bindings/rust_to_js_value.py
+++ b/Meta/Generators/libweb_bindings/rust_to_js_value.py
@@ -13,6 +13,7 @@ from Generators.libweb_bindings.context import GenerationContext
from Generators.libweb_bindings.rust_names import rust_identifier
from Generators.libweb_bindings.rust_names import rust_local_name
from Generators.libweb_bindings.rust_types import UnsupportedByRustBindings
+from Generators.libweb_bindings.rust_types import dictionary_holds_sequence_of_cells
from Generators.libweb_bindings.rust_types import holds_cells
from Generators.libweb_bindings.rust_types import is_sequence_of_cells
from Generators.libweb_bindings.rust_types import member_other_than_undefined
@@ -56,8 +57,15 @@ def rust_to_js_value(
element_type = idl_type.parameters[0]
# NB: The conversion of an element can allocate, as that of an object of an interface does for its wrapper,
# and the elements after it must live through that, in the MarkedVec that holds them (see rust_type()).
- function = "rooted_sequence_to_js" if holds_cells(element_type, context) else "sequence_to_js"
- array = f"{function}({call_context}, {expression}, |value| {inner(element_type)})"
+ if holds_cells(element_type, context):
+ dictionary = context.dictionaries.get(element_type.without_nullable().name)
+ if dictionary is not None and dictionary_holds_sequence_of_cells(dictionary, context):
+ conversion = rust_to_js_value(element_type, "value", call_context, context, what)
+ else:
+ conversion = rust_to_js_value(element_type, "ToOwned::to_owned(value)", call_context, context, what)
+ array = f"rooted_sequence_to_js({call_context}, {expression}, |value| {conversion})"
+ else:
+ array = f"sequence_to_js({call_context}, {expression}, |value| {inner(element_type)})"
if idl_type.name == "FrozenArray":
return f"frozen_array_to_js({call_context}, {array})"
return array
@@ -137,7 +145,9 @@ def write_dictionary_to_js(out: TextIO, context: GenerationContext, dictionary:
fields = []
keys = []
conversions = []
- marked_sequences = []
+ prepared_members = []
+ borrows_dictionary = dictionary_holds_sequence_of_cells(dictionary, context)
+ impl_type = f"&{path}" if borrows_dictionary else path
for member in dictionary.members:
what = f"{dictionary.name}.{member.name}"
member_type = rust_type(member.type, context, what)
@@ -149,16 +159,17 @@ def write_dictionary_to_js(out: TextIO, context: GenerationContext, dictionary:
f"{what} has the type {member.type}, with a nested sequence of values that are or can hold cells, "
"which the Rust bindings do not convert from a member of a dictionary yet"
)
- # NB: The struct holds the sequence in a Vec, which hides it from the collector. It moves to a MarkedVec
- # before the object of the dictionary is created, which is the first allocation of the conversion.
+ # Keep the original dictionary in its root while copying its sequence into a MarkedVec.
if member.required or member.default_value is not None:
- marked_sequences.append(
- f" let {local} = crate::bindings::conversions::mark_sequence(cx, &{local});\n"
+ prepared_members.append(
+ f" let {local} = crate::bindings::conversions::mark_sequence(cx, {local}.as_slice());\n"
)
else:
- marked_sequences.append(
- f" let {local} = {local}.map(|value| crate::bindings::conversions::mark_sequence(cx, &value));\n"
+ prepared_members.append(
+ f" let {local} = {local}.as_deref().map(|value| crate::bindings::conversions::mark_sequence(cx, value));\n"
)
+ elif borrows_dictionary:
+ prepared_members.append(f" let {local} = ToOwned::to_owned({local});\n")
key = f"{rust_identifier(member.name).removeprefix('r#').rstrip('_').upper()}_KEY"
keys.append(
f' static {key}: LazyLock<PropertyKey> = LazyLock::new(|| PropertyKey::from_utf8("{member.name}"));\n'
@@ -183,10 +194,10 @@ def write_dictionary_to_js(out: TextIO, context: GenerationContext, dictionary:
out.write(
f"""
/// The conversion of the dictionary {dictionary.name}, a struct with the fields:
-{"".join(fields)} impl ToJs for {path} {{
+{"".join(fields)} impl ToJs for {impl_type} {{
fn to_js(self, cx: &CallContext) -> ThrowCompletionOr<Value> {{
{"".join(keys)} let {path} {{ {bindings} }} = self;
-{"".join(marked_sequences)} let object = create_dictionary_object(cx);
+{"".join(prepared_members)} let object = create_dictionary_object(cx);
{"".join(conversions)} Ok(Value::from_object(object))
}}
}}