Truly independent web browser
LibWeb: Pass buffer source types explicitly to Rust conversions
Callum Lawf9c90b1e9880
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rsindex 9f6a338584b..2ebb0a5254c 100644--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs@@ -569,6 +569,13 @@ pub(crate) struct BufferSourceRules {pub(crate) allow_resizable: bool,}+/// The buffer source types included in a union and their conversion rules.+pub(crate) struct BufferSourceTypes<'a> {+ pub(crate) array_buffer_type: Option<&'a BufferSourceRules>,+ pub(crate) shared_array_buffer_type: Option<&'a BufferSourceRules>,+ pub(crate) array_buffer_view_type: Option<&'a BufferSourceRules>,+}+// https://webidl.spec.whatwg.org/#js-ArrayBufferpub(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.@@ -884,8 +891,13 @@ impl ArrayBufferView {/// A JS value converted to a BufferSource, with the steps of the conversion to a union type that apply to the buffer/// source types, https://webidl.spec.whatwg.org/#js-unionpub(crate) fn to_buffer_source(vm: &Vm, value: Value, rules: &BufferSourceRules) -> ThrowCompletionOr<BufferSource> {+ let types = BufferSourceTypes {+ array_buffer_type: Some(rules),+ shared_array_buffer_type: rules.allow_shared.then_some(rules),+ array_buffer_view_type: Some(rules),+ };if value.is_object()- && let Some(buffer_source) = buffer_source_if(vm, value.as_object(), rules)?+ && let Some(buffer_source) = buffer_source_if(vm, value.as_object(), types)?{return Ok(buffer_source);}@@ -899,12 +911,13 @@ pub(crate) fn to_buffer_source(vm: &Vm, value: Value, rules: &BufferSourceRules)pub(crate) fn buffer_source_if(vm: &Vm,object: Gc<Object>,- rules: &BufferSourceRules,+ types: BufferSourceTypes<'_>,) -> ThrowCompletionOr<Option<BufferSource>> {// 6. If V is an Object, V has an [[ArrayBufferData]] internal slot, and IsSharedArrayBuffer(V) is false, then:// 1. If types includes ArrayBuffer, then return the result of converting V to ArrayBuffer.if let Some(buffer) = object.downcast::<ArrayBuffer>()&& !buffer.is_shared_array_buffer()+ && let Some(rules) = types.array_buffer_type{check_buffer_of_buffer_source(vm, &buffer, "ArrayBuffer", rules)?;return Ok(Some(BufferSource::ArrayBuffer(buffer)));@@ -912,9 +925,9 @@ pub(crate) fn buffer_source_if(// 7. If V is an Object, V, has an [[ArrayBufferData]] internal slot, and IsSharedArrayBuffer(V) is true, then:// 1. If types includes SharedArrayBuffer, then return the result of converting V to SharedArrayBuffer.- if rules.allow_shared- && let Some(buffer) = object.downcast::<ArrayBuffer>()+ if let Some(buffer) = object.downcast::<ArrayBuffer>()&& buffer.is_shared_array_buffer()+ && let Some(rules) = types.shared_array_buffer_type{let buffer = to_shared_array_buffer(vm, Value::from_object(object), rules)?;return Ok(Some(BufferSource::ArrayBuffer(buffer)));@@ -922,7 +935,9 @@ pub(crate) fn buffer_source_if(// 8. If V is an Object and V has a [[DataView]] internal slot, then:// 1. If types includes DataView, then return the result of converting V to DataView.- if let Some(view) = object.downcast::<DataView>() {+ if let Some(view) = object.downcast::<DataView>()+ && let Some(rules) = types.array_buffer_view_type+ {check_buffer_of_buffer_source(vm, &view.viewed_array_buffer(), "DataView", rules)?;return Ok(Some(BufferSource::DataView(view)));}@@ -930,7 +945,9 @@ pub(crate) fn buffer_source_if(// 9. If V is an Object and V has a [[TypedArrayName]] internal slot, then:// 1. If types includes a typed array type whose name is the value of V's [[TypedArrayName]] internal slot,// then return the result of converting V to that type.- if let Some(typed_array) = object.downcast::<TypedArrayBase>() {+ if let Some(typed_array) = object.downcast::<TypedArrayBase>()+ && let Some(rules) = types.array_buffer_view_type+ {let type_name = format!("{:?}", typed_array.kind());check_buffer_of_buffer_source(vm, &typed_array.viewed_array_buffer(), &type_name, rules)?;return Ok(Some(BufferSource::TypedArray(typed_array)));diff --git a/Libraries/LibWeb/Bindings/Rust/src/compression/mod.rs b/Libraries/LibWeb/Bindings/Rust/src/compression/mod.rsindex b029e693617..3290cd0d9d8 100644--- a/Libraries/LibWeb/Bindings/Rust/src/compression/mod.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/compression/mod.rs@@ -12,7 +12,7 @@ pub(crate) mod decompression_stream;use ak::Utf16String;use libjs_rust::api::Value;-use crate::bindings::conversions::{BufferSource, BufferSourceRules, buffer_source_if};+use crate::bindings::conversions::{BufferSource, BufferSourceRules, BufferSourceTypes, buffer_source_if};use crate::bindings::{CallContext, Exception, WebIdlResult};/// The chunk as a BufferSource, or the TypeError that compressing or decompressing a chunk throws when it is not a@@ -22,8 +22,13 @@ fn buffer_source_of_chunk(cx: &CallContext, chunk: Value) -> WebIdlResult<Bufferallow_shared: false,allow_resizable: false,};+ let types = BufferSourceTypes {+ array_buffer_type: Some(&rules),+ shared_array_buffer_type: None,+ array_buffer_view_type: Some(&rules),+ };let buffer_source = if chunk.is_object() {- buffer_source_if(cx.vm(), chunk.as_object(), &rules)?+ buffer_source_if(cx.vm(), chunk.as_object(), types)?} else {None};diff --git a/Meta/Generators/libweb_bindings/rust_bindings.py b/Meta/Generators/libweb_bindings/rust_bindings.pyindex 8624dff99df..464e7bda67a 100644--- a/Meta/Generators/libweb_bindings/rust_bindings.py+++ b/Meta/Generators/libweb_bindings/rust_bindings.py@@ -2567,7 +2567,7 @@ pub(crate) mod definitions {use crate::bindings::CallContext;use crate::bindings::callback::{CallbackFunction, CallbackInterface, PromiseCallbackFunction};use crate::bindings::conversions::{- ArrayBufferView, BufferSource, BufferSourceRules, ByteString, CppInterface, EmptyDictionary, FromJs, ToJs, UsvString,+ ArrayBufferView, BufferSource, BufferSourceRules, BufferSourceTypes, ByteString, CppInterface, EmptyDictionary, FromJs, ToJs, UsvString,buffer_source_if, dictionary_member, dictionary_object, enumeration_value_to_js, implements_cpp_interface,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,diff --git a/Meta/Generators/libweb_bindings/rust_to_idl_value.py b/Meta/Generators/libweb_bindings/rust_to_idl_value.pyindex 32942ab68e2..7ae1e136aa3 100644--- a/Meta/Generators/libweb_bindings/rust_to_idl_value.py+++ b/Meta/Generators/libweb_bindings/rust_to_idl_value.py@@ -681,7 +681,11 @@ def write_union_from_js(out: TextIO, context: GenerationContext, union_type: IDL// 9. If V is an Object and V has a [[TypedArrayName]] internal slot, then:// 1. If types includes a typed array type whose name is the value of V's [[TypedArrayName]]// internal slot, then return the result of converting V to that type.- if let Some(buffer_source) = buffer_source_if(vm, value.as_object(), {rules})? {{+ if let Some(buffer_source) = buffer_source_if(vm, value.as_object(), BufferSourceTypes {{+ array_buffer_type: Some({rules}),+ shared_array_buffer_type: None,+ array_buffer_view_type: Some({rules}),+ }})? {{return Ok(Self::BufferSource(buffer_source));}}"""