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; |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame] | 33 | var $ArrayBuffer = global.ArrayBuffer; |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 34 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 35 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 36 | // --------------- Typed Arrays --------------------- |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 37 | macro TYPED_ARRAYS(FUNCTION) |
| 38 | // arrayIds below should be synchronized with Runtime_TypedArrayInitialize. |
| 39 | FUNCTION(1, Uint8Array, 1) |
| 40 | FUNCTION(2, Int8Array, 1) |
| 41 | FUNCTION(3, Uint16Array, 2) |
| 42 | FUNCTION(4, Int16Array, 2) |
| 43 | FUNCTION(5, Uint32Array, 4) |
| 44 | FUNCTION(6, Int32Array, 4) |
| 45 | FUNCTION(7, Float32Array, 4) |
| 46 | FUNCTION(8, Float64Array, 8) |
| 47 | FUNCTION(9, Uint8ClampedArray, 1) |
| 48 | endmacro |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 49 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 50 | macro TYPED_ARRAY_CONSTRUCTOR(ARRAY_ID, NAME, ELEMENT_SIZE) |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 51 | function NAMEConstructByArrayBuffer(obj, buffer, byteOffset, length) { |
machenbach@chromium.org | 4452a49 | 2014-03-18 13:03:00 +0000 | [diff] [blame] | 52 | if (!IS_UNDEFINED(byteOffset)) { |
| 53 | byteOffset = |
| 54 | ToPositiveInteger(byteOffset, "invalid_typed_array_length"); |
| 55 | } |
| 56 | if (!IS_UNDEFINED(length)) { |
| 57 | length = ToPositiveInteger(length, "invalid_typed_array_length"); |
| 58 | } |
| 59 | |
machenbach@chromium.org | 97b98c9 | 2014-03-13 03:05:00 +0000 | [diff] [blame] | 60 | var bufferByteLength = %ArrayBufferGetByteLength(buffer); |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 61 | var offset; |
| 62 | if (IS_UNDEFINED(byteOffset)) { |
| 63 | offset = 0; |
| 64 | } else { |
machenbach@chromium.org | 4452a49 | 2014-03-18 13:03:00 +0000 | [diff] [blame] | 65 | offset = byteOffset; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 66 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 67 | if (offset % ELEMENT_SIZE !== 0) { |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 68 | throw MakeRangeError("invalid_typed_array_alignment", |
titzer@chromium.org | f5a2454 | 2014-03-04 09:06:17 +0000 | [diff] [blame] | 69 | ["start offset", "NAME", ELEMENT_SIZE]); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 70 | } |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 71 | if (offset > bufferByteLength) { |
| 72 | throw MakeRangeError("invalid_typed_array_offset"); |
| 73 | } |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 74 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 75 | |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 76 | var newByteLength; |
| 77 | var newLength; |
| 78 | if (IS_UNDEFINED(length)) { |
| 79 | if (bufferByteLength % ELEMENT_SIZE !== 0) { |
| 80 | throw MakeRangeError("invalid_typed_array_alignment", |
titzer@chromium.org | f5a2454 | 2014-03-04 09:06:17 +0000 | [diff] [blame] | 81 | ["byte length", "NAME", ELEMENT_SIZE]); |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 82 | } |
| 83 | newByteLength = bufferByteLength - offset; |
| 84 | newLength = newByteLength / ELEMENT_SIZE; |
| 85 | } else { |
machenbach@chromium.org | 4452a49 | 2014-03-18 13:03:00 +0000 | [diff] [blame] | 86 | var newLength = length; |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 87 | newByteLength = newLength * ELEMENT_SIZE; |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 88 | } |
machenbach@chromium.org | 37be408 | 2013-11-26 13:50:38 +0000 | [diff] [blame] | 89 | if ((offset + newByteLength > bufferByteLength) |
machenbach@chromium.org | a221880 | 2014-03-25 07:30:47 +0000 | [diff] [blame^] | 90 | || (newLength > %_MaxSmi())) { |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 91 | throw MakeRangeError("invalid_typed_array_length"); |
| 92 | } |
machenbach@chromium.org | a221880 | 2014-03-25 07:30:47 +0000 | [diff] [blame^] | 93 | %_TypedArrayInitialize(obj, ARRAY_ID, buffer, offset, newByteLength); |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 94 | } |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 95 | |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 96 | function NAMEConstructByLength(obj, length) { |
| 97 | var l = IS_UNDEFINED(length) ? |
| 98 | 0 : ToPositiveInteger(length, "invalid_typed_array_length"); |
machenbach@chromium.org | a221880 | 2014-03-25 07:30:47 +0000 | [diff] [blame^] | 99 | if (l > %_MaxSmi()) { |
machenbach@chromium.org | ea46888 | 2013-11-18 08:53:19 +0000 | [diff] [blame] | 100 | throw MakeRangeError("invalid_typed_array_length"); |
| 101 | } |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 102 | var byteLength = l * ELEMENT_SIZE; |
| 103 | var buffer = new $ArrayBuffer(byteLength); |
machenbach@chromium.org | a221880 | 2014-03-25 07:30:47 +0000 | [diff] [blame^] | 104 | %_TypedArrayInitialize(obj, ARRAY_ID, buffer, 0, byteLength); |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | function NAMEConstructByArrayLike(obj, arrayLike) { |
| 108 | var length = arrayLike.length; |
| 109 | var l = ToPositiveInteger(length, "invalid_typed_array_length"); |
machenbach@chromium.org | 4452a49 | 2014-03-18 13:03:00 +0000 | [diff] [blame] | 110 | |
machenbach@chromium.org | a221880 | 2014-03-25 07:30:47 +0000 | [diff] [blame^] | 111 | if (l > %_MaxSmi()) { |
yangguo@chromium.org | cc53605 | 2013-11-29 11:43:20 +0000 | [diff] [blame] | 112 | throw MakeRangeError("invalid_typed_array_length"); |
| 113 | } |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 114 | if(!%TypedArrayInitializeFromArrayLike(obj, ARRAY_ID, arrayLike, l)) { |
| 115 | for (var i = 0; i < l; i++) { |
| 116 | // It is crucial that we let any execptions from arrayLike[i] |
| 117 | // propagate outside the function. |
| 118 | obj[i] = arrayLike[i]; |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 119 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 120 | } |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | function NAMEConstructor(arg1, arg2, arg3) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 124 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 125 | if (%_IsConstructCall()) { |
| 126 | if (IS_ARRAYBUFFER(arg1)) { |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 127 | NAMEConstructByArrayBuffer(this, arg1, arg2, arg3); |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 128 | } else if (IS_NUMBER(arg1) || IS_STRING(arg1) || |
| 129 | IS_BOOLEAN(arg1) || IS_UNDEFINED(arg1)) { |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 130 | NAMEConstructByLength(this, arg1); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 131 | } else { |
machenbach@chromium.org | e8412be | 2013-11-08 10:23:52 +0000 | [diff] [blame] | 132 | NAMEConstructByArrayLike(this, arg1); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 133 | } |
| 134 | } else { |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 135 | throw MakeTypeError("constructor_not_function", ["NAME"]) |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 138 | endmacro |
| 139 | |
| 140 | TYPED_ARRAYS(TYPED_ARRAY_CONSTRUCTOR) |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 141 | |
| 142 | function TypedArrayGetBuffer() { |
| 143 | return %TypedArrayGetBuffer(this); |
| 144 | } |
| 145 | |
| 146 | function TypedArrayGetByteLength() { |
| 147 | return %TypedArrayGetByteLength(this); |
| 148 | } |
| 149 | |
| 150 | function TypedArrayGetByteOffset() { |
| 151 | return %TypedArrayGetByteOffset(this); |
| 152 | } |
| 153 | |
| 154 | function TypedArrayGetLength() { |
| 155 | return %TypedArrayGetLength(this); |
| 156 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 157 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 158 | function CreateSubArray(elementSize, constructor) { |
| 159 | return function(begin, end) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 160 | var beginInt = TO_INTEGER(begin); |
machenbach@chromium.org | 4452a49 | 2014-03-18 13:03:00 +0000 | [diff] [blame] | 161 | if (!IS_UNDEFINED(end)) { |
| 162 | end = TO_INTEGER(end); |
| 163 | } |
| 164 | |
| 165 | var srcLength = %TypedArrayGetLength(this); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 166 | if (beginInt < 0) { |
| 167 | beginInt = MathMax(0, srcLength + beginInt); |
| 168 | } else { |
| 169 | beginInt = MathMin(srcLength, beginInt); |
| 170 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 171 | |
machenbach@chromium.org | 4452a49 | 2014-03-18 13:03:00 +0000 | [diff] [blame] | 172 | var endInt = IS_UNDEFINED(end) ? srcLength : end; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 173 | if (endInt < 0) { |
| 174 | endInt = MathMax(0, srcLength + endInt); |
| 175 | } else { |
| 176 | endInt = MathMin(endInt, srcLength); |
| 177 | } |
| 178 | if (endInt < beginInt) { |
| 179 | endInt = beginInt; |
| 180 | } |
| 181 | var newLength = endInt - beginInt; |
| 182 | var beginByteOffset = |
| 183 | %TypedArrayGetByteOffset(this) + beginInt * elementSize; |
| 184 | return new constructor(%TypedArrayGetBuffer(this), |
| 185 | beginByteOffset, newLength); |
| 186 | } |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 187 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 188 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 189 | function TypedArraySetFromArrayLike(target, source, sourceLength, offset) { |
| 190 | if (offset > 0) { |
| 191 | for (var i = 0; i < sourceLength; i++) { |
| 192 | target[offset + i] = source[i]; |
| 193 | } |
| 194 | } |
| 195 | else { |
| 196 | for (var i = 0; i < sourceLength; i++) { |
| 197 | target[i] = source[i]; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | function TypedArraySetFromOverlappingTypedArray(target, source, offset) { |
| 203 | var sourceElementSize = source.BYTES_PER_ELEMENT; |
| 204 | var targetElementSize = target.BYTES_PER_ELEMENT; |
| 205 | var sourceLength = source.length; |
| 206 | |
| 207 | // Copy left part. |
| 208 | function CopyLeftPart() { |
| 209 | // First un-mutated byte after the next write |
| 210 | var targetPtr = target.byteOffset + (offset + 1) * targetElementSize; |
| 211 | // Next read at sourcePtr. We do not care for memory changing before |
| 212 | // sourcePtr - we have already copied it. |
| 213 | var sourcePtr = source.byteOffset; |
| 214 | for (var leftIndex = 0; |
| 215 | leftIndex < sourceLength && targetPtr <= sourcePtr; |
| 216 | leftIndex++) { |
| 217 | target[offset + leftIndex] = source[leftIndex]; |
| 218 | targetPtr += targetElementSize; |
| 219 | sourcePtr += sourceElementSize; |
| 220 | } |
| 221 | return leftIndex; |
| 222 | } |
| 223 | var leftIndex = CopyLeftPart(); |
| 224 | |
| 225 | // Copy rigth part; |
| 226 | function CopyRightPart() { |
| 227 | // First unmutated byte before the next write |
| 228 | var targetPtr = |
| 229 | target.byteOffset + (offset + sourceLength - 1) * targetElementSize; |
| 230 | // Next read before sourcePtr. We do not care for memory changing after |
| 231 | // sourcePtr - we have already copied it. |
| 232 | var sourcePtr = |
| 233 | source.byteOffset + sourceLength * sourceElementSize; |
| 234 | for(var rightIndex = sourceLength - 1; |
| 235 | rightIndex >= leftIndex && targetPtr >= sourcePtr; |
| 236 | rightIndex--) { |
| 237 | target[offset + rightIndex] = source[rightIndex]; |
| 238 | targetPtr -= targetElementSize; |
| 239 | sourcePtr -= sourceElementSize; |
| 240 | } |
| 241 | return rightIndex; |
| 242 | } |
| 243 | var rightIndex = CopyRightPart(); |
| 244 | |
| 245 | var temp = new $Array(rightIndex + 1 - leftIndex); |
| 246 | for (var i = leftIndex; i <= rightIndex; i++) { |
| 247 | temp[i - leftIndex] = source[i]; |
| 248 | } |
| 249 | for (i = leftIndex; i <= rightIndex; i++) { |
| 250 | target[offset + i] = temp[i - leftIndex]; |
| 251 | } |
| 252 | } |
| 253 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 254 | function TypedArraySet(obj, offset) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 255 | var intOffset = IS_UNDEFINED(offset) ? 0 : TO_INTEGER(offset); |
| 256 | if (intOffset < 0) { |
| 257 | throw MakeTypeError("typed_array_set_negative_offset"); |
| 258 | } |
machenbach@chromium.org | 0a73036 | 2014-02-05 03:04:56 +0000 | [diff] [blame] | 259 | |
| 260 | if (intOffset > %MaxSmi()) { |
| 261 | throw MakeRangeError("typed_array_set_source_too_large"); |
| 262 | } |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 263 | switch (%TypedArraySetFastCases(this, obj, intOffset)) { |
| 264 | // These numbers should be synchronized with runtime.cc. |
| 265 | case 0: // TYPED_ARRAY_SET_TYPED_ARRAY_SAME_TYPE |
| 266 | return; |
| 267 | case 1: // TYPED_ARRAY_SET_TYPED_ARRAY_OVERLAPPING |
| 268 | TypedArraySetFromOverlappingTypedArray(this, obj, intOffset); |
| 269 | return; |
| 270 | case 2: // TYPED_ARRAY_SET_TYPED_ARRAY_NONOVERLAPPING |
| 271 | TypedArraySetFromArrayLike(this, obj, obj.length, intOffset); |
| 272 | return; |
| 273 | case 3: // TYPED_ARRAY_SET_NON_TYPED_ARRAY |
| 274 | var l = obj.length; |
| 275 | if (IS_UNDEFINED(l)) { |
| 276 | if (IS_NUMBER(obj)) { |
| 277 | // For number as a first argument, throw TypeError |
| 278 | // instead of silently ignoring the call, so that |
| 279 | // the user knows (s)he did something wrong. |
| 280 | // (Consistent with Firefox and Blink/WebKit) |
| 281 | throw MakeTypeError("invalid_argument"); |
| 282 | } |
| 283 | return; |
| 284 | } |
| 285 | if (intOffset + l > this.length) { |
| 286 | throw MakeRangeError("typed_array_set_source_too_large"); |
| 287 | } |
| 288 | TypedArraySetFromArrayLike(this, obj, l, intOffset); |
| 289 | return; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
| 293 | // ------------------------------------------------------------------- |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 294 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 295 | function SetupTypedArray(constructor, fun, elementSize) { |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 296 | %CheckIsBootstrapping(); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 297 | %SetCode(constructor, fun); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 298 | %FunctionSetPrototype(constructor, new $Object()); |
| 299 | |
jkummerow@chromium.org | ba72ec8 | 2013-07-22 09:21:20 +0000 | [diff] [blame] | 300 | %SetProperty(constructor, "BYTES_PER_ELEMENT", elementSize, |
| 301 | READ_ONLY | DONT_ENUM | DONT_DELETE); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 302 | %SetProperty(constructor.prototype, |
| 303 | "constructor", constructor, DONT_ENUM); |
| 304 | %SetProperty(constructor.prototype, |
| 305 | "BYTES_PER_ELEMENT", elementSize, |
| 306 | READ_ONLY | DONT_ENUM | DONT_DELETE); |
| 307 | InstallGetter(constructor.prototype, "buffer", TypedArrayGetBuffer); |
| 308 | InstallGetter(constructor.prototype, "byteOffset", TypedArrayGetByteOffset); |
| 309 | InstallGetter(constructor.prototype, "byteLength", TypedArrayGetByteLength); |
| 310 | InstallGetter(constructor.prototype, "length", TypedArrayGetLength); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 311 | |
| 312 | InstallFunctions(constructor.prototype, DONT_ENUM, $Array( |
| 313 | "subarray", CreateSubArray(elementSize, constructor), |
| 314 | "set", TypedArraySet |
| 315 | )); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 316 | } |
| 317 | |
verwaest@chromium.org | 057bd50 | 2013-11-06 12:03:29 +0000 | [diff] [blame] | 318 | macro SETUP_TYPED_ARRAY(ARRAY_ID, NAME, ELEMENT_SIZE) |
| 319 | SetupTypedArray (global.NAME, NAMEConstructor, ELEMENT_SIZE); |
| 320 | endmacro |
| 321 | |
| 322 | TYPED_ARRAYS(SETUP_TYPED_ARRAY) |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 323 | |
| 324 | // --------------------------- DataView ----------------------------- |
| 325 | |
| 326 | var $DataView = global.DataView; |
| 327 | |
| 328 | function DataViewConstructor(buffer, byteOffset, byteLength) { // length = 3 |
| 329 | if (%_IsConstructCall()) { |
| 330 | if (!IS_ARRAYBUFFER(buffer)) { |
| 331 | throw MakeTypeError('data_view_not_array_buffer', []); |
| 332 | } |
machenbach@chromium.org | 4452a49 | 2014-03-18 13:03:00 +0000 | [diff] [blame] | 333 | if (!IS_UNDEFINED(byteOffset)) { |
| 334 | byteOffset = ToPositiveInteger(byteOffset, 'invalid_data_view_offset'); |
| 335 | } |
| 336 | if (!IS_UNDEFINED(byteLength)) { |
| 337 | byteLength = TO_INTEGER(byteLength); |
| 338 | } |
| 339 | |
machenbach@chromium.org | 97b98c9 | 2014-03-13 03:05:00 +0000 | [diff] [blame] | 340 | var bufferByteLength = %ArrayBufferGetByteLength(buffer); |
machenbach@chromium.org | 4452a49 | 2014-03-18 13:03:00 +0000 | [diff] [blame] | 341 | |
| 342 | var offset = IS_UNDEFINED(byteOffset) ? 0 : byteOffset; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 343 | if (offset > bufferByteLength) { |
| 344 | throw MakeRangeError('invalid_data_view_offset'); |
| 345 | } |
machenbach@chromium.org | 4452a49 | 2014-03-18 13:03:00 +0000 | [diff] [blame] | 346 | |
| 347 | var length = IS_UNDEFINED(byteLength) |
| 348 | ? bufferByteLength - offset |
| 349 | : byteLength; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 350 | if (length < 0 || offset + length > bufferByteLength) { |
| 351 | throw new MakeRangeError('invalid_data_view_length'); |
| 352 | } |
machenbach@chromium.org | a221880 | 2014-03-25 07:30:47 +0000 | [diff] [blame^] | 353 | %_DataViewInitialize(this, buffer, offset, length); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 354 | } else { |
| 355 | throw MakeTypeError('constructor_not_function', ["DataView"]); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | function DataViewGetBuffer() { |
| 360 | if (!IS_DATAVIEW(this)) { |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 361 | throw MakeTypeError('incompatible_method_receiver', |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 362 | ['DataView.buffer', this]); |
| 363 | } |
| 364 | return %DataViewGetBuffer(this); |
| 365 | } |
| 366 | |
| 367 | function DataViewGetByteOffset() { |
| 368 | if (!IS_DATAVIEW(this)) { |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 369 | throw MakeTypeError('incompatible_method_receiver', |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 370 | ['DataView.byteOffset', this]); |
| 371 | } |
| 372 | return %DataViewGetByteOffset(this); |
| 373 | } |
| 374 | |
| 375 | function DataViewGetByteLength() { |
| 376 | if (!IS_DATAVIEW(this)) { |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 377 | throw MakeTypeError('incompatible_method_receiver', |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 378 | ['DataView.byteLength', this]); |
| 379 | } |
| 380 | return %DataViewGetByteLength(this); |
| 381 | } |
| 382 | |
machenbach@chromium.org | feecfde | 2013-11-14 10:55:00 +0000 | [diff] [blame] | 383 | macro DATA_VIEW_TYPES(FUNCTION) |
| 384 | FUNCTION(Int8) |
| 385 | FUNCTION(Uint8) |
| 386 | FUNCTION(Int16) |
| 387 | FUNCTION(Uint16) |
| 388 | FUNCTION(Int32) |
| 389 | FUNCTION(Uint32) |
| 390 | FUNCTION(Float32) |
| 391 | FUNCTION(Float64) |
| 392 | endmacro |
| 393 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 394 | function ToPositiveDataViewOffset(offset) { |
| 395 | return ToPositiveInteger(offset, 'invalid_data_view_accessor_offset'); |
| 396 | } |
| 397 | |
machenbach@chromium.org | feecfde | 2013-11-14 10:55:00 +0000 | [diff] [blame] | 398 | |
| 399 | macro DATA_VIEW_GETTER_SETTER(TYPENAME) |
| 400 | function DataViewGetTYPENAME(offset, little_endian) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 401 | if (!IS_DATAVIEW(this)) { |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 402 | throw MakeTypeError('incompatible_method_receiver', |
machenbach@chromium.org | feecfde | 2013-11-14 10:55:00 +0000 | [diff] [blame] | 403 | ['DataView.getTYPENAME', this]); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 404 | } |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 405 | if (%_ArgumentsLength() < 1) { |
| 406 | throw MakeTypeError('invalid_argument'); |
| 407 | } |
machenbach@chromium.org | feecfde | 2013-11-14 10:55:00 +0000 | [diff] [blame] | 408 | return %DataViewGetTYPENAME(this, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 409 | ToPositiveDataViewOffset(offset), |
| 410 | !!little_endian); |
| 411 | } |
| 412 | |
machenbach@chromium.org | feecfde | 2013-11-14 10:55:00 +0000 | [diff] [blame] | 413 | function DataViewSetTYPENAME(offset, value, little_endian) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 414 | if (!IS_DATAVIEW(this)) { |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 415 | throw MakeTypeError('incompatible_method_receiver', |
machenbach@chromium.org | feecfde | 2013-11-14 10:55:00 +0000 | [diff] [blame] | 416 | ['DataView.setTYPENAME', this]); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 417 | } |
jkummerow@chromium.org | fb732b1 | 2013-07-26 10:27:09 +0000 | [diff] [blame] | 418 | if (%_ArgumentsLength() < 2) { |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 419 | throw MakeTypeError('invalid_argument'); |
| 420 | } |
machenbach@chromium.org | feecfde | 2013-11-14 10:55:00 +0000 | [diff] [blame] | 421 | %DataViewSetTYPENAME(this, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 422 | ToPositiveDataViewOffset(offset), |
| 423 | TO_NUMBER_INLINE(value), |
| 424 | !!little_endian); |
| 425 | } |
machenbach@chromium.org | feecfde | 2013-11-14 10:55:00 +0000 | [diff] [blame] | 426 | endmacro |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 427 | |
machenbach@chromium.org | feecfde | 2013-11-14 10:55:00 +0000 | [diff] [blame] | 428 | DATA_VIEW_TYPES(DATA_VIEW_GETTER_SETTER) |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 429 | |
| 430 | function SetupDataView() { |
| 431 | %CheckIsBootstrapping(); |
| 432 | |
| 433 | // Setup the DataView constructor. |
| 434 | %SetCode($DataView, DataViewConstructor); |
| 435 | %FunctionSetPrototype($DataView, new $Object); |
| 436 | |
| 437 | // Set up constructor property on the DataView prototype. |
| 438 | %SetProperty($DataView.prototype, "constructor", $DataView, DONT_ENUM); |
| 439 | |
| 440 | InstallGetter($DataView.prototype, "buffer", DataViewGetBuffer); |
| 441 | InstallGetter($DataView.prototype, "byteOffset", DataViewGetByteOffset); |
| 442 | InstallGetter($DataView.prototype, "byteLength", DataViewGetByteLength); |
| 443 | |
| 444 | InstallFunctions($DataView.prototype, DONT_ENUM, $Array( |
| 445 | "getInt8", DataViewGetInt8, |
| 446 | "setInt8", DataViewSetInt8, |
| 447 | |
| 448 | "getUint8", DataViewGetUint8, |
| 449 | "setUint8", DataViewSetUint8, |
| 450 | |
| 451 | "getInt16", DataViewGetInt16, |
| 452 | "setInt16", DataViewSetInt16, |
| 453 | |
| 454 | "getUint16", DataViewGetUint16, |
| 455 | "setUint16", DataViewSetUint16, |
| 456 | |
| 457 | "getInt32", DataViewGetInt32, |
| 458 | "setInt32", DataViewSetInt32, |
| 459 | |
| 460 | "getUint32", DataViewGetUint32, |
| 461 | "setUint32", DataViewSetUint32, |
| 462 | |
| 463 | "getFloat32", DataViewGetFloat32, |
| 464 | "setFloat32", DataViewSetFloat32, |
| 465 | |
| 466 | "getFloat64", DataViewGetFloat64, |
| 467 | "setFloat64", DataViewSetFloat64 |
| 468 | )); |
| 469 | } |
| 470 | |
| 471 | SetupDataView(); |