PageRenderTime 31ms CodeModel.GetById 14ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/js/src/jit-test/tests/debug/Script-01.js

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