typedarray.js 31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28 29
// Flags: --allow-natives-syntax

30 31
// ArrayBuffer

32
function TestByteLength(param, expectedByteLength) {
33
  var ab = new ArrayBuffer(param);
34 35 36 37 38 39 40 41
  assertSame(expectedByteLength, ab.byteLength);
}

function TestArrayBufferCreation() {
  TestByteLength(1, 1);
  TestByteLength(256, 256);
  TestByteLength(2.567, 2);

42
  TestByteLength("abc", 0);
43 44 45

  TestByteLength(0, 0);

46 47 48
  assertThrows(function() { new ArrayBuffer(-10); }, RangeError);
  assertThrows(function() { new ArrayBuffer(-2.567); }, RangeError);

49
/* TODO[dslomov]: Reenable the test
50
  assertThrows(function() {
51
    var ab1 = new ArrayBuffer(0xFFFFFFFFFFFF)
52
  }, RangeError);
53
*/
54

55
  var ab = new ArrayBuffer();
56
  assertSame(0, ab.byteLength);
57 58
  assertEquals("[object ArrayBuffer]",
      Object.prototype.toString.call(ab));
59 60 61 62 63
}

TestArrayBufferCreation();

function TestByteLengthNotWritable() {
64
  var ab = new ArrayBuffer(1024);
65 66 67 68 69 70 71 72
  assertSame(1024, ab.byteLength);

  assertThrows(function() { "use strict"; ab.byteLength = 42; }, TypeError);
}

TestByteLengthNotWritable();

function TestSlice(expectedResultLen, initialLen, start, end) {
73
  var ab = new ArrayBuffer(initialLen);
74 75 76 77
  var a1 = new Uint8Array(ab);
  for (var i = 0; i < a1.length; i++) {
    a1[i] = 0xCA;
  }
78 79
  var slice = ab.slice(start, end);
  assertSame(expectedResultLen, slice.byteLength);
80 81 82 83
  var a2 = new Uint8Array(slice);
  for (var i = 0; i < a2.length; i++) {
    assertSame(0xCA, a2[i]);
  }
84 85 86
}

function TestArrayBufferSlice() {
87
  var ab = new ArrayBuffer(1024);
88 89 90 91 92 93 94 95 96
  var ab1 = ab.slice(512, 1024);
  assertSame(512, ab1.byteLength);

  TestSlice(512, 1024, 512, 1024);
  TestSlice(512, 1024, 512);

  TestSlice(0, 0, 1, 20);
  TestSlice(100, 100, 0, 100);
  TestSlice(100, 100, 0, 1000);
97

98 99 100 101 102 103 104 105 106 107
  TestSlice(0, 100, 5, 1);

  TestSlice(1, 100, -11, -10);
  TestSlice(9, 100, -10, 99);
  TestSlice(0, 100, -10, 80);
  TestSlice(10, 100, 80, -10);

  TestSlice(10, 100, 90, "100");
  TestSlice(10, 100, "90", "100");

108
  TestSlice(0,  100, 90, "abc");
109 110 111 112 113 114 115 116 117 118 119 120 121
  TestSlice(10, 100, "abc", 10);

  TestSlice(10, 100, 0.96, 10.96);
  TestSlice(10, 100, 0.96, 10.01);
  TestSlice(10, 100, 0.01, 10.01);
  TestSlice(10, 100, 0.01, 10.96);

  TestSlice(10, 100, 90);
  TestSlice(10, 100, -10);
}

TestArrayBufferSlice();

122 123
// Typed arrays

