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::gc::CustomAutoRooterGuard;
7use js::typedarray::{ArrayBufferView, ArrayBufferViewU8};
8use script_bindings::trace::RootedTraceableBox;
9
10use crate::dom::bindings::buffer_source::HeapBufferSource;
11use crate::dom::bindings::cell::DomRefCell;
12use crate::dom::bindings::codegen::Bindings::ReadableStreamBYOBRequestBinding::ReadableStreamBYOBRequestMethods;
13use crate::dom::bindings::error::{Error, Fallible};
14use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
15use crate::dom::bindings::root::{DomRoot, MutNullableDom};
16use crate::dom::stream::readablebytestreamcontroller::ReadableByteStreamController;
17use crate::dom::types::GlobalScope;
18use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
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(global: &GlobalScope, can_gc: CanGc) -> DomRoot<ReadableStreamBYOBRequest> {
39        reflect_dom_object(Box::new(Self::new_inherited()), global, can_gc)
40    }
41
42    pub(crate) fn set_controller(&self, controller: Option<&ReadableByteStreamController>) {
43        self.controller.set(controller);
44    }
45
46    pub(crate) fn set_view(&self, view: Option<HeapBufferSource<ArrayBufferViewU8>>) {
47        match view {
48            Some(view) => {
49                *self.view.borrow_mut() = view;
50            },
51            None => {
52                *self.view.borrow_mut() = HeapBufferSource::<ArrayBufferViewU8>::default();
53            },
54        }
55    }
56
57    pub(crate) fn get_view(&self) -> HeapBufferSource<ArrayBufferViewU8> {
58        self.view.borrow().clone()
59    }
60}
61
62impl ReadableStreamBYOBRequestMethods<crate::DomTypeHolder> for ReadableStreamBYOBRequest {
63    /// <https://streams.spec.whatwg.org/#rs-byob-request-view>
64    fn GetView(
65        &self,
66        _cx: SafeJSContext,
67    ) -> Option<RootedTraceableBox<js::typedarray::HeapArrayBufferView>> {
68        // Return this.[[view]].
69        self.view.borrow().typed_array_to_option()
70    }
71
72    /// <https://streams.spec.whatwg.org/#rs-byob-request-respond>
73    fn Respond(&self, bytes_written: u64, can_gc: CanGc) -> Fallible<()> {
74        let cx = GlobalScope::get_cx();
75
76        // If this.[[controller]] is undefined, throw a TypeError exception.
77        let controller = if let Some(controller) = self.controller.get() {
78            controller
79        } else {
80            return Err(Error::Type("controller is undefined".to_owned()));
81        };
82
83        {
84            let view = self.view.borrow();
85            // If ! IsDetachedBuffer(this.[[view]].[[ArrayBuffer]]) is true, throw a TypeError exception.
86            if view.get_array_buffer_view_buffer(cx).is_detached_buffer(cx) {
87                return Err(Error::Type("buffer is detached".to_owned()));
88            }
89
90            // Assert: this.[[view]].[[ByteLength]] > 0.
91            assert!(view.byte_length() > 0);
92
93            // Assert: this.[[view]].[[ViewedArrayBuffer]].[[ByteLength]] > 0.
94            assert!(view.viewed_buffer_array_byte_length(cx) > 0);
95        }
96
97        // Perform ? ReadableByteStreamControllerRespond(this.[[controller]], bytesWritten).
98        controller.respond(cx, bytes_written, can_gc)
99    }
100
101    /// <https://streams.spec.whatwg.org/#rs-byob-request-respond-with-new-view>
102    fn RespondWithNewView(
103        &self,
104        view: CustomAutoRooterGuard<ArrayBufferView>,
105        can_gc: CanGc,
106    ) -> Fallible<()> {
107        let cx = GlobalScope::get_cx();
108        let view = HeapBufferSource::<ArrayBufferViewU8>::from_view(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("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("buffer is detached".to_owned()));
120        }
121
122        // Return ? ReadableByteStreamControllerRespondWithNewView(this.[[controller]], view).
123        controller.respond_with_new_view(cx, view, can_gc)
124    }
125}