Truly independent web browser
Meta+LibWeb: Convert constructor arguments and floats in Rust bindings
Aliaksandr Kalenik17aceb0bdb5a
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rsindex c6a6afefe87..cf71d3b5b97 100644--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs@@ -80,6 +80,35 @@ pub(crate) fn to_usv_string(vm: &Vm, value: Value) -> ThrowCompletionOr<UsvStrinOk(UsvString(to_well_formed(&string)))}+/// A JS value converted to a float, https://webidl.spec.whatwg.org/#js-float+///+/// The identifier is the name of the parameter or member that the value is of, which the error names.+pub(crate) fn to_float(vm: &Vm, value: Value, identifier: &str) -> ThrowCompletionOr<f32> {+ // 1. Let x be ? ToNumber(V).+ let x = value.to_double(vm)?;++ // 2. If x is NaN, +∞, or −∞, then throw a TypeError.+ // 3. Let S be the set of finite IEEE 754 single-precision floating point values except −0, but with two special+ // values added: 2^128 and −2^128.+ // 4. Let y be the number in S that is closest to x, selecting the number with an even significand if there are two+ // equally close values. (The two special values 2^128 and −2^128 are considered to have even significands for+ // this purpose.)+ // 5. If y is 2^128 or −2^128, then throw a TypeError.+ // 6. If y is +0 and x is negative, return −0.+ // 7. Return y.+ // NB: Rounding x to the nearest float gives an infinity where step 4 gives one of the two special values.+ #[allow(clippy::cast_possible_truncation, reason = "the spec rounds to the closest float")]+ let y = x as f32;+ if !y.is_finite() {+ return vm.throw_completion(+ ErrorKind::TypeError,+ ErrorType::InvalidRestrictedFloatingPointParameter,+ &[&identifier],+ );+ }+ Ok(y)+}+/// What the extended attributes of a buffer source type allow, https://webidl.spec.whatwg.org/#es-buffer-source-typespub(crate) struct BufferSourceRules {pub(crate) allow_shared: bool,@@ -152,6 +181,13 @@ impl ToJs for u64 {}}+/// https://webidl.spec.whatwg.org/#js-float+impl ToJs for f32 {+ fn to_js(self, _: &CallContext) -> ThrowCompletionOr<Value> {+ Ok(Value::from_f64(f64::from(self)))+ }+}+impl ToJs for Utf16String {fn to_js(self, cx: &CallContext) -> ThrowCompletionOr<Value> {Ok(Value::from_string(PrimitiveString::create(cx.vm(), self)))diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.pyindex 7d75d47ed3b..97404cb4d25 100644--- a/Meta/Generators/libweb_bindings/rust_bindings.py+++ b/Meta/Generators/libweb_bindings/rust_bindings.py@@ -32,10 +32,12 @@ from Generators.libweb_bindings.rust_types import rust_parameter_typefrom Generators.libweb_bindings.rust_types import rust_typefrom Generators.libweb_bindings.wrappers import wrapper_namefrom Utils.utils import title_case_to_snake_case+from Utils.webidl_parser import Constructorfrom Utils.webidl_parser import Dictionaryfrom Utils.webidl_parser import IDLTypefrom Utils.webidl_parser import Interfacefrom Utils.webidl_parser import Module+from Utils.webidl_parser import OperationParameterRUST_KEYWORDS = {"as", "async", "await", "break", "const", "continue", "crate", "dyn", "else", "enum", "extern", "false", "fn", "for",@@ -134,8 +136,6 @@ def check_interface_is_supported(interface: Interface, module: Module, context:if len(interface.constructors) > 1:fail("it has more than one constructor")for constructor in interface.constructors:- if constructor.parameters:- fail("its constructor has parameters")if constructor.extended_attributes:fail("the Rust bindings do not handle the extended attributes "@@ -170,14 +170,8 @@ def check_interface_is_supported(interface: Interface, module: Module, context:fail(f"its attribute {attribute.name} is not readonly")check_type_is_supported(attribute.type, context, f"{interface.name}.{attribute.name}")- for name, operations in operation_overload_sets(interface).items():- if len(operations) > 1:- fail(f"its operation {name} is overloaded")- operation = operations[0]- what = f"{interface.name}.{name}()"- check_type_is_supported(operation.return_type, context, what)- check_dictionaries_are_local(operation.return_type, what)- for parameter in operation.parameters:+ def check_parameters(what: str, parameters: List[OperationParameter]) -> None:+ for parameter in parameters:if parameter.variadic:fail(f"{what} has a variadic parameter")parameter_what = f"the parameter {parameter.name} of {what}"@@ -187,11 +181,23 @@ def check_interface_is_supported(interface: Interface, module: Module, context:check_type_is_supported(parameter.type, context, parameter_what)check_dictionaries_are_local(parameter.type, parameter_what)rust_conversion_to_idl_value(- parameter.type, parameter.extended_attributes, "value", context, parameter_what+ parameter.type, parameter.extended_attributes, "value", parameter.name, context, parameter_what)if parameter.optional and parameter.default_value is not None:rust_default_value(parameter, context, parameter_what)+ for constructor in interface.constructors:+ check_parameters(constructor_description(interface), constructor.parameters)++ for name, operations in operation_overload_sets(interface).items():+ if len(operations) > 1:+ fail(f"its operation {name} is overloaded")+ operation = operations[0]+ what = f"{interface.name}.{name}()"+ check_type_is_supported(operation.return_type, context, what)+ check_dictionaries_are_local(operation.return_type, what)+ check_parameters(what, operation.parameters)+for dictionary in module.dictionaries:if dictionary.parent_name:fail(f"its dictionary {dictionary.name} inherits from another dictionary")@@ -394,18 +400,47 @@ JSObject* web_bindings_ensure_web_prototype(void* realm, u16 interface_name)# Rust side-def write_parameter_conversions(out: TextIO, interface: Interface, operation_name: str, parameters, context) -> None:+def constructor_description(interface: Interface) -> str:+ return f"the constructor of {interface.name}"+++def write_argument_count_check(out: TextIO, function_name: str, required_argument_count: int) -> None:+ """Throws the errors of the C++ bindings for a call with too few arguments, which name the operation, or the+ interface for a constructor."""+ if required_argument_count == 1:+ out.write(+ f""" if vm.argument_count() < 1 {{+ return vm.throw_completion(ErrorKind::TypeError, ErrorType::BadArgCountOne, &[&"{function_name}"]);+ }}+"""+ )+ elif required_argument_count > 1:+ out.write(+ f""" if vm.argument_count() < {required_argument_count} {{+ return vm.throw_completion(+ ErrorKind::TypeError,+ ErrorType::BadArgCountMany,+ &[&"{function_name}", &{required_argument_count}],+ );+ }}+"""+ )+++def write_parameter_conversions(+ out: TextIO, what: str, parameters: List[OperationParameter], context: GenerationContext+) -> None:for index, parameter in enumerate(parameters):- what = f"the parameter {parameter.name} of {interface.name}.{operation_name}()"+ parameter_what = f"the parameter {parameter.name} of {what}"name = rust_local_name(parameter.name)conversion = rust_conversion_to_idl_value(- parameter.type, parameter.extended_attributes, f"argument{index}", context, what+ parameter.type, parameter.extended_attributes, f"argument{index}", parameter.name, context, parameter_what)out.write(f" let argument{index} = vm.argument({index});\n")if parameter.optional and parameter.default_value is not None:out.write(f""" let {name} = if argument{index}.is_undefined() {{- {rust_default_value(parameter, context, what)}+ {rust_default_value(parameter, context, parameter_what)}}} else {{{conversion}?}};@@ -426,9 +461,17 @@ def write_parameter_conversions(out: TextIO, interface: Interface, operation_namdef implementation_method_signatures(context: GenerationContext, interface: Interface) -> List[str]:"""The methods the implementation of the interface defines, which the bindings call."""++ def parameter_list(what: str, parameters: List[OperationParameter]) -> str:+ return "".join(+ f", {rust_identifier(parameter.name)}: {rust_parameter_type(parameter, context, what)}"+ for parameter in parameters+ )+signatures = []- if interface.constructors:- signatures.append("fn construct(cx: &mut CallContext) -> WebIdlResult<Self>")+ for constructor in interface.constructors:+ parameters = parameter_list(constructor_description(interface), constructor.parameters)+ signatures.append(f"fn construct(cx: &mut CallContext{parameters}) -> WebIdlResult<Self>")for attribute in interface.regular_attributes:what = f"{interface.name}.{attribute.name}"signatures.append(@@ -438,10 +481,7 @@ def implementation_method_signatures(context: GenerationContext, interface: Intefor name, operations in operation_overload_sets(interface).items():operation = operations[0]what = f"{interface.name}.{name}()"- parameters = "".join(- f", {rust_identifier(parameter.name)}: {rust_parameter_type(parameter, context, what)}"- for parameter in operation.parameters- )+ parameters = parameter_list(what, operation.parameters)signatures.append(f"fn {rust_identifier(name)}(&self, cx: &mut CallContext{parameters}) -> "f"WebIdlResult<{rust_type(operation.return_type, context, what)}>"@@ -475,7 +515,7 @@ def write_rust_interface(}};use crate::bindings::conversions::{{- BufferSourceRules, ToJs, UsvString, to_dom_string, to_typed_array, to_usv_string,+ BufferSourceRules, ToJs, UsvString, to_dom_string, to_float, to_typed_array, to_usv_string,}};use crate::bindings::interface::{{construct_arguments, create_dictionary_object, create_wrapper, define_attribute,@@ -543,28 +583,10 @@ def write_rust_interface(let (implementation, mut cx) = this_implementation(vm)?;""")- required_argument_count = operation_length(operation)- if required_argument_count == 1:- out.write(- f""" if vm.argument_count() < 1 {{- return vm.throw_completion(ErrorKind::TypeError, ErrorType::BadArgCountOne, &[&"{name}"]);- }}-"""- )- elif required_argument_count > 1:- out.write(- f""" if vm.argument_count() < {required_argument_count} {{- return vm.throw_completion(- ErrorKind::TypeError,- ErrorType::BadArgCountMany,- &[&"{name}", &{required_argument_count}],- );- }}-"""- )+ write_argument_count_check(out, name, operation_length(operation))if operation.parameters:out.write(" let type_error_realm_scope = vm.type_error_realm_scope(cx.realm());\n")- write_parameter_conversions(out, interface, name, operation.parameters, context)+ write_parameter_conversions(out, f"{interface.name}.{name}()", operation.parameters, context)out.write(" drop(type_error_realm_scope);\n")arguments = "".join(f", {rust_local_name(parameter.name)}" for parameter in operation.parameters)out.write(@@ -622,11 +644,12 @@ def write_rust_interface(""")+ for constructor in interface.constructors:+ write_rust_constructor(out, context, interface, constructor)if interface.constructors:out.write(f"""- /// The steps of {interface.constructor_class}::construct() of LibWeb's C++ bindings, which internally create a- /// new object implementing the interface, https://webidl.spec.whatwg.org/#internally-create-a-new-object-implementing-the-interface+ /// {interface.constructor_class}::construct() of LibWeb's C++ bindings.////// # Safety///@@ -639,21 +662,41 @@ def write_rust_interface() -> RawNativeFunctionResult {{// SAFETY: The caller passes the VM and the new target.let (vm, new_target) = unsafe {{ construct_arguments(vm, new_target) }};- let construct = || -> ThrowCompletionOr<Value> {{- let realm = vm.current_realm().expect("a construction runs in a realm");- let prototype = prototype_from_new_target(vm, new_target, INTERFACE_NAME)?;- let mut cx = CallContext::new(vm, realm);- let implementation = Implementation::construct(&mut cx).map_err(|exception| exception.throw(vm))?;- let implementation = CELL_CLASS.allocate(vm, implementation);- Ok(Value::from_object(create_wrapper(vm, realm, wrapper_class(), prototype, implementation)))- }};- RawNativeFunctionResult::from_completion(construct())+ RawNativeFunctionResult::from_completion(construct(vm, new_target))}}""")out.write("}\n")+def write_rust_constructor(+ out: TextIO, context: GenerationContext, interface: Interface, constructor: Constructor+) -> None:+ """The steps of a constructor in the order of the C++ bindings: the prototype of the new object is read from the+ new target before the arguments are counted and converted, and the conversions throw in the current realm."""+ out.write(+ """+ /// The constructor steps of the interface, which internally create a new object implementing the interface,+ /// https://webidl.spec.whatwg.org/#internally-create-a-new-object-implementing-the-interface+ fn construct(vm: &Vm, new_target: Gc<Object>) -> ThrowCompletionOr<Value> {+ let realm = vm.current_realm().expect("a construction runs in a realm");+ let prototype = prototype_from_new_target(vm, new_target, INTERFACE_NAME)?;+"""+ )+ write_argument_count_check(out, interface.name, parameter_list_length(constructor.parameters))+ write_parameter_conversions(out, constructor_description(interface), constructor.parameters, context)+ arguments = "".join(f", {rust_local_name(parameter.name)}" for parameter in constructor.parameters)+ out.write(+ f""" let mut cx = CallContext::new(vm, realm);+ let implementation =+ Implementation::construct(&mut cx{arguments}).map_err(|exception| exception.throw(vm))?;+ let implementation = CELL_CLASS.allocate(vm, implementation);+ Ok(Value::from_object(create_wrapper(vm, realm, wrapper_class(), prototype, implementation)))+ }}+"""+ )++def constructor_length(interface: Interface) -> int:if not interface.constructors:return 0diff --git a/Meta/Generators/libweb_bindings/rust_types.py b/Meta/Generators/libweb_bindings/rust_types.pyindex 3d17fb7de2b..2ceb4ef69ec 100644--- a/Meta/Generators/libweb_bindings/rust_types.py+++ b/Meta/Generators/libweb_bindings/rust_types.py@@ -5,6 +5,8 @@# The Rust types of IDL values, and the Rust expressions that convert JS values to them, for the Rust bindings of the# interfaces implemented in Rust. They cover the types that those interfaces use so far; every other type is an error.+import re+from typing import Optionalfrom Generators.libweb_bindings.context import GenerationContext@@ -31,6 +33,11 @@ TYPED_ARRAY_NAMES = {# The extended attributes of a type or a parameter that the Rust bindings handle.SUPPORTED_TYPE_EXTENDED_ATTRIBUTES = {"AllowShared", "AllowResizable"}+# The floating point types, with the Rust type of their values.+FLOATING_POINT_TYPES = {"float": "f32"}++DECIMAL_LITERAL = re.compile(r"-?[0-9]+(\.[0-9]+)?([eE][-+]?[0-9]+)?")+class UnsupportedByRustBindings(Exception):pass@@ -60,6 +67,8 @@ def rust_type(idl_type: IDLType, context: GenerationContext, what: str) -> str:return "bool"if name == "unsigned long long":return "u64"+ if name in FLOATING_POINT_TYPES:+ return FLOATING_POINT_TYPES[name]if name == "undefined":return "()"if name in TYPED_ARRAY_NAMES:@@ -78,14 +87,22 @@ def rust_parameter_type(parameter: OperationParameter, context: GenerationContexdef rust_conversion_to_idl_value(- idl_type: IDLType, parameter_attributes: dict[str, str], value: str, context: GenerationContext, what: str+ idl_type: IDLType,+ parameter_attributes: dict[str, str],+ value: str,+ identifier: str,+ context: GenerationContext,+ what: str,) -> str:- """An expression of type ThrowCompletionOr<T> that converts the JS value to the IDL value."""+ """An expression of type ThrowCompletionOr<T> that converts the JS value to the IDL value. The identifier is the+ name of the parameter or member that the value is of, which the errors of some conversions name."""name = idl_type.nameif name == "DOMString":return f"to_dom_string(vm, {value})"if name == "USVString":return f"to_usv_string(vm, {value})"+ if name == "float":+ return f'to_float(vm, {value}, "{identifier}")'if name in TYPED_ARRAY_NAMES:attributes = {**idl_type.extended_attributes, **parameter_attributes}allow_shared = "true" if "AllowShared" in attributes else "false"@@ -108,6 +125,8 @@ def rust_default_value(parameter: OperationParameter, context: GenerationContextif name == "USVString":return f'UsvString::from_ascii(b"{string}")'return f'Utf16String::from_ascii(b"{string}")'+ if name in FLOATING_POINT_TYPES and default_value is not None and DECIMAL_LITERAL.fullmatch(default_value):+ return f"{default_value}_{FLOATING_POINT_TYPES[name]}"raise UnsupportedByRustBindings(f"{what} has the default value {default_value}, which the Rust bindings do not have")