Object-forceLexicalInitializationByName.js (1990B)
1 load(libdir + "asserts.js"); 2 3 var g = newGlobal({newCompartment: true}); 4 var dbg = new Debugger; 5 var gw = dbg.addDebuggee(g); 6 7 let errorOne, errorTwo; 8 9 function evalErrorStr(global, evalString) { 10 try { 11 global.evaluate(evalString); 12 return undefined; 13 } catch (e) { 14 return e.toString(); 15 } 16 } 17 18 19 assertEq(evalErrorStr(g, "let y = IDONTEXIST;"), "ReferenceError: IDONTEXIST is not defined"); 20 assertEq(evalErrorStr(g, "y = 1;"), 21 "ReferenceError: can't access lexical declaration 'y' before initialization"); 22 23 // Here we flip the uninitialized binding to undfined. 24 assertEq(gw.forceLexicalInitializationByName("y"), true); 25 assertEq(g.evaluate("y"), undefined); 26 g.evaluate("y = 1;"); 27 assertEq(g.evaluate("y"), 1); 28 29 // Ensure that bogus bindings return false, but otherwise trigger no error or 30 // side effect. 31 assertEq(gw.forceLexicalInitializationByName("idontexist"), false); 32 assertEq(evalErrorStr(g, "idontexist"), "ReferenceError: idontexist is not defined"); 33 34 // Ensure that ropes (non-atoms) behave properly 35 assertEq(gw.forceLexicalInitializationByName(("foo" + "bar" + "bop" + "zopple" + 2 + 3).slice(1)), 36 false); 37 assertEq(evalErrorStr(g, "let oobarbopzopple23 = IDONTEXIST;"), "ReferenceError: IDONTEXIST is not defined"); 38 assertEq(gw.forceLexicalInitializationByName(("foo" + "bar" + "bop" + "zopple" + 2 + 3).slice(1)), 39 true); 40 assertEq(g.evaluate("oobarbopzopple23"), undefined); 41 42 // Ensure that only strings are accepted by forceLexicalInitializationByName 43 const bad_types = [ 44 2112, 45 {geddy: "lee"}, 46 () => 1, 47 [], 48 Array, 49 "'1'", // non-identifier 50 ] 51 52 for (var badType of bad_types) { 53 assertThrowsInstanceOf(() => { 54 gw.forceLexicalInitializationByName(badType); 55 }, TypeError); 56 } 57 58 // Finally, supplying no arguments should throw a type error 59 assertThrowsInstanceOf(() => { 60 Debugger.isCompilableUnit(); 61 }, TypeError);