summaryrefslogtreecommitdiff
path: root/deps/v8/src/builtins/arraybuffer.tq
blob: 3765e57d69e6fb8198248876d2d9499ab3ffc9cd (plain)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// 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]]).
  const functionName = 'get ArrayBuffer.prototype.byteLength';
  const o = Cast<JSArrayBuffer>(receiver) otherwise
  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.[[ArrayBufferByteLength]].
  const length = o.byte_length;
  // 5. Return length.
  return Convert<Number>(length);
}

// #sec-get-arraybuffer.prototype.maxbytelength
transitioning javascript builtin ArrayBufferPrototypeGetMaxByteLength(
    js-implicit context: NativeContext, receiver: JSAny)(): Number {
  // 1. Let O be the this value.
  // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
  const functionName = 'get ArrayBuffer.prototype.maxByteLength';
  const o = Cast<JSArrayBuffer>(receiver) otherwise
  ThrowTypeError(
      MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
  if (IsSharedArrayBuffer(o)) {
    ThrowTypeError(
        MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  }
  // 4. If IsDetachedBuffer(O) is true, return 0_F.
  if (IsDetachedBuffer(o)) {
    return 0;
  }
  // 5. If IsResizableArrayBuffer(O) is true, then
  //   a. Let length be O.[[ArrayBufferMaxByteLength]].
  // 6. Else,
  //   a. Let length be O.[[ArrayBufferByteLength]].
  // 7. Return F(length);

  if (IsResizableArrayBuffer(o)) {
    return Convert<Number>(o.max_byte_length);
  }
  return Convert<Number>(o.byte_length);
}

// #sec-get-arraybuffer.prototype.resizable
transitioning javascript builtin ArrayBufferPrototypeGetResizable(
    js-implicit context: NativeContext, receiver: JSAny)(): Boolean {
  // 1. Let O be the this value.
  // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
  const functionName = 'get ArrayBuffer.prototype.resizable';
  const o = Cast<JSArrayBuffer>(receiver) otherwise
  ThrowTypeError(
      MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
  if (IsSharedArrayBuffer(o)) {
    ThrowTypeError(
        MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  }
  // 4. Return IsResizableArrayBuffer(O).
  if (IsResizableArrayBuffer(o)) {
    return True;
  }
  return False;
}

// #sec-get-arraybuffer.prototype.detached
transitioning javascript builtin ArrayBufferPrototypeGetDetached(
    js-implicit context: NativeContext, receiver: JSAny)(): Boolean {
  // 1. Let O be the this value.
  // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
  const functionName = 'get ArrayBuffer.prototype.detached';
  const o = Cast<JSArrayBuffer>(receiver) otherwise
  ThrowTypeError(
      MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception.
  if (IsSharedArrayBuffer(o)) {
    ThrowTypeError(
        MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  }
  // 4. Return IsDetachedBuffer(O).
  if (IsDetachedBuffer(o)) {
    return True;
  }
  return False;
}

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

// #sec-get-sharedarraybuffer.prototype.growable
transitioning javascript builtin SharedArrayBufferPrototypeGetGrowable(
    js-implicit context: NativeContext, receiver: JSAny)(): Boolean {
  // 1. Let O be the this value.
  // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]).
  const functionName = 'get SharedArrayBuffer.prototype.growable';
  const o = Cast<JSArrayBuffer>(receiver) otherwise
  ThrowTypeError(
      MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  // 3. If IsSharedArrayBuffer(O) is false, throw a TypeError exception.
  if (!IsSharedArrayBuffer(o)) {
    ThrowTypeError(
        MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver);
  }
  // 4. Return IsResizableArrayBuffer(O).
  if (IsResizableArrayBuffer(o)) {
    return True;
  }
  return False;
}

// #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