Backed out changeset 2450366cf7ca (bug 1891629) for causing win msix mochitest failures
[gecko.git] / dom / streams / ByteStreamHelpers.cpp
blob5022bb2c729a3a82df6d4f645c6921460a511e35
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/ByteStreamHelpers.h"
8 #include "mozilla/dom/ReadableByteStreamController.h"
9 #include "js/ArrayBuffer.h"
10 #include "js/RootingAPI.h"
11 #include "js/experimental/TypedData.h"
12 #include "mozilla/ErrorResult.h"
14 namespace mozilla::dom {
16 // https://streams.spec.whatwg.org/#transfer-array-buffer
17 // As some parts of the specifcation want to use the abrupt completion value,
18 // this function may leave a pending exception if it returns nullptr.
19 JSObject* TransferArrayBuffer(JSContext* aCx, JS::Handle<JSObject*> aObject) {
20 MOZ_ASSERT(JS::IsArrayBufferObject(aObject));
22 // Step 1.
23 MOZ_ASSERT(!JS::IsDetachedArrayBufferObject(aObject));
25 // Step 3 (Reordered)
26 size_t bufferLength = JS::GetArrayBufferByteLength(aObject);
28 // Step 2 (Reordered)
29 UniquePtr<void, JS::FreePolicy> bufferData{
30 JS::StealArrayBufferContents(aCx, aObject)};
32 // Step 4.
33 if (!JS::DetachArrayBuffer(aCx, aObject)) {
34 return nullptr;
37 // Step 5.
38 return JS::NewArrayBufferWithContents(aCx, bufferLength,
39 std::move(bufferData));
42 // https://streams.spec.whatwg.org/#can-transfer-array-buffer
43 bool CanTransferArrayBuffer(JSContext* aCx, JS::Handle<JSObject*> aObject,
44 ErrorResult& aRv) {
45 // Step 1. Assert: Type(O) is Object. (Implicit in types)
46 // Step 2. Assert: O has an [[ArrayBufferData]] internal slot.
47 MOZ_ASSERT(JS::IsArrayBufferObject(aObject));
49 // Step 3. If ! IsDetachedBuffer(O) is true, return false.
50 if (JS::IsDetachedArrayBufferObject(aObject)) {
51 return false;
54 // Step 4. If SameValue(O.[[ArrayBufferDetachKey]], undefined) is false,
55 // return false.
56 // Step 5. Return true.
57 // Note: WASM memories are the only buffers that would qualify
58 // as having an [[ArrayBufferDetachKey]] which is not undefined.
59 bool hasDefinedArrayBufferDetachKey = false;
60 if (!JS::HasDefinedArrayBufferDetachKey(aCx, aObject,
61 &hasDefinedArrayBufferDetachKey)) {
62 aRv.StealExceptionFromJSContext(aCx);
63 return false;
65 return !hasDefinedArrayBufferDetachKey;
68 // https://streams.spec.whatwg.org/#abstract-opdef-cloneasuint8array
69 JSObject* CloneAsUint8Array(JSContext* aCx, JS::Handle<JSObject*> aObject) {
70 // Step 1. Assert: Type(O) is Object. Implicit.
71 // Step 2. Assert: O has an [[ViewedArrayBuffer]] internal slot.
72 MOZ_ASSERT(JS_IsArrayBufferViewObject(aObject));
74 // Step 3. Assert: !IsDetachedBuffer(O.[[ViewedArrayBuffer]]) is false.
75 bool isShared;
76 JS::Rooted<JSObject*> viewedArrayBuffer(
77 aCx, JS_GetArrayBufferViewBuffer(aCx, aObject, &isShared));
78 if (!viewedArrayBuffer) {
79 return nullptr;
81 MOZ_ASSERT(!JS::IsDetachedArrayBufferObject(viewedArrayBuffer));
83 // Step 4. Let buffer be ?CloneArrayBuffer(O.[[ViewedArrayBuffer]],
84 // O.[[ByteOffset]], O.[[ByteLength]], %ArrayBuffer%).
85 size_t byteOffset = JS_GetTypedArrayByteOffset(aObject);
86 size_t byteLength = JS_GetTypedArrayByteLength(aObject);
87 JS::Rooted<JSObject*> buffer(
88 aCx,
89 JS::ArrayBufferClone(aCx, viewedArrayBuffer, byteOffset, byteLength));
90 if (!buffer) {
91 return nullptr;
94 // Step 5. Let array be ! Construct(%Uint8Array%, « buffer »).
95 JS::Rooted<JSObject*> array(
96 aCx, JS_NewUint8ArrayWithBuffer(aCx, buffer, 0,
97 static_cast<int64_t>(byteLength)));
98 if (!array) {
99 return nullptr;
102 // Step 6. Return array.
103 return array;
106 } // namespace mozilla::dom