Truly independent web browser
Meta: Support PutForwards and FrozenArray in the Rust bindings
Andreas Klingdd581fd92310
diff --git a/Libraries/LibJS/Rust/src/api.rs b/Libraries/LibJS/Rust/src/api.rsindex 06774508ff3..16726932041 100644--- a/Libraries/LibJS/Rust/src/api.rs+++ b/Libraries/LibJS/Rust/src/api.rs@@ -73,7 +73,7 @@ pub use crate::runtime::module_environment::ModuleEnvironment;pub use crate::runtime::module_request::ModuleRequest;pub use crate::runtime::native_function::{NativeFunction, RawNativeFunction};pub use crate::runtime::number_object::NumberObject;-pub use crate::runtime::object::{IntegrityLevel, Object, PropertyKind};+pub use crate::runtime::object::{IntegrityLevel, Object, PropertyKind, ShouldThrowExceptions};pub use crate::runtime::primitive_string::PrimitiveString;pub use crate::runtime::promise::{Promise, PromiseState, RejectionOperation};pub use crate::runtime::promise_capability::{PromiseCapability, new_promise_capability};diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rsindex a173e60915b..93021a3ea32 100644--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs@@ -14,8 +14,8 @@ use std::borrow::Cow;use ak::{Utf16String, Utf16StringUnits, convert_valid_utf16_to_utf8, utf8_length_from_valid_utf16};use libjs_rust::api::{Array, ArrayBuffer, ArrayBufferOrder, ArrayBufferShared, DataView, ErrorKind, ErrorType, FunctionObject, Gc,- MarkedVec, Must, Object, PrimitiveString, PropertyKey, ThrowCompletionOr, Trace, TypedArrayBase, TypedArrayKind,- Value, Vm, create_typed_array_on_buffer, get_iterator_from_method_impl, get_view_byte_length,+ IntegrityLevel, MarkedVec, Must, Object, PrimitiveString, PropertyKey, ThrowCompletionOr, Trace, TypedArrayBase,+ TypedArrayKind, Value, Vm, create_typed_array_on_buffer, get_iterator_from_method_impl, get_view_byte_length,is_typed_array_out_of_bounds, is_view_out_of_bounds, iterator_step_value,make_data_view_with_buffer_witness_record, make_typed_array_with_buffer_witness_record, to_well_formed,typed_array_byte_length,@@ -1245,6 +1245,21 @@ pub(crate) fn sequence_to_js<T>(Ok(Value::from_object(array))}+/// https://webidl.spec.whatwg.org/#js-frozen-array+pub(crate) fn frozen_array_to_js(cx: &CallContext, array: ThrowCompletionOr<Value>) -> ThrowCompletionOr<Value> {+ // 1. Let array be the result of converting the sequence of type T to a JavaScript value.+ let array = array?;++ // 2. Perform ! SetIntegrityLevel(array, "frozen").+ array+ .as_object()+ .set_integrity_level(cx.vm(), IntegrityLevel::Frozen)+ .must();++ // 3. Return array.+ Ok(array)+}+/// sequence_to_js() for a sequence of cells, which stay rooted while converting an element allocates.pub(crate) fn rooted_sequence_to_js<T: Trace + Clone + 'static>(cx: &CallContext,diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.pyindex cd53cac5052..d6653a51068 100644--- a/Meta/Generators/libweb_bindings/rust_bindings.py+++ b/Meta/Generators/libweb_bindings/rust_bindings.py@@ -834,13 +834,13 @@ RUST_BINDINGS_IMPORTS = """ #![allow(unused_imports)]use ak::Utf16String;use libjs_rust::api::{ArrayBuffer, DataView, EmbedderClass, ErrorKind, ErrorType, FunctionObject, Gc, JSHostClass, MarkedVec,- Object, PropertyKey, RawNativeFunctionResult, ThrowCompletionOr, TypedArrayBase, TypedArrayKind, Value, Vm,+ Object, PropertyKey, RawNativeFunctionResult, ShouldThrowExceptions, ThrowCompletionOr, TypedArrayBase, TypedArrayKind, Value, Vm,is_host_instance_of, raw_native,};use crate::bindings::conversions::{ArrayBufferView, BufferSource, BufferSourceRules, CppInterface, EmptyDictionary, FromJs, ToJs, UsvString,- implements_cpp_interface, iterator_method, record_from_js, record_to_js, rooted_sequence_to_js,+ frozen_array_to_js, implements_cpp_interface, iterator_method, record_from_js, record_to_js, rooted_sequence_to_js,sequence_from_js, sequence_to_js, to_array_buffer_view, to_boolean, to_buffer_source, to_cpp_interface,to_cpp_interface_in_union, to_dom_string,to_dom_string_treating_null_as_empty, to_double, to_float, to_long, to_long_enforcing_range, to_long_long,@@ -1007,6 +1007,8 @@ def write_rust_interface()if attribute.setter_method is not None:write_rust_attribute_setter(out, context, interface, attribute)+ elif attribute.put_forwards is not None:+ write_rust_put_forwards_setter(out, attribute)for operation_set in members.operation_sets:if len(operation_set.overloads) == 1 and operation_set.overloads[0].is_default_to_json:@@ -1067,7 +1069,8 @@ def write_rust_interface(out.write(f' define_cached_attribute(vm, realm, prototype, "{name}", {getter});\n')defined_property_names.append(name)continue- setter = "None" if attribute.attribute.readonly else f"raw_native!({attribute.setter_function})"+ has_setter = not attribute.attribute.readonly or attribute.put_forwards is not None+ setter = f"raw_native!({attribute.setter_function})" if has_setter else "None"out.write(f' define_attribute(vm, realm, prototype, "{name}", {getter}, {setter});\n')defined_property_names.append(name)for operation_set in members.operation_sets:@@ -1804,6 +1807,30 @@ def write_rust_attribute_setter()+def write_rust_put_forwards_setter(out: TextIO, attribute: RustAttribute) -> None:+ """The native function of the setter of a [PutForwards] attribute, which sets the attribute that it names on the+ value of the attribute: https://webidl.spec.whatwg.org/#dfn-attribute-setter"""+ out.write(+ f"""+ fn {attribute.setter_function}(vm: &Vm) -> ThrowCompletionOr<Value> {{+ let value = vm.argument(0);+ let (object, _) = this_object(vm, Implementation::wrapper_class(), Implementation::NAME)?;+ let receiver = object.get(vm, &PropertyKey::from_utf8("{attribute.attribute.name}"))?;+ if !receiver.is_object() {{+ return vm.throw_completion(ErrorKind::TypeError, ErrorType::NotAnObject, &[&"{attribute.attribute.name}"]);+ }}+ receiver.as_object().set(+ vm,+ &PropertyKey::from_utf8("{attribute.put_forwards}"),+ value,+ ShouldThrowExceptions::No,+ )?;+ Ok(Value::UNDEFINED)+ }}+"""+ )++def write_rust_operation(out: TextIO, context: GenerationContext, interface: Interface, name: str, function: str, overload: RustOperation) -> None:@@ -1937,7 +1964,7 @@ pub(crate) mod definitions {use crate::bindings::conversions::{ArrayBufferView, BufferSource, BufferSourceRules, CppInterface, EmptyDictionary, FromJs, ToJs, UsvString,buffer_source_if, dictionary_member, dictionary_object, enumeration_value_to_js, implements_cpp_interface,- iterator_method, record_from_js, record_to_js, rooted_sequence_to_js, sequence_from_iterable, sequence_from_js,+ frozen_array_to_js, iterator_method, record_from_js, record_to_js, rooted_sequence_to_js, sequence_from_iterable, sequence_from_js,sequence_to_js, throw_no_union_types_matched, to_array_buffer_view, to_boolean, to_buffer_source,to_cpp_interface,to_dom_string, to_dom_string_treating_null_as_empty, to_double, to_enumeration_value, to_float, to_long,diff --git a/Meta/Generators/libweb_bindings/rust_members.py b/Meta/Generators/libweb_bindings/rust_members.pyindex 2b8a7820e25..a2df37ea028 100644--- a/Meta/Generators/libweb_bindings/rust_members.py+++ b/Meta/Generators/libweb_bindings/rust_members.py@@ -40,6 +40,7 @@ SUPPORTED_ATTRIBUTE_EXTENDED_ATTRIBUTES = {"ImplementedAs","NeedsCallerRealm","NewObject",+ "PutForwards","SameObject",}SUPPORTED_OPERATION_EXTENDED_ATTRIBUTES = {@@ -77,6 +78,12 @@ class RustAttribute:# object owns: https://html.spec.whatwg.org/multipage/webappapis.html#event-handler-idl-attributesevent_type: Optional[str] = None+ @property+ def put_forwards(self) -> Optional[str]:+ """The attribute of the attribute's value that the setter of a read only attribute forwards to: [PutForwards],+ https://webidl.spec.whatwg.org/#PutForwards"""+ return self.attribute.extended_attributes.get("PutForwards")+@propertydef is_cached(self) -> bool:"""Whether the getter returns the same value for as long as the object lives, which the engine then caches on@@ -421,6 +428,8 @@ def rust_members(interface: Interface) -> RustMembers:continueif "CachedAccessor" in attribute.extended_attributes and not attribute.readonly:fail(f"its attribute {attribute.name} is [CachedAccessor] and not read only")+ if "PutForwards" in attribute.extended_attributes and not attribute.readonly:+ fail(f"its attribute {attribute.name} is [PutForwards] and not read only")if attribute.type.name in ("EventHandler", "EventHandlerNonNull"):rust_attribute = event_handler_attribute(attribute)else:diff --git a/Meta/Generators/libweb_bindings/rust_to_js_value.py b/Meta/Generators/libweb_bindings/rust_to_js_value.pyindex a24821221e2..6317aaf9377 100644--- a/Meta/Generators/libweb_bindings/rust_to_js_value.py+++ b/Meta/Generators/libweb_bindings/rust_to_js_value.py@@ -51,12 +51,15 @@ def rust_to_js_value(Some(value) => {inner(optional_member)},None => Ok(Value::UNDEFINED),}}"""- if isinstance(idl_type, IDLParameterizedType) and idl_type.name == "sequence":+ if isinstance(idl_type, IDLParameterizedType) and idl_type.name in ("sequence", "FrozenArray"):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"- return f"{function}({call_context}, {expression}, |value| {inner(element_type)})"+ array = f"{function}({call_context}, {expression}, |value| {inner(element_type)})"+ if idl_type.name == "FrozenArray":+ return f"frozen_array_to_js({call_context}, {array})"+ return arrayif isinstance(idl_type, IDLParameterizedType) and idl_type.name == "record":value_type = idl_type.parameters[1]if holds_cells(value_type, context):diff --git a/Meta/Generators/libweb_bindings/rust_types.py b/Meta/Generators/libweb_bindings/rust_types.pyindex 86225a3fde1..c712aab13af 100644--- a/Meta/Generators/libweb_bindings/rust_types.py+++ b/Meta/Generators/libweb_bindings/rust_types.py@@ -404,7 +404,7 @@ def rust_type(idl_type: IDLType, context: GenerationContext, what: str, is_resulreturn f"Option<{rust_type(idl_type.without_nullable(), context, what, is_result)}>"name = idl_type.nameif isinstance(idl_type, IDLParameterizedType):- if name == "sequence":+ if name in ("sequence", "FrozenArray"):element_type = idl_type.parameters[0]element = rust_type(element_type, context, what, is_result)# NB: A Vec would hide cells that the implementation returns from the collector until they are converted.@@ -485,7 +485,7 @@ def holds_cells(return Truereturn any_holds_cells(idl_type.flattened_member_types())if isinstance(idl_type, IDLParameterizedType):- if idl_type.name in ("sequence", "record"):+ if idl_type.name in ("sequence", "FrozenArray", "record"):return any_holds_cells(idl_type.parameters)return Trueif idl_type.name in PLAIN_DATA_TYPES or context.enumeration(idl_type) is not None: