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