js/src/jit-test/tests/debug/Environment-variables.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 // Comprehensive test of get/setVariable on many kinds of environments and
michael@0 2 // bindings.
michael@0 3
michael@0 4 load(libdir + "asserts.js");
michael@0 5
michael@0 6 var cases = [
michael@0 7 // global bindings and bindings on the global prototype chain
michael@0 8 "x = VAL; @@",
michael@0 9 "var x = VAL; @@",
michael@0 10 "Object.prototype.x = VAL; @@",
michael@0 11
michael@0 12 // let, catch, and comprehension bindings
michael@0 13 "let x = VAL; @@",
michael@0 14 "{ let x = VAL; @@ }",
michael@0 15 "let (x = VAL) { @@ }",
michael@0 16 "try { throw VAL; } catch (x) { @@ }",
michael@0 17 "try { throw VAL; } catch (x) { @@ }",
michael@0 18 "for (let x of [VAL]) { @@ }",
michael@0 19 "for each (let x in [VAL]) { @@ }",
michael@0 20 "switch (0) { default: let x = VAL; @@ }",
michael@0 21 "[function () { @@ }() for (x of [VAL])];",
michael@0 22 // "((function () { @@ })() for (x of [VAL])).next();", // bug 709367
michael@0 23
michael@0 24 // arguments
michael@0 25 "function f(x) { @@ } f(VAL);",
michael@0 26 "function f([w, x]) { @@ } f([0, VAL]);",
michael@0 27 "function f({v: x}) { @@ } f({v: VAL});",
michael@0 28 "function f([w, {v: x}]) { @@ } f([0, {v: VAL}]);",
michael@0 29
michael@0 30 // bindings in functions
michael@0 31 "function f() { var x = VAL; @@ } f();",
michael@0 32 "function f() { let x = VAL; @@ } f();",
michael@0 33 "function f([x]) { let x = VAL; @@ } f(['fail']);",
michael@0 34 "function f(x) { { let x = VAL; @@ } } f('fail');",
michael@0 35 "function f() { function x() {} x = VAL; @@ } f();",
michael@0 36
michael@0 37 // dynamic bindings
michael@0 38 "function f(s) { eval(s); @@ } f('var x = VAL');",
michael@0 39 "function f(s) { let (x = 'fail') { eval(s); } x = VAL; @@ } f('var x;');",
michael@0 40 "var x = VAL; function f(s) { eval('var x = 0;'); eval(s); @@ } f('delete x;');",
michael@0 41 "function f(obj) { with (obj) { @@ } } f({x: VAL});",
michael@0 42 "function f(obj) { with (obj) { @@ } } f(Object.create({x: VAL}));",
michael@0 43 "function f(b) { if (b) { function x(){} } x = VAL; @@ } f(1);",
michael@0 44 ];
michael@0 45
michael@0 46 var nextval = 1000;
michael@0 47
michael@0 48 function test(code, debugStmts, followupStmts) {
michael@0 49 var val = nextval++;
michael@0 50 var hits = 0;
michael@0 51
michael@0 52 var g = newGlobal();
michael@0 53 g.eval("function debugMe() { var x = 'wrong-x'; debugger; }");
michael@0 54 g.capture = null;
michael@0 55
michael@0 56 var dbg = Debugger(g);
michael@0 57 dbg.onDebuggerStatement = function (frame) {
michael@0 58 if (frame.callee !== null && frame.callee.name == 'debugMe')
michael@0 59 frame = frame.older;
michael@0 60 var env = frame.environment.find("x");
michael@0 61 assertEq(env.getVariable("x"), val)
michael@0 62 assertEq(env.setVariable("x", 'ok'), undefined);
michael@0 63 assertEq(env.getVariable("x"), 'ok');
michael@0 64
michael@0 65 // setVariable cannot create new variables.
michael@0 66 assertThrowsInstanceOf(function () { env.setVariable("newVar", 0); }, TypeError);
michael@0 67 hits++;
michael@0 68 };
michael@0 69
michael@0 70 code = code.replace("@@", debugStmts);
michael@0 71 if (followupStmts !== undefined)
michael@0 72 code += " " + followupStmts;
michael@0 73 code = code.replace(/VAL/g, uneval(val));
michael@0 74 g.eval(code);
michael@0 75 assertEq(hits, 1);
michael@0 76 }
michael@0 77
michael@0 78 for (var s of cases) {
michael@0 79 // Test triggering the debugger right in the scope in which x is bound.
michael@0 80 test(s, "debugger; assertEq(x, 'ok');");
michael@0 81
michael@0 82 // Test calling a function that triggers the debugger.
michael@0 83 test(s, "debugMe(); assertEq(x, 'ok');");
michael@0 84
michael@0 85 // Test triggering the debugger from a scope nested in x's scope.
michael@0 86 test(s, "let (y = 'irrelevant') { (function (z) { let (zz = y) { debugger; }})(); } assertEq(x, 'ok');"),
michael@0 87
michael@0 88 // Test closing over the variable and triggering the debugger later, after
michael@0 89 // leaving the variable's scope.
michael@0 90 test(s, "capture = {dbg: function () { debugger; }, get x() { return x; }};",
michael@0 91 "assertEq(capture.x, VAL); capture.dbg(); assertEq(capture.x, 'ok');");
michael@0 92 }

mercurial