arraybuffer.tq 5.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

namespace arraybuffer {

// #sec-get-arraybuffer.prototype.bytelength
transitioning javascript builtin ArrayBufferPrototypeGetByteLength(
    js-implicit context: NativeContext, receiver: JSAny)(): Number {
  // 1. Let O be the this value.
  // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
12
  const functionName = 'get ArrayBuffer.prototype.byteLength';
13 14
  const o = Cast<JSArrayBuffer>(receiver) otherwise
  ThrowTypeError(
15
      MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
16 17 18
  // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
  if (IsSharedArrayBuffer(o)) {
    ThrowTypeError(
19
        MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
20
  }
21 22 23 24 25 26
  // 4. If IsResizableArrayBuffer(O) is true, throw a TypeError exception.
  if (IsResizableArrayBuffer(o)) {
    ThrowTypeError(
        MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  }
  // 5. If IsDetachedBuffer(O) is true, throw a TypeError exception.
27
  // TODO(v8:4895): We don't actually throw here.
28
  // 6. Let length be O.[[ArrayBufferByteLength]].
29
  const length = o.byte_length;
30
  // 7. Return length.
31 32 33 34 35 36 37 38
  return Convert<Number>(length);
}

// #sec-get-sharedarraybuffer.prototype.bytelength
transitioning javascript builtin SharedArrayBufferPrototypeGetByteLength(
    js-implicit context: NativeContext, receiver: JSAny)(): Number {
  // 1. Let O be the this value.
  // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
39
  const functionName = 'get SharedArrayBuffer.prototype.byteLength';
40 41
  const o = Cast<JSArrayBuffer>(receiver) otherwise
  ThrowTypeError(
42 43
      MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  // 3. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
44 45
  if (!IsSharedArrayBuffer(o)) {
    ThrowTypeError(
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
        MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  }
  // 4. If IsResizableArrayBuffer(O) is true, throw a TypeError exception.
  if (IsResizableArrayBuffer(o)) {
    ThrowTypeError(
        MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  }
  // 5. Let length be O.[[ArrayBufferByteLength]].
  const length = o.byte_length;
  // 6. Return length.
  return Convert<Number>(length);
}

// #sec-get-resizablearraybuffer.prototype.bytelength
transitioning javascript builtin ResizableArrayBufferPrototypeGetByteLength(
    js-implicit context: NativeContext, receiver: JSAny)(): Number {
  // 1. Let O be the this value.
  // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]).
  const functionName = 'get ResizableArrayBuffer.prototype.byteLength';
  const o = Cast<JSArrayBuffer>(receiver) otherwise
  ThrowTypeError(
      MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  if (!IsResizableArrayBuffer(o)) {
    ThrowTypeError(
        MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  }
  // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
  if (IsSharedArrayBuffer(o)) {
    ThrowTypeError(
        MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
76 77 78 79 80 81 82
  }
  // 4. Let length be O.[[ArrayBufferByteLength]].
  const length = o.byte_length;
  // 5. Return length.
  return Convert<Number>(length);
}

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 120 121 122 123 124 125 126 127 128 129 130 131
// #sec-get-resizablearraybuffer.prototype.maxbytelength
transitioning javascript builtin ResizableArrayBufferPrototypeGetMaxByteLength(
    js-implicit context: NativeContext, receiver: JSAny)(): Number {
  // 1. Let O be the this value.
  // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]).
  const functionName = 'get ResizableArrayBuffer.prototype.maxByteLength';
  const o = Cast<JSArrayBuffer>(receiver) otherwise
  ThrowTypeError(
      MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  if (!IsResizableArrayBuffer(o)) {
    ThrowTypeError(
        MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  }
  // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
  if (IsSharedArrayBuffer(o)) {
    ThrowTypeError(
        MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  }
  // 4. Let length be O.[[ArrayBufferMaxByteLength]].
  const length = o.max_byte_length;
  // 5. Return length.
  return Convert<Number>(length);
}

// #sec-get-growablesharedarraybuffer.prototype.maxbytelength
transitioning javascript builtin
GrowableSharedArrayBufferPrototypeGetMaxByteLength(
    js-implicit context: NativeContext, receiver: JSAny)(): Number {
  // 1. Let O be the this value.
  // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferMaxByteLength]]).
  const functionName = 'get GrowableSharedArrayBuffer.prototype.maxByteLength';
  const o = Cast<JSArrayBuffer>(receiver) otherwise
  ThrowTypeError(
      MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  if (!IsResizableArrayBuffer(o)) {
    ThrowTypeError(
        MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  }
  // 3. If IsSharedArrayBuffer(O) is false, throw a TypeError exception.
  if (!IsSharedArrayBuffer(o)) {
    ThrowTypeError(
        MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  }
  // 4. Let length be O.[[ArrayBufferMaxByteLength]].
  const length = o.max_byte_length;
  // 5. Return length.
  return Convert<Number>(length);
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
// #sec-arraybuffer.isview
transitioning javascript builtin ArrayBufferIsView(arg: JSAny): Boolean {
  // 1. If Type(arg) is not Object, return false.
  // 2. If arg has a [[ViewedArrayBuffer]] internal slot, return true.
  // 3. Return false.
  typeswitch (arg) {
    case (JSArrayBufferView): {
      return True;
    }
    case (JSAny): {
      return False;
    }
  }
}

}  // namespace arraybuffer