1// 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
34function ArrayBufferConstructor(length) { // length = 1
35  if (%_IsConstructCall()) {
36    var byteLength = ToPositiveInteger(length, 'invalid_array_buffer_length');
37    %ArrayBufferInitialize(this, byteLength);
38  } else {
39    throw MakeTypeError('constructor_not_function', ["ArrayBuffer"]);
40  }
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  var byte_length = %ArrayBufferGetByteLength(this);
61  if (relativeStart < 0) {
62    first = MathMax(byte_length + relativeStart, 0);
63  } else {
64    first = MathMin(relativeStart, byte_length);
65  }
66  var relativeEnd = IS_UNDEFINED(end) ? byte_length : TO_INTEGER(end);
67  var fin;
68  if (relativeEnd < 0) {
69    fin = MathMax(byte_length + relativeEnd, 0);
70  } else {
71    fin = MathMin(relativeEnd, byte_length);
72  }
73
74  if (fin < first) {
75    fin = first;
76  }
77  var newLen = fin - first;
78  // TODO(dslomov): implement inheritance
79  var result = new $ArrayBuffer(newLen);
80
81  %ArrayBufferSliceImpl(this, result, first);
82  return result;
83}
84
85function ArrayBufferIsView(obj) {
86  return %ArrayBufferIsView(obj);
87}
88
89function SetUpArrayBuffer() {
90  %CheckIsBootstrapping();
91
92  // Set up the ArrayBuffer constructor function.
93  %SetCode($ArrayBuffer, ArrayBufferConstructor);
94  %FunctionSetPrototype($ArrayBuffer, new $Object());
95
96  // Set up the constructor property on the ArrayBuffer prototype object.
97  %SetProperty($ArrayBuffer.prototype, "constructor", $ArrayBuffer, DONT_ENUM);
98
99  InstallGetter($ArrayBuffer.prototype, "byteLength", ArrayBufferGetByteLength);
100
101  InstallFunctions($ArrayBuffer, DONT_ENUM, $Array(
102      "isView", ArrayBufferIsView
103  ));
104
105  InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array(
106      "slice", ArrayBufferSlice
107  ));
108}
109
110SetUpArrayBuffer();
111