1// Copyright 2013 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 5"use strict"; 6 7var $ArrayBuffer = global.ArrayBuffer; 8 9// ------------------------------------------------------------------- 10 11function ArrayBufferConstructor(length) { // length = 1 12 if (%_IsConstructCall()) { 13 var byteLength = ToPositiveInteger(length, 'invalid_array_buffer_length'); 14 %ArrayBufferInitialize(this, byteLength); 15 } else { 16 throw MakeTypeError('constructor_not_function', ["ArrayBuffer"]); 17 } 18} 19 20function ArrayBufferGetByteLen() { 21 if (!IS_ARRAYBUFFER(this)) { 22 throw MakeTypeError('incompatible_method_receiver', 23 ['ArrayBuffer.prototype.byteLength', this]); 24 } 25 return %_ArrayBufferGetByteLength(this); 26} 27 28// ES6 Draft 15.13.5.5.3 29function ArrayBufferSlice(start, end) { 30 if (!IS_ARRAYBUFFER(this)) { 31 throw MakeTypeError('incompatible_method_receiver', 32 ['ArrayBuffer.prototype.slice', this]); 33 } 34 35 var relativeStart = TO_INTEGER(start); 36 if (!IS_UNDEFINED(end)) { 37 end = TO_INTEGER(end); 38 } 39 var first; 40 var byte_length = %_ArrayBufferGetByteLength(this); 41 if (relativeStart < 0) { 42 first = MathMax(byte_length + relativeStart, 0); 43 } else { 44 first = MathMin(relativeStart, byte_length); 45 } 46 var relativeEnd = IS_UNDEFINED(end) ? byte_length : end; 47 var fin; 48 if (relativeEnd < 0) { 49 fin = MathMax(byte_length + relativeEnd, 0); 50 } else { 51 fin = MathMin(relativeEnd, byte_length); 52 } 53 54 if (fin < first) { 55 fin = first; 56 } 57 var newLen = fin - first; 58 // TODO(dslomov): implement inheritance 59 var result = new $ArrayBuffer(newLen); 60 61 %ArrayBufferSliceImpl(this, result, first); 62 return result; 63} 64 65function ArrayBufferIsViewJS(obj) { 66 return %ArrayBufferIsView(obj); 67} 68 69function SetUpArrayBuffer() { 70 %CheckIsBootstrapping(); 71 72 // Set up the ArrayBuffer constructor function. 73 %SetCode($ArrayBuffer, ArrayBufferConstructor); 74 %FunctionSetPrototype($ArrayBuffer, new $Object()); 75 76 // Set up the constructor property on the ArrayBuffer prototype object. 77 %AddNamedProperty( 78 $ArrayBuffer.prototype, "constructor", $ArrayBuffer, DONT_ENUM); 79 80 InstallGetter($ArrayBuffer.prototype, "byteLength", ArrayBufferGetByteLen); 81 82 InstallFunctions($ArrayBuffer, DONT_ENUM, $Array( 83 "isView", ArrayBufferIsViewJS 84 )); 85 86 InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array( 87 "slice", ArrayBufferSlice 88 )); 89} 90 91SetUpArrayBuffer(); 92