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: --harmony-exponentiation-operator 6 7function TestBasic() { 8 assertEquals(-(8 ** 2), -64); 9 assertEquals(+(8 ** 2), 64); 10 assertEquals(~(8 ** 2), -65); 11 assertEquals(!(8 ** 2), false); 12 13 assertEquals(2 ** -2, 0.25); 14 15 var o = { p: 1 }; 16 assertEquals(2 ** delete o.p, 2); 17 18 assertEquals(2 ** void 1, NaN); 19 20 assertEquals(2 ** typeof 1, NaN); 21 22 var s = "2"; 23 var n = 2; 24 25 assertEquals(2 ** "2", 4); 26 assertEquals(2 ** +"2", 4); 27 assertEquals(2 ** +s, 4); 28 assertEquals(2 ** s, 4); 29 assertEquals(2 ** 2, 4); 30 assertEquals(2 ** +2, 4); 31 assertEquals(2 ** +n, 4); 32 assertEquals(2 ** n, 4); 33 34 assertEquals(2 ** -"2", 0.25); 35 assertEquals(2 ** -s, 0.25); 36 assertEquals(2 ** -2, 0.25); 37 assertEquals(2 ** -n, 0.25); 38 39 assertEquals(2 ** ~"2", 0.125); 40 assertEquals(2 ** ~s, 0.125); 41 assertEquals(2 ** ~2, 0.125); 42 assertEquals(2 ** ~n, 0.125); 43 44 assertEquals(2 ** !"2", 1); 45 assertEquals(2 ** !s, 1); 46 assertEquals(2 ** !2, 1); 47 assertEquals(2 ** !n, 1); 48 49 var exponent = 2; 50 assertEquals(2 ** 3, 8); 51 assertEquals(3 * 2 ** 3, 24); 52 assertEquals(2 ** ++exponent, 8); 53 assertEquals(2 ** -1 * 2, 1); 54 assertEquals(2 ** 2 * 4, 16); 55 assertEquals(2 ** 2 / 2, 2); 56 assertEquals(2 ** (3 ** 2), 512); 57 assertEquals(2 ** 3 ** 2, 512); 58 assertEquals(2 * 3 ** 2, 18); 59 assertEquals(16 / 2 ** 2, 4); 60} 61TestBasic(); 62 63 64function TestAssignment() { 65 var base = -5; 66 assertEquals(base **= 3, -125); 67 assertEquals(base, -125); 68} 69TestAssignment(); 70 71 72function TestPrecedence() { 73 var base = 4; 74 assertEquals(--base ** 2, 9); // 3 ** 2 75 assertEquals(++base ** 2, 16); // 4 ** 2 76 assertEquals(base++ ** 2, 16); // 4 ** 2 77 assertEquals(base-- ** 2, 25); // 5 ** 2 78 79 assertEquals(4, base); 80 assertEquals(--base ** --base ** 2, 81 Math.pow(3, Math.pow(2, 2))); 82 83 assertEquals(2, base); 84 assertEquals(++base ** ++base ** 2, 85 Math.pow(3, Math.pow(4, 2))); 86 87 base = 4; 88 assertEquals(base-- ** base-- ** 2, 89 Math.pow(4, Math.pow(3, 2))); 90 91 assertEquals(2, base); 92 assertEquals(base++ ** base++ ** 2, 93 Math.pow(2, Math.pow(3, 2))); 94} 95TestPrecedence(); 96 97 98function TestInvariants() { 99 assertEquals(NaN, 2 ** NaN); 100 assertEquals(NaN, (+0) ** NaN); 101 assertEquals(NaN, (-0) ** NaN); 102 assertEquals(NaN, Infinity ** NaN); 103 assertEquals(NaN, (-Infinity) ** NaN); 104 105 assertEquals(1, NaN ** +0); 106 assertEquals(1, NaN ** -0); 107 108 assertEquals(NaN, NaN ** NaN); 109 assertEquals(NaN, NaN ** 2.2); 110 assertEquals(NaN, NaN ** 1); 111 assertEquals(NaN, NaN ** -1); 112 assertEquals(NaN, NaN ** -2.2); 113 assertEquals(NaN, NaN ** Infinity); 114 assertEquals(NaN, NaN ** -Infinity); 115 116 assertEquals(Infinity, 1.1 ** Infinity); 117 assertEquals(Infinity, (-1.1) ** Infinity); 118 assertEquals(Infinity, 2 ** Infinity); 119 assertEquals(Infinity, (-2) ** Infinity); 120 121 // Because +0 == -0, we need to compare 1/{+,-}0 to {+,-}Infinity 122 assertEquals(+Infinity, 1/1.1 ** -Infinity); 123 assertEquals(+Infinity, 1/(-1.1) ** -Infinity); 124 assertEquals(+Infinity, 1/2 ** -Infinity); 125 assertEquals(+Infinity, 1/(-2) ** -Infinity); 126 127 assertEquals(NaN, 1 ** Infinity); 128 assertEquals(NaN, 1 ** -Infinity); 129 assertEquals(NaN, (-1) ** Infinity); 130 assertEquals(NaN, (-1) ** -Infinity); 131 132 assertEquals(+0, 0.1 ** Infinity); 133 assertEquals(+0, (-0.1) ** Infinity); 134 assertEquals(+0, 0.999 ** Infinity); 135 assertEquals(+0, (-0.999) ** Infinity); 136 137 assertEquals(Infinity, 0.1 ** -Infinity); 138 assertEquals(Infinity, (-0.1) ** -Infinity); 139 assertEquals(Infinity, 0.999 ** -Infinity); 140 assertEquals(Infinity, (-0.999) ** -Infinity); 141 142 assertEquals(Infinity, Infinity ** 0.1); 143 assertEquals(Infinity, Infinity ** 2); 144 145 assertEquals(+Infinity, 1/Infinity ** -0.1); 146 assertEquals(+Infinity, 1/Infinity ** -2); 147 148 assertEquals(-Infinity, (-Infinity) ** 3); 149 assertEquals(-Infinity, (-Infinity) ** 13); 150 151 assertEquals(Infinity, (-Infinity) ** 3.1); 152 assertEquals(Infinity, (-Infinity) ** 2); 153 154 assertEquals(-Infinity, 1/(-Infinity) ** -3); 155 assertEquals(-Infinity, 1/(-Infinity) ** -13); 156 157 assertEquals(+Infinity, 1/(-Infinity) ** -3.1); 158 assertEquals(+Infinity, 1/(-Infinity) ** -2); 159 160 assertEquals(+Infinity, 1/(+0) ** 1.1); 161 assertEquals(+Infinity, 1/(+0) ** 2); 162 163 assertEquals(Infinity, (+0) ** -1.1); 164 assertEquals(Infinity, (+0) ** -2); 165 166 assertEquals(-Infinity, 1/(-0) ** 3); 167 assertEquals(-Infinity, 1/(-0) ** 13); 168 169 assertEquals(+Infinity, 1/(-0) ** 3.1); 170 assertEquals(+Infinity, 1/(-0) ** 2); 171 172 assertEquals(-Infinity, (-0) ** -3); 173 assertEquals(-Infinity, (-0) ** -13); 174 175 assertEquals(Infinity, (-0) ** -3.1); 176 assertEquals(Infinity, (-0) ** -2); 177 178 assertEquals(NaN, (-0.00001) ** 1.1); 179 assertEquals(NaN, (-0.00001) ** -1.1); 180 assertEquals(NaN, (-1.1) ** 1.1); 181 assertEquals(NaN, (-1.1) ** -1.1); 182 assertEquals(NaN, (-2) ** 1.1); 183 assertEquals(NaN, (-2) ** -1.1); 184 assertEquals(NaN, (-1000) ** 1.1); 185 assertEquals(NaN, (-1000) ** -1.1); 186 187 assertEquals(+Infinity, 1/(-0) ** 0.5); 188 assertEquals(+Infinity, 1/(-0) ** 0.6); 189 assertEquals(-Infinity, 1/(-0) ** 1); 190 assertEquals(-Infinity, 1/(-0) ** 10000000001); 191 192 assertEquals(+Infinity, (-0) ** -0.5); 193 assertEquals(+Infinity, (-0) ** -0.6); 194 assertEquals(-Infinity, (-0) ** -1); 195 assertEquals(-Infinity, (-0) ** -10000000001); 196 197 assertEquals(4, 16 ** 0.5); 198 assertEquals(NaN, (-16) ** 0.5); 199 assertEquals(0.25, 16 ** -0.5); 200 assertEquals(NaN, (-16) ** -0.5); 201} 202TestInvariants(); 203 204 205function TestOperationOrder() { 206 var log = []; 207 var handler = { 208 get(t, n) { 209 var result = Reflect.get(t, n); 210 var str = typeof result === "object" ? "[object Object]" : String(result); 211 log.push(`[[Get]](${String(n)}) -> ${str}`); 212 return result; 213 }, 214 set(t, n, v) { 215 var result = Reflect.set(t, n, v); 216 log.push(`[[Set]](${String(n)}, ${String(v)}) -> ${String(result)}`); 217 return result; 218 }, 219 has() { assertUnreachable("trap 'has' invoked"); }, 220 deleteProperty() { assertUnreachable("trap 'deleteProperty' invoked"); }, 221 ownKeys() { assertUnreachable("trap 'ownKeys' invoked"); }, 222 apply() { assertUnreachable("trap 'apply' invoked"); }, 223 construct() { assertUnreachable("trap 'construct' invoked"); }, 224 getPrototypeOf() { assertUnreachable("trap 'getPrototypeOf' invoked"); }, 225 setPrototypeOf() { assertUnreachable("trap 'setPrototypeOf' invoked"); }, 226 isExtensible() { assertUnreachable("trap 'isExtensible' invoked"); }, 227 preventExtensions() { 228 assertUnreachable("trap 'preventExtensions' invoked"); }, 229 getOwnPropertyDescriptor() { 230 assertUnreachable("trap 'getOwnPropertyDescriptor' invoked"); }, 231 defineProperty() { assertUnreachable("trap 'defineProperty' invoked"); }, 232 }; 233 var P = new Proxy({ x: 2 }, handler); 234 235 assertEquals(256, P.x **= "8"); 236 assertEquals([ 237 "[[Get]](x) -> 2", 238 "[[Set]](x, 256) -> true" 239 ], log); 240 241 log = []; 242 var O = new Proxy({ p: P }, handler); 243 assertEquals(65536, O.p.x **= 2 ); 244 assertEquals([ 245 "[[Get]](p) -> [object Object]", 246 "[[Get]](x) -> 256", 247 "[[Set]](x, 65536) -> true" 248 ], log); 249} 250TestOperationOrder(); 251 252 253function TestOverrideMathPow() { 254 var MathPow = MathPow; 255 Math.pow = function(a, b) { 256 assertUnreachable(`Math.pow(${String(a)}, ${String(b)}) invoked`); 257 } 258 259 TestBasic(); 260 TestAssignment(); 261 TestInvariants(); 262 TestOperationOrder(); 263 264 Math.pow = MathPow; 265} 266TestOverrideMathPow(); 267 268function TestBadAssignmentLHS() { 269 assertThrows("if (false) { 17 **= 10; }", ReferenceError); 270 assertThrows("if (false) { '17' **= 10; }", ReferenceError); 271 assertThrows("if (false) { /17/ **= 10; }", ReferenceError); 272 assertThrows("if (false) { ({ valueOf() { return 17; } } **= 10); }", 273 ReferenceError); 274 // TODO(caitp): a Call expression as LHS should be an early ReferenceError! 275 // assertThrows("if (false) { Array() **= 10; }", ReferenceError); 276 assertThrows(() => Array() **= 10, ReferenceError); 277} 278TestBadAssignmentLHS(); 279