Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2014 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 | // Based on Mozilla Array.of() tests at http://dxr.mozilla.org/mozilla-central/source/js/src/jit-test/tests/collections |
| 6 | |
| 7 | 'use strict'; |
| 8 | |
| 9 | var typedArrayConstructors = [ |
| 10 | Uint8Array, |
| 11 | Int8Array, |
| 12 | Uint16Array, |
| 13 | Int16Array, |
| 14 | Uint32Array, |
| 15 | Int32Array, |
| 16 | Uint8ClampedArray, |
| 17 | Float32Array, |
| 18 | Float64Array]; |
| 19 | |
| 20 | |
| 21 | function TestTypedArrayOf(constructor) { |
| 22 | // %TypedArray%.of basics. |
| 23 | var a = constructor.of(); |
| 24 | assertEquals(0, a.length); |
| 25 | assertEquals(constructor.prototype, Object.getPrototypeOf(a)); |
| 26 | assertEquals(false, Array.isArray(a)); |
| 27 | |
| 28 | // Items are coerced to numerical values. |
| 29 | a = constructor.of(undefined, null, [], true, false, 3.14); |
| 30 | |
| 31 | // For typed arrays of floating point values, values are not rounded. |
| 32 | if (constructor === Float32Array || constructor === Float64Array) { |
| 33 | assertEquals(NaN, a[0]); |
| 34 | assertEquals(0, a[1]); |
| 35 | assertEquals(0, a[2]); |
| 36 | assertEquals(1, a[3]); |
| 37 | assertEquals(0, a[4]); |
| 38 | assertEquals(true, Math.abs(a[5] - 3.14) < 1e-6); |
| 39 | } else { |
| 40 | assertEquals(0, a[0]); |
| 41 | assertEquals(0, a[1]); |
| 42 | assertEquals(0, a[2]); |
| 43 | assertEquals(1, a[3]); |
| 44 | assertEquals(0, a[4]); |
| 45 | assertEquals(3, a[5]); |
| 46 | } |
| 47 | |
| 48 | var aux = []; |
| 49 | for (var i = 0; i < 100; i++) |
| 50 | aux[i] = i; |
| 51 | |
| 52 | a = constructor.of.apply(constructor, aux); |
| 53 | assertEquals(aux.length, a.length); |
| 54 | assertArrayEquals(aux, a); |
| 55 | |
| 56 | // %TypedArray%.of can be called on subclasses of TypedArrays |
| 57 | var hits = 0; |
| 58 | class Bag extends constructor { |
| 59 | constructor(length) { |
| 60 | super(length); |
| 61 | assertEquals(arguments.length, 1); |
| 62 | assertEquals(length, 2); |
| 63 | hits++; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | hits = 0; |
| 68 | a = Bag.of(5, 6); |
| 69 | assertEquals(1, hits); |
| 70 | assertEquals(2, a.length); |
| 71 | assertArrayEquals([5, 6], a); |
| 72 | assertEquals(Bag.prototype, a.__proto__); |
| 73 | |
| 74 | hits = 0; |
| 75 | var actual = constructor.of.call(Bag, 5, 6); |
| 76 | assertEquals(1, hits); |
| 77 | assertEquals(2, a.length); |
| 78 | assertArrayEquals([5, 6], a); |
| 79 | assertEquals(Bag.prototype, a.__proto__); |
| 80 | |
| 81 | // %TypedArray%.of does not trigger prototype setters. |
| 82 | // (It defines elements rather than assigning to them.) |
| 83 | var status = "pass"; |
| 84 | Object.defineProperty(constructor.prototype, "0", { |
| 85 | set: function(v) { status = "fail"; } |
| 86 | }); |
| 87 | assertEquals(1, constructor.of(1)[0], 1); |
| 88 | assertEquals("pass", status); |
| 89 | |
| 90 | // Note that %TypedArray%.of does not trigger "length" setter itself, as |
| 91 | // it relies on the constructor to set "length" to the value passed to it. |
| 92 | // If the constructor does not assign "length", the setter should not be |
| 93 | // invoked. |
| 94 | |
| 95 | // Setter on the newly created object. |
| 96 | class Pack extends constructor { |
| 97 | constructor(length) { |
| 98 | super(length); |
| 99 | Object.defineProperty(this, "length", { |
| 100 | set: function (v) { status = "fail"; } |
| 101 | }); |
| 102 | } |
| 103 | } |
| 104 | var pack = Pack.of(5, 6, 7, 8); |
| 105 | assertEquals("pass", status); |
| 106 | |
| 107 | // when the setter is on the new object's prototype |
| 108 | class Bevy extends constructor {} |
| 109 | Object.defineProperty(Bevy.prototype, "length", { |
| 110 | set: function (v) { status = "fail"; } |
| 111 | }); |
| 112 | var bevy = Bevy.of(3); |
| 113 | assertEquals("pass", status); |
| 114 | |
| 115 | // Check superficial features of %TypedArray%.of. |
| 116 | var desc = Object.getOwnPropertyDescriptor(constructor.__proto__, "of"); |
| 117 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 118 | assertEquals(desc.configurable, true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 119 | assertEquals(desc.enumerable, false); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 120 | assertEquals(desc.writable, true); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 121 | assertEquals(constructor.of.length, 0); |
| 122 | |
| 123 | // %TypedArray%.of is not a constructor. |
| 124 | assertThrows(function() { new constructor.of(); }, TypeError); |
| 125 | |
| 126 | // For receivers which are not constructors %TypedArray%.of does not |
| 127 | // allocate a typed array using a default constructor, but throws an |
| 128 | // exception. Note that this is different from Array.of, which uses |
| 129 | // Array as default constructor. |
| 130 | for (var x of [undefined, null, false, true, "cow", 42, 3.14]) { |
| 131 | assertThrows(function () { constructor.of.call(x); }, TypeError); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | for (var constructor of typedArrayConstructors) { |
| 136 | TestTypedArrayOf(constructor); |
| 137 | } |