Nacre/vendor/servo-script/dom/stream/readablestreambyobrequest.rs
astar76 0ecfd0f573 S031: root due timers in vendored servo-script (R5-01 SIGSEGV), crash-adblock regression smoke
fire_timer moved due timers into an unrooted Vec; a GC inside one callback swept
the functions of the timers still waiting. Heavy-web reruns and cargo test still pending.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-12 20:38:47 -06:00

125 lines
4.8 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom_struct::dom_struct;
use js::context::JSContext;
use js::gc::CustomAutoRooterGuard;
use js::typedarray::{ArrayBufferView, ArrayBufferViewU8};
use script_bindings::cell::DomRefCell;
use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
use script_bindings::trace::RootedTraceableBox;
use crate::dom::bindings::buffer_source::HeapBufferSource;
use crate::dom::bindings::codegen::Bindings::ReadableStreamBYOBRequestBinding::ReadableStreamBYOBRequestMethods;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::stream::readablebytestreamcontroller::ReadableByteStreamController;
use crate::dom::types::GlobalScope;
/// <https://streams.spec.whatwg.org/#readablestreambyobrequest>
#[dom_struct]
pub(crate) struct ReadableStreamBYOBRequest {
reflector_: Reflector,
controller: MutNullableDom<ReadableByteStreamController>,
#[ignore_malloc_size_of = "mozjs"]
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(
cx: &mut JSContext,
global: &GlobalScope,
) -> DomRoot<ReadableStreamBYOBRequest> {
reflect_dom_object_with_cx(Box::new(Self::new_inherited()), global, cx)
}
pub(crate) fn set_controller(&self, controller: Option<&ReadableByteStreamController>) {
self.controller.set(controller);
}
pub(crate) fn set_view(
&self,
view: Option<RootedTraceableBox<HeapBufferSource<ArrayBufferViewU8>>>,
) {
match view {
Some(view) => {
*self.view.borrow_mut() = *view.into_box();
},
None => {
*self.view.borrow_mut() = HeapBufferSource::<ArrayBufferViewU8>::default();
},
}
}
pub(crate) fn get_view(&self) -> RootedTraceableBox<HeapBufferSource<ArrayBufferViewU8>> {
RootedTraceableBox::new(self.view.borrow().clone())
}
}
impl ReadableStreamBYOBRequestMethods<crate::DomTypeHolder> for ReadableStreamBYOBRequest {
/// <https://streams.spec.whatwg.org/#rs-byob-request-view>
fn GetView(&self) -> Option<RootedTraceableBox<js::typedarray::HeapArrayBufferView>> {
// Return this.[[view]].
self.view.borrow().typed_array_to_option()
}
/// <https://streams.spec.whatwg.org/#rs-byob-request-respond>
fn Respond(&self, cx: &mut JSContext, bytes_written: u64) -> Fallible<()> {
// If this.[[controller]] is undefined, throw a TypeError exception.
let controller = if let Some(controller) = self.controller.get() {
controller
} else {
return Err(Error::Type(c"controller is undefined".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(c"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);
}
// Perform ? ReadableByteStreamControllerRespond(this.[[controller]], bytesWritten).
controller.respond(cx, bytes_written)
}
/// <https://streams.spec.whatwg.org/#rs-byob-request-respond-with-new-view>
fn RespondWithNewView(
&self,
cx: &mut JSContext,
view: CustomAutoRooterGuard<ArrayBufferView>,
) -> Fallible<()> {
let view = HeapBufferSource::<ArrayBufferViewU8>::from_view(cx, view);
// If this.[[controller]] is undefined, throw a TypeError exception.
let controller = if let Some(controller) = self.controller.get() {
controller
} else {
return Err(Error::Type(c"controller is undefined".to_owned()));
};
// If ! IsDetachedBuffer(view.[[ViewedArrayBuffer]]) is true, throw a TypeError exception.
if view.is_detached_buffer(cx) {
return Err(Error::Type(c"buffer is detached".to_owned()));
}
// Return ? ReadableByteStreamControllerRespondWithNewView(this.[[controller]], view).
controller.respond_with_new_view(cx, &view)
}
}