124 125 126
function TestTypedArray(constr, elementSize, typicalElement) {
  assertSame(elementSize, constr.BYTES_PER_ELEMENT);

127
  var ab = new ArrayBuffer(256*elementSize);
128

129
  var a0 = new constr(30);
130 131 132
  assertEquals("[object " + constr.name + "]",
      Object.prototype.toString.call(a0));

133
  assertTrue(ArrayBuffer.isView(a0));
134 135 136 137 138 139
  assertSame(elementSize, a0.BYTES_PER_ELEMENT);
  assertSame(30, a0.length);
  assertSame(30*elementSize, a0.byteLength);
  assertSame(0, a0.byteOffset);
  assertSame(30*elementSize, a0.buffer.byteLength);

140
  var aLen0 = new constr(0);
141 142 143 144 145 146
  assertSame(elementSize, aLen0.BYTES_PER_ELEMENT);
  assertSame(0, aLen0.length);
  assertSame(0, aLen0.byteLength);
  assertSame(0, aLen0.byteOffset);
  assertSame(0, aLen0.buffer.byteLength);

147
  var aOverBufferLen0 = new constr(ab, 128*elementSize, 0);
148 149 150 151 152 153
  assertSame(ab, aOverBufferLen0.buffer);
  assertSame(elementSize, aOverBufferLen0.BYTES_PER_ELEMENT);
  assertSame(0, aOverBufferLen0.length);
  assertSame(0, aOverBufferLen0.byteLength);
  assertSame(128*elementSize, aOverBufferLen0.byteOffset);

154
  var a1 = new constr(ab, 128*elementSize, 128);
155 156 157 158 159 160 161
  assertSame(ab, a1.buffer);
  assertSame(elementSize, a1.BYTES_PER_ELEMENT);
  assertSame(128, a1.length);
  assertSame(128*elementSize, a1.byteLength);
  assertSame(128*elementSize, a1.byteOffset);


162
  var a2 = new constr(ab, 64*elementSize, 128);
163 164 165 166 167 168
  assertSame(ab, a2.buffer);
  assertSame(elementSize, a2.BYTES_PER_ELEMENT);
  assertSame(128, a2.length);
  assertSame(128*elementSize, a2.byteLength);
  assertSame(64*elementSize, a2.byteOffset);

169
  var a3 = new constr(ab, 192*elementSize);
170 171 172 173 174
  assertSame(ab, a3.buffer);
  assertSame(64, a3.length);
  assertSame(64*elementSize, a3.byteLength);
  assertSame(192*elementSize, a3.byteOffset);

175
  var a4 = new constr(ab);
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  assertSame(ab, a4.buffer);
  assertSame(256, a4.length);
  assertSame(256*elementSize, a4.byteLength);
  assertSame(0, a4.byteOffset);


  var i;
  for (i = 0; i < 128; i++) {
    a1[i] = typicalElement;
  }

  for (i = 0; i < 128; i++) {
    assertSame(typicalElement, a1[i]);
  }

  for (i = 0; i < 64; i++) {
    assertSame(0, a2[i]);
  }

  for (i = 64; i < 128; i++) {
    assertSame(typicalElement, a2[i]);
  }

  for (i = 0; i < 64; i++) {
    assertSame(typicalElement, a3[i]);
  }

  for (i = 0; i < 128; i++) {
    assertSame(0, a4[i]);
  }

  for (i = 128; i < 256; i++) {
    assertSame(typicalElement, a4[i]);
  }

211
  var aAtTheEnd = new constr(ab, 256*elementSize);
212 213 214 215 216
  assertSame(elementSize, aAtTheEnd.BYTES_PER_ELEMENT);
  assertSame(0, aAtTheEnd.length);
  assertSame(0, aAtTheEnd.byteLength);
  assertSame(256*elementSize, aAtTheEnd.byteOffset);

217
  assertThrows(function () { new constr(ab, 257*elementSize); }, RangeError);
218
  assertThrows(
219
      function () { new constr(ab, 128*elementSize, 192); },
220
      RangeError);
221 222

  if (elementSize !== 1) {
223
    assertThrows(function() { new constr(ab, 128*elementSize - 1, 10); },
224
                 RangeError);
225
    var unalignedArrayBuffer = new ArrayBuffer(10*elementSize + 1);
226
    var goodArray = new constr(unalignedArrayBuffer, 0, 10);
227 228
    assertSame(10, goodArray.length);
    assertSame(10*elementSize, goodArray.byteLength);
229 230
    assertThrows(function() { new constr(unalignedArrayBuffer)}, RangeError);
    assertThrows(function() { new constr(unalignedArrayBuffer, 5*elementSize)},
231 232 233
                 RangeError);
  }

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  var aFromUndef = new constr();
  assertSame(elementSize, aFromUndef.BYTES_PER_ELEMENT);
  assertSame(0, aFromUndef.length);
  assertSame(0*elementSize, aFromUndef.byteLength);
  assertSame(0, aFromUndef.byteOffset);
  assertSame(0*elementSize, aFromUndef.buffer.byteLength);

  var aFromNull = new constr(null);
  assertSame(elementSize, aFromNull.BYTES_PER_ELEMENT);
  assertSame(0, aFromNull.length);
  assertSame(0*elementSize, aFromNull.byteLength);
  assertSame(0, aFromNull.byteOffset);
  assertSame(0*elementSize, aFromNull.buffer.byteLength);

  var aFromBool = new constr(true);
  assertSame(elementSize, aFromBool.BYTES_PER_ELEMENT);
  assertSame(1, aFromBool.length);
  assertSame(1*elementSize, aFromBool.byteLength);
  assertSame(0, aFromBool.byteOffset);
  assertSame(1*elementSize, aFromBool.buffer.byteLength);

255
  var aFromString = new constr("30");
256 257 258 259 260 261
  assertSame(elementSize, aFromString.BYTES_PER_ELEMENT);
  assertSame(30, aFromString.length);
  assertSame(30*elementSize, aFromString.byteLength);
  assertSame(0, aFromString.byteOffset);
  assertSame(30*elementSize, aFromString.buffer.byteLength);

262 263
  assertThrows(function() { new constr(Symbol()); }, TypeError);

264 265
  assertThrows(function() { new constr(-1); }, RangeError);

266 267 268 269
  var jsArray = [];
  for (i = 0; i < 30; i++) {
    jsArray.push(typicalElement);
  }
270
  var aFromArray = new constr(jsArray);
271 272 273 274 275 276 277 278
  assertSame(elementSize, aFromArray.BYTES_PER_ELEMENT);
  assertSame(30, aFromArray.length);
  assertSame(30*elementSize, aFromArray.byteLength);
  assertSame(0, aFromArray.byteOffset);
  assertSame(30*elementSize, aFromArray.buffer.byteLength);
  for (i = 0; i < 30; i++) {
    assertSame(typicalElement, aFromArray[i]);
  }
279 280

  var abLen0 = new ArrayBuffer(0);
281
  var aOverAbLen0 = new constr(abLen0);
282 283 284 285 286
  assertSame(abLen0, aOverAbLen0.buffer);
  assertSame(elementSize, aOverAbLen0.BYTES_PER_ELEMENT);
  assertSame(0, aOverAbLen0.length);
  assertSame(0, aOverAbLen0.byteLength);
  assertSame(0, aOverAbLen0.byteOffset);
287

288
  var aNoParam = new constr();
289 290 291 292
  assertSame(elementSize, aNoParam.BYTES_PER_ELEMENT);
  assertSame(0, aNoParam.length);
  assertSame(0, aNoParam.byteLength);
  assertSame(0, aNoParam.byteOffset);
293 294 295 296

  var a = new constr(ab, 64*elementSize, 128);
  assertEquals("[object " + constr.name + "]",
      Object.prototype.toString.call(a));
297
  var desc = Object.getOwnPropertyDescriptor(
298
      constr.prototype.__proto__, Symbol.toStringTag);
299 300 301 302 303
  assertTrue(desc.configurable);
  assertFalse(desc.enumerable);
  assertFalse(!!desc.writable);
  assertFalse(!!desc.set);
  assertEquals("function", typeof desc.get);
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

  // Test that the constructor can be called with an iterable
  function* gen() { for (var i = 0; i < 10; i++) yield i; }
  var genArr = new constr(gen());
  assertEquals(10, genArr.length);
  assertEquals(0, genArr[0]);
  assertEquals(9, genArr[9]);
  // Arrays can be converted to TypedArrays
  genArr = new constr([1, 2, 3]);
  assertEquals(3, genArr.length);
  assertEquals(1, genArr[0]);
  assertEquals(3, genArr[2]);
  // Redefining Array.prototype[Symbol.iterator] still works
  var arrayIterator = Array.prototype[Symbol.iterator];
  Array.prototype[Symbol.iterator] = gen;
  genArr = new constr([1, 2, 3]);
  assertEquals(10, genArr.length);
  assertEquals(0, genArr[0]);
  assertEquals(9, genArr[9]);
  Array.prototype[Symbol.iterator] = arrayIterator;
  // Other array-like things can be made into a TypedArray
  var myObject = { 0: 5, 1: 6, length: 2 };
  genArr = new constr(myObject);
  assertEquals(2, genArr.length);
  assertEquals(5, genArr[0]);
  assertEquals(6, genArr[1]);
  // Iterator takes precedence over array-like, and the property
  // is read only once.
  var iteratorReadCount = 0;
  Object.defineProperty(myObject, Symbol.iterator, {
    get: function() { iteratorReadCount++; return gen; }
  });
  genArr = new constr(myObject);
  assertEquals(10, genArr.length);
  assertEquals(0, genArr[0]);
  assertEquals(9, genArr[9]);
  assertEquals(1, iteratorReadCount);
341 342 343

  // Modified %ArrayIteratorPrototype%.next() method is honoured (v8:5699)
  const ArrayIteratorPrototype = Object.getPrototypeOf([][Symbol.iterator]());
344 345
  const ArrayIteratorPrototypeNextDescriptor =
      Object.getOwnPropertyDescriptor(ArrayIteratorPrototype, 'next');
346 347 348 349 350 351
  const ArrayIteratorPrototypeNext = ArrayIteratorPrototype.next;
  ArrayIteratorPrototype.next = function() {
    return { done: true };
  };
  genArr = new constr([1, 2, 3]);
  assertEquals(0, genArr.length);
352

353 354
  ArrayIteratorPrototype.next = ArrayIteratorPrototypeNext;

355 356 357 358 359 360 361 362 363 364 365 366 367
  // Modified %ArrayIteratorPrototype%.next() is only loaded during the iterator
  // prologue.
  let nextMethod = ArrayIteratorPrototypeNext;
  let getNextCount = 0;
  Object.defineProperty(ArrayIteratorPrototype, 'next', {
    get() {
      getNextCount++;
      return nextMethod;
    },
    set(v) { nextMethod = v; },
    configurable: true
  });

368 369 370 371 372 373 374 375
  genArr = new constr(Object.defineProperty([1, , 3], 1, {
    get() {
      ArrayIteratorPrototype.next = function() {
        return { done: true };
      }
      return 2;
    }
  }));
376 377 378 379
  Object.defineProperty(ArrayIteratorPrototype, 'next',
                        ArrayIteratorPrototypeNextDescriptor);
  assertEquals(1, getNextCount);
  assertEquals(3, genArr.length);
380 381
  assertEquals(1, genArr[0]);
  assertEquals(2, genArr[1]);
382
  assertEquals(3, genArr[2]);
383
  ArrayIteratorPrototype.next = ArrayIteratorPrototypeNext;
384 385
}

