Skip to content

Truly independent web browser

Meta: Support ArrayBuffer arguments in Rust interfaces

Shannon Boothb42763651bb1
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
index c332ccbbc40..93431471519 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
@@ -495,6 +495,32 @@ pub(crate) struct BufferSourceRules {
pub(crate) allow_resizable: bool,
}
+// https://webidl.spec.whatwg.org/#js-ArrayBuffer
+pub(crate) fn to_array_buffer(vm: &Vm, value: Value, rules: &BufferSourceRules) -> ThrowCompletionOr<Gc<ArrayBuffer>> {
+ // 1. If V is not an Object, or V does not have an [[ArrayBufferData]] internal slot, then throw a TypeError.
+ let buffer = value
+ .is_object()
+ .then(|| value.as_object().downcast::<ArrayBuffer>())
+ .flatten();
+ let Some(buffer) = buffer else {
+ return vm.throw_completion(ErrorKind::TypeError, ErrorType::NotAnObjectOfType, &[&"ArrayBuffer"]);
+ };
+ // 2. If IsSharedArrayBuffer(V) is true, then throw a TypeError.
+ if buffer.is_shared_array_buffer() {
+ return vm.throw_completion(ErrorKind::TypeError, ErrorType::SharedArrayBuffer, &[]);
+ }
+ // 3. If the conversion is not to an IDL type associated with the [AllowResizable] extended attribute, and IsFixedLengthArrayBuffer(V) is false, then throw a TypeError.
+ if !rules.allow_resizable && !buffer.is_fixed_length() {
+ return vm.throw_completion(
+ ErrorKind::TypeError,
+ ErrorType::NotAnObjectOfType,
+ &[&"fixed-length ArrayBuffer"],
+ );
+ }
+ // 4. Return the IDL ArrayBuffer value that is a reference to the same object as V.
+ Ok(buffer)
+}
+
/// A JS value converted to an IDL typed array value of the kind, whose name is the type's name.
pub(crate) fn to_typed_array(
vm: &Vm,
diff --git a/Meta/Generators/libweb_bindings/rust_to_idl_value.py b/Meta/Generators/libweb_bindings/rust_to_idl_value.py
index f243f5a4c9f..4cc413d8aec 100644
--- a/Meta/Generators/libweb_bindings/rust_to_idl_value.py
+++ b/Meta/Generators/libweb_bindings/rust_to_idl_value.py
@@ -144,6 +144,10 @@ def rust_to_idl_value(
return f'to_double(vm, {value}, "{identifier}")'
if name == "unrestricted double":
return f"to_unrestricted_double(vm, {value})"
+ if name == "ArrayBuffer":
+ if "AllowShared" in attributes:
+ raise UnsupportedByRustBindings(f"{what} has [AllowShared] ArrayBuffer, which Rust does not convert yet")
+ return f"crate::bindings::conversions::to_array_buffer(vm, {value}, {buffer_source_rules(idl_type, parameter_attributes)})"
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.py
index 464ab8dad09..78f61a362b0 100644
--- a/Meta/Generators/libweb_bindings/rust_types.py
+++ b/Meta/Generators/libweb_bindings/rust_types.py
@@ -435,7 +435,7 @@ def rust_type(idl_type: IDLType, context: GenerationContext, what: str, is_resul
return "Gc<Object>"
if name in TYPED_ARRAY_NAMES:
return "Gc<TypedArrayBase>"
- if name == "ArrayBuffer" and is_result:
+ if name == "ArrayBuffer":
return "Gc<ArrayBuffer>"
callback_function = context.callback_function(idl_type)
if callback_function is not None: