Truly independent web browser
Meta+LibWeb: Convert doubles and callback results in Rust bindings
Aliaksandr Kalenikb9d4a12f875a
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rsindex 6ed773421c9..b2dc73c5889 100644--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs@@ -11,10 +11,10 @@ use core::mem::MaybeUninit;use ak::{Utf16String, Utf16StringUnits, convert_valid_utf16_to_utf8, utf8_length_from_valid_utf16};use libjs_rust::api::{- Array, ArrayBuffer, ArrayBufferOrder, DataView, ErrorKind, ErrorType, Gc, Object, PrimitiveString, PropertyKey,- ThrowCompletionOr, TypedArrayBase, TypedArrayKind, Value, Vm, get_view_byte_length, is_typed_array_out_of_bounds,- is_view_out_of_bounds, make_data_view_with_buffer_witness_record, make_typed_array_with_buffer_witness_record,- to_well_formed, typed_array_byte_length,+ Array, ArrayBuffer, ArrayBufferOrder, DataView, ErrorKind, ErrorType, FunctionObject, Gc, Object, PrimitiveString,+ PropertyKey, ThrowCompletionOr, TypedArrayBase, TypedArrayKind, Value, Vm, get_view_byte_length,+ is_typed_array_out_of_bounds, is_view_out_of_bounds, make_data_view_with_buffer_witness_record,+ make_typed_array_with_buffer_witness_record, to_well_formed, typed_array_byte_length,};use crate::bindings::CallContext;@@ -147,6 +147,15 @@ pub(crate) fn to_float(vm: &Vm, value: Value, identifier: &str) -> ThrowCompletiOk(y)}+/// A JS value converted to an unrestricted double, https://webidl.spec.whatwg.org/#js-unrestricted-double+pub(crate) fn to_unrestricted_double(vm: &Vm, value: Value) -> ThrowCompletionOr<f64> {+ // 1. Let x be ? ToNumber(V).+ // 2. If x is NaN, then return the IDL unrestricted double value that represents the IEEE 754 NaN value with the+ // bit pattern 0x7ff8000000000000.+ // 3. Return the IDL unrestricted double value that represents the same numeric value as x.+ value.to_double(vm)+}+/// 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,@@ -379,6 +388,20 @@ impl ToJs for f32 {}}+/// https://webidl.spec.whatwg.org/#js-unrestricted-double+impl ToJs for f64 {+ fn to_js(self, _: &CallContext) -> ThrowCompletionOr<Value> {+ Ok(Value::from_f64(self))+ }+}++/// A callback function converted to a JS value, https://webidl.spec.whatwg.org/#js-callback-function+impl ToJs for Gc<FunctionObject> {+ fn to_js(self, _: &CallContext) -> ThrowCompletionOr<Value> {+ Ok(Value::from_object(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/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rsindex dcd99056185..49135b1ae7c 100644--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/mod.rs@@ -18,6 +18,7 @@ pub(crate) mod conversions;#[cfg(web_idl_bindings)]pub(crate) mod generated;pub(crate) mod interface;+pub(crate) mod realm_data;pub(crate) mod services;use ak::Utf16String;diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/realm_data.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/realm_data.rsnew file mode 100644index 00000000000..ce1f69fa2e7--- /dev/null+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/realm_data.rs@@ -0,0 +1,37 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++//! What the interfaces implemented in Rust keep for a realm: the objects that a specification has a global object+//! hold, which every object of an interface in the realm of that global returns.++use core::cell::Cell;++use libjs_rust::api::{EmbedderCell, EmbedderClass, EmbedderValue, FunctionObject, Gc, Realm, Trace, Vm};++#[derive(Default, Trace)]+#[gc(crate = libjs_rust)]+pub(crate) struct RealmData {+ /// https://streams.spec.whatwg.org/#count-queuing-strategy-size-function+ pub(crate) count_queuing_strategy_size_function: Cell<Option<Gc<FunctionObject>>>,+ /// https://streams.spec.whatwg.org/#byte-length-queuing-strategy-size-function+ pub(crate) byte_length_queuing_strategy_size_function: Cell<Option<Gc<FunctionObject>>>,+}++static REALM_DATA_CLASS: EmbedderClass<RealmData> = EmbedderClass::new("RealmData");++impl EmbedderValue for RealmData {+ const CLASS: &'static EmbedderClass<Self> = &REALM_DATA_CLASS;+}++/// The data of the realm, which the first use in a realm creates.+pub(crate) fn realm_data(vm: &Vm, realm: Gc<Realm>) -> Gc<EmbedderCell<RealmData>> {+ if let Some(data) = realm.embedder_data::<RealmData>() {+ return data;+ }+ let data = REALM_DATA_CLASS.allocate(vm, RealmData::default());+ realm.set_embedder_data(data);+ data+}diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.pyindex b02c75bd0e1..768492a9be6 100644--- a/Meta/Generators/libweb_bindings/rust_bindings.py+++ b/Meta/Generators/libweb_bindings/rust_bindings.py@@ -421,13 +421,13 @@ def write_rust_interface(use ak::Utf16String;use libjs_rust::api::{{- EmbedderCell, EmbedderClass, EmbedderValue, ErrorKind, ErrorType, Gc, JSHostClass, Object,+ EmbedderCell, EmbedderClass, EmbedderValue, ErrorKind, ErrorType, FunctionObject, Gc, JSHostClass, Object,RawNativeFunctionResult, ThrowCompletionOr, TypedArrayBase, TypedArrayKind, Value, Vm, raw_native,}};use crate::bindings::conversions::{{BufferSource, BufferSourceRules, EmptyDictionary, FromJs, ToJs, UsvString, record_to_js, sequence_to_js,- to_boolean, to_buffer_source, to_dom_string, to_float, to_typed_array, to_usv_string,+ to_boolean, to_buffer_source, to_dom_string, to_float, to_typed_array, to_unrestricted_double, to_usv_string,}};use crate::bindings::interface::{{construct_arguments, create_wrapper, define_attribute, define_interface_object_properties, define_operation,@@ -636,7 +636,7 @@ pub(crate) mod definitions {use crate::bindings::conversions::{BufferSource, BufferSourceRules, EmptyDictionary, FromJs, ToJs, UsvString, dictionary_member,dictionary_object, record_to_js, sequence_to_js, to_boolean, to_buffer_source, to_dom_string, to_float,- to_typed_array, to_usv_string,+ to_typed_array, to_unrestricted_double, to_usv_string,};use crate::bindings::interface::{create_dictionary_object, set_dictionary_member};"""diff --git a/Meta/Generators/libweb_bindings/rust_to_idl_value.py b/Meta/Generators/libweb_bindings/rust_to_idl_value.pyindex 6120b95f264..2ea734c8d98 100644--- a/Meta/Generators/libweb_bindings/rust_to_idl_value.py+++ b/Meta/Generators/libweb_bindings/rust_to_idl_value.py@@ -64,6 +64,8 @@ def rust_to_idl_value(return f"to_boolean({value})"if name == "float":return f'to_float(vm, {value}, "{identifier}")'+ if name == "unrestricted double":+ return f"to_unrestricted_double(vm, {value})"if name in TYPED_ARRAY_NAMES:rules = buffer_source_rules(idl_type, parameter_attributes)return f'to_typed_array(vm, {value}, TypedArrayKind::{name}, "{name}", {rules})'diff --git a/Meta/Generators/libweb_bindings/rust_types.py b/Meta/Generators/libweb_bindings/rust_types.pyindex d91fde2390e..5653c435235 100644--- a/Meta/Generators/libweb_bindings/rust_types.py+++ b/Meta/Generators/libweb_bindings/rust_types.py@@ -38,7 +38,7 @@ TYPED_ARRAY_NAMES = {SUPPORTED_TYPE_EXTENDED_ATTRIBUTES = {"AllowShared", "AllowResizable"}# The floating point types, with the Rust type of their values.-FLOATING_POINT_TYPES = {"float": "f32"}+FLOATING_POINT_TYPES = {"float": "f32", "unrestricted double": "f64"}# The unions that the bindings runtime has a type for, by the name of the typedef that names the union.RUNTIME_UNION_TYPES = {"BufferSource": "BufferSource"}@@ -160,6 +160,8 @@ def rust_type(idl_type: IDLType, context: GenerationContext, what: str) -> str:return "()"if name in TYPED_ARRAY_NAMES:return "Gc<TypedArrayBase>"+ if context.callback_function(idl_type) is not None:+ return "Gc<FunctionObject>"dictionary = resolved_dictionary(idl_type, context)if dictionary is not None:return rust_dictionary_path(dictionary, context)