1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/ReadableStreamBYOBRequest.h"
9 #include "mozilla/dom/ByteStreamHelpers.h"
10 #include "js/ArrayBuffer.h"
11 #include "js/TypeDecls.h"
12 #include "mozilla/dom/ReadableByteStreamController.h"
13 #include "mozilla/dom/ReadableStream.h"
14 #include "mozilla/dom/ReadableStreamBYOBRequestBinding.h"
15 #include "js/experimental/TypedData.h"
16 #include "mozilla/dom/ReadableStreamController.h"
18 #include "nsIGlobalObject.h"
19 #include "nsWrapperCache.h"
21 namespace mozilla::dom
{
23 ReadableStreamBYOBRequest::ReadableStreamBYOBRequest(nsIGlobalObject
* aGlobal
)
25 mozilla::HoldJSObjects(this);
28 ReadableStreamBYOBRequest::~ReadableStreamBYOBRequest() {
29 mozilla::DropJSObjects(this);
32 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WITH_JS_MEMBERS(ReadableStreamBYOBRequest
,
33 (mGlobal
, mController
),
36 NS_IMPL_CYCLE_COLLECTING_ADDREF(ReadableStreamBYOBRequest
)
37 NS_IMPL_CYCLE_COLLECTING_RELEASE(ReadableStreamBYOBRequest
)
39 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ReadableStreamBYOBRequest
)
40 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
41 NS_INTERFACE_MAP_ENTRY(nsISupports
)
44 JSObject
* ReadableStreamBYOBRequest::WrapObject(
45 JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
) {
46 return ReadableStreamBYOBRequest_Binding::Wrap(aCx
, this, aGivenProto
);
49 // https://streams.spec.whatwg.org/#rs-byob-request-view
50 void ReadableStreamBYOBRequest::GetView(
51 JSContext
* cx
, JS::MutableHandle
<JSObject
*> aRetVal
) const {
56 // https://streams.spec.whatwg.org/#rs-byob-request-respond
57 void ReadableStreamBYOBRequest::Respond(JSContext
* aCx
, uint64_t bytesWritten
,
61 aRv
.ThrowTypeError("Undefined Controller");
67 JS::Rooted
<JSObject
*> view(aCx
, mView
);
68 JS::Rooted
<JSObject
*> arrayBuffer(
69 aCx
, JS_GetArrayBufferViewBuffer(aCx
, view
, &isSharedMemory
));
71 aRv
.StealExceptionFromJSContext(aCx
);
75 if (JS::IsDetachedArrayBufferObject(arrayBuffer
)) {
76 aRv
.ThrowTypeError("View of Detached buffer");
81 MOZ_ASSERT(JS_GetArrayBufferViewByteLength(view
) > 0);
84 MOZ_ASSERT(JS::GetArrayBufferByteLength(arrayBuffer
) > 0);
87 RefPtr
<ReadableByteStreamController
> controller(mController
);
88 ReadableByteStreamControllerRespond(aCx
, controller
, bytesWritten
, aRv
);
91 // https://streams.spec.whatwg.org/#rs-byob-request-respond-with-new-view
92 void ReadableStreamBYOBRequest::RespondWithNewView(JSContext
* aCx
,
93 const ArrayBufferView
& view
,
97 aRv
.ThrowTypeError("Undefined Controller");
103 JS::Rooted
<JSObject
*> rootedViewObj(aCx
, view
.Obj());
104 JS::Rooted
<JSObject
*> viewedArrayBuffer(
105 aCx
, JS_GetArrayBufferViewBuffer(aCx
, rootedViewObj
, &isSharedMemory
));
106 if (!viewedArrayBuffer
) {
107 aRv
.StealExceptionFromJSContext(aCx
);
111 if (JS::IsDetachedArrayBufferObject(viewedArrayBuffer
)) {
112 aRv
.ThrowTypeError("View of Detatched Array Buffer");
117 RefPtr
<ReadableByteStreamController
> controller(mController
);
118 ReadableByteStreamControllerRespondWithNewView(aCx
, controller
, rootedViewObj
,
122 void ReadableStreamBYOBRequest::SetController(
123 ReadableByteStreamController
* aController
) {
124 mController
= aController
;
127 } // namespace mozilla::dom