destructuring-requireobjectcoercible.js (2430B)
1 load(libdir + 'asserts.js'); 2 load(libdir + 'iteration.js'); 3 4 function f(v) 5 { 6 if (v + "") 7 ({} = v); 8 } 9 10 f(true); 11 f({}); 12 assertThrowsInstanceOf(() => f(null), TypeError); 13 assertThrowsInstanceOf(() => f(undefined), TypeError); 14 15 function g(v) 16 { 17 if (v + "") 18 ({} = v); 19 } 20 21 g(true); 22 g({}); 23 assertThrowsInstanceOf(() => g(undefined), TypeError); 24 assertThrowsInstanceOf(() => g(null), TypeError); 25 26 function h(v) 27 { 28 if (v + "") 29 ([] = v); 30 } 31 32 h([true]); 33 h("foo"); 34 assertThrowsInstanceOf(() => h(undefined), TypeError); 35 assertThrowsInstanceOf(() => h(null), TypeError); 36 37 Object.defineProperty(Boolean.prototype, "v", 38 { get() { "use strict"; return typeof this; }, 39 enumerable: true, 40 configurable: true }); 41 42 Object.defineProperty(Number.prototype, "v", 43 { get() { "use strict"; return typeof this; }, 44 enumerable: true, 45 configurable: true }); 46 47 Object.defineProperty(String.prototype, "v", 48 { get() { "use strict"; return typeof this; }, 49 enumerable: true, 50 configurable: true }); 51 52 Object.defineProperty(Symbol.prototype, "v", 53 { get() { "use strict"; return typeof this; }, 54 enumerable: true, 55 configurable: true }); 56 57 function primitiveThisSupported() 58 { 59 return 3.14.custom === "number"; 60 } 61 62 function primitiveThisTests() 63 { 64 function f(v) 65 { 66 var type = typeof v; 67 68 ({ v } = v); 69 70 assertEq(v, type); 71 } 72 73 f(true); 74 f(3.14); 75 f(72); 76 f("ohai"); 77 f(Symbol.iterator); 78 79 assertThrowsInstanceOf(() => f(undefined), TypeError); 80 assertThrowsInstanceOf(() => f(null), TypeError); 81 82 function g(v) 83 { 84 var type = typeof v; 85 86 ({ v } = v); 87 88 assertEq(v, type); 89 } 90 91 g(true); 92 g(3.14); 93 g(72); 94 g("ohai"); 95 g(Symbol.iterator); 96 97 assertThrowsInstanceOf(() => g(null), TypeError); 98 assertThrowsInstanceOf(() => g(undefined), TypeError); 99 } 100 if (primitiveThisSupported()) 101 primitiveThisTests(); 102 103 // Ensure the internal implementation of destructuring object pattern 104 // assignment -- using a self-hosted intrinsic function -- works even when lazy 105 // standard class initialization hasn't occurred. Unfortunately we can't use 106 // |newGlobal()| because that method eagerly initializes standard classes. 107 evalcx("({} = 1);", evalcx("lazy"));