386 387 388 389 390 391 392 393
TestTypedArray(Uint8Array, 1, 0xFF);
TestTypedArray(Int8Array, 1, -0x7F);
TestTypedArray(Uint16Array, 2, 0xFFFF);
TestTypedArray(Int16Array, 2, -0x7FFF);
TestTypedArray(Uint32Array, 4, 0xFFFFFFFF);
TestTypedArray(Int32Array, 4, -0x7FFFFFFF);
TestTypedArray(Float32Array, 4, 0.5);
TestTypedArray(Float64Array, 8, 0.5);
394 395
TestTypedArray(Uint8ClampedArray, 1, 0xFF);

396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
function SubarrayTestCase(constructor, item, expectedResultLen, expectedStartIndex,
                          initialLen, start, end) {
  var a = new constructor(initialLen);
  var s = a.subarray(start, end);
  assertSame(constructor, s.constructor);
  assertSame(expectedResultLen, s.length);
  if (s.length > 0) {
    s[0] = item;
    assertSame(item, a[expectedStartIndex]);
  }
}

function TestSubArray(constructor, item) {
  SubarrayTestCase(constructor, item, 512, 512, 1024, 512, 1024);
  SubarrayTestCase(constructor, item, 512, 512, 1024, 512);

  SubarrayTestCase(constructor, item, 0, undefined, 0, 1, 20);
  SubarrayTestCase(constructor, item, 100, 0,       100, 0, 100);
  SubarrayTestCase(constructor, item, 100, 0,       100,  0, 1000);
  SubarrayTestCase(constructor, item, 0, undefined, 100, 5, 1);

  SubarrayTestCase(constructor, item, 1, 89,        100, -11, -10);
  SubarrayTestCase(constructor, item, 9, 90,        100, -10, 99);
  SubarrayTestCase(constructor, item, 0, undefined, 100, -10, 80);
  SubarrayTestCase(constructor, item, 10,80,        100, 80, -10);

  SubarrayTestCase(constructor, item, 10,90,        100, 90, "100");
  SubarrayTestCase(constructor, item, 10,90,        100, "90", "100");

  SubarrayTestCase(constructor, item, 0, undefined, 100, 90, "abc");
  SubarrayTestCase(constructor, item, 10,0,         100, "abc", 10);

  SubarrayTestCase(constructor, item, 10,0,         100, 0.96, 10.96);
  SubarrayTestCase(constructor, item, 10,0,         100, 0.96, 10.01);
  SubarrayTestCase(constructor, item, 10,0,         100, 0.01, 10.01);
  SubarrayTestCase(constructor, item, 10,0,         100, 0.01, 10.96);


  SubarrayTestCase(constructor, item, 10,90,        100, 90);
  SubarrayTestCase(constructor, item, 10,90,        100, -10);
436 437 438 439 440

  var method = constructor.prototype.subarray;
  method.call(new constructor(100), 0, 100);
  var o = {};
  assertThrows(function() { method.call(o, 0, 100); }, TypeError);
441 442 443 444 445 446 447 448 449 450 451 452
}

