File: ByteStreamHelpers.cpp

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (119 lines) | stat: -rw-r--r-- 4,171 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/ByteStreamHelpers.h"

#include "js/ArrayBuffer.h"
#include "js/RootingAPI.h"
#include "js/experimental/TypedData.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/dom/ReadableByteStreamController.h"

namespace mozilla::dom {

// https://streams.spec.whatwg.org/#transfer-array-buffer
// As some parts of the specifcation want to use the abrupt completion value,
// this function may leave a pending exception if it returns nullptr.
//
// This can be called with a CCW to an ArrayBuffer Object as we handle the
// case explicitly.
JSObject* TransferArrayBuffer(JSContext* aCx, JS::Handle<JSObject*> aObject) {
  JS::Rooted<JSObject*> unwrappedObj(aCx, JS::UnwrapArrayBuffer(aObject));
  if (!unwrappedObj) {
    js::ReportAccessDenied(aCx);
    return nullptr;
  }

  size_t bufferLength = 0;
  UniquePtr<void, JS::FreePolicy> bufferData;
  {
    JSAutoRealm ar(aCx, unwrappedObj);

    // Step 1.
    MOZ_ASSERT(!JS::IsDetachedArrayBufferObject(unwrappedObj));

    // Step 3 (Reordered)
    bufferLength = JS::GetArrayBufferByteLength(unwrappedObj);

    // Step 2 (Reordered)
    bufferData.reset(JS::StealArrayBufferContents(aCx, unwrappedObj));

    // Step 4.
    if (!JS::DetachArrayBuffer(aCx, unwrappedObj)) {
      return nullptr;
    }
  }

  // Step 5.
  return JS::NewArrayBufferWithContents(aCx, bufferLength,
                                        std::move(bufferData));
}

// https://streams.spec.whatwg.org/#can-transfer-array-buffer
bool CanTransferArrayBuffer(JSContext* aCx, JS::Handle<JSObject*> aObject,
                            ErrorResult& aRv) {
  // Step 1. Assert: Type(O) is Object. (Implicit in types)
  // Step 2. Assert: O has an [[ArrayBufferData]] internal slot.
  MOZ_ASSERT(JS::IsArrayBufferObject(aObject));

  // Step 3. If ! IsDetachedBuffer(O) is true, return false.
  if (JS::IsDetachedArrayBufferObject(aObject)) {
    return false;
  }

  // Step 4. If SameValue(O.[[ArrayBufferDetachKey]], undefined) is false,
  // return false.
  // Step 5. Return true.
  // Note: WASM memories are the only buffers that would qualify
  // as having an [[ArrayBufferDetachKey]] which is not undefined.
  bool hasDefinedArrayBufferDetachKey = false;
  if (!JS::HasDefinedArrayBufferDetachKey(aCx, aObject,
                                          &hasDefinedArrayBufferDetachKey)) {
    aRv.StealExceptionFromJSContext(aCx);
    return false;
  }
  return !hasDefinedArrayBufferDetachKey;
}

// https://streams.spec.whatwg.org/#abstract-opdef-cloneasuint8array
JSObject* CloneAsUint8Array(JSContext* aCx, JS::Handle<JSObject*> aObject) {
  // Step 1. Assert: Type(O) is Object. Implicit.
  // Step 2. Assert: O has an [[ViewedArrayBuffer]] internal slot.
  MOZ_ASSERT(JS_IsArrayBufferViewObject(aObject));

  // Step 3. Assert: !IsDetachedBuffer(O.[[ViewedArrayBuffer]]) is false.
  bool isShared;
  JS::Rooted<JSObject*> viewedArrayBuffer(
      aCx, JS_GetArrayBufferViewBuffer(aCx, aObject, &isShared));
  if (!viewedArrayBuffer) {
    return nullptr;
  }
  MOZ_ASSERT(!JS::IsDetachedArrayBufferObject(viewedArrayBuffer));

  // Step 4. Let buffer be ?CloneArrayBuffer(O.[[ViewedArrayBuffer]],
  //         O.[[ByteOffset]], O.[[ByteLength]], %ArrayBuffer%).
  size_t byteOffset = JS_GetTypedArrayByteOffset(aObject);
  size_t byteLength = JS_GetTypedArrayByteLength(aObject);
  JS::Rooted<JSObject*> buffer(
      aCx,
      JS::ArrayBufferClone(aCx, viewedArrayBuffer, byteOffset, byteLength));
  if (!buffer) {
    return nullptr;
  }

  // Step 5. Let array be ! Construct(%Uint8Array%, « buffer »).
  JS::Rooted<JSObject*> array(
      aCx, JS_NewUint8ArrayWithBuffer(aCx, buffer, 0,
                                      static_cast<int64_t>(byteLength)));
  if (!array) {
    return nullptr;
  }

  // Step 6. Return array.
  return array;
}

}  // namespace mozilla::dom