OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // MODULE |
| 6 // Flags: --expose-debug-as debug |
| 7 |
| 8 |
| 9 var Debug = debug.Debug; |
| 10 |
| 11 var test_name; |
| 12 var listener_delegate; |
| 13 var listener_called; |
| 14 var exception; |
| 15 var begin_test_count = 0; |
| 16 var end_test_count = 0; |
| 17 var break_count = 0; |
| 18 |
| 19 function listener(event, exec_state, event_data, data) { |
| 20 try { |
| 21 if (event == Debug.DebugEvent.Break) { |
| 22 break_count++; |
| 23 listener_called = true; |
| 24 listener_delegate(exec_state); |
| 25 } |
| 26 } catch (e) { |
| 27 exception = e; |
| 28 } |
| 29 } |
| 30 |
| 31 Debug.setListener(listener); |
| 32 |
| 33 |
| 34 function BeginTest(name) { |
| 35 test_name = name; |
| 36 listener_delegate = null; |
| 37 listener_called = false; |
| 38 exception = null; |
| 39 begin_test_count++; |
| 40 } |
| 41 |
| 42 function EndTest() { |
| 43 assertTrue(listener_called, "listener not called for " + test_name); |
| 44 assertNull(exception, test_name + " / " + exception); |
| 45 end_test_count++; |
| 46 } |
| 47 |
| 48 |
| 49 // Check that two scope are the same. |
| 50 function assertScopeMirrorEquals(scope1, scope2) { |
| 51 assertEquals(scope1.scopeType(), scope2.scopeType()); |
| 52 assertEquals(scope1.frameIndex(), scope2.frameIndex()); |
| 53 assertEquals(scope1.scopeIndex(), scope2.scopeIndex()); |
| 54 assertPropertiesEqual(scope1.scopeObject().value(), scope2.scopeObject().value
()); |
| 55 } |
| 56 |
| 57 function CheckFastAllScopes(scopes, exec_state) |
| 58 { |
| 59 var fast_all_scopes = exec_state.frame().allScopes(true); |
| 60 var length = fast_all_scopes.length; |
| 61 assertTrue(scopes.length >= length); |
| 62 for (var i = 0; i < scopes.length && i < length; i++) { |
| 63 var scope = fast_all_scopes[length - i - 1]; |
| 64 assertTrue(scope.isScope()); |
| 65 assertEquals(scopes[scopes.length - i - 1], scope.scopeType()); |
| 66 } |
| 67 } |
| 68 |
| 69 |
| 70 // Check that the scope chain contains the expected types of scopes. |
| 71 function CheckScopeChain(scopes, exec_state) { |
| 72 var all_scopes = exec_state.frame().allScopes(); |
| 73 assertEquals(scopes.length, exec_state.frame().scopeCount()); |
| 74 assertEquals(scopes.length, all_scopes.length, "FrameMirror.allScopes length")
; |
| 75 for (var i = 0; i < scopes.length; i++) { |
| 76 var scope = exec_state.frame().scope(i); |
| 77 assertTrue(scope.isScope()); |
| 78 assertEquals(scopes[i], scope.scopeType()); |
| 79 assertScopeMirrorEquals(all_scopes[i], scope); |
| 80 } |
| 81 CheckFastAllScopes(scopes, exec_state); |
| 82 |
| 83 // Get the debug command processor. |
| 84 var dcp = exec_state.debugCommandProcessor("unspecified_running_state"); |
| 85 |
| 86 // Send a scopes request and check the result. |
| 87 var json; |
| 88 var request_json = '{"seq":0,"type":"request","command":"scopes"}'; |
| 89 var response_json = dcp.processDebugJSONRequest(request_json); |
| 90 var response = JSON.parse(response_json); |
| 91 assertEquals(scopes.length, response.body.scopes.length); |
| 92 for (var i = 0; i < scopes.length; i++) { |
| 93 assertEquals(i, response.body.scopes[i].index); |
| 94 assertEquals(scopes[i], response.body.scopes[i].type); |
| 95 if (scopes[i] == debug.ScopeType.Local || |
| 96 scopes[i] == debug.ScopeType.Script || |
| 97 scopes[i] == debug.ScopeType.Closure) { |
| 98 assertTrue(response.body.scopes[i].object.ref < 0); |
| 99 } else { |
| 100 assertTrue(response.body.scopes[i].object.ref >= 0); |
| 101 } |
| 102 var found = false; |
| 103 for (var j = 0; j < response.refs.length && !found; j++) { |
| 104 found = response.refs[j].handle == response.body.scopes[i].object.ref; |
| 105 } |
| 106 assertTrue(found, "Scope object " + response.body.scopes[i].object.ref + " n
ot found"); |
| 107 } |
| 108 } |
| 109 |
| 110 |
| 111 function CheckScopeDoesNotHave(properties, number, exec_state) { |
| 112 var scope = exec_state.frame().scope(number); |
| 113 for (var p of properties) { |
| 114 var property_mirror = scope.scopeObject().property(p); |
| 115 assertTrue(property_mirror.isUndefined(), 'property ' + p + ' found in scope
'); |
| 116 } |
| 117 } |
| 118 |
| 119 |
| 120 // Check that the scope contains at least minimum_content. For functions just |
| 121 // check that there is a function. |
| 122 function CheckScopeContent(minimum_content, number, exec_state) { |
| 123 var scope = exec_state.frame().scope(number); |
| 124 var minimum_count = 0; |
| 125 for (var p in minimum_content) { |
| 126 var property_mirror = scope.scopeObject().property(p); |
| 127 assertFalse(property_mirror.isUndefined(), 'property ' + p + ' not found in
scope'); |
| 128 if (typeof(minimum_content[p]) === 'function') { |
| 129 assertTrue(property_mirror.value().isFunction()); |
| 130 } else { |
| 131 assertEquals(minimum_content[p], property_mirror.value().value(), 'propert
y ' + p + ' has unexpected value'); |
| 132 } |
| 133 minimum_count++; |
| 134 } |
| 135 |
| 136 // 'arguments' and might be exposed in the local and closure scope. Just |
| 137 // ignore this. |
| 138 var scope_size = scope.scopeObject().properties().length; |
| 139 if (!scope.scopeObject().property('arguments').isUndefined()) { |
| 140 scope_size--; |
| 141 } |
| 142 // Ditto for 'this'. |
| 143 if (!scope.scopeObject().property('this').isUndefined()) { |
| 144 scope_size--; |
| 145 } |
| 146 // Temporary variables introduced by the parser have not been materialized. |
| 147 assertTrue(scope.scopeObject().property('').isUndefined()); |
| 148 |
| 149 if (scope_size < minimum_count) { |
| 150 print('Names found in scope:'); |
| 151 var names = scope.scopeObject().propertyNames(); |
| 152 for (var i = 0; i < names.length; i++) { |
| 153 print(names[i]); |
| 154 } |
| 155 } |
| 156 assertTrue(scope_size >= minimum_count); |
| 157 |
| 158 // Get the debug command processor. |
| 159 var dcp = exec_state.debugCommandProcessor("unspecified_running_state"); |
| 160 |
| 161 // Send a scope request for information on a single scope and check the |
| 162 // result. |
| 163 var request_json = '{"seq":0,"type":"request","command":"scope","arguments":{"
number":'; |
| 164 request_json += scope.scopeIndex(); |
| 165 request_json += '}}'; |
| 166 var response_json = dcp.processDebugJSONRequest(request_json); |
| 167 var response = JSON.parse(response_json); |
| 168 assertEquals(scope.scopeType(), response.body.type); |
| 169 assertEquals(number, response.body.index); |
| 170 if (scope.scopeType() == debug.ScopeType.Local || |
| 171 scope.scopeType() == debug.ScopeType.Script || |
| 172 scope.scopeType() == debug.ScopeType.Closure) { |
| 173 assertTrue(response.body.object.ref < 0); |
| 174 } else { |
| 175 assertTrue(response.body.object.ref >= 0); |
| 176 } |
| 177 var found = false; |
| 178 for (var i = 0; i < response.refs.length && !found; i++) { |
| 179 found = response.refs[i].handle == response.body.object.ref; |
| 180 } |
| 181 assertTrue(found, "Scope object " + response.body.object.ref + " not found"); |
| 182 } |
| 183 |
| 184 |
| 185 //////////////////////////////////////////////////////////////////////////////// |
| 186 // Actual tests. |
| 187 //////////////////////////////////////////////////////////////////////////////// |
| 188 |
| 189 |
| 190 BeginTest(); |
| 191 listener_delegate = function(exec_state) { |
| 192 CheckScopeChain([debug.ScopeType.Module, |
| 193 debug.ScopeType.Script, |
| 194 debug.ScopeType.Global], exec_state); |
| 195 CheckScopeContent( |
| 196 {local_var: undefined, exported_var: undefined, imported_var: undefined}, |
| 197 0, exec_state); |
| 198 CheckScopeDoesNotHave( |
| 199 ["doesnotexist", "local_let", "exported_let", "imported_let"], |
| 200 0, exec_state); |
| 201 }; |
| 202 debugger; |
| 203 EndTest(); |
| 204 |
| 205 let local_let = 1; |
| 206 var local_var = 2; |
| 207 export let exported_let = 3; |
| 208 export var exported_var = 4; |
| 209 import {exported_let as imported_let} from "modules-debug-scopes2.js"; |
| 210 import {exported_var as imported_var} from "modules-debug-scopes2.js"; |
| 211 |
| 212 BeginTest(); |
| 213 listener_delegate = function(exec_state) { |
| 214 CheckScopeChain([debug.ScopeType.Module, |
| 215 debug.ScopeType.Script, |
| 216 debug.ScopeType.Global], exec_state); |
| 217 CheckScopeContent( |
| 218 {local_let: 1, local_var: 2, exported_let: 3, exported_var: 4, |
| 219 imported_let: 3, imported_var: 4}, 0, exec_state); |
| 220 }; |
| 221 debugger; |
| 222 EndTest(); |
| 223 |
| 224 local_let += 10; |
| 225 local_var += 10; |
| 226 exported_let += 10; |
| 227 exported_var += 10; |
| 228 |
| 229 BeginTest(); |
| 230 listener_delegate = function(exec_state) { |
| 231 CheckScopeChain([debug.ScopeType.Module, |
| 232 debug.ScopeType.Script, |
| 233 debug.ScopeType.Global], exec_state); |
| 234 CheckScopeContent( |
| 235 {local_let: 11, local_var: 12, exported_let: 13, exported_var: 14, |
| 236 imported_let: 13, imported_var: 14}, 0, exec_state); |
| 237 }; |
| 238 debugger; |
| 239 EndTest(); |
OLD | NEW |