1// Copyright 2020 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5namespace arraybuffer { 6 7// #sec-get-arraybuffer.prototype.bytelength 8transitioning javascript builtin ArrayBufferPrototypeGetByteLength( 9 js-implicit context: NativeContext, receiver: JSAny)(): Number { 10 // 1. Let O be the this value. 11 // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). 12 const functionName = 'get ArrayBuffer.prototype.byteLength'; 13 const o = Cast<JSArrayBuffer>(receiver) otherwise 14 ThrowTypeError( 15 MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver); 16 // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. 17 if (IsSharedArrayBuffer(o)) { 18 ThrowTypeError( 19 MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver); 20 } 21 // 4. Let length be O.[[ArrayBufferByteLength]]. 22 const length = o.byte_length; 23 // 5. Return length. 24 return Convert<Number>(length); 25} 26 27// #sec-get-arraybuffer.prototype.maxbytelength 28transitioning javascript builtin ArrayBufferPrototypeGetMaxByteLength( 29 js-implicit context: NativeContext, receiver: JSAny)(): Number { 30 // 1. Let O be the this value. 31 // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). 32 const functionName = 'get ArrayBuffer.prototype.maxByteLength'; 33 const o = Cast<JSArrayBuffer>(receiver) otherwise 34 ThrowTypeError( 35 MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver); 36 // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. 37 if (IsSharedArrayBuffer(o)) { 38 ThrowTypeError( 39 MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver); 40 } 41 // 4. If IsDetachedBuffer(O) is true, return 0_F. 42 if (IsDetachedBuffer(o)) { 43 return 0; 44 } 45 // 5. If IsResizableArrayBuffer(O) is true, then 46 // a. Let length be O.[[ArrayBufferMaxByteLength]]. 47 // 6. Else, 48 // a. Let length be O.[[ArrayBufferByteLength]]. 49 // 7. Return F(length); 50 51 if (IsResizableArrayBuffer(o)) { 52 return Convert<Number>(o.max_byte_length); 53 } 54 return Convert<Number>(o.byte_length); 55} 56 57// #sec-get-arraybuffer.prototype.resizable 58transitioning javascript builtin ArrayBufferPrototypeGetResizable( 59 js-implicit context: NativeContext, receiver: JSAny)(): Boolean { 60 // 1. Let O be the this value. 61 // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). 62 const functionName = 'get ArrayBuffer.prototype.resizable'; 63 const o = Cast<JSArrayBuffer>(receiver) otherwise 64 ThrowTypeError( 65 MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver); 66 // 3. If IsSharedArrayBuffer(O) is true, throw a TypeError exception. 67 if (IsSharedArrayBuffer(o)) { 68 ThrowTypeError( 69 MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver); 70 } 71 // 4. Return IsResizableArrayBuffer(O). 72 if (IsResizableArrayBuffer(o)) { 73 return True; 74 } 75 return False; 76} 77 78// #sec-get-growablesharedarraybuffer.prototype.maxbytelength 79transitioning javascript builtin 80SharedArrayBufferPrototypeGetMaxByteLength( 81 js-implicit context: NativeContext, receiver: JSAny)(): Number { 82 // 1. Let O be the this value. 83 // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). 84 const functionName = 'get SharedArrayBuffer.prototype.maxByteLength'; 85 const o = Cast<JSArrayBuffer>(receiver) otherwise 86 ThrowTypeError( 87 MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver); 88 // 3. If IsSharedArrayBuffer(O) is false, throw a TypeError exception. 89 if (!IsSharedArrayBuffer(o)) { 90 ThrowTypeError( 91 MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver); 92 } 93 // 4. If IsResizableArrayBuffer(O) is true, then 94 // a. Let length be O.[[ArrayBufferMaxByteLength]]. 95 // 5. Else, 96 // a. Let length be O.[[ArrayBufferByteLength]]. 97 // 6. Return F(length); 98 dcheck(IsResizableArrayBuffer(o) || o.max_byte_length == o.byte_length); 99 return Convert<Number>(o.max_byte_length); 100} 101 102// #sec-get-sharedarraybuffer.prototype.growable 103transitioning javascript builtin SharedArrayBufferPrototypeGetGrowable( 104 js-implicit context: NativeContext, receiver: JSAny)(): Boolean { 105 // 1. Let O be the this value. 106 // 2. Perform ? RequireInternalSlot(O, [[ArrayBufferData]]). 107 const functionName = 'get SharedArrayBuffer.prototype.growable'; 108 const o = Cast<JSArrayBuffer>(receiver) otherwise 109 ThrowTypeError( 110 MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver); 111 // 3. If IsSharedArrayBuffer(O) is false, throw a TypeError exception. 112 if (!IsSharedArrayBuffer(o)) { 113 ThrowTypeError( 114 MessageTemplate::kIncompatibleMethodReceiver, functionName, receiver); 115 } 116 // 4. Return IsResizableArrayBuffer(O). 117 if (IsResizableArrayBuffer(o)) { 118 return True; 119 } 120 return False; 121} 122 123// #sec-arraybuffer.isview 124transitioning javascript builtin ArrayBufferIsView(arg: JSAny): Boolean { 125 // 1. If Type(arg) is not Object, return false. 126 // 2. If arg has a [[ViewedArrayBuffer]] internal slot, return true. 127 // 3. Return false. 128 typeswitch (arg) { 129 case (JSArrayBufferView): { 130 return True; 131 } 132 case (JSAny): { 133 return False; 134 } 135 } 136} 137 138} // namespace arraybuffer 139