| 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) { |
| 129 if (this.success_expected_) { |
| 130 runnable(); |
| 131 } else { |
| 132 assertThrows(runnable); |
| 133 } |
| 134 } |
| 135 |
| 136 |
| 137 // Test scopes visible from closures. |
| 138 |
| 139 var closure_test_cases = [ |
| 140 new ClosureTestCase(0, 'cat', 'v1', 5, 5, true, |
| 141 function Factory(debug_stop) { |
| 142 var v1 = 'cat'; |
| 143 return function() { |
| 144 if (debug_stop) debugger; |
| 145 return v1; |
| 146 } |
| 147 }), |
| 148 |
| 149 new ClosureTestCase(0, 4, 't', 7, 9, true, function Factory(debug_stop) { |
| 150 var t = 2; |
| 151 var r = eval("t"); |
| 152 return function() { |
| 153 if (debug_stop) debugger; |
| 154 return r + t; |
| 155 } |
| 156 }), |
| 157 |
| 158 new ClosureTestCase(0, 6, 't', 10, 13, true, function Factory(debug_stop) { |
| 159 var t = 2; |
| 160 var r = eval("t = 3"); |
| 161 return function() { |
| 162 if (debug_stop) debugger; |
| 163 return r + t; |
| 164 } |
| 165 }), |
| 166 |
| 167 new ClosureTestCase(0, 17, 's', 'Bird', 'Bird', true, |
| 168 function Factory(debug_stop) { |
| 169 eval("var s = 17"); |
| 170 return function() { |
| 171 if (debug_stop) debugger; |
| 172 return s; |
| 173 } |
| 174 }), |
| 175 |
| 176 new ClosureTestCase(2, 'capybara', 'foo', 77, 77, true, |
| 177 function Factory(debug_stop) { |
| 178 var foo = "capybara"; |
| 179 return (function() { |
| 180 var bar = "fish"; |
| 181 try { |
| 182 throw {name: "test exception"}; |
| 183 } catch (e) { |
| 184 return function() { |
| 185 if (debug_stop) debugger; |
| 186 bar = "beast"; |
| 187 return foo; |
| 188 } |
| 189 } |
| 190 })(); |
| 191 }), |
| 192 |
| 193 new ClosureTestCase(0, 'AlphaBeta', 'eee', 5, '5Beta', true, |
| 194 function Factory(debug_stop) { |
| 195 var foo = "Beta"; |
| 196 return (function() { |
| 197 var bar = "fish"; |
| 198 try { |
| 199 throw "Alpha"; |
| 200 } catch (eee) { |
| 201 return function() { |
| 202 if (debug_stop) debugger; |
| 203 return eee + foo; |
| 204 } |
| 205 } |
| 206 })(); |
| 207 }) |
| 208 ]; |
| 209 |
| 210 for (var i = 0; i < closure_test_cases.length; i++) { |
| 211 closure_test_cases[i].run_pause_test(); |
| 212 } |
| 213 |
| 214 for (var i = 0; i < closure_test_cases.length; i++) { |
| 215 closure_test_cases[i].run_closure_test(); |
| 216 } |
| 217 |
| 218 |
| 219 // Test local scope. |
| 220 |
| 221 RunPauseTest(0, 'HelloYou', 'u', 'We', 'HelloWe', (function Factory() { |
| 222 return function() { |
| 223 var u = "You"; |
| 224 var v = "Hello"; |
| 225 debugger; |
| 226 return v + u; |
| 227 } |
| 228 })()); |
| 229 |
| 230 RunPauseTest(0, 'Helloworld', 'p', 'GoodBye', 'HelloGoodBye', |
| 231 (function Factory() { |
| 232 function H(p) { |
| 233 var v = "Hello"; |
| 234 debugger; |
| 235 return v + p; |
| 236 } |
| 237 return function() { |
| 238 return H("world"); |
| 239 } |
| 240 })()); |
| 241 |
| 242 RunPauseTest(0, 'mouse', 'v1', 'dog', 'dog', (function Factory() { |
| 243 return function() { |
| 244 var v1 = 'cat'; |
| 245 eval("v1 = 'mouse'"); |
| 100 debugger; | 246 debugger; |
| 101 return v1; | 247 return v1; |
| 102 } | 248 } |
| 103 })()); | 249 })()); |
| 104 | 250 |
| 105 RunPauseTest(1, 'v2', 11, (function Factory(v2) { | 251 RunPauseTest(0, 'mouse', 'v1', 'dog', 'dog', (function Factory() { |
| 106 return function() { | 252 return function() { |
| 107 debugger; | 253 eval("var v1 = 'mouse'"); |
| 108 return v2; | 254 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; | 255 return v1; |
| 135 } | 256 } |
| 136 })()); | 257 })()); |
| 137 | 258 |
| 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 | 259 |
| 160 // Test value description protocol JSON | 260 // Test value description protocol JSON |
| 261 |
| 161 assertEquals(true, Debug.TestApi.CommandProcessorResolveValue({value: true})); | 262 assertEquals(true, Debug.TestApi.CommandProcessorResolveValue({value: true})); |
| 162 | 263 |
| 163 assertSame(null, Debug.TestApi.CommandProcessorResolveValue({type: "null"})); | 264 assertSame(null, Debug.TestApi.CommandProcessorResolveValue({type: "null"})); |
| 164 assertSame(undefined, | 265 assertSame(undefined, |
| 165 Debug.TestApi.CommandProcessorResolveValue({type: "undefined"})); | 266 Debug.TestApi.CommandProcessorResolveValue({type: "undefined"})); |
| 166 | 267 |
| 167 assertSame("123", Debug.TestApi.CommandProcessorResolveValue( | 268 assertSame("123", Debug.TestApi.CommandProcessorResolveValue( |
| 168 {type: "string", stringDescription: "123"})); | 269 {type: "string", stringDescription: "123"})); |
| 169 assertSame(123, Debug.TestApi.CommandProcessorResolveValue( | 270 assertSame(123, Debug.TestApi.CommandProcessorResolveValue( |
| 170 {type: "number", stringDescription: "123"})); | 271 {type: "number", stringDescription: "123"})); |
| 171 | 272 |
| 172 assertSame(Number, Debug.TestApi.CommandProcessorResolveValue( | 273 assertSame(Number, Debug.TestApi.CommandProcessorResolveValue( |
| 173 {handle: Debug.MakeMirror(Number).handle()})); | 274 {handle: Debug.MakeMirror(Number).handle()})); |
| 174 assertSame(RunClosureTest, Debug.TestApi.CommandProcessorResolveValue( | 275 assertSame(RunClosureTest, Debug.TestApi.CommandProcessorResolveValue( |
| 175 {handle: Debug.MakeMirror(RunClosureTest).handle()})); | 276 {handle: Debug.MakeMirror(RunClosureTest).handle()})); |
| 176 | |
| OLD | NEW |