Skip to content

Truly independent web browser

LibWeb: Convert TextDecoderStream chunks to AllowSharedBufferSource

Shannon Boothb08820066568
diff --git a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
index 953449d7cba..c332ccbbc40 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/bindings/conversions.rs
@@ -775,8 +775,15 @@ pub(crate) fn buffer_source_if(
return Ok(Some(BufferSource::ArrayBuffer(buffer)));
}
- // FIXME: 7. If V is an Object, V has an [[ArrayBufferData]] internal slot, and IsSharedArrayBuffer(V) is true,
- // then:
+ // 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>()
+ && buffer.is_shared_array_buffer()
+ {
+ check_buffer_of_buffer_source(vm, &buffer, "SharedArrayBuffer", rules)?;
+ return Ok(Some(BufferSource::ArrayBuffer(buffer)));
+ }
// 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.
diff --git a/Libraries/LibWeb/Bindings/Rust/src/encoding/text_decoder_stream.rs b/Libraries/LibWeb/Bindings/Rust/src/encoding/text_decoder_stream.rs
index 7f5a7c7fb60..10fefbcd911 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/encoding/text_decoder_stream.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/encoding/text_decoder_stream.rs
@@ -13,7 +13,7 @@ use encoding_rs::Encoding;
use libjs_rust::api::{EmbedderClass, EmbedderValue, PrimitiveString, Trace, Value};
use libtextcodec_rust::{DecodingFailed, StreamingDecoder, encoding_for_label};
-use crate::bindings::conversions::{UsvString, buffer_source_type_of};
+use crate::bindings::conversions::{BufferSourceRules, UsvString, to_buffer_source};
use crate::bindings::interface::Ref;
use crate::bindings::interface::Wrappable;
use crate::bindings::{CallContext, Exception, WebIdlResult};
@@ -165,11 +165,11 @@ impl TransformSteps for Decoder {
// https://encoding.spec.whatwg.org/#decode-and-enqueue-a-chunk
fn transform(&self, cx: &CallContext, transform: Ref<TransformStream>, chunk: Value) -> WebIdlResult<()> {
// 1. Let bufferSource be the result of converting chunk to an AllowSharedBufferSource.
- let Some(buffer_source) = buffer_source_type_of(chunk) else {
- return Err(Exception::TypeError(Utf16String::from_ascii(
- b"Chunk is not a BufferSource",
- )));
+ let rules = BufferSourceRules {
+ allow_shared: true,
+ allow_resizable: false,
};
+ let buffer_source = to_buffer_source(cx.vm(), chunk, &rules)?;
// 2. Push a copy of bufferSource to decoder's I/O queue.
// 3. Let output be the I/O queue of scalar values « end-of-queue ».
diff --git a/Tests/LibWeb/Text/expected/Encoding/TextDecoderStream-resizable-chunk.txt b/Tests/LibWeb/Text/expected/Encoding/TextDecoderStream-resizable-chunk.txt
new file mode 100644
index 00000000000..ee94773af4c
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Encoding/TextDecoderStream-resizable-chunk.txt
@@ -0,0 +1,3 @@
+resizable ArrayBuffer: rejected with TypeError
+Uint8Array on a resizable ArrayBuffer: rejected with TypeError
+fixed-length ArrayBuffer: fulfilled
diff --git a/Tests/LibWeb/Text/expected/Encoding/TextDecoderStream-shared-chunk.txt b/Tests/LibWeb/Text/expected/Encoding/TextDecoderStream-shared-chunk.txt
new file mode 100644
index 00000000000..f9683a5bea6
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Encoding/TextDecoderStream-shared-chunk.txt
@@ -0,0 +1 @@
+SharedArrayBuffer chunk: 65536 code units, starting with "hi"
diff --git a/Tests/LibWeb/Text/input/Encoding/TextDecoderStream-resizable-chunk.html b/Tests/LibWeb/Text/input/Encoding/TextDecoderStream-resizable-chunk.html
new file mode 100644
index 00000000000..4626626b770
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Encoding/TextDecoderStream-resizable-chunk.html
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ async function write(chunk) {
+ const stream = new TextDecoderStream();
+ stream.readable.getReader().read().catch(() => {});
+ const writer = stream.writable.getWriter();
+ try {
+ await writer.write(chunk);
+ return "fulfilled";
+ } catch (error) {
+ return `rejected with ${error.name}`;
+ }
+ }
+
+ asyncTest(async done => {
+ const resizable = new ArrayBuffer(1, { maxByteLength: 2 });
+ println(`resizable ArrayBuffer: ${await write(resizable)}`);
+ println(`Uint8Array on a resizable ArrayBuffer: ${await write(new Uint8Array(resizable))}`);
+ println(`fixed-length ArrayBuffer: ${await write(new ArrayBuffer(1))}`);
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/Encoding/TextDecoderStream-shared-chunk.html b/Tests/LibWeb/Text/input/Encoding/TextDecoderStream-shared-chunk.html
new file mode 100644
index 00000000000..5bb00ed4991
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Encoding/TextDecoderStream-shared-chunk.html
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const buffer = new WebAssembly.Memory({ initial: 1, maximum: 1, shared: true }).buffer;
+ new Uint8Array(buffer).set([0x68, 0x69]);
+ const stream = new TextDecoderStream();
+ const writer = stream.writable.getWriter();
+ writer.write(buffer);
+ writer.close();
+ const reader = stream.readable.getReader();
+ const { value } = await reader.read();
+ println(`SharedArrayBuffer chunk: ${value.length} code units, starting with "${value.slice(0, 2)}"`);
+ done();
+ });
+</script>