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;
33 // Accepts a function/closure 'fun' that must have a debugger statement inside.
34 // A variable 'variable_name' must be initialized before debugger statement
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.
37 function RunPauseTest(scope_number, expected_old_result, variable_name,
38 new_value, expected_new_result, fun) {
39 var actual_old_result = fun();
40 assertEquals(expected_old_result, actual_old_result);
42 var listener_delegate;
43 var listener_called = false;
46 function listener_delegate(exec_state) {
47 var scope = exec_state.frame(0).scope(scope_number);
48 scope.setVariableValue(variable_name, new_value);
51 function listener(event, exec_state, event_data, data) {
53 if (event == Debug.DebugEvent.Break) {
54 listener_called = true;
55 listener_delegate(exec_state);
62 // Add the debug event listener.
63 Debug.setListener(listener);
67 actual_new_result = fun();
69 Debug.setListener(null);
72 if (exception != null) {
73 assertUnreachable("Exception in listener\n" + exception.stack);
75 assertTrue(listener_called);
77 assertEquals(expected_new_result, actual_new_result);
80 // Accepts a closure 'fun' that returns a variable from it's outer scope.
81 // The test changes the value of variable via the handle to function and checks
82 // that the return value changed accordingly.
83 function RunClosureTest(scope_number, expected_old_result, variable_name,
84 new_value, expected_new_result, fun) {
85 var actual_old_result = fun();
86 assertEquals(expected_old_result, actual_old_result);
88 var fun_mirror = Debug.MakeMirror(fun);
90 var scope = fun_mirror.scope(scope_number);
91 scope.setVariableValue(variable_name, new_value);
93 var actual_new_result = fun();
95 assertEquals(expected_new_result, actual_new_result);
99 function ClosureTestCase(scope_index, old_result, variable_name, new_value,
100 new_result, success_expected, factory) {
101 this.scope_index_ = scope_index;
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;
110 ClosureTestCase.prototype.run_pause_test = function() {
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);
119 ClosureTestCase.prototype.run_closure_test = function() {
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);
128 ClosureTestCase.prototype.run_and_catch_ = function(runnable) {
129 if (this.success_expected_) {
132 assertThrows(runnable);
137 // Test scopes visible from closures.
139 var closure_test_cases = [
140 new ClosureTestCase(0, 'cat', 'v1', 5, 5, true,
141 function Factory(debug_stop) {
144 if (debug_stop) debugger;
149 new ClosureTestCase(0, 4, 't', 7, 9, true, function Factory(debug_stop) {
153 if (debug_stop) debugger;
158 new ClosureTestCase(0, 6, 't', 10, 13, true, function Factory(debug_stop) {
160 var r = eval("t = 3");
162 if (debug_stop) debugger;
167 new ClosureTestCase(0, 17, 's', 'Bird', 'Bird', true,
168 function Factory(debug_stop) {
171 if (debug_stop) debugger;
176 new ClosureTestCase(2, 'capybara', 'foo', 77, 77, true,
177 function Factory(debug_stop) {
178 var foo = "capybara";
182 throw {name: "test exception"};
185 if (debug_stop) debugger;
193 new ClosureTestCase(0, 'AlphaBeta', 'eee', 5, '5Beta', true,
194 function Factory(debug_stop) {
202 if (debug_stop) debugger;
210 for (var i = 0; i < closure_test_cases.length; i++) {
211 closure_test_cases[i].run_pause_test();
214 for (var i = 0; i < closure_test_cases.length; i++) {
215 closure_test_cases[i].run_closure_test();
221 RunPauseTest(0, 'HelloYou', 'u', 'We', 'HelloWe', (function Factory() {
230 RunPauseTest(0, 'Helloworld', 'p', 'GoodBye', 'HelloGoodBye',
231 (function Factory() {
242 RunPauseTest(0, 'mouse', 'v1', 'dog', 'dog', (function Factory() {
245 eval("v1 = 'mouse'");
251 RunPauseTest(0, 'mouse', 'v1', 'dog', 'dog', (function Factory() {
253 eval("var v1 = 'mouse'");
260 // Check that we correctly update local variable that
261 // is referenced from an inner closure.
262 RunPauseTest(0, 'Blue', 'v', 'Green', 'Green', (function Factory() {
276 // Check that we correctly update parameter, that is known to be stored
277 // both on stack and in heap.
278 RunPauseTest(0, 5, 'p', 2012, 2012, (function Factory() {
292 // Test value description protocol JSON
294 assertEquals(true, Debug.TestApi.CommandProcessorResolveValue({value: true}));
296 assertSame(null, Debug.TestApi.CommandProcessorResolveValue({type: "null"}));
297 assertSame(undefined,
298 Debug.TestApi.CommandProcessorResolveValue({type: "undefined"}));
300 assertSame("123", Debug.TestApi.CommandProcessorResolveValue(
301 {type: "string", stringDescription: "123"}));
302 assertSame(123, Debug.TestApi.CommandProcessorResolveValue(
303 {type: "number", stringDescription: "123"}));
305 assertSame(Number, Debug.TestApi.CommandProcessorResolveValue(
306 {handle: Debug.MakeMirror(Number).handle()}));
307 assertSame(RunClosureTest, Debug.TestApi.CommandProcessorResolveValue(
308 {handle: Debug.MakeMirror(RunClosureTest).handle()}));