Jelajahi Sumber

LibWeb: Implement ReadableByteStreamController.[[CancelSteps]]

Matthew Olsson 2 tahun lalu
induk
melakukan
c97f6b7701

+ 38 - 0
Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp

@@ -12,6 +12,7 @@
 #include <LibWeb/Streams/AbstractOperations.h>
 #include <LibWeb/Streams/ReadableByteStreamController.h>
 #include <LibWeb/Streams/ReadableStream.h>
+#include <LibWeb/Streams/ReadableStreamBYOBRequest.h>
 #include <LibWeb/Streams/ReadableStreamDefaultController.h>
 #include <LibWeb/Streams/ReadableStreamDefaultReader.h>
 #include <LibWeb/Streams/ReadableStreamGenericReader.h>
@@ -744,6 +745,26 @@ WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller_from_underly
     return set_up_readable_stream_default_controller(stream, controller, move(start_algorithm), move(pull_algorithm), move(cancel_algorithm), high_water_mark, move(size_algorithm));
 }
 
+// https://streams.spec.whatwg.org/#readable-byte-stream-controller-clear-algorithms
+void readable_byte_stream_controller_clear_algorithms(ReadableByteStreamController& controller)
+{
+    // 1. Set controller.[[pullAlgorithm]] to undefined.
+    controller.set_pull_algorithm({});
+
+    // 2. Set controller.[[cancelAlgorithm]] to undefined.
+    controller.set_cancel_algorithm({});
+}
+
+// https://streams.spec.whatwg.org/#readable-byte-stream-controller-clear-pending-pull-intos
+void readable_byte_stream_controller_clear_pending_pull_intos(ReadableByteStreamController& controller)
+{
+    // 1. Perform ! ReadableByteStreamControllerInvalidateBYOBRequest(controller).
+    readable_byte_stream_controller_invalidate_byob_request(controller);
+
+    // 2. Set controller.[[pendingPullIntos]] to a new empty list.
+    controller.pending_pull_intos().clear();
+}
+
 // https://streams.spec.whatwg.org/#readable-byte-stream-controller-get-desired-size
 Optional<double> readable_byte_stream_controller_get_desired_size(ReadableByteStreamController const& controller)
 {
@@ -762,6 +783,23 @@ Optional<double> readable_byte_stream_controller_get_desired_size(ReadableByteSt
     return controller.strategy_hwm() - controller.queue_total_size();
 }
 
+// https://streams.spec.whatwg.org/#readable-byte-stream-controller-invalidate-byob-request
+void readable_byte_stream_controller_invalidate_byob_request(ReadableByteStreamController& controller)
+{
+    // 1. If controller.[[byobRequest]] is null, return.
+    if (!controller.byob_request())
+        return;
+
+    // 2. Set controller.[[byobRequest]].[[controller]] to undefined.
+    controller.byob_request()->set_controller({});
+
+    // 3. Set controller.[[byobRequest]].[[view]] to null.
+    controller.byob_request()->set_view({});
+
+    // 4. Set controller.[[byobRequest]] to null.
+    controller.set_byob_request({});
+}
+
 // https://streams.spec.whatwg.org/#acquire-writable-stream-default-writer
 WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStreamDefaultWriter>> acquire_writable_stream_default_writer(WritableStream& stream)
 {

+ 3 - 0
Userland/Libraries/LibWeb/Streams/AbstractOperations.h

@@ -53,7 +53,10 @@ bool readable_stream_default_controller_can_close_or_enqueue(ReadableStreamDefau
 WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller(ReadableStream&, ReadableStreamDefaultController&, StartAlgorithm&&, PullAlgorithm&&, CancelAlgorithm&&, double high_water_mark, SizeAlgorithm&&);
 WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller_from_underlying_source(ReadableStream&, JS::Value underlying_source_value, UnderlyingSource, double high_water_mark, SizeAlgorithm&&);
 
+void readable_byte_stream_controller_clear_algorithms(ReadableByteStreamController&);
+void readable_byte_stream_controller_clear_pending_pull_intos(ReadableByteStreamController&);
 Optional<double> readable_byte_stream_controller_get_desired_size(ReadableByteStreamController const&);
+void readable_byte_stream_controller_invalidate_byob_request(ReadableByteStreamController&);
 
 WebIDL::ExceptionOr<JS::NonnullGCPtr<WritableStreamDefaultWriter>> acquire_writable_stream_default_writer(WritableStream&);
 bool is_writable_stream_locked(WritableStream const&);

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

@@ -22,6 +22,25 @@ ReadableByteStreamController::ReadableByteStreamController(JS::Realm& realm)
 {
 }
 
+// https://streams.spec.whatwg.org/#rbs-controller-private-cancel
+WebIDL::ExceptionOr<JS::GCPtr<WebIDL::Promise>> ReadableByteStreamController::cancel_steps(JS::Value reason)
+{
+    // 1. Perform ! ReadableByteStreamControllerClearPendingPullIntos(this).
+    readable_byte_stream_controller_clear_pending_pull_intos(*this);
+
+    // 2. Perform ! ResetQueue(this).
+    reset_queue(*this);
+
+    // 3. Let result be the result of performing this.[[cancelAlgorithm]], passing in reason.
+    auto result = (*m_cancel_algorithm)(reason);
+
+    // 4. Perform ! ReadableByteStreamControllerClearAlgorithms(this).
+    readable_byte_stream_controller_clear_algorithms(*this);
+
+    // 5. Return result.
+    return result;
+}
+
 void ReadableByteStreamController::visit_edges(Cell::Visitor& visitor)
 {
     Base::visit_edges(visitor);

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

@@ -116,6 +116,8 @@ public:
     JS::GCPtr<ReadableStream> stream() { return m_stream; }
     void set_stream(JS::GCPtr<ReadableStream> stream) { m_stream = stream; }
 
+    WebIDL::ExceptionOr<JS::GCPtr<WebIDL::Promise>> cancel_steps(JS::Value reason);
+
 private:
     explicit ReadableByteStreamController(JS::Realm&);
 

+ 6 - 0
Userland/Libraries/LibWeb/Streams/ReadableStreamBYOBRequest.h

@@ -8,6 +8,8 @@
 
 #include <LibJS/Runtime/TypedArray.h>
 #include <LibWeb/Bindings/PlatformObject.h>
+#include <LibWeb/Forward.h>
+#include <LibWeb/Streams/ReadableByteStreamController.h>
 
 namespace Web::Streams {
 
@@ -20,6 +22,10 @@ public:
 
     JS::GCPtr<JS::TypedArrayBase> view();
 
+    void set_controller(JS::GCPtr<ReadableByteStreamController> value) { m_controller = value; }
+
+    void set_view(JS::GCPtr<JS::TypedArrayBase> value) { m_view = value; }
+
 private:
     explicit ReadableStreamBYOBRequest(JS::Realm&);