TestSubArray(Uint8Array, 0xFF);
TestSubArray(Int8Array, -0x7F);
TestSubArray(Uint16Array, 0xFFFF);
TestSubArray(Int16Array, -0x7FFF);
TestSubArray(Uint32Array, 0xFFFFFFFF);
TestSubArray(Int32Array, -0x7FFFFFFF);
TestSubArray(Float32Array, 0.5);
TestSubArray(Float64Array, 0.5);
TestSubArray(Uint8ClampedArray, 0xFF);

453 454 455 456 457 458 459 460
function TestTypedArrayOutOfRange(constructor, value, result) {
  var a = new constructor(1);
  a[0] = value;
  assertSame(result, a[0]);
}

TestTypedArrayOutOfRange(Uint8Array, 0x1FA, 0xFA);
TestTypedArrayOutOfRange(Uint8Array, -1, 0xFF);
461

462 463 464 465 466 467 468 469
TestTypedArrayOutOfRange(Int8Array, 0x1FA, 0x7A - 0x80);

TestTypedArrayOutOfRange(Uint16Array, 0x1FFFA, 0xFFFA);
TestTypedArrayOutOfRange(Uint16Array, -1, 0xFFFF);
TestTypedArrayOutOfRange(Int16Array, 0x1FFFA, 0x7FFA - 0x8000);

TestTypedArrayOutOfRange(Uint32Array, 0x1FFFFFFFA, 0xFFFFFFFA);
TestTypedArrayOutOfRange(Uint32Array, -1, 0xFFFFFFFF);
470
TestTypedArrayOutOfRange(Int32Array, 0x1FFFFFFFA, 0x7FFFFFFA - 0x80000000);
471 472 473

TestTypedArrayOutOfRange(Uint8ClampedArray, 0x1FA, 0xFF);
TestTypedArrayOutOfRange(Uint8ClampedArray, -1, 0);
474

475 476 477 478 479 480 481 482 483 484 485 486 487
var typedArrayConstructors = [
  Uint8Array,
  Int8Array,
  Uint16Array,
  Int16Array,
  Uint32Array,
  Int32Array,
  Uint8ClampedArray,
  Float32Array,
  Float64Array];

