ReadableStream: remove the use of get_js_stream and use DomRoot<ReadableStream> (#34836)

* Remove the use of get_js_stream and use DomRoot<ReadableStream>

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

* return an error instead of Option

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

---------

Signed-off-by: Taym Haddadi <haddadi.taym@gmail.com>
This commit is contained in:
Taym Haddadi 2025-01-05 12:37:15 +01:00 committed by GitHub
parent 15eb405f36
commit bcad0d50e9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 43 additions and 52 deletions

View file

@ -3,7 +3,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::cell::Cell;
use std::ptr::{self, NonNull};
use std::ptr::{self};
use std::rc::Rc;
use dom_struct::dom_struct;
@ -239,15 +239,15 @@ impl ReadableStream {
global: &GlobalScope,
bytes: Vec<u8>,
can_gc: CanGc,
) -> DomRoot<ReadableStream> {
) -> Fallible<DomRoot<ReadableStream>> {
let stream = ReadableStream::new_with_external_underlying_source(
global,
UnderlyingSourceType::Memory(bytes.len()),
can_gc,
);
)?;
stream.enqueue_native(bytes);
stream.controller_close_native();
stream
Ok(stream)
}
/// Build a stream backed by a Rust underlying source.
@ -257,7 +257,7 @@ impl ReadableStream {
global: &GlobalScope,
source: UnderlyingSourceType,
can_gc: CanGc,
) -> DomRoot<ReadableStream> {
) -> Fallible<DomRoot<ReadableStream>> {
assert!(source.is_native());
let stream = ReadableStream::new_with_proto(
global,
@ -272,10 +272,8 @@ impl ReadableStream {
extract_size_algorithm(&QueuingStrategy::empty()),
can_gc,
);
controller
.setup(stream.clone(), can_gc)
.expect("Setup of controller with external underlying source cannot fail");
stream
controller.setup(stream.clone(), can_gc)?;
Ok(stream)
}
/// Call into the release steps of the controller,
@ -323,13 +321,6 @@ impl ReadableStream {
}
}
/// Get a pointer to the underlying JS object.
/// TODO: remove,
/// by using at call point the `ReadableStream` directly instead of a JSObject.
pub fn get_js_stream(&self) -> NonNull<JSObject> {
NonNull::new(*self.reflector().get_jsobject())
.expect("Couldn't get a non-null pointer to JS stream object.")
}
/// Endpoint to enqueue chunks directly from Rust.
/// Note: in other use cases this call happens via the controller.
pub fn enqueue_native(&self, bytes: Vec<u8>) {