script: implement ReadableByteStreamController (#35410)

* script: implement ReadableByteStreamController

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* implement can_copy_data_block_bytes and copy_data_block_bytes

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Remove BufferSource::Default

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* implement StartAlgorithmFulfillmentHandler, StartAlgorithmRejectionHandler, PullAlgorithmFulfillmentHandler, PullAlgorithmRejectionHandler for ReadableByteStreamController

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* implement perform_pull_into

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* fix build

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Fix clippy

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Fix build

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Remove RefCell from PullIntoDescriptor and QueueEntry

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Remove commented code

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* add perform_cancel_steps, perform_release_steps and perform_pull_steps

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* fix clippy

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* fix crown

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* unskip readable-byte-streams

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Fix CRASH

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* fix clippy

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Fix more CRASHS

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* fix more crashes

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Fix bad-buffers-and-views.any.js test

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Update test expectations

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Fix BorrowMutError crashes

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* fix view_byte_length  test

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Fix non-transferable-buffers test

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Pass contexts as much as possible by reference

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Make respond_internal Fallible

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Fix crwon

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* fix process pull into descriptors using queue logic and resulting double-borrow

Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com>

* Fix clippy

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* FIx more crashes

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Fix timeout tests

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Fix all tests

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Remove all error! logs

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Remove  #[allow(unsafe_code)]

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Fix lint
Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Fix tidy

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

* Fix test expectation

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>

---------

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>
Signed-off-by: gterzian <2792687+gterzian@users.noreply.github.com>
Co-authored-by: gterzian <2792687+gterzian@users.noreply.github.com>
This commit is contained in:
Taym Haddadi 2025-03-10 12:43:04 -07:00 committed by GitHub
parent 459aee27b6
commit a5cf04c479
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 3410 additions and 324 deletions

View file

@ -4,17 +4,19 @@
use dom_struct::dom_struct;
use js::gc::CustomAutoRooterGuard;
use js::jsapi::Heap;
use js::typedarray::{ArrayBufferView, ArrayBufferViewU8};
use super::bindings::buffer_source::{BufferSource, HeapBufferSource};
use super::bindings::buffer_source::HeapBufferSource;
use super::bindings::cell::DomRefCell;
use super::bindings::reflector::reflect_dom_object;
use super::bindings::root::DomRoot;
use crate::dom::bindings::codegen::Bindings::ReadableStreamBYOBRequestBinding::ReadableStreamBYOBRequestMethods;
use crate::dom::bindings::import::module::{Error, Fallible};
use crate::dom::bindings::reflector::Reflector;
use crate::dom::bindings::root::MutNullableDom;
use crate::dom::readablebytestreamcontroller::ReadableByteStreamController;
use crate::dom::types::GlobalScope;
use crate::script_runtime::JSContext as SafeJSContext;
use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
/// <https://streams.spec.whatwg.org/#readablestreambyobrequest>
#[dom_struct]
@ -22,18 +24,47 @@ pub(crate) struct ReadableStreamBYOBRequest {
reflector_: Reflector,
controller: MutNullableDom<ReadableByteStreamController>,
#[ignore_malloc_size_of = "mozjs"]
view: HeapBufferSource<ArrayBufferViewU8>,
view: DomRefCell<HeapBufferSource<ArrayBufferViewU8>>,
}
impl ReadableStreamBYOBRequest {
fn new_inherited() -> ReadableStreamBYOBRequest {
ReadableStreamBYOBRequest {
reflector_: Reflector::new(),
controller: MutNullableDom::new(None),
view: DomRefCell::new(HeapBufferSource::<ArrayBufferViewU8>::default()),
}
}
pub(crate) fn new(global: &GlobalScope, can_gc: CanGc) -> DomRoot<ReadableStreamBYOBRequest> {
reflect_dom_object(Box::new(Self::new_inherited()), global, can_gc)
}
pub(crate) fn set_controller(&self, controller: Option<&ReadableByteStreamController>) {
self.controller.set(controller);
}
pub(crate) fn set_view(&self, view: Option<HeapBufferSource<ArrayBufferViewU8>>) {
match view {
Some(view) => {
*self.view.borrow_mut() = view;
},
None => {
*self.view.borrow_mut() = HeapBufferSource::<ArrayBufferViewU8>::default();
},
}
}
}
impl ReadableStreamBYOBRequestMethods<crate::DomTypeHolder> for ReadableStreamBYOBRequest {
/// <https://streams.spec.whatwg.org/#rs-byob-request-view>
fn GetView(&self, _cx: SafeJSContext) -> Option<js::typedarray::ArrayBufferView> {
// Return this.[[view]].
self.view.buffer_to_option()
self.view.borrow().typed_array_to_option()
}
/// <https://streams.spec.whatwg.org/#rs-byob-request-respond>
fn Respond(&self, bytes_written: u64) -> Fallible<()> {
fn Respond(&self, bytes_written: u64, can_gc: CanGc) -> Fallible<()> {
let cx = GlobalScope::get_cx();
// If this.[[controller]] is undefined, throw a TypeError exception.
@ -43,27 +74,32 @@ impl ReadableStreamBYOBRequestMethods<crate::DomTypeHolder> for ReadableStreamBY
return Err(Error::Type("controller is undefined".to_owned()));
};
// If ! IsDetachedBuffer(this.[[view]].[[ArrayBuffer]]) is true, throw a TypeError exception.
if self.view.is_detached_buffer(cx) {
return Err(Error::Type("buffer is detached".to_owned()));
{
let view = self.view.borrow();
// If ! IsDetachedBuffer(this.[[view]].[[ArrayBuffer]]) is true, throw a TypeError exception.
if view.get_array_buffer_view_buffer(cx).is_detached_buffer(cx) {
return Err(Error::Type("buffer is detached".to_owned()));
}
// Assert: this.[[view]].[[ByteLength]] > 0.
assert!(view.byte_length() > 0);
// Assert: this.[[view]].[[ViewedArrayBuffer]].[[ByteLength]] > 0.
assert!(view.viewed_buffer_array_byte_length(cx) > 0);
}
// Assert: this.[[view]].[[ByteLength]] > 0.
assert!(self.view.byte_length() > 0);
// Assert: this.[[view]].[[ViewedArrayBuffer]].[[ByteLength]] > 0.
assert!(self.view.viewed_buffer_array_byte_length(cx) > 0);
// Perform ? ReadableByteStreamControllerRespond(this.[[controller]], bytesWritten).
controller.respond(bytes_written)
controller.respond(cx, bytes_written, can_gc)
}
/// <https://streams.spec.whatwg.org/#rs-byob-request-respond-with-new-view>
#[allow(unsafe_code)]
fn RespondWithNewView(&self, view: CustomAutoRooterGuard<ArrayBufferView>) -> Fallible<()> {
let view = HeapBufferSource::<ArrayBufferViewU8>::new(BufferSource::ArrayBufferView(
Heap::boxed(unsafe { *view.underlying_object() }),
));
fn RespondWithNewView(
&self,
view: CustomAutoRooterGuard<ArrayBufferView>,
can_gc: CanGc,
) -> Fallible<()> {
let cx = GlobalScope::get_cx();
let view = HeapBufferSource::<ArrayBufferViewU8>::from_view(view);
// If this.[[controller]] is undefined, throw a TypeError exception.
let controller = if let Some(controller) = self.controller.get() {
@ -73,11 +109,11 @@ impl ReadableStreamBYOBRequestMethods<crate::DomTypeHolder> for ReadableStreamBY
};
// If ! IsDetachedBuffer(view.[[ViewedArrayBuffer]]) is true, throw a TypeError exception.
if self.view.is_detached_buffer(GlobalScope::get_cx()) {
if view.is_detached_buffer(cx) {
return Err(Error::Type("buffer is detached".to_owned()));
}
// Return ? ReadableByteStreamControllerRespondWithNewView(this.[[controller]], view).
controller.respond_with_new_view(view)
controller.respond_with_new_view(cx, view, can_gc)
}
}