function TestPropertyTypeChecks(constructor) {
  function CheckProperty(name) {
488
    assertThrows(function() { 'use strict'; new constructor(10)[name] = 0; })
489
    var d = Object.getOwnPropertyDescriptor(constructor.prototype.__proto__, name);
490
    var o = {};
491
    assertThrows(function() {d.get.call(o);}, TypeError);
492 493 494
    for (var i = 0; i < typedArrayConstructors.length; i++) {
      var ctor = typedArrayConstructors[i];
      var a = new ctor(10);
495
      d.get.call(a); // shouldn't throw
496 497 498 499 500 501 502 503 504
    }
  }

  CheckProperty("buffer");
  CheckProperty("byteOffset");
  CheckProperty("byteLength");
  CheckProperty("length");
}

505
for(i = 0; i < typedArrayConstructors.length; i++) {
506 507 508
  TestPropertyTypeChecks(typedArrayConstructors[i]);
}

509

510 511 512 513 514 515 516 517 518
function TestTypedArraySet() {
  // Test array.set in different combinations.

  function assertArrayPrefix(expected, array) {
    for (var i = 0; i < expected.length; ++i) {
      assertEquals(expected[i], array[i]);
    }
  }

519 520 521 522 523 524 525 526 527 528
  a = new Uint32Array();
  a.set('');
  assertEquals(0, a.length);

  assertThrows(() => a.set('abc'), RangeError);

  a = new Uint8Array(3);
  a.set('123');
  assertArrayEquals([1, 2, 3], a);

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
  var a11 = new Int16Array([1, 2, 3, 4, 0, -1])
  var a12 = new Uint16Array(15)
  a12.set(a11, 3)
  assertArrayPrefix([0, 0, 0, 1, 2, 3, 4, 0, 0xffff, 0, 0], a12)
  assertThrows(function(){ a11.set(a12) })

  var a21 = [1, undefined, 10, NaN, 0, -1, {valueOf: function() {return 3}}]
  var a22 = new Int32Array(12)
  a22.set(a21, 2)
  assertArrayPrefix([0, 0, 1, 0, 10, 0, 0, -1, 3, 0], a22)

  var a31 = new Float32Array([2, 4, 6, 8, 11, NaN, 1/0, -3])
  var a32 = a31.subarray(2, 6)
  a31.set(a32, 4)
  assertArrayPrefix([2, 4, 6, 8, 6, 8, 11, NaN], a31)
  assertArrayPrefix([6, 8, 6, 8], a32)

  var a4 = new Uint8ClampedArray([3,2,5,6])
  a4.set(a4)
  assertArrayPrefix([3, 2, 5, 6], a4)

  // Cases with overlapping backing store but different element sizes.
  var b = new ArrayBuffer(4)
  var a5 = new Int16Array(b)
  var a50 = new Int8Array(b)
  var a51 = new Int8Array(b, 0, 2)
  var a52 = new Int8Array(b, 1, 2)
  var a53 = new Int8Array(b, 2, 2)
557
  var a54 = new Int8Array(b, 0, 0)
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588

  a5.set([0x5050, 0x0a0a])
  assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50)
  assertArrayPrefix([0x50, 0x50], a51)
  assertArrayPrefix([0x50, 0x0a], a52)
  assertArrayPrefix([0x0a, 0x0a], a53)

  a50.set([0x50, 0x50, 0x0a, 0x0a])
  a51.set(a5)
  assertArrayPrefix([0x50, 0x0a, 0x0a, 0x0a], a50)

  a50.set([0x50, 0x50, 0x0a, 0x0a])
  a52.set(a5)
  assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50)

  a50.set([0x50, 0x50, 0x0a, 0x0a])
  a53.set(a5)
  assertArrayPrefix([0x50, 0x50, 0x50, 0x0a], a50)

  a50.set([0x50, 0x51, 0x0a, 0x0b])
  a5.set(a51)
  assertArrayPrefix([0x0050, 0x0051], a5)

  a50.set([0x50, 0x51, 0x0a, 0x0b])
  a5.set(a52)
  assertArrayPrefix([0x0051, 0x000a], a5)

  a50.set([0x50, 0x51, 0x0a, 0x0b])
  a5.set(a53)
  assertArrayPrefix([0x000a, 0x000b], a5)

589 590 591 592
  a50.set([0x50, 0x51, 0x0a, 0x0b])
  a5.set(a54, 0)
  assertArrayPrefix([0x50, 0x51, 0x0a, 0x0b], a50)

593 594 595 596 597 598 599 600 601 602
  // Mixed types of same size.
  var a61 = new Float32Array([1.2, 12.3])
  var a62 = new Int32Array(2)
  a62.set(a61)
  assertArrayPrefix([1, 12], a62)
  a61.set(a62)
  assertArrayPrefix([1, 12], a61)

  // Invalid source
  var a = new Uint16Array(50);
603 604 605 606 607 608 609
  var expected = [];
  for (i = 0; i < 50; i++) {
    a[i] = i;
    expected.push(i);
  }
  a.set({});
  assertArrayPrefix(expected, a);
610 611
  assertThrows(function() { a.set.call({}) }, TypeError);
  assertThrows(function() { a.set.call([]) }, TypeError);
612 613 614

  assertThrows(function() { a.set(0); }, TypeError);
  assertThrows(function() { a.set(0, 1); }, TypeError);
