Skip to content

Truly independent web browser

LibWeb: Support SharedArrayBuffer conversion in Rust bindings

Callum Law794f8f775d4f
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
index d10a66dcd4f..9f6a338584b 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
@@ -595,6 +595,41 @@ pub(crate) fn to_array_buffer(vm: &Vm, value: Value, rules: &BufferSourceRules)
Ok(buffer)
}
+// https://webidl.spec.whatwg.org/#js-buffer-source-types
+pub(crate) fn to_shared_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,
+ &[&"SharedArrayBuffer"],
+ );
+ };
+ // 2. If IsSharedArrayBuffer(V) is false, then throw a TypeError.
+ if !buffer.is_shared_array_buffer() {
+ return vm.throw_completion(ErrorKind::TypeError, ErrorType::NotASharedArrayBuffer, &[]);
+ }
+ // 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 SharedArrayBuffer"],
+ );
+ }
+ // 4. Return the IDL SharedArrayBuffer 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,
@@ -881,7 +916,7 @@ pub(crate) fn buffer_source_if(
&& let Some(buffer) = object.downcast::<ArrayBuffer>()
&& buffer.is_shared_array_buffer()
{
- check_buffer_of_buffer_source(vm, &buffer, "SharedArrayBuffer", rules)?;
+ let buffer = to_shared_array_buffer(vm, Value::from_object(object), rules)?;
return Ok(Some(BufferSource::ArrayBuffer(buffer)));
}
diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.py
index 7f7c209e6c0..8624dff99df 100644
--- a/Meta/Generators/libweb_bindings/rust_bindings.py
+++ b/Meta/Generators/libweb_bindings/rust_bindings.py
@@ -1151,7 +1151,7 @@ RUST_BINDINGS_IMPORTS = """ #![allow(unused_imports)]
sequence_from_js, sequence_to_js, to_array_buffer_view, to_boolean, to_buffer_source, to_byte_string, 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,
- to_long_long_clamping, to_long_long_enforcing_range, to_object, to_promise, to_typed_array,
+ to_long_long_clamping, to_long_long_enforcing_range, to_object, to_promise, to_shared_array_buffer, to_typed_array,
to_unrestricted_double, to_unrestricted_float, to_unsigned_long, to_unsigned_long_clamping,
to_unsigned_long_enforcing_range,
to_unsigned_long_long, to_unsigned_long_long_enforcing_range, to_usv_string,
@@ -2573,7 +2573,7 @@ pub(crate) mod definitions {
sequence_to_js, throw_no_union_types_matched, to_array_buffer_view, to_boolean, to_buffer_source,
to_byte_string, to_cpp_interface,
to_dom_string, to_dom_string_treating_null_as_empty, to_double, to_enumeration_value, to_float, to_long,
- to_long_enforcing_range, to_long_long, to_long_long_enforcing_range, to_object, to_typed_array,
+ to_long_enforcing_range, to_long_long, to_long_long_enforcing_range, to_object, to_shared_array_buffer, to_typed_array,
to_unrestricted_double, to_unrestricted_float, to_unsigned_long, to_unsigned_long_clamping,
to_unsigned_long_enforcing_range,
to_unsigned_long_long, to_unsigned_long_long_enforcing_range, to_usv_string,
diff --git a/Meta/Generators/libweb_bindings/rust_to_idl_value.py b/Meta/Generators/libweb_bindings/rust_to_idl_value.py
index bb3877d5b04..32942ab68e2 100644
--- a/Meta/Generators/libweb_bindings/rust_to_idl_value.py
+++ b/Meta/Generators/libweb_bindings/rust_to_idl_value.py
@@ -156,6 +156,8 @@ def rust_to_idl_value(
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 == "SharedArrayBuffer":
+ return f"to_shared_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 688782efe38..7a921d5534b 100644
--- a/Meta/Generators/libweb_bindings/rust_types.py
+++ b/Meta/Generators/libweb_bindings/rust_types.py
@@ -492,7 +492,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":
+ if name in ("ArrayBuffer", "SharedArrayBuffer"):
return "Gc<ArrayBuffer>"
callback_function = context.callback_function(idl_type)
if callback_function is not None: