OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --allow-natives-syntax |
| 6 |
| 7 // Disable the neutering protector. |
| 8 %ArrayBufferNeuter(new ArrayBuffer(1024)); |
| 9 |
| 10 // ArrayBuffer |
| 11 |
| 12 function TestByteLength(param, expectedByteLength) { |
| 13 var ab = new ArrayBuffer(param); |
| 14 assertSame(expectedByteLength, ab.byteLength); |
| 15 } |
| 16 |
| 17 function TestArrayBufferCreation() { |
| 18 TestByteLength(1, 1); |
| 19 TestByteLength(256, 256); |
| 20 TestByteLength(2.567, 2); |
| 21 |
| 22 TestByteLength("abc", 0); |
| 23 |
| 24 TestByteLength(0, 0); |
| 25 |
| 26 assertThrows(function() { new ArrayBuffer(-10); }, RangeError); |
| 27 assertThrows(function() { new ArrayBuffer(-2.567); }, RangeError); |
| 28 |
| 29 /* TODO[dslomov]: Reenable the test |
| 30 assertThrows(function() { |
| 31 var ab1 = new ArrayBuffer(0xFFFFFFFFFFFF) |
| 32 }, RangeError); |
| 33 */ |
| 34 |
| 35 var ab = new ArrayBuffer(); |
| 36 assertSame(0, ab.byteLength); |
| 37 assertEquals("[object ArrayBuffer]", |
| 38 Object.prototype.toString.call(ab)); |
| 39 } |
| 40 |
| 41 TestArrayBufferCreation(); |
| 42 |
| 43 function TestByteLengthNotWritable() { |
| 44 var ab = new ArrayBuffer(1024); |
| 45 assertSame(1024, ab.byteLength); |
| 46 |
| 47 assertThrows(function() { "use strict"; ab.byteLength = 42; }, TypeError); |
| 48 } |
| 49 |
| 50 TestByteLengthNotWritable(); |
| 51 |
| 52 function TestSlice(expectedResultLen, initialLen, start, end) { |
| 53 var ab = new ArrayBuffer(initialLen); |
| 54 var a1 = new Uint8Array(ab); |
| 55 for (var i = 0; i < a1.length; i++) { |
| 56 a1[i] = 0xCA; |
| 57 } |
| 58 var slice = ab.slice(start, end); |
| 59 assertSame(expectedResultLen, slice.byteLength); |
| 60 var a2 = new Uint8Array(slice); |
| 61 for (var i = 0; i < a2.length; i++) { |
| 62 assertSame(0xCA, a2[i]); |
| 63 } |
| 64 } |
| 65 |
| 66 function TestArrayBufferSlice() { |
| 67 var ab = new ArrayBuffer(1024); |
| 68 var ab1 = ab.slice(512, 1024); |
| 69 assertSame(512, ab1.byteLength); |
| 70 |
| 71 TestSlice(512, 1024, 512, 1024); |
| 72 TestSlice(512, 1024, 512); |
| 73 |
| 74 TestSlice(0, 0, 1, 20); |
| 75 TestSlice(100, 100, 0, 100); |
| 76 TestSlice(100, 100, 0, 1000); |
| 77 |
| 78 TestSlice(0, 100, 5, 1); |
| 79 |
| 80 TestSlice(1, 100, -11, -10); |
| 81 TestSlice(9, 100, -10, 99); |
| 82 TestSlice(0, 100, -10, 80); |
| 83 TestSlice(10, 100, 80, -10); |
| 84 |
| 85 TestSlice(10, 100, 90, "100"); |
| 86 TestSlice(10, 100, "90", "100"); |
| 87 |
| 88 TestSlice(0, 100, 90, "abc"); |
| 89 TestSlice(10, 100, "abc", 10); |
| 90 |
| 91 TestSlice(10, 100, 0.96, 10.96); |
| 92 TestSlice(10, 100, 0.96, 10.01); |
| 93 TestSlice(10, 100, 0.01, 10.01); |
| 94 TestSlice(10, 100, 0.01, 10.96); |
| 95 |
| 96 TestSlice(10, 100, 90); |
| 97 TestSlice(10, 100, -10); |
| 98 } |
| 99 |
| 100 TestArrayBufferSlice(); |
| 101 |
| 102 // Typed arrays |
| 103 |
| 104 function TestTypedArray(constr, elementSize, typicalElement) { |
| 105 assertSame(elementSize, constr.BYTES_PER_ELEMENT); |
| 106 |
| 107 var ab = new ArrayBuffer(256*elementSize); |
| 108 |
| 109 var a0 = new constr(30); |
| 110 assertEquals("[object " + constr.name + "]", |
| 111 Object.prototype.toString.call(a0)); |
| 112 |
| 113 assertTrue(ArrayBuffer.isView(a0)); |
| 114 assertSame(elementSize, a0.BYTES_PER_ELEMENT); |
| 115 assertSame(30, a0.length); |
| 116 assertSame(30*elementSize, a0.byteLength); |
| 117 assertSame(0, a0.byteOffset); |
| 118 assertSame(30*elementSize, a0.buffer.byteLength); |
| 119 |
| 120 var aLen0 = new constr(0); |
| 121 assertSame(elementSize, aLen0.BYTES_PER_ELEMENT); |
| 122 assertSame(0, aLen0.length); |
| 123 assertSame(0, aLen0.byteLength); |
| 124 assertSame(0, aLen0.byteOffset); |
| 125 assertSame(0, aLen0.buffer.byteLength); |
| 126 |
| 127 var aOverBufferLen0 = new constr(ab, 128*elementSize, 0); |
| 128 assertSame(ab, aOverBufferLen0.buffer); |
| 129 assertSame(elementSize, aOverBufferLen0.BYTES_PER_ELEMENT); |
| 130 assertSame(0, aOverBufferLen0.length); |
| 131 assertSame(0, aOverBufferLen0.byteLength); |
| 132 assertSame(128*elementSize, aOverBufferLen0.byteOffset); |
| 133 |
| 134 var a1 = new constr(ab, 128*elementSize, 128); |
| 135 assertSame(ab, a1.buffer); |
| 136 assertSame(elementSize, a1.BYTES_PER_ELEMENT); |
| 137 assertSame(128, a1.length); |
| 138 assertSame(128*elementSize, a1.byteLength); |
| 139 assertSame(128*elementSize, a1.byteOffset); |
| 140 |
| 141 |
| 142 var a2 = new constr(ab, 64*elementSize, 128); |
| 143 assertSame(ab, a2.buffer); |
| 144 assertSame(elementSize, a2.BYTES_PER_ELEMENT); |
| 145 assertSame(128, a2.length); |
| 146 assertSame(128*elementSize, a2.byteLength); |
| 147 assertSame(64*elementSize, a2.byteOffset); |
| 148 |
| 149 var a3 = new constr(ab, 192*elementSize); |
| 150 assertSame(ab, a3.buffer); |
| 151 assertSame(64, a3.length); |
| 152 assertSame(64*elementSize, a3.byteLength); |
| 153 assertSame(192*elementSize, a3.byteOffset); |
| 154 |
| 155 var a4 = new constr(ab); |
| 156 assertSame(ab, a4.buffer); |
| 157 assertSame(256, a4.length); |
| 158 assertSame(256*elementSize, a4.byteLength); |
| 159 assertSame(0, a4.byteOffset); |
| 160 |
| 161 |
| 162 var i; |
| 163 for (i = 0; i < 128; i++) { |
| 164 a1[i] = typicalElement; |
| 165 } |
| 166 |
| 167 for (i = 0; i < 128; i++) { |
| 168 assertSame(typicalElement, a1[i]); |
| 169 } |
| 170 |
| 171 for (i = 0; i < 64; i++) { |
| 172 assertSame(0, a2[i]); |
| 173 } |
| 174 |
| 175 for (i = 64; i < 128; i++) { |
| 176 assertSame(typicalElement, a2[i]); |
| 177 } |
| 178 |
| 179 for (i = 0; i < 64; i++) { |
| 180 assertSame(typicalElement, a3[i]); |
| 181 } |
| 182 |
| 183 for (i = 0; i < 128; i++) { |
| 184 assertSame(0, a4[i]); |
| 185 } |
| 186 |
| 187 for (i = 128; i < 256; i++) { |
| 188 assertSame(typicalElement, a4[i]); |
| 189 } |
| 190 |
| 191 var aAtTheEnd = new constr(ab, 256*elementSize); |
| 192 assertSame(elementSize, aAtTheEnd.BYTES_PER_ELEMENT); |
| 193 assertSame(0, aAtTheEnd.length); |
| 194 assertSame(0, aAtTheEnd.byteLength); |
| 195 assertSame(256*elementSize, aAtTheEnd.byteOffset); |
| 196 |
| 197 assertThrows(function () { new constr(ab, 257*elementSize); }, RangeError); |
| 198 assertThrows( |
| 199 function () { new constr(ab, 128*elementSize, 192); }, |
| 200 RangeError); |
| 201 |
| 202 if (elementSize !== 1) { |
| 203 assertThrows(function() { new constr(ab, 128*elementSize - 1, 10); }, |
| 204 RangeError); |
| 205 var unalignedArrayBuffer = new ArrayBuffer(10*elementSize + 1); |
| 206 var goodArray = new constr(unalignedArrayBuffer, 0, 10); |
| 207 assertSame(10, goodArray.length); |
| 208 assertSame(10*elementSize, goodArray.byteLength); |
| 209 assertThrows(function() { new constr(unalignedArrayBuffer)}, RangeError); |
| 210 assertThrows(function() { new constr(unalignedArrayBuffer, 5*elementSize)}, |
| 211 RangeError); |
| 212 } |
| 213 |
| 214 var aFromUndef = new constr(); |
| 215 assertSame(elementSize, aFromUndef.BYTES_PER_ELEMENT); |
| 216 assertSame(0, aFromUndef.length); |
| 217 assertSame(0*elementSize, aFromUndef.byteLength); |
| 218 assertSame(0, aFromUndef.byteOffset); |
| 219 assertSame(0*elementSize, aFromUndef.buffer.byteLength); |
| 220 |
| 221 var aFromNull = new constr(null); |
| 222 assertSame(elementSize, aFromNull.BYTES_PER_ELEMENT); |
| 223 assertSame(0, aFromNull.length); |
| 224 assertSame(0*elementSize, aFromNull.byteLength); |
| 225 assertSame(0, aFromNull.byteOffset); |
| 226 assertSame(0*elementSize, aFromNull.buffer.byteLength); |
| 227 |
| 228 var aFromBool = new constr(true); |
| 229 assertSame(elementSize, aFromBool.BYTES_PER_ELEMENT); |
| 230 assertSame(1, aFromBool.length); |
| 231 assertSame(1*elementSize, aFromBool.byteLength); |
| 232 assertSame(0, aFromBool.byteOffset); |
| 233 assertSame(1*elementSize, aFromBool.buffer.byteLength); |
| 234 |
| 235 var aFromString = new constr("30"); |
| 236 assertSame(elementSize, aFromString.BYTES_PER_ELEMENT); |
| 237 assertSame(30, aFromString.length); |
| 238 assertSame(30*elementSize, aFromString.byteLength); |
| 239 assertSame(0, aFromString.byteOffset); |
| 240 assertSame(30*elementSize, aFromString.buffer.byteLength); |
| 241 |
| 242 assertThrows(function() { new constr(Symbol()); }, TypeError); |
| 243 |
| 244 var jsArray = []; |
| 245 for (i = 0; i < 30; i++) { |
| 246 jsArray.push(typicalElement); |
| 247 } |
| 248 var aFromArray = new constr(jsArray); |
| 249 assertSame(elementSize, aFromArray.BYTES_PER_ELEMENT); |
| 250 assertSame(30, aFromArray.length); |
| 251 assertSame(30*elementSize, aFromArray.byteLength); |
| 252 assertSame(0, aFromArray.byteOffset); |
| 253 assertSame(30*elementSize, aFromArray.buffer.byteLength); |
| 254 for (i = 0; i < 30; i++) { |
| 255 assertSame(typicalElement, aFromArray[i]); |
| 256 } |
| 257 |
| 258 var abLen0 = new ArrayBuffer(0); |
| 259 var aOverAbLen0 = new constr(abLen0); |
| 260 assertSame(abLen0, aOverAbLen0.buffer); |
| 261 assertSame(elementSize, aOverAbLen0.BYTES_PER_ELEMENT); |
| 262 assertSame(0, aOverAbLen0.length); |
| 263 assertSame(0, aOverAbLen0.byteLength); |
| 264 assertSame(0, aOverAbLen0.byteOffset); |
| 265 |
| 266 var aNoParam = new constr(); |
| 267 assertSame(elementSize, aNoParam.BYTES_PER_ELEMENT); |
| 268 assertSame(0, aNoParam.length); |
| 269 assertSame(0, aNoParam.byteLength); |
| 270 assertSame(0, aNoParam.byteOffset); |
| 271 |
| 272 var a = new constr(ab, 64*elementSize, 128); |
| 273 assertEquals("[object " + constr.name + "]", |
| 274 Object.prototype.toString.call(a)); |
| 275 var desc = Object.getOwnPropertyDescriptor( |
| 276 constr.prototype.__proto__, Symbol.toStringTag); |
| 277 assertTrue(desc.configurable); |
| 278 assertFalse(desc.enumerable); |
| 279 assertFalse(!!desc.writable); |
| 280 assertFalse(!!desc.set); |
| 281 assertEquals("function", typeof desc.get); |
| 282 |
| 283 // Test that the constructor can be called with an iterable |
| 284 function* gen() { for (var i = 0; i < 10; i++) yield i; } |
| 285 var genArr = new constr(gen()); |
| 286 assertEquals(10, genArr.length); |
| 287 assertEquals(0, genArr[0]); |
| 288 assertEquals(9, genArr[9]); |
| 289 // Arrays can be converted to TypedArrays |
| 290 genArr = new constr([1, 2, 3]); |
| 291 assertEquals(3, genArr.length); |
| 292 assertEquals(1, genArr[0]); |
| 293 assertEquals(3, genArr[2]); |
| 294 // Redefining Array.prototype[Symbol.iterator] still works |
| 295 var arrayIterator = Array.prototype[Symbol.iterator]; |
| 296 Array.prototype[Symbol.iterator] = gen; |
| 297 genArr = new constr([1, 2, 3]); |
| 298 assertEquals(10, genArr.length); |
| 299 assertEquals(0, genArr[0]); |
| 300 assertEquals(9, genArr[9]); |
| 301 Array.prototype[Symbol.iterator] = arrayIterator; |
| 302 // Other array-like things can be made into a TypedArray |
| 303 var myObject = { 0: 5, 1: 6, length: 2 }; |
| 304 genArr = new constr(myObject); |
| 305 assertEquals(2, genArr.length); |
| 306 assertEquals(5, genArr[0]); |
| 307 assertEquals(6, genArr[1]); |
| 308 // Iterator takes precedence over array-like, and the property |
| 309 // is read only once. |
| 310 var iteratorReadCount = 0; |
| 311 Object.defineProperty(myObject, Symbol.iterator, { |
| 312 get: function() { iteratorReadCount++; return gen; } |
| 313 }); |
| 314 genArr = new constr(myObject); |
| 315 assertEquals(10, genArr.length); |
| 316 assertEquals(0, genArr[0]); |
| 317 assertEquals(9, genArr[9]); |
| 318 assertEquals(1, iteratorReadCount); |
| 319 } |
| 320 |
| 321 TestTypedArray(Uint8Array, 1, 0xFF); |
| 322 TestTypedArray(Int8Array, 1, -0x7F); |
| 323 TestTypedArray(Uint16Array, 2, 0xFFFF); |
| 324 TestTypedArray(Int16Array, 2, -0x7FFF); |
| 325 TestTypedArray(Uint32Array, 4, 0xFFFFFFFF); |
| 326 TestTypedArray(Int32Array, 4, -0x7FFFFFFF); |
| 327 TestTypedArray(Float32Array, 4, 0.5); |
| 328 TestTypedArray(Float64Array, 8, 0.5); |
| 329 TestTypedArray(Uint8ClampedArray, 1, 0xFF); |
| 330 |
| 331 function SubarrayTestCase(constructor, item, expectedResultLen, expectedStartInd
ex, |
| 332 initialLen, start, end) { |
| 333 var a = new constructor(initialLen); |
| 334 var s = a.subarray(start, end); |
| 335 assertSame(constructor, s.constructor); |
| 336 assertSame(expectedResultLen, s.length); |
| 337 if (s.length > 0) { |
| 338 s[0] = item; |
| 339 assertSame(item, a[expectedStartIndex]); |
| 340 } |
| 341 } |
| 342 |
| 343 function TestSubArray(constructor, item) { |
| 344 SubarrayTestCase(constructor, item, 512, 512, 1024, 512, 1024); |
| 345 SubarrayTestCase(constructor, item, 512, 512, 1024, 512); |
| 346 |
| 347 SubarrayTestCase(constructor, item, 0, undefined, 0, 1, 20); |
| 348 SubarrayTestCase(constructor, item, 100, 0, 100, 0, 100); |
| 349 SubarrayTestCase(constructor, item, 100, 0, 100, 0, 1000); |
| 350 SubarrayTestCase(constructor, item, 0, undefined, 100, 5, 1); |
| 351 |
| 352 SubarrayTestCase(constructor, item, 1, 89, 100, -11, -10); |
| 353 SubarrayTestCase(constructor, item, 9, 90, 100, -10, 99); |
| 354 SubarrayTestCase(constructor, item, 0, undefined, 100, -10, 80); |
| 355 SubarrayTestCase(constructor, item, 10,80, 100, 80, -10); |
| 356 |
| 357 SubarrayTestCase(constructor, item, 10,90, 100, 90, "100"); |
| 358 SubarrayTestCase(constructor, item, 10,90, 100, "90", "100"); |
| 359 |
| 360 SubarrayTestCase(constructor, item, 0, undefined, 100, 90, "abc"); |
| 361 SubarrayTestCase(constructor, item, 10,0, 100, "abc", 10); |
| 362 |
| 363 SubarrayTestCase(constructor, item, 10,0, 100, 0.96, 10.96); |
| 364 SubarrayTestCase(constructor, item, 10,0, 100, 0.96, 10.01); |
| 365 SubarrayTestCase(constructor, item, 10,0, 100, 0.01, 10.01); |
| 366 SubarrayTestCase(constructor, item, 10,0, 100, 0.01, 10.96); |
| 367 |
| 368 |
| 369 SubarrayTestCase(constructor, item, 10,90, 100, 90); |
| 370 SubarrayTestCase(constructor, item, 10,90, 100, -10); |
| 371 |
| 372 var method = constructor.prototype.subarray; |
| 373 method.call(new constructor(100), 0, 100); |
| 374 var o = {}; |
| 375 assertThrows(function() { method.call(o, 0, 100); }, TypeError); |
| 376 } |
| 377 |
| 378 TestSubArray(Uint8Array, 0xFF); |
| 379 TestSubArray(Int8Array, -0x7F); |
| 380 TestSubArray(Uint16Array, 0xFFFF); |
| 381 TestSubArray(Int16Array, -0x7FFF); |
| 382 TestSubArray(Uint32Array, 0xFFFFFFFF); |
| 383 TestSubArray(Int32Array, -0x7FFFFFFF); |
| 384 TestSubArray(Float32Array, 0.5); |
| 385 TestSubArray(Float64Array, 0.5); |
| 386 TestSubArray(Uint8ClampedArray, 0xFF); |
| 387 |
| 388 function TestTypedArrayOutOfRange(constructor, value, result) { |
| 389 var a = new constructor(1); |
| 390 a[0] = value; |
| 391 assertSame(result, a[0]); |
| 392 } |
| 393 |
| 394 TestTypedArrayOutOfRange(Uint8Array, 0x1FA, 0xFA); |
| 395 TestTypedArrayOutOfRange(Uint8Array, -1, 0xFF); |
| 396 |
| 397 TestTypedArrayOutOfRange(Int8Array, 0x1FA, 0x7A - 0x80); |
| 398 |
| 399 TestTypedArrayOutOfRange(Uint16Array, 0x1FFFA, 0xFFFA); |
| 400 TestTypedArrayOutOfRange(Uint16Array, -1, 0xFFFF); |
| 401 TestTypedArrayOutOfRange(Int16Array, 0x1FFFA, 0x7FFA - 0x8000); |
| 402 |
| 403 TestTypedArrayOutOfRange(Uint32Array, 0x1FFFFFFFA, 0xFFFFFFFA); |
| 404 TestTypedArrayOutOfRange(Uint32Array, -1, 0xFFFFFFFF); |
| 405 TestTypedArrayOutOfRange(Int32Array, 0x1FFFFFFFA, 0x7FFFFFFA - 0x80000000); |
| 406 |
| 407 TestTypedArrayOutOfRange(Uint8ClampedArray, 0x1FA, 0xFF); |
| 408 TestTypedArrayOutOfRange(Uint8ClampedArray, -1, 0); |
| 409 |
| 410 var typedArrayConstructors = [ |
| 411 Uint8Array, |
| 412 Int8Array, |
| 413 Uint16Array, |
| 414 Int16Array, |
| 415 Uint32Array, |
| 416 Int32Array, |
| 417 Uint8ClampedArray, |
| 418 Float32Array, |
| 419 Float64Array]; |
| 420 |
| 421 function TestPropertyTypeChecks(constructor) { |
| 422 function CheckProperty(name) { |
| 423 assertThrows(function() { 'use strict'; new constructor(10)[name] = 0; }) |
| 424 var d = Object.getOwnPropertyDescriptor(constructor.prototype.__proto__, nam
e); |
| 425 var o = {}; |
| 426 assertThrows(function() {d.get.call(o);}, TypeError); |
| 427 for (var i = 0; i < typedArrayConstructors.length; i++) { |
| 428 var ctor = typedArrayConstructors[i]; |
| 429 var a = new ctor(10); |
| 430 d.get.call(a); // shouldn't throw |
| 431 } |
| 432 } |
| 433 |
| 434 CheckProperty("buffer"); |
| 435 CheckProperty("byteOffset"); |
| 436 CheckProperty("byteLength"); |
| 437 CheckProperty("length"); |
| 438 } |
| 439 |
| 440 for(i = 0; i < typedArrayConstructors.length; i++) { |
| 441 TestPropertyTypeChecks(typedArrayConstructors[i]); |
| 442 } |
| 443 |
| 444 |
| 445 function TestTypedArraySet() { |
| 446 // Test array.set in different combinations. |
| 447 |
| 448 function assertArrayPrefix(expected, array) { |
| 449 for (var i = 0; i < expected.length; ++i) { |
| 450 assertEquals(expected[i], array[i]); |
| 451 } |
| 452 } |
| 453 |
| 454 var a11 = new Int16Array([1, 2, 3, 4, 0, -1]) |
| 455 var a12 = new Uint16Array(15) |
| 456 a12.set(a11, 3) |
| 457 assertArrayPrefix([0, 0, 0, 1, 2, 3, 4, 0, 0xffff, 0, 0], a12) |
| 458 assertThrows(function(){ a11.set(a12) }) |
| 459 |
| 460 var a21 = [1, undefined, 10, NaN, 0, -1, {valueOf: function() {return 3}}] |
| 461 var a22 = new Int32Array(12) |
| 462 a22.set(a21, 2) |
| 463 assertArrayPrefix([0, 0, 1, 0, 10, 0, 0, -1, 3, 0], a22) |
| 464 |
| 465 var a31 = new Float32Array([2, 4, 6, 8, 11, NaN, 1/0, -3]) |
| 466 var a32 = a31.subarray(2, 6) |
| 467 a31.set(a32, 4) |
| 468 assertArrayPrefix([2, 4, 6, 8, 6, 8, 11, NaN], a31) |
| 469 assertArrayPrefix([6, 8, 6, 8], a32) |
| 470 |
| 471 var a4 = new Uint8ClampedArray([3,2,5,6]) |
| 472 a4.set(a4) |
| 473 assertArrayPrefix([3, 2, 5, 6], a4) |
| 474 |
| 475 // Cases with overlapping backing store but different element sizes. |
| 476 var b = new ArrayBuffer(4) |
| 477 var a5 = new Int16Array(b) |
| 478 var a50 = new Int8Array(b) |
| 479 var a51 = new Int8Array(b, 0, 2) |
| 480 var a52 = new Int8Array(b, 1, 2) |
| 481 var a53 = new Int8Array(b, 2, 2) |
| 482 |
| 483 a5.set([0x5050, 0x0a0a]) |
| 484 assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50) |
| 485 assertArrayPrefix([0x50, 0x50], a51) |
| 486 assertArrayPrefix([0x50, 0x0a], a52) |
| 487 assertArrayPrefix([0x0a, 0x0a], a53) |
| 488 |
| 489 a50.set([0x50, 0x50, 0x0a, 0x0a]) |
| 490 a51.set(a5) |
| 491 assertArrayPrefix([0x50, 0x0a, 0x0a, 0x0a], a50) |
| 492 |
| 493 a50.set([0x50, 0x50, 0x0a, 0x0a]) |
| 494 a52.set(a5) |
| 495 assertArrayPrefix([0x50, 0x50, 0x0a, 0x0a], a50) |
| 496 |
| 497 a50.set([0x50, 0x50, 0x0a, 0x0a]) |
| 498 a53.set(a5) |
| 499 assertArrayPrefix([0x50, 0x50, 0x50, 0x0a], a50) |
| 500 |
| 501 a50.set([0x50, 0x51, 0x0a, 0x0b]) |
| 502 a5.set(a51) |
| 503 assertArrayPrefix([0x0050, 0x0051], a5) |
| 504 |
| 505 a50.set([0x50, 0x51, 0x0a, 0x0b]) |
| 506 a5.set(a52) |
| 507 assertArrayPrefix([0x0051, 0x000a], a5) |
| 508 |
| 509 a50.set([0x50, 0x51, 0x0a, 0x0b]) |
| 510 a5.set(a53) |
| 511 assertArrayPrefix([0x000a, 0x000b], a5) |
| 512 |
| 513 // Mixed types of same size. |
| 514 var a61 = new Float32Array([1.2, 12.3]) |
| 515 var a62 = new Int32Array(2) |
| 516 a62.set(a61) |
| 517 assertArrayPrefix([1, 12], a62) |
| 518 a61.set(a62) |
| 519 assertArrayPrefix([1, 12], a61) |
| 520 |
| 521 // Invalid source |
| 522 var a = new Uint16Array(50); |
| 523 var expected = []; |
| 524 for (i = 0; i < 50; i++) { |
| 525 a[i] = i; |
| 526 expected.push(i); |
| 527 } |
| 528 a.set({}); |
| 529 assertArrayPrefix(expected, a); |
| 530 assertThrows(function() { a.set.call({}) }, TypeError); |
| 531 assertThrows(function() { a.set.call([]) }, TypeError); |
| 532 |
| 533 assertThrows(function() { a.set(0); }, TypeError); |
| 534 assertThrows(function() { a.set(0, 1); }, TypeError); |
| 535 |
| 536 assertEquals(1, a.set.length); |
| 537 } |
| 538 |
| 539 TestTypedArraySet(); |
| 540 |
| 541 function TestTypedArraysWithIllegalIndices() { |
| 542 var a = new Int32Array(100); |
| 543 |
| 544 a[-10] = 10; |
| 545 assertEquals(undefined, a[-10]); |
| 546 a["-10"] = 10; |
| 547 assertEquals(undefined, a["-10"]); |
| 548 |
| 549 var s = " -10"; |
| 550 a[s] = 10; |
| 551 assertEquals(10, a[s]); |
| 552 var s1 = " -10 "; |
| 553 a[s] = 10; |
| 554 assertEquals(10, a[s]); |
| 555 |
| 556 a["-1e2"] = 10; |
| 557 assertEquals(10, a["-1e2"]); |
| 558 assertEquals(undefined, a[-1e2]); |
| 559 |
| 560 a["-0"] = 256; |
| 561 var s2 = " -0"; |
| 562 a[s2] = 255; |
| 563 assertEquals(undefined, a["-0"]); |
| 564 assertEquals(255, a[s2]); |
| 565 assertEquals(0, a[-0]); |
| 566 |
| 567 a[-Infinity] = 50; |
| 568 assertEquals(undefined, a[-Infinity]); |
| 569 |
| 570 a[1.5] = 10; |
| 571 assertEquals(undefined, a[1.5]); |
| 572 var nan = Math.sqrt(-1); |
| 573 a[nan] = 5; |
| 574 assertEquals(undefined, a[nan]); |
| 575 |
| 576 var x = 0; |
| 577 var y = -0; |
| 578 assertEquals(Infinity, 1/x); |
| 579 assertEquals(-Infinity, 1/y); |
| 580 a[x] = 5; |
| 581 a[y] = 27; |
| 582 assertEquals(27, a[x]); |
| 583 assertEquals(27, a[y]); |
| 584 } |
| 585 |
| 586 TestTypedArraysWithIllegalIndices(); |
| 587 |
| 588 function TestTypedArraysWithIllegalIndicesStrict() { |
| 589 'use strict'; |
| 590 var a = new Int32Array(100); |
| 591 |
| 592 a[-10] = 10; |
| 593 assertEquals(undefined, a[-10]); |
| 594 a["-10"] = 10; |
| 595 assertEquals(undefined, a["-10"]); |
| 596 |
| 597 var s = " -10"; |
| 598 a[s] = 10; |
| 599 assertEquals(10, a[s]); |
| 600 var s1 = " -10 "; |
| 601 a[s] = 10; |
| 602 assertEquals(10, a[s]); |
| 603 |
| 604 a["-1e2"] = 10; |
| 605 assertEquals(10, a["-1e2"]); |
| 606 assertEquals(undefined, a[-1e2]); |
| 607 |
| 608 a["-0"] = 256; |
| 609 var s2 = " -0"; |
| 610 a[s2] = 255; |
| 611 assertEquals(undefined, a["-0"]); |
| 612 assertEquals(255, a[s2]); |
| 613 assertEquals(0, a[-0]); |
| 614 |
| 615 /* Chromium bug: 424619 |
| 616 * a[-Infinity] = 50; |
| 617 * assertEquals(undefined, a[-Infinity]); |
| 618 */ |
| 619 a[1.5] = 10; |
| 620 assertEquals(undefined, a[1.5]); |
| 621 var nan = Math.sqrt(-1); |
| 622 a[nan] = 5; |
| 623 assertEquals(undefined, a[nan]); |
| 624 |
| 625 var x = 0; |
| 626 var y = -0; |
| 627 assertEquals(Infinity, 1/x); |
| 628 assertEquals(-Infinity, 1/y); |
| 629 a[x] = 5; |
| 630 a[y] = 27; |
| 631 assertEquals(27, a[x]); |
| 632 assertEquals(27, a[y]); |
| 633 } |
| 634 |
| 635 TestTypedArraysWithIllegalIndicesStrict(); |
| 636 |
| 637 // DataView |
| 638 function TestDataViewConstructor() { |
| 639 var ab = new ArrayBuffer(256); |
| 640 |
| 641 var d1 = new DataView(ab, 1, 255); |
| 642 assertTrue(ArrayBuffer.isView(d1)); |
| 643 assertSame(ab, d1.buffer); |
| 644 assertSame(1, d1.byteOffset); |
| 645 assertSame(255, d1.byteLength); |
| 646 |
| 647 var d2 = new DataView(ab, 2); |
| 648 assertSame(ab, d2.buffer); |
| 649 assertSame(2, d2.byteOffset); |
| 650 assertSame(254, d2.byteLength); |
| 651 |
| 652 var d3 = new DataView(ab); |
| 653 assertSame(ab, d3.buffer); |
| 654 assertSame(0, d3.byteOffset); |
| 655 assertSame(256, d3.byteLength); |
| 656 |
| 657 var d3a = new DataView(ab, 1, 0); |
| 658 assertSame(ab, d3a.buffer); |
| 659 assertSame(1, d3a.byteOffset); |
| 660 assertSame(0, d3a.byteLength); |
| 661 |
| 662 var d3b = new DataView(ab, 256, 0); |
| 663 assertSame(ab, d3b.buffer); |
| 664 assertSame(256, d3b.byteOffset); |
| 665 assertSame(0, d3b.byteLength); |
| 666 |
| 667 var d3c = new DataView(ab, 256); |
| 668 assertSame(ab, d3c.buffer); |
| 669 assertSame(256, d3c.byteOffset); |
| 670 assertSame(0, d3c.byteLength); |
| 671 |
| 672 var d4 = new DataView(ab, 1, 3.1415926); |
| 673 assertSame(ab, d4.buffer); |
| 674 assertSame(1, d4.byteOffset); |
| 675 assertSame(3, d4.byteLength); |
| 676 |
| 677 |
| 678 // error cases |
| 679 assertThrows(function() { new DataView(ab, -1); }, RangeError); |
| 680 assertThrows(function() { new DataView(); }, TypeError); |
| 681 assertThrows(function() { new DataView([]); }, TypeError); |
| 682 assertThrows(function() { new DataView(ab, 257); }, RangeError); |
| 683 assertThrows(function() { new DataView(ab, 1, 1024); }, RangeError); |
| 684 } |
| 685 |
| 686 TestDataViewConstructor(); |
| 687 |
| 688 function TestDataViewPropertyTypeChecks() { |
| 689 var a = new DataView(new ArrayBuffer(10)); |
| 690 function CheckProperty(name) { |
| 691 var d = Object.getOwnPropertyDescriptor(DataView.prototype, name); |
| 692 var o = {} |
| 693 assertThrows(function() {d.get.call(o);}, TypeError); |
| 694 d.get.call(a); // shouldn't throw |
| 695 } |
| 696 |
| 697 CheckProperty("buffer"); |
| 698 CheckProperty("byteOffset"); |
| 699 CheckProperty("byteLength"); |
| 700 |
| 701 function CheckGetSetLength(name) { |
| 702 assertEquals(1, DataView.prototype["get" + name].length); |
| 703 assertEquals(2, DataView.prototype["set" + name].length); |
| 704 } |
| 705 CheckGetSetLength("Int8"); |
| 706 CheckGetSetLength("Uint8"); |
| 707 CheckGetSetLength("Int16"); |
| 708 CheckGetSetLength("Uint16"); |
| 709 CheckGetSetLength("Int32"); |
| 710 CheckGetSetLength("Uint32"); |
| 711 CheckGetSetLength("Float32"); |
| 712 CheckGetSetLength("Float64"); |
| 713 } |
| 714 |
| 715 |
| 716 TestDataViewPropertyTypeChecks(); |
| 717 |
| 718 |
| 719 function TestDataViewToStringTag() { |
| 720 var a = new DataView(new ArrayBuffer(10)); |
| 721 assertEquals("[object DataView]", Object.prototype.toString.call(a)); |
| 722 var desc = Object.getOwnPropertyDescriptor( |
| 723 DataView.prototype, Symbol.toStringTag); |
| 724 assertTrue(desc.configurable); |
| 725 assertFalse(desc.enumerable); |
| 726 assertFalse(desc.writable); |
| 727 assertEquals("DataView", desc.value); |
| 728 } |
| 729 |
| 730 |
| 731 // General tests for properties |
| 732 |
| 733 // Test property attribute [[Enumerable]] |
| 734 function TestEnumerable(func, obj) { |
| 735 function props(x) { |
| 736 var array = []; |
| 737 for (var p in x) array.push(p); |
| 738 return array.sort(); |
| 739 } |
| 740 assertArrayEquals([], props(func)); |
| 741 assertArrayEquals([], props(func.prototype)); |
| 742 if (obj) |
| 743 assertArrayEquals([], props(obj)); |
| 744 } |
| 745 TestEnumerable(ArrayBuffer, new ArrayBuffer()); |
| 746 for(i = 0; i < typedArrayConstructors.length; i++) { |
| 747 TestEnumerable(typedArrayConstructors[i]); |
| 748 } |
| 749 TestEnumerable(DataView, new DataView(new ArrayBuffer())); |
| 750 |
| 751 // Test arbitrary properties on ArrayBuffer |
| 752 function TestArbitrary(m) { |
| 753 function TestProperty(map, property, value) { |
| 754 map[property] = value; |
| 755 assertEquals(value, map[property]); |
| 756 } |
| 757 for (var i = 0; i < 20; i++) { |
| 758 TestProperty(m, 'key' + i, 'val' + i); |
| 759 TestProperty(m, 'foo' + i, 'bar' + i); |
| 760 } |
| 761 } |
| 762 TestArbitrary(new ArrayBuffer(256)); |
| 763 for(i = 0; i < typedArrayConstructors.length; i++) { |
| 764 TestArbitrary(new typedArrayConstructors[i](10)); |
| 765 } |
| 766 TestArbitrary(new DataView(new ArrayBuffer(256))); |
| 767 |
| 768 |
| 769 // Test direct constructor call |
| 770 assertThrows(function() { ArrayBuffer(); }, TypeError); |
| 771 assertThrows(function() { DataView(new ArrayBuffer()); }, TypeError); |
| 772 |
| 773 function TestNonConfigurableProperties(constructor) { |
| 774 var arr = new constructor([100]) |
| 775 assertFalse(Object.getOwnPropertyDescriptor(arr,"0").configurable) |
| 776 assertFalse(delete arr[0]) |
| 777 } |
| 778 |
| 779 for(i = 0; i < typedArrayConstructors.length; i++) { |
| 780 TestNonConfigurableProperties(typedArrayConstructors[i]); |
| 781 } |
OLD | NEW |