OLD | NEW |
| (Empty) |
1 // Copyright 2012 the V8 project authors. All rights reserved. | |
2 // Redistribution and use in source and binary forms, with or without | |
3 // modification, are permitted provided that the following conditions are | |
4 // met: | |
5 // | |
6 // * Redistributions of source code must retain the above copyright | |
7 // notice, this list of conditions and the following disclaimer. | |
8 // * Redistributions in binary form must reproduce the above | |
9 // copyright notice, this list of conditions and the following | |
10 // disclaimer in the documentation and/or other materials provided | |
11 // with the distribution. | |
12 // * Neither the name of Google Inc. nor the names of its | |
13 // contributors may be used to endorse or promote products derived | |
14 // from this software without specific prior written permission. | |
15 // | |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | |
28 // Flags: --expose-debug-as debug | |
29 | |
30 // Get the Debug object exposed from the debug context global object. | |
31 var Debug = debug.Debug; | |
32 | |
33 function CheckScope(scope_mirror, scope_expectations, expected_scope_type) { | |
34 assertEquals(expected_scope_type, scope_mirror.scopeType()); | |
35 | |
36 var scope_object = scope_mirror.scopeObject().value(); | |
37 | |
38 for (var name in scope_expectations) { | |
39 var actual = scope_object[name]; | |
40 var expected = scope_expectations[name]; | |
41 assertEquals(expected, actual); | |
42 } | |
43 } | |
44 | |
45 var ScopeType = debug.ScopeType; | |
46 | |
47 var f1 = (function F1(x) { | |
48 function F2(y) { | |
49 var z = x + y; | |
50 with ({w: 5, v: "Capybara"}) { | |
51 var F3 = function(a, b) { | |
52 function F4(p) { | |
53 return p + a + b + z + w + v.length; | |
54 } | |
55 return F4; | |
56 } | |
57 return F3(4, 5); | |
58 } | |
59 } | |
60 return F2(17); | |
61 })(5); | |
62 | |
63 var mirror = Debug.MakeMirror(f1); | |
64 | |
65 assertEquals(6, mirror.scopeCount()); | |
66 | |
67 CheckScope(mirror.scope(0), { a: 4, b: 5 }, ScopeType.Closure); | |
68 CheckScope(mirror.scope(1), { w: 5, v: "Capybara" }, ScopeType.With); | |
69 CheckScope(mirror.scope(2), { z: 22 }, ScopeType.Closure); | |
70 CheckScope(mirror.scope(3), { x: 5 }, ScopeType.Closure); | |
71 CheckScope(mirror.scope(4), {}, ScopeType.Script); | |
72 CheckScope(mirror.scope(5), {}, ScopeType.Global); | |
73 | |
74 var f2 = function() { return 5; } | |
75 | |
76 var mirror = Debug.MakeMirror(f2); | |
77 | |
78 assertEquals(2, mirror.scopeCount()); | |
79 | |
80 CheckScope(mirror.scope(0), {}, ScopeType.Script); | |
81 CheckScope(mirror.scope(1), {}, ScopeType.Global); | |
82 | |
83 var f3 = (function F1(invisible_parameter) { | |
84 var invisible1 = 1; | |
85 var visible1 = 10; | |
86 return (function F2() { | |
87 var invisible2 = 2; | |
88 return (function F3() { | |
89 var visible2 = 20; | |
90 return (function () {return visible1 + visible2 + visible1a;}); | |
91 })(); | |
92 })(); | |
93 })(5); | |
94 | |
95 var mirror = Debug.MakeMirror(f3); | |
96 | |
97 assertEquals(4, mirror.scopeCount()); | |
98 | |
99 CheckScope(mirror.scope(0), { visible2: 20 }, ScopeType.Closure); | |
100 CheckScope(mirror.scope(1), { visible1: 10 }, ScopeType.Closure); | |
101 CheckScope(mirror.scope(2), {}, ScopeType.Script); | |
102 CheckScope(mirror.scope(3), {}, ScopeType.Global); | |
103 | |
104 | |
105 var f4 = (function One() { | |
106 try { | |
107 throw "I'm error 1"; | |
108 } catch (e1) { | |
109 try { | |
110 throw "I'm error 2"; | |
111 } catch (e2) { | |
112 return function GetError() { | |
113 return e1 + e2; | |
114 }; | |
115 } | |
116 } | |
117 })(); | |
118 | |
119 var mirror = Debug.MakeMirror(f4); | |
120 | |
121 assertEquals(4, mirror.scopeCount()); | |
122 | |
123 CheckScope(mirror.scope(0), { e2: "I'm error 2" }, ScopeType.Catch); | |
124 CheckScope(mirror.scope(1), { e1: "I'm error 1" }, ScopeType.Catch); | |
125 CheckScope(mirror.scope(2), {}, ScopeType.Script); | |
126 CheckScope(mirror.scope(3), {}, ScopeType.Global); | |
127 | |
128 | |
129 var f5 = (function Raz(p1, p2) { | |
130 var p3 = p1 + p2; | |
131 return (function() { | |
132 var p4 = 20; | |
133 var p5 = 21; | |
134 var p6 = 22; | |
135 return eval("(function(p7){return p1 + p4 + p6 + p7})"); | |
136 })(); | |
137 })(1,2); | |
138 | |
139 var mirror = Debug.MakeMirror(f5); | |
140 | |
141 assertEquals(4, mirror.scopeCount()); | |
142 | |
143 CheckScope(mirror.scope(0), { p4: 20, p6: 22 }, ScopeType.Closure); | |
144 CheckScope(mirror.scope(1), { p1: 1 }, ScopeType.Closure); | |
145 CheckScope(mirror.scope(2), {}, ScopeType.Script); | |
146 CheckScope(mirror.scope(3), {}, ScopeType.Global); | |
147 | |
148 | |
149 function CheckNoScopeVisible(f) { | |
150 var mirror = Debug.MakeMirror(f); | |
151 assertEquals(0, mirror.scopeCount()); | |
152 } | |
153 | |
154 CheckNoScopeVisible(Number); | |
155 | |
156 CheckNoScopeVisible(Function.toString); | |
OLD | NEW |