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
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.
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.
28 // Flags: --expose-debug-as debug
30 // Get the Debug object exposed from the debug context global object.
31 var Debug = debug.Debug;
32 var DebugCommandProcessor = debug.DebugCommandProcessor;
34 // Accepts a function/closure 'fun' that must have a debugger statement inside.
35 // A variable 'variable_name' must be initialized before debugger statement
36 // and returned after the statement. The test will alter variable value when
37 // on debugger statement and check that returned value reflects the change.
38 function RunPauseTest(scope_number, expected_old_result, variable_name,
39 new_value, expected_new_result, fun) {
40 var actual_old_result = fun();
41 assertEquals(expected_old_result, actual_old_result);
43 var listener_delegate;
44 var listener_called = false;
47 function listener_delegate(exec_state) {
48 var scope = exec_state.frame(0).scope(scope_number);
49 scope.setVariableValue(variable_name, new_value);
52 function listener(event, exec_state, event_data, data) {
54 if (event == Debug.DebugEvent.Break) {
55 listener_called = true;
56 listener_delegate(exec_state);
63 // Add the debug event listener.
64 Debug.setListener(listener);
68 actual_new_result = fun();
70 Debug.setListener(null);
73 if (exception != null) {
74 assertUnreachable("Exception in listener\n" + exception.stack);
76 assertTrue(listener_called);
78 assertEquals(expected_new_result, actual_new_result);
81 // Accepts a closure 'fun' that returns a variable from it's outer scope.
82 // The test changes the value of variable via the handle to function and checks
83 // that the return value changed accordingly.
84 function RunClosureTest(scope_number, expected_old_result, variable_name,
85 new_value, expected_new_result, fun) {
86 var actual_old_result = fun();
87 assertEquals(expected_old_result, actual_old_result);
89 var fun_mirror = Debug.MakeMirror(fun);
91 var scope = fun_mirror.scope(scope_number);
92 scope.setVariableValue(variable_name, new_value);
94 var actual_new_result = fun();
96 assertEquals(expected_new_result, actual_new_result);
100 function ClosureTestCase(scope_index, old_result, variable_name, new_value,
101 new_result, success_expected, factory) {
102 this.scope_index_ = scope_index;
103 this.old_result_ = old_result;
104 this.variable_name_ = variable_name;
105 this.new_value_ = new_value;
106 this.new_result_ = new_result;
107 this.success_expected_ = success_expected;
108 this.factory_ = factory;
111 ClosureTestCase.prototype.run_pause_test = function() {
113 var fun = this.factory_(true);
114 this.run_and_catch_(function() {
115 RunPauseTest(th.scope_index_ + 1, th.old_result_, th.variable_name_,
116 th.new_value_, th.new_result_, fun);
120 ClosureTestCase.prototype.run_closure_test = function() {
122 var fun = this.factory_(false);
123 this.run_and_catch_(function() {
124 RunClosureTest(th.scope_index_, th.old_result_, th.variable_name_,
125 th.new_value_, th.new_result_, fun);
129 ClosureTestCase.prototype.run_and_catch_ = function(runnable) {
130 if (this.success_expected_) {
133 assertThrows(runnable);
138 // Test scopes visible from closures.
140 var closure_test_cases = [
141 new ClosureTestCase(0, 'cat', 'v1', 5, 5, true,
142 function Factory(debug_stop) {
145 if (debug_stop) debugger;
150 new ClosureTestCase(0, 4, 't', 7, 9, true, function Factory(debug_stop) {
154 if (debug_stop) debugger;
159 new ClosureTestCase(0, 6, 't', 10, 13, true, function Factory(debug_stop) {
161 var r = eval("t = 3");
163 if (debug_stop) debugger;
168 new ClosureTestCase(0, 17, 's', 'Bird', 'Bird', true,
169 function Factory(debug_stop) {
172 if (debug_stop) debugger;
177 new ClosureTestCase(2, 'capybara', 'foo', 77, 77, true,
178 function Factory(debug_stop) {
179 var foo = "capybara";
183 throw {name: "test exception"};
186 if (debug_stop) debugger;
194 new ClosureTestCase(0, 'AlphaBeta', 'eee', 5, '5Beta', true,
195 function Factory(debug_stop) {
203 if (debug_stop) debugger;
211 for (var i = 0; i < closure_test_cases.length; i++) {
212 closure_test_cases[i].run_pause_test();
215 for (var i = 0; i < closure_test_cases.length; i++) {
216 closure_test_cases[i].run_closure_test();
222 RunPauseTest(0, 'HelloYou', 'u', 'We', 'HelloWe', (function Factory() {
231 RunPauseTest(0, 'Helloworld', 'p', 'GoodBye', 'HelloGoodBye',
232 (function Factory() {
243 RunPauseTest(0, 'mouse', 'v1', 'dog', 'dog', (function Factory() {
246 eval("v1 = 'mouse'");
252 RunPauseTest(0, 'mouse', 'v1', 'dog', 'dog', (function Factory() {
254 eval("var v1 = 'mouse'");
261 // Check that we correctly update local variable that
262 // is referenced from an inner closure.
263 RunPauseTest(0, 'Blue', 'v', 'Green', 'Green', (function Factory() {
277 // Check that we correctly update parameter, that is known to be stored
278 // both on stack and in heap.
279 RunPauseTest(0, 5, 'p', 2012, 2012, (function Factory() {
293 // Test value description protocol JSON
295 assertEquals(true, DebugCommandProcessor.resolveValue_({value: true}));
297 assertSame(null, DebugCommandProcessor.resolveValue_({type: "null"}));
298 assertSame(undefined,
299 DebugCommandProcessor.resolveValue_({type: "undefined"}));
301 assertSame("123", DebugCommandProcessor.resolveValue_(
302 {type: "string", stringDescription: "123"}));
303 assertSame(123, DebugCommandProcessor.resolveValue_(
304 {type: "number", stringDescription: "123"}));
306 assertSame(Number, DebugCommandProcessor.resolveValue_(
307 {handle: Debug.MakeMirror(Number).handle()}));
308 assertSame(RunClosureTest, DebugCommandProcessor.resolveValue_(
309 {handle: Debug.MakeMirror(RunClosureTest).handle()}));