mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 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 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 30 | // This file relies on the fact that the following declaration has been made |
| 31 | // in runtime.js: |
| 32 | // var $Array = global.Array; |
| 33 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 34 | var $ArrayBuffer = global.__ArrayBuffer; |
| 35 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 36 | // ------------------------------------------------------------------- |
| 37 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 38 | function ArrayBufferConstructor(byteLength) { // length = 1 |
| 39 | if (%_IsConstructCall()) { |
| 40 | var l = TO_POSITIVE_INTEGER(byteLength); |
| 41 | %ArrayBufferInitialize(this, l); |
| 42 | } else { |
| 43 | return new $ArrayBuffer(byteLength); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | function ArrayBufferGetByteLength() { |
| 48 | if (!IS_ARRAYBUFFER(this)) { |
| 49 | throw MakeTypeError('incompatible_method_receiver', |
| 50 | ['ArrayBuffer.prototype.byteLength', this]); |
| 51 | } |
| 52 | return %ArrayBufferGetByteLength(this); |
| 53 | } |
| 54 | |
| 55 | // ES6 Draft 15.13.5.5.3 |
| 56 | function ArrayBufferSlice(start, end) { |
| 57 | if (!IS_ARRAYBUFFER(this)) { |
| 58 | throw MakeTypeError('incompatible_method_receiver', |
| 59 | ['ArrayBuffer.prototype.slice', this]); |
| 60 | } |
| 61 | |
| 62 | var relativeStart = TO_INTEGER(start); |
| 63 | var first; |
| 64 | if (relativeStart < 0) { |
| 65 | first = MathMax(this.byteLength + relativeStart, 0); |
| 66 | } else { |
| 67 | first = MathMin(relativeStart, this.byteLength); |
| 68 | } |
| 69 | var relativeEnd = IS_UNDEFINED(end) ? this.byteLength : TO_INTEGER(end); |
| 70 | var fin; |
| 71 | if (relativeEnd < 0) { |
| 72 | fin = MathMax(this.byteLength + relativeEnd, 0); |
| 73 | } else { |
| 74 | fin = MathMin(relativeEnd, this.byteLength); |
| 75 | } |
| 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 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 85 | // --------------- Typed Arrays --------------------- |
| 86 | |
| 87 | function CreateTypedArrayConstructor(name, elementSize, arrayId, constructor) { |
| 88 | return function (buffer, byteOffset, length) { |
| 89 | if (%_IsConstructCall()) { |
| 90 | if (!IS_ARRAYBUFFER(buffer)) { |
| 91 | throw MakeTypeError("Type error!"); |
| 92 | } |
| 93 | var offset = IS_UNDEFINED(byteOffset) |
| 94 | ? 0 : offset = TO_POSITIVE_INTEGER(byteOffset); |
| 95 | |
| 96 | if (offset % elementSize !== 0) { |
| 97 | throw MakeRangeError("invalid_typed_array_alignment", |
| 98 | "start offset", name, elementSize); |
| 99 | } |
| 100 | var bufferByteLength = %ArrayBufferGetByteLength(buffer); |
| 101 | if (offset >= bufferByteLength) { |
| 102 | throw MakeRangeError("invalid_typed_array_offset"); |
| 103 | } |
| 104 | |
| 105 | var newByteLength; |
| 106 | var newLength; |
| 107 | if (IS_UNDEFINED(length)) { |
| 108 | if (bufferByteLength % elementSize !== 0) { |
| 109 | throw MakeRangeError("invalid_typed_array_alignment", |
| 110 | "byte length", name, elementSize); |
| 111 | } |
| 112 | newByteLength = bufferByteLength - offset; |
| 113 | newLength = newByteLength / elementSize; |
| 114 | } else { |
| 115 | var newLength = TO_POSITIVE_INTEGER(length); |
| 116 | newByteLength = newLength * elementSize; |
| 117 | } |
| 118 | if (newByteLength > bufferByteLength) { |
| 119 | throw MakeRangeError("invalid_typed_array_length"); |
| 120 | } |
| 121 | %TypedArrayInitialize(this, arrayId, buffer, offset, newByteLength); |
| 122 | } else { |
| 123 | return new constructor(buffer, byteOffset, length); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | function TypedArrayGetBuffer() { |
| 129 | return %TypedArrayGetBuffer(this); |
| 130 | } |
| 131 | |
| 132 | function TypedArrayGetByteLength() { |
| 133 | return %TypedArrayGetByteLength(this); |
| 134 | } |
| 135 | |
| 136 | function TypedArrayGetByteOffset() { |
| 137 | return %TypedArrayGetByteOffset(this); |
| 138 | } |
| 139 | |
| 140 | function TypedArrayGetLength() { |
| 141 | return %TypedArrayGetLength(this); |
| 142 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 143 | |
| 144 | |
| 145 | // ------------------------------------------------------------------- |
| 146 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 147 | function SetUpArrayBuffer() { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 148 | %CheckIsBootstrapping(); |
| 149 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 150 | // Set up the ArrayBuffer constructor function. |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 151 | %SetCode($ArrayBuffer, ArrayBufferConstructor); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 152 | %FunctionSetPrototype($ArrayBuffer, new $Object()); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 153 | |
| 154 | // Set up the constructor property on the ArrayBuffer prototype object. |
| 155 | %SetProperty($ArrayBuffer.prototype, "constructor", $ArrayBuffer, DONT_ENUM); |
| 156 | |
| 157 | InstallGetter($ArrayBuffer.prototype, "byteLength", ArrayBufferGetByteLength); |
| 158 | |
| 159 | InstallFunctions($ArrayBuffer.prototype, DONT_ENUM, $Array( |
| 160 | "slice", ArrayBufferSlice |
| 161 | )); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 162 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 163 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame^] | 164 | SetUpArrayBuffer(); |
| 165 | |
| 166 | function SetupTypedArray(arrayId, name, constructor, elementSize) { |
| 167 | var f = CreateTypedArrayConstructor(name, elementSize, |
| 168 | arrayId, constructor); |
| 169 | %SetCode(constructor, f); |
| 170 | %FunctionSetPrototype(constructor, new $Object()); |
| 171 | |
| 172 | %SetProperty(constructor.prototype, |
| 173 | "constructor", constructor, DONT_ENUM); |
| 174 | %SetProperty(constructor.prototype, |
| 175 | "BYTES_PER_ELEMENT", elementSize, |
| 176 | READ_ONLY | DONT_ENUM | DONT_DELETE); |
| 177 | InstallGetter(constructor.prototype, "buffer", TypedArrayGetBuffer); |
| 178 | InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset); |
| 179 | InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength); |
| 180 | InstallGetter(constructor.prototype, "length", TypedArrayGetLength); |
| 181 | } |
| 182 | |
| 183 | // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. |
| 184 | SetupTypedArray(1, "Uint8Array", global.__Uint8Array, 1); |
| 185 | SetupTypedArray(2, "Int8Array", global.__Int8Array, 1); |
| 186 | SetupTypedArray(3, "Uint16Array", global.__Uint16Array, 2); |
| 187 | SetupTypedArray(4, "Int16Array", global.__Int16Array, 2); |
| 188 | SetupTypedArray(5, "Uint32Array", global.__Uint32Array, 4); |
| 189 | SetupTypedArray(6, "Int32Array", global.__Int32Array, 4); |
| 190 | SetupTypedArray(7, "Float32Array", global.__Float32Array, 4); |
| 191 | SetupTypedArray(8, "Float64Array", global.__Float64Array, 8); |
| 192 | |