ByteStreamHelpers.cpp (4171B)
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/. */ 6 7 #include "mozilla/dom/ByteStreamHelpers.h" 8 9 #include "js/ArrayBuffer.h" 10 #include "js/RootingAPI.h" 11 #include "js/experimental/TypedData.h" 12 #include "mozilla/ErrorResult.h" 13 #include "mozilla/dom/ReadableByteStreamController.h" 14 15 namespace mozilla::dom { 16 17 // https://streams.spec.whatwg.org/#transfer-array-buffer 18 // As some parts of the specifcation want to use the abrupt completion value, 19 // this function may leave a pending exception if it returns nullptr. 20 // 21 // This can be called with a CCW to an ArrayBuffer Object as we handle the 22 // case explicitly. 23 JSObject* TransferArrayBuffer(JSContext* aCx, JS::Handle<JSObject*> aObject) { 24 JS::Rooted<JSObject*> unwrappedObj(aCx, JS::UnwrapArrayBuffer(aObject)); 25 if (!unwrappedObj) { 26 js::ReportAccessDenied(aCx); 27 return nullptr; 28 } 29 30 size_t bufferLength = 0; 31 UniquePtr<void, JS::FreePolicy> bufferData; 32 { 33 JSAutoRealm ar(aCx, unwrappedObj); 34 35 // Step 1. 36 MOZ_ASSERT(!JS::IsDetachedArrayBufferObject(unwrappedObj)); 37 38 // Step 3 (Reordered) 39 bufferLength = JS::GetArrayBufferByteLength(unwrappedObj); 40 41 // Step 2 (Reordered) 42 bufferData.reset(JS::StealArrayBufferContents(aCx, unwrappedObj)); 43 44 // Step 4. 45 if (!JS::DetachArrayBuffer(aCx, unwrappedObj)) { 46 return nullptr; 47 } 48 } 49 50 // Step 5. 51 return JS::NewArrayBufferWithContents(aCx, bufferLength, 52 std::move(bufferData)); 53 } 54 55 // https://streams.spec.whatwg.org/#can-transfer-array-buffer 56 bool CanTransferArrayBuffer(JSContext* aCx, JS::Handle<JSObject*> aObject, 57 ErrorResult& aRv) { 58 // Step 1. Assert: Type(O) is Object. (Implicit in types) 59 // Step 2. Assert: O has an [[ArrayBufferData]] internal slot. 60 MOZ_ASSERT(JS::IsArrayBufferObject(aObject)); 61 62 // Step 3. If ! IsDetachedBuffer(O) is true, return false. 63 if (JS::IsDetachedArrayBufferObject(aObject)) { 64 return false; 65 } 66 67 // Step 4. If SameValue(O.[[ArrayBufferDetachKey]], undefined) is false, 68 // return false. 69 // Step 5. Return true. 70 // Note: WASM memories are the only buffers that would qualify 71 // as having an [[ArrayBufferDetachKey]] which is not undefined. 72 bool hasDefinedArrayBufferDetachKey = false; 73 if (!JS::HasDefinedArrayBufferDetachKey(aCx, aObject, 74 &hasDefinedArrayBufferDetachKey)) { 75 aRv.StealExceptionFromJSContext(aCx); 76 return false; 77 } 78 return !hasDefinedArrayBufferDetachKey; 79 } 80 81 // https://streams.spec.whatwg.org/#abstract-opdef-cloneasuint8array 82 JSObject* CloneAsUint8Array(JSContext* aCx, JS::Handle<JSObject*> aObject) { 83 // Step 1. Assert: Type(O) is Object. Implicit. 84 // Step 2. Assert: O has an [[ViewedArrayBuffer]] internal slot. 85 MOZ_ASSERT(JS_IsArrayBufferViewObject(aObject)); 86 87 // Step 3. Assert: !IsDetachedBuffer(O.[[ViewedArrayBuffer]]) is false. 88 bool isShared; 89 JS::Rooted<JSObject*> viewedArrayBuffer( 90 aCx, JS_GetArrayBufferViewBuffer(aCx, aObject, &isShared)); 91 if (!viewedArrayBuffer) { 92 return nullptr; 93 } 94 MOZ_ASSERT(!JS::IsDetachedArrayBufferObject(viewedArrayBuffer)); 95 96 // Step 4. Let buffer be ?CloneArrayBuffer(O.[[ViewedArrayBuffer]], 97 // O.[[ByteOffset]], O.[[ByteLength]], %ArrayBuffer%). 98 size_t byteOffset = JS_GetTypedArrayByteOffset(aObject); 99 size_t byteLength = JS_GetTypedArrayByteLength(aObject); 100 JS::Rooted<JSObject*> buffer( 101 aCx, 102 JS::ArrayBufferClone(aCx, viewedArrayBuffer, byteOffset, byteLength)); 103 if (!buffer) { 104 return nullptr; 105 } 106 107 // Step 5. Let array be ! Construct(%Uint8Array%, « buffer »). 108 JS::Rooted<JSObject*> array( 109 aCx, JS_NewUint8ArrayWithBuffer(aCx, buffer, 0, 110 static_cast<int64_t>(byteLength))); 111 if (!array) { 112 return nullptr; 113 } 114 115 // Step 6. Return array. 116 return array; 117 } 118 119 } // namespace mozilla::dom