615 616

  assertEquals(1, a.set.length);
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631

  // Shared buffer that does not overlap.
  var buf = new ArrayBuffer(32);
  var a101 = new Int8Array(buf, 0, 16);
  var b101 = new Uint8Array(buf, 16);
  b101[0] = 42;
  a101.set(b101);
  assertArrayPrefix([42], a101);

  buf = new ArrayBuffer(32);
  var a101 = new Int8Array(buf, 0, 16);
  var b101 = new Uint8Array(buf, 16);
  a101[0] = 42;
  b101.set(a101);
  assertArrayPrefix([42], b101);
632 633 634 635 636 637 638

  // Detached array buffer when accessing a source element
  var a111 = new Int8Array(100);
  var evilarr = new Array(100);
  var detached = false;
  evilarr[1] = {
    [Symbol.toPrimitive]() {
639
      %ArrayBufferDetach(a111.buffer);
640 641 642 643 644 645
      detached = true;
      return 1;
    }
  };
  assertThrows(() => a111.set(evilarr), TypeError);
  assertEquals(true, detached);
646 647 648 649 650

  // Check if the target is a typed array before converting offset to integer
  var tmp = {
    [Symbol.toPrimitive]() {
      assertUnreachable("Parameter should not be processed when " +
651
                        "array.[[ViewedArrayBuffer]] is detached.");
652 653 654 655 656
      return 1;
    }
  };
  assertThrows(() => Int8Array.prototype.set.call(1, tmp), TypeError);
  assertThrows(() => Int8Array.prototype.set.call([], tmp), TypeError);
657 658 659

  // Detached array buffer when converting offset.
  {
660 661 662 663 664
    for (const klass of typedArrayConstructors) {
      const xs = new klass(10);
      let detached = false;
      const offset = {
        [Symbol.toPrimitive]() {
665
          %ArrayBufferDetach(xs.buffer);
666 667 668 669 670 671 672 673 674 675 676 677 678 679
          detached = true;
          return 0;
        }
      };
      assertThrows(() => xs.set(xs, offset), TypeError);
      assertEquals(true, detached);
    }
  }

  // Detached JSTypedArray source argument.
  {
    for (const klass of typedArrayConstructors) {
      const a = new klass(2);
      for (let i = 0; i < a.length; i++) a[i] = i;
680
      %ArrayBufferDetach(a.buffer);
681 682 683 684

      const b = new klass(2);
      assertThrows(() => b.set(a), TypeError);
    }
685 686 687 688
  }

  // Various offset edge cases.
  {
689 690 691 692 693 694 695 696 697
    for (const klass of typedArrayConstructors) {
      const xs = new klass(10);
      assertThrows(() => xs.set(xs, -1), RangeError);
      assertThrows(() => xs.set(xs, -1 * 2**64), RangeError);
      xs.set(xs, -0.0);
      xs.set(xs, 0.0);
      xs.set(xs, 0.5);
      assertThrows(() => xs.set(xs, 2**64), RangeError);
    }
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
  }

  // Exhaustively test elements kind combinations with JSArray source arg.
  {
    const kSize = 3;
    const targets = typedArrayConstructors.map(klass => new klass(kSize));
    const sources = [ [0,1,2]        // PACKED_SMI
                    , [0,,2]         // HOLEY_SMI
                    , [0.1,0.2,0.3]  // PACKED_DOUBLE
                    , [0.1,,0.3]     // HOLEY_DOUBLE
                    , [{},{},{}]     // PACKED
                    , [{},,{}]       // HOLEY
                    , []             // DICTIONARY (patched later)
                    ];

    // Migrate to DICTIONARY_ELEMENTS.
    Object.defineProperty(sources[6], 0, {});

    assertTrue(%HasSmiElements(sources[0]));
    assertTrue(%HasFastElements(sources[0]) && !%HasHoleyElements(sources[0]));
    assertTrue(%HasSmiElements(sources[1]));
    assertTrue(%HasFastElements(sources[1]) && %HasHoleyElements(sources[1]));
    assertTrue(%HasDoubleElements(sources[2]));
    assertTrue(%HasFastElements(sources[2]) && !%HasHoleyElements(sources[2]));
    assertTrue(%HasDoubleElements(sources[3]));
    assertTrue(%HasFastElements(sources[3]) && %HasHoleyElements(sources[3]));
    assertTrue(%HasObjectElements(sources[4]));
    assertTrue(%HasFastElements(sources[4]) && !%HasHoleyElements(sources[4]));
    assertTrue(%HasObjectElements(sources[4]));
    assertTrue(%HasFastElements(sources[4]) && !%HasHoleyElements(sources[4]));
    assertTrue(%HasObjectElements(sources[5]));
    assertTrue(%HasFastElements(sources[5]) && %HasHoleyElements(sources[5]));
    assertTrue(%HasDictionaryElements(sources[6]));

    for (const target of targets) {
      for (const source of sources) {
        target.set(source);
        %HeapObjectVerify(target);
        %HeapObjectVerify(source);
      }
    }
  }
740 741 742 743
}

TestTypedArraySet();

