|
| 1 | +function shouldBe(actual, expected) { |
| 2 | + if (actual !== expected) |
| 3 | + throw new Error("Bad value: " + actual + ", expected: " + expected); |
| 4 | +} |
| 5 | + |
| 6 | +function shiftOnce(array) { |
| 7 | + return array.shift(); |
| 8 | +} |
| 9 | +noInline(shiftOnce); |
| 10 | + |
| 11 | +function drain(array) { |
| 12 | + var results = []; |
| 13 | + while (array.length) |
| 14 | + results.push(array.shift()); |
| 15 | + return results; |
| 16 | +} |
| 17 | +noInline(drain); |
| 18 | + |
| 19 | +// Length-0: fast path returns undefined. |
| 20 | +(function () { |
| 21 | + for (var i = 0; i < testLoopCount; ++i) { |
| 22 | + var array = []; |
| 23 | + shouldBe(shiftOnce(array), undefined); |
| 24 | + shouldBe(array.length, 0); |
| 25 | + } |
| 26 | +})(); |
| 27 | + |
| 28 | +// Length-1 Int32 fast path. |
| 29 | +(function () { |
| 30 | + for (var i = 0; i < testLoopCount; ++i) { |
| 31 | + var array = [42]; |
| 32 | + shouldBe(shiftOnce(array), 42); |
| 33 | + shouldBe(array.length, 0); |
| 34 | + } |
| 35 | +})(); |
| 36 | + |
| 37 | +// Length-1 Double fast path. |
| 38 | +(function () { |
| 39 | + for (var i = 0; i < testLoopCount; ++i) { |
| 40 | + var array = [3.5]; |
| 41 | + shouldBe(shiftOnce(array), 3.5); |
| 42 | + shouldBe(array.length, 0); |
| 43 | + } |
| 44 | +})(); |
| 45 | + |
| 46 | +// Length-1 Contiguous fast path. |
| 47 | +(function () { |
| 48 | + for (var i = 0; i < testLoopCount; ++i) { |
| 49 | + var array = ["only"]; |
| 50 | + shouldBe(shiftOnce(array), "only"); |
| 51 | + shouldBe(array.length, 0); |
| 52 | + } |
| 53 | +})(); |
| 54 | + |
| 55 | +// Multi-element Int32: drains via slow path (operationArrayShift). |
| 56 | +(function () { |
| 57 | + for (var i = 0; i < testLoopCount; ++i) { |
| 58 | + var results = drain([1, 2, 3, 4, 5]); |
| 59 | + shouldBe(results.length, 5); |
| 60 | + shouldBe(results[0], 1); |
| 61 | + shouldBe(results[1], 2); |
| 62 | + shouldBe(results[2], 3); |
| 63 | + shouldBe(results[3], 4); |
| 64 | + shouldBe(results[4], 5); |
| 65 | + } |
| 66 | +})(); |
| 67 | + |
| 68 | +// Multi-element Double: drains via slow path. |
| 69 | +(function () { |
| 70 | + for (var i = 0; i < testLoopCount; ++i) { |
| 71 | + var results = drain([1.5, 2.5, 3.5, 4.5]); |
| 72 | + shouldBe(results.length, 4); |
| 73 | + shouldBe(results[0], 1.5); |
| 74 | + shouldBe(results[1], 2.5); |
| 75 | + shouldBe(results[2], 3.5); |
| 76 | + shouldBe(results[3], 4.5); |
| 77 | + } |
| 78 | +})(); |
| 79 | + |
| 80 | +// Multi-element Contiguous: drains via slow path. |
| 81 | +(function () { |
| 82 | + for (var i = 0; i < testLoopCount; ++i) { |
| 83 | + var results = drain(["a", "b", "c"]); |
| 84 | + shouldBe(results.length, 3); |
| 85 | + shouldBe(results[0], "a"); |
| 86 | + shouldBe(results[1], "b"); |
| 87 | + shouldBe(results[2], "c"); |
| 88 | + } |
| 89 | +})(); |
| 90 | + |
| 91 | +// Length-1 Int32 hole: storage[0] is empty, fast path must take slow case. |
| 92 | +(function () { |
| 93 | + for (var i = 0; i < testLoopCount; ++i) { |
| 94 | + var array = [1]; |
| 95 | + delete array[0]; |
| 96 | + shouldBe(array.length, 1); |
| 97 | + shouldBe(shiftOnce(array), undefined); |
| 98 | + shouldBe(array.length, 0); |
| 99 | + } |
| 100 | +})(); |
| 101 | + |
| 102 | +// Length-1 Double hole: storage[0] is NaN-bit-pattern, fast path must take slow case. |
| 103 | +(function () { |
| 104 | + for (var i = 0; i < testLoopCount; ++i) { |
| 105 | + var array = [1.5]; |
| 106 | + delete array[0]; |
| 107 | + shouldBe(array.length, 1); |
| 108 | + shouldBe(shiftOnce(array), undefined); |
| 109 | + shouldBe(array.length, 0); |
| 110 | + } |
| 111 | +})(); |
| 112 | + |
| 113 | +// Length-1 Double containing real NaN: fast path branchIfNaN takes slow case but result must be NaN. |
| 114 | +(function () { |
| 115 | + for (var i = 0; i < testLoopCount; ++i) { |
| 116 | + var array = [NaN]; |
| 117 | + var result = shiftOnce(array); |
| 118 | + shouldBe(result !== result, true); |
| 119 | + shouldBe(array.length, 0); |
| 120 | + } |
| 121 | +})(); |
| 122 | + |
| 123 | +// Repeated shift on the same array exercises arrayMode transitions and slow path. |
| 124 | +(function () { |
| 125 | + for (var i = 0; i < testLoopCount; ++i) { |
| 126 | + var array = [10, 20, 30]; |
| 127 | + shouldBe(shiftOnce(array), 10); |
| 128 | + shouldBe(shiftOnce(array), 20); |
| 129 | + shouldBe(shiftOnce(array), 30); |
| 130 | + shouldBe(shiftOnce(array), undefined); |
| 131 | + shouldBe(array.length, 0); |
| 132 | + } |
| 133 | +})(); |
| 134 | + |
| 135 | +// With Array.prototype[0] set, a length-1 hole array must shift to the |
| 136 | +// prototype value. The intrinsic's slow path (operationArrayShift) reads |
| 137 | +// through the prototype chain via getIndex. |
| 138 | +(function () { |
| 139 | + function shiftIsolated(array) { |
| 140 | + return array.shift(); |
| 141 | + } |
| 142 | + noInline(shiftIsolated); |
| 143 | + |
| 144 | + for (var i = 0; i < testLoopCount; ++i) |
| 145 | + shiftIsolated([1]); |
| 146 | + |
| 147 | + Array.prototype[0] = "proto-zero"; |
| 148 | + try { |
| 149 | + var array = [42]; |
| 150 | + delete array[0]; |
| 151 | + shouldBe(shiftIsolated(array), "proto-zero"); |
| 152 | + shouldBe(array.length, 0); |
| 153 | + } finally { |
| 154 | + delete Array.prototype[0]; |
| 155 | + } |
| 156 | +})(); |
0 commit comments