for-in-with-assignment-semantics.js (1325B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 const unreachable = () => { throw "unreachable"; }; 6 7 // var-initializer expression is executed before for-in expression. 8 var log = ""; 9 for (var x = (log += "head") in (log += "|expr", null)) unreachable(); 10 assertEq(log, "head|expr"); 11 12 log = ""; 13 for (var x = (log += "head") in (log += "|expr", {})) unreachable(); 14 assertEq(log, "head|expr"); 15 16 17 // for-in expression isn't executed when var-initializer throws exception. 18 function ExpectedError() {} 19 assertThrowsInstanceOf(() => { 20 var throwErr = () => { throw new ExpectedError(); }; 21 for (var x = throwErr() in unreachable()) unreachable(); 22 }, ExpectedError); 23 24 25 // Ensure environment operations are performed correctly. 26 var scope = new Proxy({x: 0}, new Proxy({}, { 27 get(t, pk, r) { 28 log += pk + "|"; 29 } 30 })); 31 32 log = ""; 33 with (scope) { 34 for (var x = 0 in {}) ; 35 } 36 assertEq(log, "has|get|set|getOwnPropertyDescriptor|defineProperty|"); 37 38 log = ""; 39 with (scope) { 40 for (var x = 0 in {p: 0}) ; 41 } 42 assertEq(log, "has|get|set|getOwnPropertyDescriptor|defineProperty|".repeat(2)); 43 44 45 if (typeof reportCompare === "function") 46 reportCompare(true, true);