744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
function TestTypedArraysWithIllegalIndices() {
  var a = new Int32Array(100);

  a[-10] = 10;
  assertEquals(undefined, a[-10]);
  a["-10"] = 10;
  assertEquals(undefined, a["-10"]);

  var s = "    -10";
  a[s] = 10;
  assertEquals(10, a[s]);
  var s1 = "    -10   ";
  a[s] = 10;
  assertEquals(10, a[s]);

  a["-1e2"] = 10;
  assertEquals(10, a["-1e2"]);
  assertEquals(undefined, a[-1e2]);

763 764 765 766 767 768 769
  a["-0"] = 256;
  var s2 = "     -0";
  a[s2] = 255;
  assertEquals(undefined, a["-0"]);
  assertEquals(255, a[s2]);
  assertEquals(0, a[-0]);

770 771 772
  a[-Infinity] = 50;
  assertEquals(undefined, a[-Infinity]);

773
  a[1.5] = 10;
dcarney's avatar
dcarney committed
774
  assertEquals(undefined, a[1.5]);
775 776
  var nan = Math.sqrt(-1);
  a[nan] = 5;
dcarney's avatar
dcarney committed
777
  assertEquals(undefined, a[nan]);
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810

  var x = 0;
  var y = -0;
  assertEquals(Infinity, 1/x);
  assertEquals(-Infinity, 1/y);
  a[x] = 5;
  a[y] = 27;
  assertEquals(27, a[x]);
  assertEquals(27, a[y]);
}

TestTypedArraysWithIllegalIndices();

function TestTypedArraysWithIllegalIndicesStrict() {
  'use strict';
  var a = new Int32Array(100);

  a[-10] = 10;
  assertEquals(undefined, a[-10]);
  a["-10"] = 10;
  assertEquals(undefined, a["-10"]);

  var s = "    -10";
  a[s] = 10;
  assertEquals(10, a[s]);
  var s1 = "    -10   ";
  a[s] = 10;
  assertEquals(10, a[s]);

  a["-1e2"] = 10;
  assertEquals(10, a["-1e2"]);
  assertEquals(undefined, a[-1e2]);

811 812 813 814 815 816 817
  a["-0"] = 256;
  var s2 = "     -0";
  a[s2] = 255;
  assertEquals(undefined, a["-0"]);
  assertEquals(255, a[s2]);
  assertEquals(0, a[-0]);

818 819 820 821 822
  /* Chromium bug: 424619
   * a[-Infinity] = 50;
   * assertEquals(undefined, a[-Infinity]);
   */
  a[1.5] = 10;
dcarney's avatar
dcarney committed
823
  assertEquals(undefined, a[1.5]);
824 825
  var nan = Math.sqrt(-1);
  a[nan] = 5;
dcarney's avatar
dcarney committed
826
  assertEquals(undefined, a[nan]);
827 828 829 830 831 832 833 834 835 836 837 838 839

  var x = 0;
  var y = -0;
  assertEquals(Infinity, 1/x);
  assertEquals(-Infinity, 1/y);
  a[x] = 5;
  a[y] = 27;
  assertEquals(27, a[x]);
  assertEquals(27, a[y]);
}

TestTypedArraysWithIllegalIndicesStrict();

840 841 842 843 844
// DataView
function TestDataViewConstructor() {
  var ab = new ArrayBuffer(256);

  var d1 = new DataView(ab, 1, 255);
845
  assertTrue(ArrayBuffer.isView(d1));
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
  assertSame(ab, d1.buffer);
  assertSame(1, d1.byteOffset);
  assertSame(255, d1.byteLength);

  var d2 = new DataView(ab, 2);
  assertSame(ab, d2.buffer);
  assertSame(2, d2.byteOffset);
  assertSame(254, d2.byteLength);

  var d3 = new DataView(ab);
  assertSame(ab, d3.buffer);
  assertSame(0, d3.byteOffset);
  assertSame(256, d3.byteLength);

  var d3a = new DataView(ab, 1, 0);
  assertSame(ab, d3a.buffer);
  assertSame(1, d3a.byteOffset);
  assertSame(0, d3a.byteLength);

  var d3b = new DataView(ab, 256, 0);
  assertSame(ab, d3b.buffer);
  assertSame(256, d3b.byteOffset);
  assertSame(0, d3b.byteLength);

  var d3c = new DataView(ab, 256);
  assertSame(ab, d3c.buffer);
  assertSame(256, d3c.byteOffset);
  assertSame(0, d3c.byteLength);

875
  var d4 = new DataView(ab, 1, 3.1415926);
876
  assertSame(ab, d4.buffer);
877 878
  assertSame(1, d4.byteOffset);
  assertSame(3, d4.byteLength);
879 880 881


  // error cases
882
  assertThrows(function() { new DataView(ab, -1); }, RangeError);
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
  assertThrows(function() { new DataView(); }, TypeError);
  assertThrows(function() { new DataView([]); }, TypeError);
  assertThrows(function() { new DataView(ab, 257); }, RangeError);
  assertThrows(function() { new DataView(ab, 1, 1024); }, RangeError);
}

TestDataViewConstructor();

