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