| 1 |
// We get the same Debugger.Script object instance each time we ask. |
| 2 |
|
| 3 |
var global = newGlobal({newCompartment: true}); |
| 4 |
global.eval('function f() { debugger; }'); |
| 5 |
global.eval('function g() { debugger; }'); |
| 6 |
|
| 7 |
var debug = new Debugger(global); |
| 8 |
|
| 9 |
function evalAndNoteScripts(prog) { |
| 10 |
var scripts = {}; |
| 11 |
debug.onDebuggerStatement = function(frame) { |
| 12 |
if (frame.type == "call") |
| 13 |
assertEq(frame.script, frame.callee.script); |
| 14 |
scripts.frame = frame.script; |
| 15 |
if (frame.arguments[0]) |
| 16 |
scripts.argument = frame.arguments[0].script; |
| 17 |
}; |
| 18 |
global.eval(prog); |
| 19 |
return scripts; |
| 20 |
} |
| 21 |
|
| 22 |
// If we create a frame for a function and pass it as a value, those should |
| 23 |
// both yield the same Debugger.Script instance. |
| 24 |
var scripts = evalAndNoteScripts('f(f)'); |
| 25 |
assertEq(scripts.frame, scripts.argument); |
| 26 |
var fScript = scripts.argument; |
| 27 |
|
| 28 |
// If we call a second time, we should still get the same instance. |
| 29 |
scripts = evalAndNoteScripts('f(f)'); |
| 30 |
assertEq(scripts.frame, fScript); |
| 31 |
assertEq(scripts.argument, fScript); |
| 32 |
|
| 33 |
// If we call with a different argument, we should get a different Debugger.Script. |
| 34 |
scripts = evalAndNoteScripts('f(g)'); |
| 35 |
assertEq(scripts.frame !== scripts.argument, true); |
| 36 |
assertEq(scripts.frame, fScript); |
| 37 |
var gScript = scripts.argument; |
| 38 |
|
| 39 |
// See if we can get g via the frame. |
| 40 |
scripts = evalAndNoteScripts('g(f)'); |
| 41 |
assertEq(scripts.frame !== scripts.argument, true); |
| 42 |
assertEq(scripts.frame, gScript); |
| 43 |
assertEq(scripts.argument, fScript); |
| 44 |
|
| 45 |
// Different closures made from the same 'function' expression should yield |
| 46 |
// the same script. |
| 47 |
global.eval('function gen1(x) { return function clo(y) { return x+y; }; }'); |
| 48 |
global.eval('var clo1 = gen1(42);'); |
| 49 |
global.eval('var clo2 = gen1("smoot");'); |
| 50 |
var scripts1 = evalAndNoteScripts('f(clo1)'); |
| 51 |
var scripts2 = evalAndNoteScripts('f(clo2)'); |
| 52 |
assertEq(scripts1.argument, scripts2.argument); |
| 53 |
|
| 54 |
// Different closures made from the same 'function' declaration should yield |
| 55 |
// the same script. |
| 56 |
global.eval('function gen2(x) { function clo(y) { return x+y; }; return clo; }'); |
| 57 |
global.eval('var clo1 = gen2(42);'); |
| 58 |
global.eval('var clo2 = gen2("smoot");'); |
| 59 |
var scripts1 = evalAndNoteScripts('f(clo1)'); |
| 60 |
var scripts2 = evalAndNoteScripts('f(clo2)'); |
| 61 |
assertEq(scripts1.argument, scripts2.argument); |
| 62 |
|
| 63 |
// Different closures made from the same 'function' statement should yield |
| 64 |
// the same script. |
| 65 |
global.eval('function gen3(x) { if (true) { function clo(y) { return x+y; }; return clo; } }'); |
| 66 |
global.eval('var clo1 = gen3(42);'); |
| 67 |
global.eval('var clo2 = gen3("smoot");'); |
| 68 |
var scripts1 = evalAndNoteScripts('f(clo1)'); |
| 69 |
var scripts2 = evalAndNoteScripts('f(clo2)'); |
| 70 |
assertEq(scripts1.argument, scripts2.argument); |