function TestDataViewPropertyTypeChecks() {
  var a = new DataView(new ArrayBuffer(10));
  function CheckProperty(name) {
    var d = Object.getOwnPropertyDescriptor(DataView.prototype, name);
    var o = {}
    assertThrows(function() {d.get.call(o);}, TypeError);
    d.get.call(a); // shouldn't throw
  }

  CheckProperty("buffer");
  CheckProperty("byteOffset");
  CheckProperty("byteLength");
903 904 905 906 907 908 909 910 911 912 913 914 915

  function CheckGetSetLength(name) {
    assertEquals(1, DataView.prototype["get" + name].length);
    assertEquals(2, DataView.prototype["set" + name].length);
  }
  CheckGetSetLength("Int8");
  CheckGetSetLength("Uint8");
  CheckGetSetLength("Int16");
  CheckGetSetLength("Uint16");
  CheckGetSetLength("Int32");
  CheckGetSetLength("Uint32");
  CheckGetSetLength("Float32");
  CheckGetSetLength("Float64");
916 917 918 919 920
}


TestDataViewPropertyTypeChecks();

921 922 923 924 925 926 927 928 929 930 931 932 933

function TestDataViewToStringTag() {
  var a = new DataView(new ArrayBuffer(10));
  assertEquals("[object DataView]", Object.prototype.toString.call(a));
  var desc = Object.getOwnPropertyDescriptor(
      DataView.prototype, Symbol.toStringTag);
  assertTrue(desc.configurable);
  assertFalse(desc.enumerable);
  assertFalse(desc.writable);
  assertEquals("DataView", desc.value);
}


934 935
// General tests for properties

936
// Test property attribute [[Enumerable]]
937
function TestEnumerable(func, obj) {
938 939 940 941 942 943 944
  function props(x) {
    var array = [];
    for (var p in x) array.push(p);
    return array.sort();
  }
  assertArrayEquals([], props(func));
  assertArrayEquals([], props(func.prototype));
945 946
  if (obj)
    assertArrayEquals([], props(obj));
947
}
948
TestEnumerable(ArrayBuffer, new ArrayBuffer());
949
for(i = 0; i < typedArrayConstructors.length; i++) {
950 951
  TestEnumerable(typedArrayConstructors[i]);
}
952
TestEnumerable(DataView, new DataView(new ArrayBuffer()));
953 954 955 956 957 958 959 960

// Test arbitrary properties on ArrayBuffer
function TestArbitrary(m) {
  function TestProperty(map, property, value) {
    map[property] = value;
    assertEquals(value, map[property]);
  }
  for (var i = 0; i < 20; i++) {
961
    TestProperty(m, 'key' + i, 'val' + i);
962 963 964
    TestProperty(m, 'foo' + i, 'bar' + i);
  }
}
965
TestArbitrary(new ArrayBuffer(256));
966 967
for(i = 0; i < typedArrayConstructors.length; i++) {
  TestArbitrary(new typedArrayConstructors[i](10));
968
}
969
TestArbitrary(new DataView(new ArrayBuffer(256)));
970

971 972

// Test direct constructor call
973 974
assertThrows(function() { ArrayBuffer(); }, TypeError);
assertThrows(function() { DataView(new ArrayBuffer()); }, TypeError);
975 976 977 978 979 980 981 982 983 984

function TestNonConfigurableProperties(constructor) {
  var arr = new constructor([100])
  assertFalse(Object.getOwnPropertyDescriptor(arr,"0").configurable)
  assertFalse(delete arr[0])
}

for(i = 0; i < typedArrayConstructors.length; i++) {
  TestNonConfigurableProperties(typedArrayConstructors[i]);
}
985 986 987 988 989 990 991 992 993

(function TestInitialization() {
  for (var i = 0; i <= 128; i++) {
    var arr = new Uint8Array(i);
    for (var j = 0; j < i; j++) {
      assertEquals(0, arr[j]);
    }
  }
})();
994 995 996 997 998 999 1000

(function TestBufferLengthTooLong() {
  try {
    var buf = new ArrayBuffer(2147483648);
  } catch (e) {
    // The ArrayBuffer allocation fails on 32-bit archs, so no need to try to
    // construct the typed array.
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    return;
  }
  assertThrows(function() {
    new Int8Array(buf);
  }, RangeError);
})();

(function TestByteLengthErrorMessage() {
  try {
    new Uint32Array(new ArrayBuffer(17));
  } catch (e) {
    assertEquals("byte length of Uint32Array should be a multiple of 4",
                 e.message);
1014 1015
  }
})();
1016 1017 1018 1019 1020 1021 1022 1023 1024

// Regression test 761654
assertThrows(function LargeSourceArray() {
  let v0 = {};
  v0.length =  2 ** 32; // too large for uint32
  let a = new Int8Array();

  a.set(v0);
});
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050

function TestMapCustomSpeciesConstructor(constructor) {
  const sample = new constructor([40, 42, 42]);
  let result, ctorThis;

  sample.constructor = {};
  sample.constructor[Symbol.species] = function(count) {
    result = arguments;
    ctorThis = this;
    return new constructor(count);
  };

  sample.map(function(v) { return v; });

  assertSame(result.length, 1, "called with 1 argument");
  assertSame(result[0], 3, "[0] is the new captured length");

  assertTrue(
    ctorThis instanceof sample.constructor[Symbol.species],
    "`this` value in the @@species fn is an instance of the function itself"
  );
};

for(i = 0; i < typedArrayConstructors.length; i++) {
  TestPropertyTypeChecks(typedArrayConstructors[i]);
}