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.
7 var $ArrayBuffer = global.ArrayBuffer;
9 // -------------------------------------------------------------------
11 function ArrayBufferConstructor(length) { // length = 1
12 if (%_IsConstructCall()) {
13 var byteLength = ToPositiveInteger(length, 'invalid_array_buffer_length');
14 %ArrayBufferInitialize(this, byteLength);
16 throw MakeTypeError('constructor_not_function', ["ArrayBuffer"]);
20 function ArrayBufferGetByteLen() {
21 if (!IS_ARRAYBUFFER(this)) {
22 throw MakeTypeError('incompatible_method_receiver',
23 ['ArrayBuffer.prototype.byteLength', this]);
25 return %_ArrayBufferGetByteLength(this);
28 // ES6 Draft 15.13.5.5.3
29 function ArrayBufferSlice(start, end) {
30 if (!IS_ARRAYBUFFER(this)) {
31 throw MakeTypeError('incompatible_method_receiver',
32 ['ArrayBuffer.prototype.slice', this]);
35 var relativeStart = TO_INTEGER(start);
36 if (!IS_UNDEFINED(end)) {
37 end = TO_INTEGER(end);
40 var byte_length = %_ArrayBufferGetByteLength(this);
41 if (relativeStart < 0) {
42 first = $max(byte_length + relativeStart, 0);
44 first = $min(relativeStart, byte_length);
46 var relativeEnd = IS_UNDEFINED(end) ? byte_length : end;
48 if (relativeEnd < 0) {
49 fin = $max(byte_length + relativeEnd, 0);
51 fin = $min(relativeEnd, byte_length);
57 var newLen = fin - first;
58 // TODO(dslomov): implement inheritance
59 var result = new $ArrayBuffer(newLen);
61 %ArrayBufferSliceImpl(this, result, first);
65 function ArrayBufferIsViewJS(obj) {
66 return %ArrayBufferIsView(obj);
69 function SetUpArrayBuffer() {
70 %CheckIsBootstrapping();
72 // Set up the ArrayBuffer constructor function.
73 %SetCode($ArrayBuffer, ArrayBufferConstructor);
74 %FunctionSetPrototype($ArrayBuffer, new $Object());
76 // Set up the constructor property on the ArrayBuffer prototype object.
78 $ArrayBuffer.prototype, "constructor", $ArrayBuffer, DONT_ENUM);
80 %AddNamedProperty($ArrayBuffer.prototype,
81 symbolToStringTag, "ArrayBuffer", DONT_ENUM | READ_ONLY);
83 InstallGetter($ArrayBuffer.prototype, "byteLength", ArrayBufferGetByteLen);
85 InstallFunctions($ArrayBuffer, DONT_ENUM, $Array(
86 "isView", ArrayBufferIsViewJS
89 InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array(
90 "slice", ArrayBufferSlice