Skip to content

Truly independent web browser

Meta: Convert Rust record parameters whose values hold cells

Shannon Boothe68caf3e3da9
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
index a0736d1c1db..b83155094bb 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
@@ -142,7 +142,8 @@ impl From<UsvString> for Utf16String {
}
/// https://webidl.spec.whatwg.org/#idl-ByteString
-#[derive(Clone, Eq, PartialEq)]
+#[derive(Clone, Eq, PartialEq, Trace)]
+#[gc(crate = libjs_rust)]
pub(crate) struct ByteString(Vec<u8>);
impl ByteString {
@@ -1002,15 +1003,38 @@ pub(crate) fn sequence_from_iterable<T, S: SequenceStorage<T>>(
Ok(sequence)
}
-/// A JS value converted to an IDL record, https://webidl.spec.whatwg.org/#js-record, with the conversions of its keys
-/// and values. The entries are in a Vec in the order of the record, which hides cells from the collector, so their
-/// types hold none.
-pub(crate) fn record_from_js<K: PartialEq, V>(
+pub(crate) trait RecordStorage<K, V> {
+ /// https://infra.spec.whatwg.org/#map-set
+ fn set(&mut self, key: K, value: V);
+}
+
+impl<K: PartialEq, V> RecordStorage<K, V> for Vec<(K, V)> {
+ fn set(&mut self, key: K, value: V) {
+ match self.iter_mut().find(|(existing_key, _)| *existing_key == key) {
+ Some(entry) => entry.1 = value,
+ None => self.push((key, value)),
+ }
+ }
+}
+
+impl<K: PartialEq + Trace + Clone + 'static, V: Trace + Clone + 'static> RecordStorage<K, V> for MarkedVec<'_, (K, V)> {
+ fn set(&mut self, key: K, value: V) {
+ let index = self.values().iter().position(|(existing_key, _)| *existing_key == key);
+ match index {
+ Some(index) => MarkedVec::set(self, index, (key, value)),
+ None => self.push((key, value)),
+ }
+ }
+}
+
+/// https://webidl.spec.whatwg.org/#js-record
+pub(crate) fn record_from_js<K: PartialEq, V, R: RecordStorage<K, V>>(
vm: &Vm,
value: Value,
+ storage: R,
mut key_from_js: impl FnMut(Value) -> ThrowCompletionOr<K>,
mut value_from_js: impl FnMut(Value) -> ThrowCompletionOr<V>,
-) -> ThrowCompletionOr<Vec<(K, V)>> {
+) -> ThrowCompletionOr<R> {
// 1. If O is not an Object, throw a TypeError.
if !value.is_object() {
return vm.throw_completion(ErrorKind::TypeError, ErrorType::NotAnObject, &[&value]);
@@ -1018,7 +1042,7 @@ pub(crate) fn record_from_js<K: PartialEq, V>(
let object = value.as_object();
// 2. Let result be a new empty instance of record<K, V>.
- let mut result: Vec<(K, V)> = Vec::new();
+ let mut result = storage;
// 3. Let keys be ? O.[[OwnPropertyKeys]]().
let keys = object.internal_own_property_keys(vm)?;
@@ -1046,10 +1070,7 @@ pub(crate) fn record_from_js<K: PartialEq, V>(
let typed_value = value_from_js(value)?;
// 4. Set result[typedKey] to typedValue.
- match result.iter_mut().find(|(existing_key, _)| *existing_key == typed_key) {
- Some(entry) => entry.1 = typed_value,
- None => result.push((typed_key, typed_value)),
- }
+ result.set(typed_key, typed_value);
}
// 5. Return result.
diff --git a/Meta/Generators/libweb_bindings/rust_to_idl_value.py b/Meta/Generators/libweb_bindings/rust_to_idl_value.py
index 4cfefe3df04..bc79f5cfd47 100644
--- a/Meta/Generators/libweb_bindings/rust_to_idl_value.py
+++ b/Meta/Generators/libweb_bindings/rust_to_idl_value.py
@@ -29,6 +29,7 @@ from Generators.libweb_bindings.rust_types import inheriting_interfaces
from Generators.libweb_bindings.rust_types import is_callback_interface
from Generators.libweb_bindings.rust_types import is_named_union
from Generators.libweb_bindings.rust_types import is_promise_type
+from Generators.libweb_bindings.rust_types import is_record_of_cells
from Generators.libweb_bindings.rust_types import is_sequence_of_cells
from Generators.libweb_bindings.rust_types import is_unnamed_union_of_cpp_interfaces
from Generators.libweb_bindings.rust_types import member_other_than_undefined
@@ -111,6 +112,10 @@ def rust_to_idl_value(
element = rust_sequence_element_to_idl_value(idl_type, identifier, context, what)
return f"sequence_from_js(vm, {value}, Vec::new(), |value| {element})"
if isinstance(idl_type, IDLParameterizedType) and idl_type.name == "record":
+ if keeps_cells_alive and is_record_of_cells(idl_type, context):
+ key = rust_to_idl_value(idl_type.parameters[0], {}, "key", identifier, context, what)
+ record_value = rust_to_idl_value(idl_type.parameters[1], {}, "value", identifier, context, what)
+ return f"record_from_js(vm, {value}, MarkedVec::new(vm), |key| {key}, |value| {record_value})"
return rust_record_to_idl_value(idl_type, value, identifier, context, what)
if isinstance(idl_type, IDLParameterizedType) and idl_type.name == "Promise":
return f"to_promise(vm, {value})"
@@ -232,7 +237,7 @@ def rust_record_to_idl_value(
)
key = rust_to_idl_value(key_type, {}, "key", identifier, context, what)
record_value = rust_to_idl_value(value_type, {}, "value", identifier, context, what)
- return f"record_from_js(vm, {value}, |key| {key}, |value| {record_value})"
+ return f"record_from_js(vm, {value}, Vec::new(), |key| {key}, |value| {record_value})"
@dataclass
diff --git a/Meta/Generators/libweb_bindings/rust_types.py b/Meta/Generators/libweb_bindings/rust_types.py
index ace99ea70d2..9a4e14d4165 100644
--- a/Meta/Generators/libweb_bindings/rust_types.py
+++ b/Meta/Generators/libweb_bindings/rust_types.py
@@ -531,6 +531,17 @@ def is_sequence_of_cells(idl_type: IDLType, context: GenerationContext) -> bool:
)
+def is_record_of_cells(idl_type: IDLType, context: GenerationContext) -> bool:
+ """Whether the type is a record whose values are or can hold cells, which the bindings gather in a MarkedVec for a
+ parameter."""
+ return (
+ isinstance(idl_type, IDLParameterizedType)
+ and idl_type.name == "record"
+ and not idl_type.nullable
+ and holds_cells(idl_type.parameters[1], context)
+ )
+
+
def dictionary_holds_sequence_of_cells(dictionary: Dictionary, context: GenerationContext) -> bool:
"""Whether a member of the dictionary is a sequence of values that are or can hold cells, which the struct of the
dictionary keeps alive in a MarkedVec, for as long as the reference to the VM that it was converted with lives."""
@@ -556,6 +567,10 @@ def rust_parameter_type(parameter: OperationParameter, context: GenerationContex
if is_sequence_of_cells(parameter.type, context):
assert isinstance(parameter.type, IDLParameterizedType)
value_type = f"MarkedVec<'_, {rust_type(parameter.type.parameters[0], context, what)}>"
+ elif is_record_of_cells(parameter.type, context):
+ assert isinstance(parameter.type, IDLParameterizedType)
+ key_type, record_value_type = (rust_type(type, context, what) for type in parameter.type.parameters)
+ value_type = f"MarkedVec<'_, ({key_type}, {record_value_type})>"
else:
value_type = rust_type(parameter.type, context, what)
if parameter.optional and parameter.default_value is None: