Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2015 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 | |
Ben Murdoch | 014dc51 | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5 | // Test array functions do not cause infinite loops when length is negative, |
| 6 | // max_value, etc. |
| 7 | |
| 8 | // ArrayToString |
| 9 | |
| 10 | var o = { length: Number.MIN_VALUE }; |
| 11 | var result = Array.prototype.toString.call(o); |
| 12 | assertEquals("[object Object]", result); |
| 13 | |
| 14 | // ArrayToLocaleString |
| 15 | |
| 16 | var o = { length: Number.MIN_VALUE }; |
| 17 | var result = Array.prototype.toLocaleString.call(o); |
| 18 | assertEquals("", result); |
| 19 | |
| 20 | // ArrayJoin |
| 21 | |
| 22 | var o = { length: Number.MIN_VALUE }; |
| 23 | var result = Array.prototype.join.call(o); |
| 24 | assertEquals(0, result.length); |
| 25 | |
| 26 | // ArrayPush |
| 27 | |
| 28 | var o = { length: Number.MIN_VALUE }; |
| 29 | Array.prototype.push.call(o, 1); |
| 30 | assertEquals(1, o.length); |
| 31 | assertEquals(1, o[0]); |
| 32 | |
| 33 | var o = { length: Number.MAX_VALUE }; |
| 34 | assertThrows(() => Array.prototype.push.call(o, 1), TypeError); |
| 35 | |
| 36 | // ArrayPop |
| 37 | |
| 38 | var o = { length: 0 }; |
| 39 | Array.prototype.pop.call(o); |
| 40 | assertEquals(0, o.length); |
| 41 | |
| 42 | var o = { length: Number.MIN_VALUE }; |
| 43 | Array.prototype.pop.call(o); |
| 44 | assertEquals(0, o.length); |
| 45 | |
| 46 | var o = { length: Number.MAX_VALUE }; |
| 47 | Array.prototype.pop.call(o); |
| 48 | assertEquals(o.length, Number.MAX_SAFE_INTEGER - 1); |
| 49 | |
| 50 | // ArrayReverse |
| 51 | |
| 52 | var o = { 0: 'foo', length: Number.MIN_VALUE } |
| 53 | var result = Array.prototype.reverse.call(o); |
| 54 | assertEquals('object', typeof(result)); |
| 55 | assertEquals(Number.MIN_VALUE, result.length); |
| 56 | assertEquals(Number.MIN_VALUE, o.length); |
| 57 | |
| 58 | // ArrayShift |
| 59 | |
| 60 | var o = { 0: "foo", length: Number.MIN_VALUE } |
| 61 | var result = Array.prototype.shift.call(o); |
| 62 | assertEquals(undefined, result); |
| 63 | assertEquals(0, o.length); |
| 64 | |
| 65 | // ArrayUnshift |
| 66 | |
| 67 | var o = { length: 0 }; |
| 68 | Array.prototype.unshift.call(o); |
| 69 | assertEquals(0, o.length); |
| 70 | |
| 71 | var o = { length: 0 }; |
| 72 | Array.prototype.unshift.call(o, 'foo'); |
| 73 | assertEquals('foo', o[0]); |
| 74 | assertEquals(1, o.length); |
| 75 | |
| 76 | var o = { length: Number.MIN_VALUE }; |
| 77 | Array.prototype.unshift.call(o); |
| 78 | assertEquals(0, o.length); |
| 79 | |
| 80 | var o = { length: Number.MIN_VALUE }; |
| 81 | Array.prototype.unshift.call(o, 'foo'); |
| 82 | assertEquals('foo', o[0]); |
| 83 | assertEquals(1, o.length); |
| 84 | |
| 85 | // ArraySplice |
| 86 | |
| 87 | var o = { length: Number.MIN_VALUE }; |
| 88 | Array.prototype.splice.call(o); |
| 89 | assertEquals(0, o.length); |
| 90 | |
| 91 | var o = { length: Number.MIN_VALUE }; |
| 92 | Array.prototype.splice.call(o, 0, 10, ['foo']); |
| 93 | assertEquals(['foo'], o[0]); |
| 94 | assertEquals(1, o.length); |
| 95 | |
| 96 | var o = { length: Number.MIN_VALUE }; |
| 97 | Array.prototype.splice.call(o, -1); |
| 98 | assertEquals(0, o.length); |
| 99 | |
| 100 | var o = { length: Number.MAX_SAFE_INTEGER }; |
| 101 | Array.prototype.splice.call(o, -1); |
| 102 | assertEquals(Number.MAX_SAFE_INTEGER - 1, o.length); |
| 103 | |
| 104 | // ArraySlice |
| 105 | |
| 106 | var o = { length: Number.MIN_VALUE }; |
| 107 | var result = Array.prototype.slice.call(o); |
| 108 | assertEquals(0, result.length); |
| 109 | |
| 110 | var o = { length: Number.MIN_VALUE }; |
| 111 | var result = Array.prototype.slice.call(o, Number.MAX_VALUE); |
| 112 | assertEquals(0, result.length); |
| 113 | |
| 114 | var o = { length: Number.MAX_VALUE }; |
| 115 | var result = Array.prototype.slice.call(o, Number.MAX_VALUE - 1); |
| 116 | assertEquals(0, result.length); |
| 117 | |
| 118 | // ArrayIndexOf |
| 119 | |
| 120 | var o = { length: Number.MIN_VALUE }; |
| 121 | var result = Array.prototype.indexOf.call(o); |
| 122 | assertEquals(-1, result); |
| 123 | |
| 124 | var o = { length: Number.MAX_SAFE_INTEGER } |
| 125 | o[Number.MAX_SAFE_INTEGER - 1] = "foo" |
| 126 | var result = Array.prototype.indexOf.call(o, |
| 127 | "foo", Number.MAX_SAFE_INTEGER - 2); |
| 128 | assertEquals(Number.MAX_SAFE_INTEGER - 1, result); |
| 129 | |
| 130 | var o = { length: Number.MAX_SAFE_INTEGER }; |
| 131 | o[Number.MAX_SAFE_INTEGER - 1] = "foo"; |
| 132 | var result = Array.prototype.indexOf.call(o, "foo", -1); |
| 133 | assertEquals(Number.MAX_SAFE_INTEGER - 1, result); |
| 134 | |
| 135 | // ArrayLastIndexOf |
| 136 | |
| 137 | var o = { length: Number.MIN_VALUE }; |
| 138 | var result = Array.prototype.lastIndexOf.call(o); |
| 139 | assertEquals(-1, result); |
| 140 | |
| 141 | var o = { length: Number.MAX_SAFE_INTEGER } |
| 142 | o[Number.MAX_SAFE_INTEGER - 1] = "foo" |
| 143 | var result = Array.prototype.lastIndexOf.call(o, |
| 144 | "foo", Number.MAX_SAFE_INTEGER); |
| 145 | assertEquals(Number.MAX_SAFE_INTEGER - 1, result); |
| 146 | |
| 147 | var o = { length: Number.MAX_SAFE_INTEGER }; |
| 148 | o[Number.MAX_SAFE_INTEGER - 1] = "foo"; |
| 149 | var result = Array.prototype.lastIndexOf.call(o, "foo", -1); |
| 150 | assertEquals(Number.MAX_SAFE_INTEGER - 1, result); |
| 151 | |
| 152 | // ArrayFilter |
| 153 | |
| 154 | var func = function(v) { return v; } |
| 155 | |
| 156 | var o = { length: Number.MIN_VALUE }; |
| 157 | Array.prototype.filter.call(o, func); |
| 158 | assertEquals(Number.MIN_VALUE, o.length); |
| 159 | |
| 160 | // ArrayForEach |
| 161 | |
| 162 | var o = { length: Number.MIN_VALUE }; |
| 163 | Array.prototype.forEach.call(o, func); |
| 164 | assertEquals(Number.MIN_VALUE, o.length); |
| 165 | |
| 166 | // ArraySome |
| 167 | |
| 168 | var o = { length: Number.MIN_VALUE }; |
| 169 | Array.prototype.some.call(o, func); |
| 170 | assertEquals(Number.MIN_VALUE, o.length); |
| 171 | |
| 172 | // ArrayEvery |
| 173 | |
| 174 | var o = { length: Number.MIN_VALUE }; |
| 175 | Array.prototype.every.call(o, func); |
| 176 | assertEquals(Number.MIN_VALUE, o.length); |
| 177 | |
| 178 | // ArrayMap |
| 179 | |
| 180 | var o = { length: Number.MIN_VALUE }; |
| 181 | Array.prototype.map.call(o, func); |
| 182 | assertEquals(Number.MIN_VALUE, o.length); |
| 183 | |
| 184 | // ArrayReduce |
| 185 | |
| 186 | var o = { length: Number.MIN_VALUE }; |
| 187 | Array.prototype.reduce.call(o, func, 0); |
| 188 | assertEquals(Number.MIN_VALUE, o.length); |
| 189 | |
| 190 | // ArrayReduceRight |
| 191 | |
| 192 | var o = { length: Number.MIN_VALUE }; |
| 193 | Array.prototype.reduceRight.call(o, func, 0); |
| 194 | assertEquals(Number.MIN_VALUE, o.length); |
| 195 | |
| 196 | // ArrayFill |
| 197 | |
| 198 | var o = { length: Number.MIN_VALUE }; |
| 199 | Array.prototype.fill(o, 0); |
| 200 | assertEquals(Number.MIN_VALUE, o.length); |