OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 16 matching lines...) Expand all Loading... | |
27 | 27 |
28 // Flags: --expose-debug-as debug | 28 // Flags: --expose-debug-as debug |
29 | 29 |
30 // Get the Debug object exposed from the debug context global object. | 30 // Get the Debug object exposed from the debug context global object. |
31 var Debug = debug.Debug; | 31 var Debug = debug.Debug; |
32 | 32 |
33 // Accepts a function/closure 'fun' that must have a debugger statement inside. | 33 // Accepts a function/closure 'fun' that must have a debugger statement inside. |
34 // A variable 'variable_name' must be initialized before debugger statement | 34 // A variable 'variable_name' must be initialized before debugger statement |
35 // and returned after the statement. The test will alter variable value when | 35 // and returned after the statement. The test will alter variable value when |
36 // on debugger statement and check that returned value reflects the change. | 36 // on debugger statement and check that returned value reflects the change. |
37 function RunPauseTest(scope_number, variable_name, expected_new_value, fun) { | 37 function RunPauseTest(scope_number, expected_old_result, variable_name, |
38 var old_value = fun(); | 38 new_value, expected_new_result, fun) { |
39 var actual_old_result = fun(); | |
40 assertEquals(expected_old_result, actual_old_result); | |
39 | 41 |
40 var listener_delegate; | 42 var listener_delegate; |
41 var listener_called = false; | 43 var listener_called = false; |
42 var exception = null; | 44 var exception = null; |
43 | 45 |
44 function listener_delegate(exec_state) { | 46 function listener_delegate(exec_state) { |
45 var scope = exec_state.frame(0).scope(scope_number); | 47 var scope = exec_state.frame(0).scope(scope_number); |
46 scope.setVariableValue(variable_name, expected_new_value); | 48 scope.setVariableValue(variable_name, new_value); |
47 } | 49 } |
48 | 50 |
49 function listener(event, exec_state, event_data, data) { | 51 function listener(event, exec_state, event_data, data) { |
50 try { | 52 try { |
51 if (event == Debug.DebugEvent.Break) { | 53 if (event == Debug.DebugEvent.Break) { |
52 listener_called = true; | 54 listener_called = true; |
53 listener_delegate(exec_state); | 55 listener_delegate(exec_state); |
54 } | 56 } |
55 } catch (e) { | 57 } catch (e) { |
56 exception = e; | 58 exception = e; |
57 } | 59 } |
58 } | 60 } |
59 | 61 |
60 // Add the debug event listener. | 62 // Add the debug event listener. |
61 Debug.setListener(listener); | 63 Debug.setListener(listener); |
62 | 64 |
63 var actual_new_value; | 65 var actual_new_value; |
64 try { | 66 try { |
65 actual_new_value = fun(); | 67 actual_new_result = fun(); |
66 } finally { | 68 } finally { |
67 Debug.setListener(null); | 69 Debug.setListener(null); |
68 } | 70 } |
69 | 71 |
70 if (exception != null) { | 72 if (exception != null) { |
71 assertUnreachable("Exception: " + exception); | 73 assertUnreachable("Exception in listener\n" + exception.stack); |
72 } | 74 } |
73 assertTrue(listener_called); | 75 assertTrue(listener_called); |
74 | 76 |
75 assertTrue(old_value != actual_new_value); | 77 assertEquals(expected_new_result, actual_new_result); |
76 assertTrue(expected_new_value == actual_new_value); | |
77 } | 78 } |
78 | 79 |
79 // Accepts a closure 'fun' that returns a variable from it's outer scope. | 80 // Accepts a closure 'fun' that returns a variable from it's outer scope. |
80 // The test changes the value of variable via the handle to function and checks | 81 // The test changes the value of variable via the handle to function and checks |
81 // that the return value changed accordingly. | 82 // that the return value changed accordingly. |
82 function RunClosureTest(scope_number, variable_name, expected_new_value, fun) { | 83 function RunClosureTest(scope_number, expected_old_result, variable_name, |
83 var old_value = fun(); | 84 new_value, expected_new_result, fun) { |
85 var actual_old_result = fun(); | |
86 assertEquals(expected_old_result, actual_old_result); | |
84 | 87 |
85 var fun_mirror = Debug.MakeMirror(fun); | 88 var fun_mirror = Debug.MakeMirror(fun); |
86 | 89 |
87 var scope = fun_mirror.scope(scope_number); | 90 var scope = fun_mirror.scope(scope_number); |
88 scope.setVariableValue(variable_name, expected_new_value); | 91 scope.setVariableValue(variable_name, new_value); |
89 | 92 |
90 var actual_new_value = fun(); | 93 var actual_new_result = fun(); |
91 | 94 |
92 assertTrue(old_value != actual_new_value); | 95 assertEquals(expected_new_result, actual_new_result); |
93 assertTrue(expected_new_value == actual_new_value); | 96 } |
94 } | 97 |
95 | 98 |
96 // Test changing variable value when in pause | 99 function ClosureTestCase(scope_index, old_result, variable_name, new_value, |
97 RunPauseTest(1, 'v1', 5, (function Factory() { | 100 new_result, success_expected, factory) { |
98 var v1 = 'cat'; | 101 this.scope_index_ = scope_index; |
99 return function() { | 102 this.old_result_ = old_result; |
103 this.variable_name_ = variable_name; | |
104 this.new_value_ = new_value; | |
105 this.new_result_ = new_result; | |
106 this.success_expected_ = success_expected; | |
107 this.factory_ = factory; | |
108 } | |
109 | |
110 ClosureTestCase.prototype.run_pause_test = function() { | |
111 var th = this; | |
112 var fun = this.factory_(true); | |
113 this.run_and_catch_(function() { | |
114 RunPauseTest(th.scope_index_ + 1, th.old_result_, th.variable_name_, | |
115 th.new_value_, th.new_result_, fun); | |
116 }); | |
117 } | |
118 | |
119 ClosureTestCase.prototype.run_closure_test = function() { | |
120 var th = this; | |
121 var fun = this.factory_(false); | |
122 this.run_and_catch_(function() { | |
123 RunClosureTest(th.scope_index_, th.old_result_, th.variable_name_, | |
124 th.new_value_, th.new_result_, fun); | |
125 }); | |
126 } | |
127 | |
128 ClosureTestCase.prototype.run_and_catch_ = function(runnable) { | |
Yang
2012/12/10 14:15:11
Why does this end with an underscore?
Peter Rybin
2012/12/11 23:20:44
I mean it a private member.
Yang
2012/12/12 09:53:40
Well, javascript doesn't really have a private con
| |
129 if (this.success_expected_) { | |
130 runnable(); | |
131 } else { | |
132 try { | |
Yang
2012/12/10 14:15:11
you could use
assertThrows(function() { runnable()
Peter Rybin
2012/12/11 23:20:44
Done.
| |
133 runnable(); | |
134 assertUnreachable(); | |
135 } catch (e) { | |
136 } | |
137 } | |
138 } | |
139 | |
140 | |
141 // Test scopes visible from closures. | |
142 | |
143 var closure_test_cases = [ | |
144 new ClosureTestCase(0, 'cat', 'v1', 5, 5, true, | |
145 function Factory(debug_stop) { | |
146 var v1 = 'cat'; | |
147 return function() { | |
148 if (debug_stop) debugger; | |
149 return v1; | |
150 } | |
151 }), | |
152 | |
153 new ClosureTestCase(0, 4, 't', 7, 9, true, function Factory(debug_stop) { | |
154 var t = 2; | |
155 var r = eval("t"); | |
156 return function() { | |
157 if (debug_stop) debugger; | |
158 return r + t; | |
159 } | |
160 }), | |
161 | |
162 new ClosureTestCase(0, 6, 't', 10, 13, true, function Factory(debug_stop) { | |
163 var t = 2; | |
164 var r = eval("t = 3"); | |
165 return function() { | |
166 if (debug_stop) debugger; | |
167 return r + t; | |
168 } | |
169 }), | |
170 | |
171 new ClosureTestCase(0, 17, 's', 'Bird', 'Bird', true, | |
172 function Factory(debug_stop) { | |
173 eval("var s = 17"); | |
174 return function() { | |
175 if (debug_stop) debugger; | |
176 return s; | |
177 } | |
178 }), | |
179 | |
180 new ClosureTestCase(2, 'capybara', 'foo', 77, 77, true, | |
181 function Factory(debug_stop) { | |
182 var foo = "capybara"; | |
183 return (function() { | |
184 var bar = "fish"; | |
185 try { | |
186 throw {name: "test exception"}; | |
187 } catch (e) { | |
188 return function() { | |
189 if (debug_stop) debugger; | |
190 bar = "beast"; | |
191 return foo; | |
192 } | |
193 } | |
194 })(); | |
195 }), | |
196 | |
197 new ClosureTestCase(0, 'AlphaBeta', 'eee', 5, '5Beta', true, | |
198 function Factory(debug_stop) { | |
199 var foo = "Beta"; | |
200 return (function() { | |
201 var bar = "fish"; | |
202 try { | |
203 throw "Alpha"; | |
204 } catch (eee) { | |
205 return function() { | |
206 if (debug_stop) debugger; | |
207 return eee + foo; | |
208 } | |
209 } | |
210 })(); | |
211 }) | |
212 ]; | |
213 | |
214 for (var i = 0; i < closure_test_cases.length; i++) { | |
215 closure_test_cases[i].run_pause_test(); | |
216 } | |
217 | |
218 for (var i = 0; i < closure_test_cases.length; i++) { | |
219 closure_test_cases[i].run_closure_test(); | |
220 } | |
221 | |
222 | |
223 // Test local scope. | |
224 | |
225 RunPauseTest(0, 'HelloYou', 'u', 'We', 'HelloWe', (function Factory() { | |
226 return function() { | |
227 var u = "You"; | |
228 var v = "Hello"; | |
229 debugger; | |
230 return v + u; | |
231 } | |
232 })()); | |
233 | |
234 RunPauseTest(0, 'Helloworld', 'p', 'GoodBye', 'HelloGoodBye', | |
235 (function Factory() { | |
236 function H(p) { | |
237 var v = "Hello"; | |
238 debugger; | |
239 return v + p; | |
240 } | |
241 return function() { | |
242 return H("world"); | |
243 } | |
244 })()); | |
245 | |
246 RunPauseTest(0, 'mouse', 'v1', 'dog', 'dog', (function Factory() { | |
247 return function() { | |
248 var v1 = 'cat'; | |
249 eval("v1 = 'mouse'"); | |
100 debugger; | 250 debugger; |
101 return v1; | 251 return v1; |
102 } | 252 } |
103 })()); | 253 })()); |
104 | 254 |
105 RunPauseTest(1, 'v2', 11, (function Factory(v2) { | 255 RunPauseTest(0, 'mouse', 'v1', 'dog', 'dog', (function Factory() { |
106 return function() { | 256 return function() { |
107 debugger; | 257 eval("var v1 = 'mouse'"); |
108 return v2; | 258 debugger; |
109 } | |
110 })('dog')); | |
111 | |
112 RunPauseTest(3, 'foo', 77, (function Factory() { | |
113 var foo = "capybara"; | |
114 return (function() { | |
115 var bar = "fish"; | |
116 try { | |
117 throw {name: "test exception"}; | |
118 } catch (e) { | |
119 return function() { | |
120 debugger; | |
121 bar = "beast"; | |
122 return foo; | |
123 } | |
124 } | |
125 })(); | |
126 })()); | |
127 | |
128 | |
129 | |
130 // Test changing variable value in closure by handle | |
131 RunClosureTest(0, 'v1', 5, (function Factory() { | |
132 var v1 = 'cat'; | |
133 return function() { | |
134 return v1; | 259 return v1; |
135 } | 260 } |
136 })()); | 261 })()); |
137 | 262 |
138 RunClosureTest(0, 'v2', 11, (function Factory(v2) { | |
139 return function() { | |
140 return v2; | |
141 } | |
142 })('dog')); | |
143 | |
144 RunClosureTest(2, 'foo', 77, (function Factory() { | |
145 var foo = "capybara"; | |
146 return (function() { | |
147 var bar = "fish"; | |
148 try { | |
149 throw {name: "test exception"}; | |
150 } catch (e) { | |
151 return function() { | |
152 bar = "beast"; | |
153 return foo; | |
154 } | |
155 } | |
156 })(); | |
157 })()); | |
158 | |
159 | 263 |
160 // Test value description protocol JSON | 264 // Test value description protocol JSON |
265 | |
161 assertEquals(true, Debug.TestApi.CommandProcessorResolveValue({value: true})); | 266 assertEquals(true, Debug.TestApi.CommandProcessorResolveValue({value: true})); |
162 | 267 |
163 assertSame(null, Debug.TestApi.CommandProcessorResolveValue({type: "null"})); | 268 assertSame(null, Debug.TestApi.CommandProcessorResolveValue({type: "null"})); |
164 assertSame(undefined, | 269 assertSame(undefined, |
165 Debug.TestApi.CommandProcessorResolveValue({type: "undefined"})); | 270 Debug.TestApi.CommandProcessorResolveValue({type: "undefined"})); |
166 | 271 |
167 assertSame("123", Debug.TestApi.CommandProcessorResolveValue( | 272 assertSame("123", Debug.TestApi.CommandProcessorResolveValue( |
168 {type: "string", stringDescription: "123"})); | 273 {type: "string", stringDescription: "123"})); |
169 assertSame(123, Debug.TestApi.CommandProcessorResolveValue( | 274 assertSame(123, Debug.TestApi.CommandProcessorResolveValue( |
170 {type: "number", stringDescription: "123"})); | 275 {type: "number", stringDescription: "123"})); |
171 | 276 |
172 assertSame(Number, Debug.TestApi.CommandProcessorResolveValue( | 277 assertSame(Number, Debug.TestApi.CommandProcessorResolveValue( |
173 {handle: Debug.MakeMirror(Number).handle()})); | 278 {handle: Debug.MakeMirror(Number).handle()})); |
174 assertSame(RunClosureTest, Debug.TestApi.CommandProcessorResolveValue( | 279 assertSame(RunClosureTest, Debug.TestApi.CommandProcessorResolveValue( |
175 {handle: Debug.MakeMirror(RunClosureTest).handle()})); | 280 {handle: Debug.MakeMirror(RunClosureTest).handle()})); |
176 | 281 |
282 | |
Yang
2012/12/10 14:15:11
stray edit?
Peter Rybin
2012/12/11 23:20:44
Done.
| |
OLD | NEW |