blob: 4a4f570146568464dfe8938502e9a827be60c6cd [file] [log] [blame]
ulan@chromium.org57ff8812013-05-10 08:16:55 +00001// Copyright 2013 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28"use strict";
29
30var $ArrayBuffer = global.ArrayBuffer;
31
32// -------------------------------------------------------------------
33
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000034function ArrayBufferConstructor(length) { // length = 1
ulan@chromium.org57ff8812013-05-10 08:16:55 +000035 if (%_IsConstructCall()) {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000036 var byteLength = ToPositiveInteger(length, 'invalid_array_buffer_length');
37 %ArrayBufferInitialize(this, byteLength);
ulan@chromium.org57ff8812013-05-10 08:16:55 +000038 } else {
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000039 throw MakeTypeError('constructor_not_function', ["ArrayBuffer"]);
ulan@chromium.org57ff8812013-05-10 08:16:55 +000040 }
41}
42
43function ArrayBufferGetByteLength() {
44 if (!IS_ARRAYBUFFER(this)) {
45 throw MakeTypeError('incompatible_method_receiver',
46 ['ArrayBuffer.prototype.byteLength', this]);
47 }
48 return %ArrayBufferGetByteLength(this);
49}
50
51// ES6 Draft 15.13.5.5.3
52function ArrayBufferSlice(start, end) {
53 if (!IS_ARRAYBUFFER(this)) {
54 throw MakeTypeError('incompatible_method_receiver',
55 ['ArrayBuffer.prototype.slice', this]);
56 }
57
58 var relativeStart = TO_INTEGER(start);
59 var first;
60 if (relativeStart < 0) {
61 first = MathMax(this.byteLength + relativeStart, 0);
62 } else {
63 first = MathMin(relativeStart, this.byteLength);
64 }
65 var relativeEnd = IS_UNDEFINED(end) ? this.byteLength : TO_INTEGER(end);
66 var fin;
67 if (relativeEnd < 0) {
68 fin = MathMax(this.byteLength + relativeEnd, 0);
69 } else {
70 fin = MathMin(relativeEnd, this.byteLength);
71 }
72
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +000073 if (fin < first) {
74 fin = first;
75 }
ulan@chromium.org57ff8812013-05-10 08:16:55 +000076 var newLen = fin - first;
77 // TODO(dslomov): implement inheritance
78 var result = new $ArrayBuffer(newLen);
79
80 %ArrayBufferSliceImpl(this, result, first);
81 return result;
82}
83
84function SetUpArrayBuffer() {
85 %CheckIsBootstrapping();
86
87 // Set up the ArrayBuffer constructor function.
88 %SetCode($ArrayBuffer, ArrayBufferConstructor);
89 %FunctionSetPrototype($ArrayBuffer, new $Object());
90
91 // Set up the constructor property on the ArrayBuffer prototype object.
92 %SetProperty($ArrayBuffer.prototype, "constructor", $ArrayBuffer, DONT_ENUM);
93
94 InstallGetter($ArrayBuffer.prototype, "byteLength", ArrayBufferGetByteLength);
95
96 InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array(
97 "slice", ArrayBufferSlice
98 ));
99}
100
101SetUpArrayBuffer();
102
103