Skip to main content

script/dom/stream/
readablestreambyobrequest.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5use dom_struct::dom_struct;
6use js::context::JSContext;
7use js::gc::CustomAutoRooterGuard;
8use js::typedarray::{ArrayBufferView, ArrayBufferViewU8};
9use script_bindings::cell::DomRefCell;
10use script_bindings::reflector::{Reflector, reflect_dom_object_with_cx};
11use script_bindings::trace::RootedTraceableBox;
12
13use crate::dom::bindings::buffer_source::HeapBufferSource;
14use crate::dom::bindings::codegen::Bindings::ReadableStreamBYOBRequestBinding::ReadableStreamBYOBRequestMethods;
15use crate::dom::bindings::error::{Error, Fallible};
16use crate::dom::bindings::root::{DomRoot, MutNullableDom};
17use crate::dom::stream::readablebytestreamcontroller::ReadableByteStreamController;
18use crate::dom::types::GlobalScope;
19
20/// <https://streams.spec.whatwg.org/#readablestreambyobrequest>
21#[dom_struct]
22pub(crate) struct ReadableStreamBYOBRequest {
23    reflector_: Reflector,
24    controller: MutNullableDom<ReadableByteStreamController>,
25    #[ignore_malloc_size_of = "mozjs"]
26    view: DomRefCell<HeapBufferSource<ArrayBufferViewU8>>,
27}
28
29impl ReadableStreamBYOBRequest {
30    fn new_inherited() -> ReadableStreamBYOBRequest {
31        ReadableStreamBYOBRequest {
32            reflector_: Reflector::new(),
33            controller: MutNullableDom::new(None),
34            view: DomRefCell::new(HeapBufferSource::<ArrayBufferViewU8>::default()),
35        }
36    }
37
38    pub(crate) fn new(
39        cx: &mut JSContext,
40        global: &GlobalScope,
41    ) -> DomRoot<ReadableStreamBYOBRequest> {
42        reflect_dom_object_with_cx(Box::new(Self::new_inherited()), global, cx)
43    }
44
45    pub(crate) fn set_controller(&self, controller: Option<&ReadableByteStreamController>) {
46        self.controller.set(controller);
47    }
48
49    pub(crate) fn set_view(
50        &self,
51        view: Option<RootedTraceableBox<HeapBufferSource<ArrayBufferViewU8>>>,
52    ) {
53        match view {
54            Some(view) => {
55                *self.view.borrow_mut() = *view.into_box();
56            },
57            None => {
58                *self.view.borrow_mut() = HeapBufferSource::<ArrayBufferViewU8>::default();
59            },
60        }
61    }
62
63    pub(crate) fn get_view(&self) -> RootedTraceableBox<HeapBufferSource<ArrayBufferViewU8>> {
64        RootedTraceableBox::new(self.view.borrow().clone())
65    }
66}
67
68impl ReadableStreamBYOBRequestMethods<crate::DomTypeHolder> for ReadableStreamBYOBRequest {
69    /// <https://streams.spec.whatwg.org/#rs-byob-request-view>
70    fn GetView(&self) -> Option<RootedTraceableBox<js::typedarray::HeapArrayBufferView>> {
71        // Return this.[[view]].
72        self.view.borrow().typed_array_to_option()
73    }
74
75    /// <https://streams.spec.whatwg.org/#rs-byob-request-respond>
76    fn Respond(&self, cx: &mut JSContext, bytes_written: u64) -> Fallible<()> {
77        // If this.[[controller]] is undefined, throw a TypeError exception.
78        let controller = if let Some(controller) = self.controller.get() {
79            controller
80        } else {
81            return Err(Error::Type(c"controller is undefined".to_owned()));
82        };
83
84        {
85            let view = self.view.borrow();
86            // If ! IsDetachedBuffer(this.[[view]].[[ArrayBuffer]]) is true, throw a TypeError exception.
87            if view.get_array_buffer_view_buffer(cx).is_detached_buffer(cx) {
88                return Err(Error::Type(c"buffer is detached".to_owned()));
89            }
90
91            // Assert: this.[[view]].[[ByteLength]] > 0.
92            assert!(view.byte_length() > 0);
93
94            // Assert: this.[[view]].[[ViewedArrayBuffer]].[[ByteLength]] > 0.
95            assert!(view.viewed_buffer_array_byte_length(cx) > 0);
96        }
97
98        // Perform ? ReadableByteStreamControllerRespond(this.[[controller]], bytesWritten).
99        controller.respond(cx, bytes_written)
100    }
101
102    /// <https://streams.spec.whatwg.org/#rs-byob-request-respond-with-new-view>
103    fn RespondWithNewView(
104        &self,
105        cx: &mut JSContext,
106        view: CustomAutoRooterGuard<ArrayBufferView>,
107    ) -> Fallible<()> {
108        let view = HeapBufferSource::<ArrayBufferViewU8>::from_view(cx, view);
109
110        // If this.[[controller]] is undefined, throw a TypeError exception.
111        let controller = if let Some(controller) = self.controller.get() {
112            controller
113        } else {
114            return Err(Error::Type(c"controller is undefined".to_owned()));
115        };
116
117        // If ! IsDetachedBuffer(view.[[ViewedArrayBuffer]]) is true, throw a TypeError exception.
118        if view.is_detached_buffer(cx) {
119            return Err(Error::Type(c"buffer is detached".to_owned()));
120        }
121
122        // Return ? ReadableByteStreamControllerRespondWithNewView(this.[[controller]], view).
123        controller.respond_with_new_view(cx, &view)
124    }
125}