2013-05-06 16:19:27 +00:00
|
|
|
// Copyright 2013 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2013-05-06 16:19:27 +00:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var $ArrayBuffer = global.ArrayBuffer;
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------
|
|
|
|
|
2013-06-24 13:58:52 +00:00
|
|
|
function ArrayBufferConstructor(length) { // length = 1
|
2013-05-06 16:19:27 +00:00
|
|
|
if (%_IsConstructCall()) {
|
2013-06-24 13:58:52 +00:00
|
|
|
var byteLength = ToPositiveInteger(length, 'invalid_array_buffer_length');
|
|
|
|
%ArrayBufferInitialize(this, byteLength);
|
2013-05-06 16:19:27 +00:00
|
|
|
} else {
|
2013-06-27 07:42:08 +00:00
|
|
|
throw MakeTypeError('constructor_not_function', ["ArrayBuffer"]);
|
2013-05-06 16:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-24 11:11:30 +00:00
|
|
|
function ArrayBufferGetByteLen() {
|
2013-05-06 16:19:27 +00:00
|
|
|
if (!IS_ARRAYBUFFER(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['ArrayBuffer.prototype.byteLength', this]);
|
|
|
|
}
|
2014-03-28 15:25:24 +00:00
|
|
|
return %_ArrayBufferGetByteLength(this);
|
2013-05-06 16:19:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ES6 Draft 15.13.5.5.3
|
|
|
|
function ArrayBufferSlice(start, end) {
|
|
|
|
if (!IS_ARRAYBUFFER(this)) {
|
|
|
|
throw MakeTypeError('incompatible_method_receiver',
|
|
|
|
['ArrayBuffer.prototype.slice', this]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var relativeStart = TO_INTEGER(start);
|
2014-03-18 10:55:29 +00:00
|
|
|
if (!IS_UNDEFINED(end)) {
|
|
|
|
end = TO_INTEGER(end);
|
|
|
|
}
|
2013-05-06 16:19:27 +00:00
|
|
|
var first;
|
2014-03-28 15:25:24 +00:00
|
|
|
var byte_length = %_ArrayBufferGetByteLength(this);
|
2013-05-06 16:19:27 +00:00
|
|
|
if (relativeStart < 0) {
|
2014-03-12 19:25:40 +00:00
|
|
|
first = MathMax(byte_length + relativeStart, 0);
|
2013-05-06 16:19:27 +00:00
|
|
|
} else {
|
2014-03-12 19:25:40 +00:00
|
|
|
first = MathMin(relativeStart, byte_length);
|
2013-05-06 16:19:27 +00:00
|
|
|
}
|
2014-03-18 10:55:29 +00:00
|
|
|
var relativeEnd = IS_UNDEFINED(end) ? byte_length : end;
|
2013-05-06 16:19:27 +00:00
|
|
|
var fin;
|
|
|
|
if (relativeEnd < 0) {
|
2014-03-12 19:25:40 +00:00
|
|
|
fin = MathMax(byte_length + relativeEnd, 0);
|
2013-05-06 16:19:27 +00:00
|
|
|
} else {
|
2014-03-12 19:25:40 +00:00
|
|
|
fin = MathMin(relativeEnd, byte_length);
|
2013-05-06 16:19:27 +00:00
|
|
|
}
|
|
|
|
|
2013-06-24 13:58:52 +00:00
|
|
|
if (fin < first) {
|
|
|
|
fin = first;
|
|
|
|
}
|
2013-05-06 16:19:27 +00:00
|
|
|
var newLen = fin - first;
|
|
|
|
// TODO(dslomov): implement inheritance
|
|
|
|
var result = new $ArrayBuffer(newLen);
|
|
|
|
|
|
|
|
%ArrayBufferSliceImpl(this, result, first);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-05-14 08:51:10 +00:00
|
|
|
function ArrayBufferIsViewJS(obj) {
|
2013-10-10 08:36:44 +00:00
|
|
|
return %ArrayBufferIsView(obj);
|
|
|
|
}
|
|
|
|
|
2013-05-06 16:19:27 +00:00
|
|
|
function SetUpArrayBuffer() {
|
|
|
|
%CheckIsBootstrapping();
|
|
|
|
|
|
|
|
// Set up the ArrayBuffer constructor function.
|
|
|
|
%SetCode($ArrayBuffer, ArrayBufferConstructor);
|
|
|
|
%FunctionSetPrototype($ArrayBuffer, new $Object());
|
|
|
|
|
|
|
|
// Set up the constructor property on the ArrayBuffer prototype object.
|
2014-07-14 14:05:30 +00:00
|
|
|
%AddNamedProperty(
|
|
|
|
$ArrayBuffer.prototype, "constructor", $ArrayBuffer, DONT_ENUM);
|
2013-05-06 16:19:27 +00:00
|
|
|
|
2014-10-21 17:21:32 +00:00
|
|
|
%AddNamedProperty($ArrayBuffer.prototype,
|
|
|
|
symbolToStringTag, "ArrayBuffer", DONT_ENUM | READ_ONLY);
|
|
|
|
|
2014-04-24 11:11:30 +00:00
|
|
|
InstallGetter($ArrayBuffer.prototype, "byteLength", ArrayBufferGetByteLen);
|
2013-05-06 16:19:27 +00:00
|
|
|
|
2013-10-10 08:36:44 +00:00
|
|
|
InstallFunctions($ArrayBuffer, DONT_ENUM, $Array(
|
2014-05-14 08:51:10 +00:00
|
|
|
"isView", ArrayBufferIsViewJS
|
2013-10-10 08:36:44 +00:00
|
|
|
));
|
|
|
|
|
2013-05-06 16:19:27 +00:00
|
|
|
InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array(
|
|
|
|
"slice", ArrayBufferSlice
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
SetUpArrayBuffer();
|