ソースを参照

LibWeb: Implement ReadableByteStreamController.enqueue

Shannon Booth 1 年間 前
コミット
48aa9fbe07

+ 21 - 0
Userland/Libraries/LibWeb/Streams/ReadableByteStreamController.cpp

@@ -11,6 +11,7 @@
 #include <LibWeb/Streams/ReadableStream.h>
 #include <LibWeb/Streams/ReadableStream.h>
 #include <LibWeb/Streams/ReadableStreamBYOBRequest.h>
 #include <LibWeb/Streams/ReadableStreamBYOBRequest.h>
 #include <LibWeb/Streams/ReadableStreamDefaultReader.h>
 #include <LibWeb/Streams/ReadableStreamDefaultReader.h>
+#include <LibWeb/WebIDL/Buffers.h>
 
 
 namespace Web::Streams {
 namespace Web::Streams {
 
 
@@ -67,6 +68,26 @@ void ReadableByteStreamController::initialize(JS::Realm& realm)
     set_prototype(&Bindings::ensure_web_prototype<Bindings::ReadableByteStreamControllerPrototype>(realm, "ReadableByteStreamController"_fly_string));
     set_prototype(&Bindings::ensure_web_prototype<Bindings::ReadableByteStreamControllerPrototype>(realm, "ReadableByteStreamController"_fly_string));
 }
 }
 
 
+// https://streams.spec.whatwg.org/#rbs-controller-enqueue
+WebIDL::ExceptionOr<void> ReadableByteStreamController::enqueue(JS::Handle<WebIDL::ArrayBufferView>& chunk)
+{
+    // 1. If chunk.[[ByteLength]] is 0, throw a TypeError exception.
+    // 2. If chunk.[[ViewedArrayBuffer]].[[ArrayBufferByteLength]] is 0, throw a TypeError exception.
+    if (chunk->byte_length() == 0 || chunk->viewed_array_buffer()->byte_length() == 0)
+        return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Cannot enqueue chunk with byte length of zero"sv };
+
+    // 3. If this.[[closeRequested]] is true, throw a TypeError exception.
+    if (m_close_requested)
+        return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Close is requested for controller"sv };
+
+    // 4. If this.[[stream]].[[state]] is not "readable", throw a TypeError exception.
+    if (!m_stream->is_readable())
+        return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Stream is not readable"sv };
+
+    // 5. Return ? ReadableByteStreamControllerEnqueue(this, chunk).
+    return readable_byte_stream_controller_enqueue(*this, chunk->raw_object());
+}
+
 // https://streams.spec.whatwg.org/#rbs-controller-private-cancel
 // https://streams.spec.whatwg.org/#rbs-controller-private-cancel
 WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> ReadableByteStreamController::cancel_steps(JS::Value reason)
 WebIDL::ExceptionOr<JS::NonnullGCPtr<WebIDL::Promise>> ReadableByteStreamController::cancel_steps(JS::Value reason)
 {
 {

+ 1 - 0
Userland/Libraries/LibWeb/Streams/ReadableByteStreamController.h

@@ -89,6 +89,7 @@ public:
     Optional<double> desired_size() const;
     Optional<double> desired_size() const;
     WebIDL::ExceptionOr<void> close();
     WebIDL::ExceptionOr<void> close();
     void error(JS::Value error);
     void error(JS::Value error);
+    WebIDL::ExceptionOr<void> enqueue(JS::Handle<WebIDL::ArrayBufferView>&);
 
 
     Optional<u64> const& auto_allocate_chunk_size() { return m_auto_allocate_chunk_size; }
     Optional<u64> const& auto_allocate_chunk_size() { return m_auto_allocate_chunk_size; }
     void set_auto_allocate_chunk_size(Optional<u64> value) { m_auto_allocate_chunk_size = value; }
     void set_auto_allocate_chunk_size(Optional<u64> value) { m_auto_allocate_chunk_size = value; }

+ 1 - 1
Userland/Libraries/LibWeb/Streams/ReadableByteStreamController.idl

@@ -8,5 +8,5 @@ interface ReadableByteStreamController {
 
 
     undefined close();
     undefined close();
     undefined error(optional any e);
     undefined error(optional any e);
-    // FIXME: undefined enqueue(ArrayBufferView chunk);
+    undefined enqueue(ArrayBufferView chunk);
 };
 };