Commit ee2f80c6 authored by neis's avatar neis Committed by Commit bot

[modules] Add partial support for debug-scopes.

Setting variables is not yet implemented..

R=adamk@chromium.org
BUG=v8:1569

Review-Url: https://codereview.chromium.org/2445683002
Cr-Commit-Position: refs/heads/master@{#40559}
parent 5c57fcce
......@@ -647,12 +647,8 @@ int ScopeInfo::ModuleIndex(Handle<String> name, VariableMode* mode,
int entry = ModuleVariablesIndex();
for (int i = 0; i < module_vars_count; ++i) {
if (*name == get(entry + kModuleVariableNameOffset)) {
int index = Smi::cast(get(entry + kModuleVariableIndexOffset))->value();
int properties =
Smi::cast(get(entry + kModuleVariablePropertiesOffset))->value();
*mode = VariableModeField::decode(properties);
*init_flag = InitFlagField::decode(properties);
*maybe_assigned_flag = MaybeAssignedFlagField::decode(properties);
int index;
ModuleVariable(i, nullptr, &index, mode, init_flag, maybe_assigned_flag);
return index;
}
entry += kModuleVariableEntryLength;
......@@ -794,6 +790,34 @@ int ScopeInfo::ModuleVariableCountIndex() { return ModuleInfoIndex() + 1; }
int ScopeInfo::ModuleVariablesIndex() { return ModuleVariableCountIndex() + 1; }
void ScopeInfo::ModuleVariable(int i, String** name, int* index,
VariableMode* mode,
InitializationFlag* init_flag,
MaybeAssignedFlag* maybe_assigned_flag) {
DCHECK_LE(0, i);
DCHECK_LT(i, Smi::cast(get(ModuleVariableCountIndex()))->value());
int entry = ModuleVariablesIndex() + i * kModuleVariableEntryLength;
int properties =
Smi::cast(get(entry + kModuleVariablePropertiesOffset))->value();
if (name != nullptr) {
*name = String::cast(get(entry + kModuleVariableNameOffset));
}
if (index != nullptr) {
*index = Smi::cast(get(entry + kModuleVariableIndexOffset))->value();
}
if (mode != nullptr) {
*mode = VariableModeField::decode(properties);
}
if (init_flag != nullptr) {
*init_flag = InitFlagField::decode(properties);
}
if (maybe_assigned_flag != nullptr) {
*maybe_assigned_flag = MaybeAssignedFlagField::decode(properties);
}
}
#ifdef DEBUG
static void PrintList(const char* list_name,
......@@ -904,5 +928,20 @@ Handle<ModuleInfo> ModuleInfo::New(Isolate* isolate, Zone* zone,
return result;
}
Handle<ModuleInfoEntry> ModuleInfo::LookupRegularImport(
Handle<ModuleInfo> info, Handle<String> local_name) {
Isolate* isolate = info->GetIsolate();
Handle<FixedArray> regular_imports(info->regular_imports(), isolate);
for (int i = 0, n = regular_imports->length(); i < n; ++i) {
Handle<ModuleInfoEntry> entry(
ModuleInfoEntry::cast(regular_imports->get(i)), isolate);
if (String::cast(entry->local_name())->Equals(*local_name)) {
return entry;
}
}
UNREACHABLE();
return Handle<ModuleInfoEntry>();
}
} // namespace internal
} // namespace v8
......@@ -82,7 +82,7 @@ Context* Context::declaration_context() {
Context* Context::closure_context() {
Context* current = this;
while (!current->IsFunctionContext() && !current->IsScriptContext() &&
!current->IsNativeContext()) {
!current->IsModuleContext() && !current->IsNativeContext()) {
current = current->previous();
DCHECK(current->closure() == closure());
}
......
......@@ -101,6 +101,8 @@ ScopeIterator::ScopeIterator(Isolate* isolate, FrameInspector* frame_inspector,
// Language mode may be inherited from the eval caller.
// Retrieve it from shared function info.
info->set_language_mode(shared_info->language_mode());
} else if (scope_info->scope_type() == MODULE_SCOPE) {
info->set_module();
} else {
DCHECK(scope_info->scope_type() == SCRIPT_SCOPE);
}
......@@ -608,17 +610,10 @@ MaybeHandle<JSObject> ScopeIterator::MaterializeModuleScope() {
Handle<Context> context = CurrentContext();
DCHECK(context->IsModuleContext());
Handle<ScopeInfo> scope_info(context->scope_info());
// Allocate and initialize a JSObject with all the members of the debugged
// module.
Handle<JSObject> module_scope =
isolate_->factory()->NewJSObjectWithNullProto();
// Fill all context locals.
CopyContextLocalsToScopeObject(scope_info, context, module_scope);
// TODO(neis): Also collect stack locals as well as imports and exports.
CopyModuleVarsToScopeObject(scope_info, context, module_scope);
return module_scope;
}
......@@ -789,6 +784,51 @@ void ScopeIterator::CopyContextLocalsToScopeObject(
}
}
void ScopeIterator::CopyModuleVarsToScopeObject(Handle<ScopeInfo> scope_info,
Handle<Context> context,
Handle<JSObject> scope_object) {
Isolate* isolate = scope_info->GetIsolate();
int module_variable_count =
Smi::cast(scope_info->get(scope_info->ModuleVariableCountIndex()))
->value();
for (int i = 0; i < module_variable_count; ++i) {
Handle<String> local_name;
bool is_export;
{
String* name;
int index;
scope_info->ModuleVariable(i, &name, &index);
CHECK(!ScopeInfo::VariableIsSynthetic(name));
local_name = handle(name, isolate);
is_export = index == Variable::kModuleExportIndex;
}
Handle<Object> value;
if (is_export) {
value =
Module::LoadExport(handle(context->module(), isolate), local_name);
} else {
Handle<ModuleInfo> module_info(scope_info->ModuleDescriptorInfo(),
isolate);
Handle<ModuleInfoEntry> entry =
ModuleInfo::LookupRegularImport(module_info, local_name);
Handle<String> import_name(String::cast(entry->import_name()), isolate);
int module_request = Smi::cast(entry->module_request())->value();
value = Module::LoadImport(handle(context->module(), isolate),
import_name, module_request);
}
// Reflect variables under TDZ as undefined in scope object.
if (value->IsTheHole(isolate)) continue;
// This should always succeed.
// TODO(verwaest): Use AddDataProperty instead.
JSObject::SetOwnPropertyIgnoreAttributes(scope_object, local_name, value,
NONE)
.Check();
}
}
void ScopeIterator::CopyContextExtensionToScopeObject(
Handle<Context> context, Handle<JSObject> scope_object,
KeyCollectionMode mode) {
......
......@@ -153,6 +153,9 @@ class ScopeIterator {
void CopyContextLocalsToScopeObject(Handle<ScopeInfo> scope_info,
Handle<Context> context,
Handle<JSObject> scope_object);
void CopyModuleVarsToScopeObject(Handle<ScopeInfo> scope_info,
Handle<Context> context,
Handle<JSObject> scope_object);
void CopyContextExtensionToScopeObject(Handle<Context> context,
Handle<JSObject> scope_object,
KeyCollectionMode mode);
......
......@@ -257,6 +257,7 @@ var ScopeType = { Global: 0,
Block: 5,
Script: 6,
Eval: 7,
Module: 8,
};
/**
......
......@@ -4686,6 +4686,14 @@ class ScopeInfo : public FixedArray {
VariableLocation* location, InitializationFlag* init_flag,
MaybeAssignedFlag* maybe_assigned_flag);
// Get metadata of i-th MODULE-allocated variable, where 0 <= i <
// ModuleVariableCount. The metadata is returned via out-arguments, which may
// be nullptr if the corresponding information is not requested
void ModuleVariable(int i, String** name, int* index,
VariableMode* mode = nullptr,
InitializationFlag* init_flag = nullptr,
MaybeAssignedFlag* maybe_assigned_flag = nullptr);
// Used for the function name variable for named function expressions, and for
// the receiver.
enum VariableAllocationInfo { NONE, STACK, CONTEXT, UNUSED };
......@@ -4753,14 +4761,19 @@ class ModuleInfoEntry : public FixedArray {
class ModuleInfo : public FixedArray {
public:
DECLARE_CAST(ModuleInfo)
static Handle<ModuleInfo> New(Isolate* isolate, Zone* zone,
ModuleDescriptor* descr);
inline FixedArray* module_requests() const;
inline FixedArray* special_exports() const;
inline FixedArray* regular_exports() const;
inline FixedArray* namespace_imports() const;
inline FixedArray* regular_imports() const;
static Handle<ModuleInfoEntry> LookupRegularImport(Handle<ModuleInfo> info,
Handle<String> local_name);
#ifdef DEBUG
inline bool Equals(ModuleInfo* other) const;
#endif
......
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// MODULE
// Flags: --expose-debug-as debug --allow-natives-syntax
// These tests are copied from mjsunit/debug-scopes.js and adapted for modules.
var Debug = debug.Debug;
var test_name;
var listener_delegate;
var listener_called;
var exception;
var begin_test_count = 0;
var end_test_count = 0;
var break_count = 0;
// Debug event listener which delegates.
function listener(event, exec_state, event_data, data) {
try {
if (event == Debug.DebugEvent.Break) {
break_count++;
listener_called = true;
listener_delegate(exec_state);
}
} catch (e) {
exception = e;
}
}
// Add the debug event listener.
Debug.setListener(listener);
// Initialize for a new test.
function BeginTest(name) {
test_name = name;
listener_delegate = null;
listener_called = false;
exception = null;
begin_test_count++;
}
// Check result of a test.
function EndTest() {
assertTrue(listener_called, "listener not called for " + test_name);
assertNull(exception, test_name + " / " + exception);
end_test_count++;
}
// Check that two scope are the same.
function assertScopeMirrorEquals(scope1, scope2) {
assertEquals(scope1.scopeType(), scope2.scopeType());
assertEquals(scope1.frameIndex(), scope2.frameIndex());
assertEquals(scope1.scopeIndex(), scope2.scopeIndex());
assertPropertiesEqual(scope1.scopeObject().value(), scope2.scopeObject().value());
}
function CheckFastAllScopes(scopes, exec_state)
{
var fast_all_scopes = exec_state.frame().allScopes(true);
var length = fast_all_scopes.length;
assertTrue(scopes.length >= length);
for (var i = 0; i < scopes.length && i < length; i++) {
var scope = fast_all_scopes[length - i - 1];
assertTrue(scope.isScope());
assertEquals(scopes[scopes.length - i - 1], scope.scopeType());
}
}
// Check that the scope chain contains the expected types of scopes.
function CheckScopeChain(scopes, exec_state) {
var all_scopes = exec_state.frame().allScopes();
assertEquals(scopes.length, exec_state.frame().scopeCount());
assertEquals(scopes.length, all_scopes.length, "FrameMirror.allScopes length");
for (var i = 0; i < scopes.length; i++) {
var scope = exec_state.frame().scope(i);
assertTrue(scope.isScope());
assertEquals(scopes[i], scope.scopeType());
assertScopeMirrorEquals(all_scopes[i], scope);
}
CheckFastAllScopes(scopes, exec_state);
// Get the debug command processor.
var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
// Send a scopes request and check the result.
var json;
var request_json = '{"seq":0,"type":"request","command":"scopes"}';
var response_json = dcp.processDebugJSONRequest(request_json);
var response = JSON.parse(response_json);
assertEquals(scopes.length, response.body.scopes.length);
for (var i = 0; i < scopes.length; i++) {
assertEquals(i, response.body.scopes[i].index);
assertEquals(scopes[i], response.body.scopes[i].type);
if (scopes[i] == debug.ScopeType.Local ||
scopes[i] == debug.ScopeType.Script ||
scopes[i] == debug.ScopeType.Closure) {
assertTrue(response.body.scopes[i].object.ref < 0);
} else {
assertTrue(response.body.scopes[i].object.ref >= 0);
}
var found = false;
for (var j = 0; j < response.refs.length && !found; j++) {
found = response.refs[j].handle == response.body.scopes[i].object.ref;
}
assertTrue(found, "Scope object " + response.body.scopes[i].object.ref + " not found");
}
}
// Check that the scope chain contains the expected names of scopes.
function CheckScopeChainNames(names, exec_state) {
var all_scopes = exec_state.frame().allScopes();
assertEquals(names.length, all_scopes.length, "FrameMirror.allScopes length");
for (var i = 0; i < names.length; i++) {
var scope = exec_state.frame().scope(i);
assertTrue(scope.isScope());
assertEquals(names[i], scope.details().name())
}
}
// Check that the scope contains at least minimum_content. For functions just
// check that there is a function.
function CheckScopeContent(minimum_content, number, exec_state) {
var scope = exec_state.frame().scope(number);
var minimum_count = 0;
for (var p in minimum_content) {
var property_mirror = scope.scopeObject().property(p);
assertFalse(property_mirror.isUndefined(), 'property ' + p + ' not found in scope');
if (typeof(minimum_content[p]) === 'function') {
assertTrue(property_mirror.value().isFunction());
} else {
assertEquals(minimum_content[p], property_mirror.value().value(), 'property ' + p + ' has unexpected value');
}
minimum_count++;
}
// 'arguments' and might be exposed in the local and closure scope. Just
// ignore this.
var scope_size = scope.scopeObject().properties().length;
if (!scope.scopeObject().property('arguments').isUndefined()) {
scope_size--;
}
// Ditto for 'this'.
if (!scope.scopeObject().property('this').isUndefined()) {
scope_size--;
}
// Temporary variables introduced by the parser have not been materialized.
assertTrue(scope.scopeObject().property('').isUndefined());
if (scope_size < minimum_count) {
print('Names found in scope:');
var names = scope.scopeObject().propertyNames();
for (var i = 0; i < names.length; i++) {
print(names[i]);
}
}
assertTrue(scope_size >= minimum_count);
// Get the debug command processor.
var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
// Send a scope request for information on a single scope and check the
// result.
var request_json = '{"seq":0,"type":"request","command":"scope","arguments":{"number":';
request_json += scope.scopeIndex();
request_json += '}}';
var response_json = dcp.processDebugJSONRequest(request_json);
var response = JSON.parse(response_json);
assertEquals(scope.scopeType(), response.body.type);
assertEquals(number, response.body.index);
if (scope.scopeType() == debug.ScopeType.Local ||
scope.scopeType() == debug.ScopeType.Script ||
scope.scopeType() == debug.ScopeType.Closure) {
assertTrue(response.body.object.ref < 0);
} else {
assertTrue(response.body.object.ref >= 0);
}
var found = false;
for (var i = 0; i < response.refs.length && !found; i++) {
found = response.refs[i].handle == response.body.object.ref;
}
assertTrue(found, "Scope object " + response.body.object.ref + " not found");
}
// Check that the scopes have positions as expected.
function CheckScopeChainPositions(positions, exec_state) {
var all_scopes = exec_state.frame().allScopes();
assertTrue(positions.length <= all_scopes.length, "FrameMirror.allScopes length");
for (var i = 0; i < positions.length; i++) {
var scope = exec_state.frame().scope(i);
assertTrue(scope.isScope());
var position = positions[i];
if (!position)
continue;
print(`Checking position.start = ${position.start}, .end = ${position.end}`);
assertEquals(position.start, scope.details().startPosition())
assertEquals(position.end, scope.details().endPosition())
}
}
// Simple empty local scope.
BeginTest("Local 1");
function local_1() {
debugger;
}
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({}, 0, exec_state);
};
local_1();
EndTest();
// Local scope with a parameter.
BeginTest("Local 2");
function local_2(a) {
debugger;
}
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({a:1}, 0, exec_state);
};
local_2(1);
EndTest();
// Local scope with a parameter and a local variable.
BeginTest("Local 3");
function local_3(a) {
var x = 3;
debugger;
}
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({a:1,x:3}, 0, exec_state);
};
local_3(1);
EndTest();
// Local scope with parameters and local variables.
BeginTest("Local 4");
function local_4(a, b) {
var x = 3;
var y = 4;
debugger;
}
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({a:1,b:2,x:3,y:4}, 0, exec_state);
};
local_4(1, 2);
EndTest();
// Empty local scope with use of eval.
BeginTest("Local 5");
function local_5() {
eval('');
debugger;
}
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({}, 0, exec_state);
};
local_5();
EndTest();
// Local introducing local variable using eval.
BeginTest("Local 6");
function local_6() {
eval('var i = 5');
debugger;
}
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({}, 0, exec_state);
};
local_6();
EndTest();
// Local scope with parameters and local variables.
BeginTest("Local 7");
function local_7(a, b) {
var x = 3;
var y = 4;
eval('var i = 5');
eval('var j = 6');
debugger;
}
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({a:1,b:2,x:3,y:4}, 0, exec_state);
};
local_7(1, 2);
EndTest();
// Simple closure formed by returning an inner function referering the outer
// functions arguments.
BeginTest("Closure 1");
function closure_1(a) {
function f() {
debugger;
return a;
};
return f;
}
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Closure,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({a:1}, 1, exec_state);
CheckScopeChainNames(["f", "closure_1", undefined, undefined, undefined], exec_state)
};
closure_1(1)();
EndTest();
// Simple closure formed by returning an inner function referering the outer
// functions arguments. Due to VM optimizations parts of the actual closure is
// missing from the debugger information.
BeginTest("Closure 2");
function closure_2(a, b) {
var x = a + 2;
var y = b + 2;
function f() {
debugger;
return a + x;
};
return f;
}
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Closure,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({a:1,x:3}, 1, exec_state);
CheckScopeChainNames(["f", "closure_2", undefined, undefined, undefined], exec_state)
};
closure_2(1, 2)();
EndTest();
// Simple closure formed by returning an inner function referering the outer
// functions arguments. Using all arguments and locals from the outer function
// in the inner function makes these part of the debugger information on the
// closure.
BeginTest("Closure 3");
function closure_3(a, b) {
var x = a + 2;
var y = b + 2;
function f() {
debugger;
return a + b + x + y;
};
return f;
}
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Closure,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({a:1,b:2,x:3,y:4}, 1, exec_state);
CheckScopeChainNames(["f", "closure_3", undefined, undefined, undefined], exec_state)
};
closure_3(1, 2)();
EndTest();
// Simple closure formed by returning an inner function referering the outer
// functions arguments. Using all arguments and locals from the outer function
// in the inner function makes these part of the debugger information on the
// closure. Use the inner function as well...
BeginTest("Closure 4");
function closure_4(a, b) {
var x = a + 2;
var y = b + 2;
function f() {
debugger;
if (f) {
return a + b + x + y;
}
};
return f;
}
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Closure,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({a:1,b:2,x:3,y:4,f:function(){}}, 1, exec_state);
CheckScopeChainNames(["f", "closure_4", undefined, undefined, undefined], exec_state)
};
closure_4(1, 2)();
EndTest();
// Simple closure formed by returning an inner function referering the outer
// functions arguments. In the presence of eval all arguments and locals
// (including the inner function itself) from the outer function becomes part of
// the debugger infformation on the closure.
BeginTest("Closure 5");
function closure_5(a, b) {
var x = 3;
var y = 4;
function f() {
eval('');
debugger;
return 1;
};
return f;
}
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Closure,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({a:1,b:2,x:3,y:4,f:function(){}}, 1, exec_state);
CheckScopeChainNames(["f", "closure_5", undefined, undefined, undefined], exec_state)
};
closure_5(1, 2)();
EndTest();
// Two closures. Due to optimizations only the parts actually used are provided
// through the debugger information.
BeginTest("Closure 6");
let some_global;
function closure_6(a, b) {
function f(a, b) {
var x = 3;
var y = 4;
return function() {
var x = 3;
var y = 4;
debugger;
some_global = a;
return f;
};
}
return f(a, b);
}
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Closure,
debug.ScopeType.Closure,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({a:1}, 1, exec_state);
CheckScopeContent({f:function(){}}, 2, exec_state);
CheckScopeChainNames([undefined, "f", "closure_6", undefined, undefined, undefined], exec_state)
};
closure_6(1, 2)();
EndTest();
// Two closures. In the presence of eval all information is provided as the
// compiler cannot determine which parts are used.
BeginTest("Closure 7");
function closure_7(a, b) {
var x = 3;
var y = 4;
eval('var i = 5');
eval('var j = 6');
function f(a, b) {
var x = 3;
var y = 4;
eval('var i = 5');
eval('var j = 6');
return function() {
debugger;
some_global = a;
return f;
};
}
return f(a, b);
}
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Closure,
debug.ScopeType.Closure,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({}, 0, exec_state);
CheckScopeContent({a:1,b:2,x:3,y:4}, 1, exec_state);
CheckScopeContent({a:1,b:2,x:3,y:4,f:function(){}}, 2, exec_state);
CheckScopeChainNames([undefined, "f", "closure_7", undefined, undefined, undefined], exec_state)
};
closure_7(1, 2)();
EndTest();
// Closure that may be optimized out.
BeginTest("Closure 8");
function closure_8() {
(function inner(x) {
debugger;
})(2);
}
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({x: 2}, 0, exec_state);
CheckScopeChainNames(["inner", undefined, undefined, undefined], exec_state)
};
closure_8();
EndTest();
BeginTest("Closure 9");
let closure_9 = Function(' \
eval("var y = 1;"); \
eval("var z = 1;"); \
(function inner(x) { \
y++; \
z++; \
debugger; \
})(2); \
')
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Closure,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeChainNames(["inner", undefined, undefined, undefined], exec_state)
};
closure_9();
EndTest();
// Test global scope.
BeginTest("Global");
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Module, debug.ScopeType.Script, debug.ScopeType.Global], exec_state);
CheckScopeChainNames([undefined, undefined, undefined], exec_state)
};
debugger;
EndTest();
BeginTest("Catch block 1");
function catch_block_1() {
try {
throw 'Exception';
} catch (e) {
debugger;
}
};
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Catch,
debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({e:'Exception'}, 0, exec_state);
CheckScopeChainNames(["catch_block_1", "catch_block_1", undefined, undefined, undefined], exec_state)
};
catch_block_1();
EndTest();
BeginTest("Catch block 3");
function catch_block_3() {
eval("var y = 78;");
try {
throw 'Exception';
} catch (e) {
debugger;
}
};
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Catch,
debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({e:'Exception'}, 0, exec_state);
CheckScopeContent({}, 1, exec_state);
CheckScopeChainNames(["catch_block_3", "catch_block_3", undefined, undefined, undefined], exec_state)
};
catch_block_3();
EndTest();
// Test catch in global scope.
BeginTest("Catch block 5");
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Catch,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({e:'Exception'}, 0, exec_state);
CheckScopeChainNames([undefined, undefined, undefined, undefined], exec_state)
};
try {
throw 'Exception';
} catch (e) {
debugger;
}
EndTest();
// Closure inside catch in global code.
BeginTest("Catch block 6");
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Catch,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({x: 2}, 0, exec_state);
CheckScopeContent({e:'Exception'}, 1, exec_state);
CheckScopeChainNames([undefined, undefined, undefined, undefined, undefined], exec_state)
};
try {
throw 'Exception';
} catch (e) {
(function(x) {
debugger;
})(2);
}
EndTest();
// Catch block in function that is marked for optimization while being executed.
BeginTest("Catch block 7");
function catch_block_7() {
%OptimizeFunctionOnNextCall(catch_block_7);
try {
throw 'Exception';
} catch (e) {
debugger;
}
};
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Catch,
debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({e:'Exception'}, 0, exec_state);
CheckScopeChainNames(["catch_block_7", "catch_block_7", undefined, undefined, undefined], exec_state)
};
catch_block_7();
EndTest();
BeginTest("Classes and methods 1");
listener_delegate = function(exec_state) {
"use strict"
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent({}, 1, exec_state);
CheckScopeChainNames(["m", undefined, undefined, undefined], exec_state)
};
(function() {
"use strict";
class C1 {
m() {
debugger;
}
}
new C1().m();
})();
EndTest();
BeginTest("Scope positions");
var code1 = "function f() { \n" +
" var a = 1; \n" +
" function b() { \n" +
" debugger; \n" +
" return a + 1; \n" +
" } \n" +
" b(); \n" +
"} \n" +
"f(); \n";
listener_delegate = function(exec_state) {
CheckScopeChainPositions([{start: 58, end: 118}, {start: 10, end: 162}], exec_state);
}
eval(code1);
EndTest();
BeginTest("Scope positions in for statement");
var code3 = "function for_statement() { \n" +
" for (let i = 0; i < 1; i++) { \n" +
" debugger; \n" +
" } \n" +
"} \n" +
"for_statement(); \n";
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Block,
debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeChainPositions([{start: 52, end: 111}, {start: 22, end: 145}], exec_state);
}
eval(code3);
EndTest();
BeginTest("Scope positions in for statement with lexical block");
var code4 = "function for_statement() { \n" +
" for (let i = 0; i < 1; i++) { \n" +
" let j; \n" +
" debugger; \n" +
" } \n" +
"} \n" +
"for_statement(); \n";
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Block,
debug.ScopeType.Block,
debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeChainPositions([{start: 66, end: 147}, {start: 52, end: 147}, {start: 22, end: 181}], exec_state);
}
eval(code4);
EndTest();
BeginTest("Scope positions in lexical for each statement");
var code5 = "function for_each_statement() { \n" +
" for (let i of [0]) { \n" +
" debugger; \n" +
" } \n" +
"} \n" +
"for_each_statement(); \n";
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Block,
debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeChainPositions([{start: 55, end: 111}, {start: 27, end: 145}], exec_state);
}
eval(code5);
EndTest();
BeginTest("Scope positions in lexical for each statement with lexical block");
var code6 = "function for_each_statement() { \n" +
" for (let i of [0]) { \n" +
" let j; \n" +
" debugger; \n" +
" } \n" +
"} \n" +
"for_each_statement(); \n";
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Block,
debug.ScopeType.Block,
debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeChainPositions([{start: 57, end: 147}, {start: 55, end: 147}, {start: 27, end: 181}], exec_state);
}
eval(code6);
EndTest();
BeginTest("Scope positions in non-lexical for each statement");
var code7 = "function for_each_statement() { \n" +
" var i; \n" +
" for (i of [0]) { \n" +
" debugger; \n" +
" } \n" +
"} \n" +
"for_each_statement(); \n";
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeChainPositions([{start: 27, end: 181}], exec_state);
}
eval(code7);
EndTest();
BeginTest("Scope positions in non-lexical for each statement with lexical block");
var code8 = "function for_each_statement() { \n" +
" var i; \n" +
" for (i of [0]) { \n" +
" let j; \n" +
" debugger; \n" +
" } \n" +
"} \n" +
"for_each_statement(); \n";
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Block,
debug.ScopeType.Local,
debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeChainPositions([{start: 89, end: 183}, {start: 27, end: 217}], exec_state);
}
eval(code8);
EndTest();
assertEquals(begin_test_count, break_count,
'one or more tests did not enter the debugger');
assertEquals(begin_test_count, end_test_count,
'one or more tests did not have its result checked');
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// MODULE
// Flags: --expose-debug-as debug
var Debug = debug.Debug;
var test_name;
var listener_delegate;
var listener_called;
var exception;
var begin_test_count = 0;
var end_test_count = 0;
var break_count = 0;
function listener(event, exec_state, event_data, data) {
try {
if (event == Debug.DebugEvent.Break) {
break_count++;
listener_called = true;
listener_delegate(exec_state);
}
} catch (e) {
exception = e;
}
}
Debug.setListener(listener);
function BeginTest(name) {
test_name = name;
listener_delegate = null;
listener_called = false;
exception = null;
begin_test_count++;
}
function EndTest() {
assertTrue(listener_called, "listener not called for " + test_name);
assertNull(exception, test_name + " / " + exception);
end_test_count++;
}
// Check that two scope are the same.
function assertScopeMirrorEquals(scope1, scope2) {
assertEquals(scope1.scopeType(), scope2.scopeType());
assertEquals(scope1.frameIndex(), scope2.frameIndex());
assertEquals(scope1.scopeIndex(), scope2.scopeIndex());
assertPropertiesEqual(scope1.scopeObject().value(), scope2.scopeObject().value());
}
function CheckFastAllScopes(scopes, exec_state)
{
var fast_all_scopes = exec_state.frame().allScopes(true);
var length = fast_all_scopes.length;
assertTrue(scopes.length >= length);
for (var i = 0; i < scopes.length && i < length; i++) {
var scope = fast_all_scopes[length - i - 1];
assertTrue(scope.isScope());
assertEquals(scopes[scopes.length - i - 1], scope.scopeType());
}
}
// Check that the scope chain contains the expected types of scopes.
function CheckScopeChain(scopes, exec_state) {
var all_scopes = exec_state.frame().allScopes();
assertEquals(scopes.length, exec_state.frame().scopeCount());
assertEquals(scopes.length, all_scopes.length, "FrameMirror.allScopes length");
for (var i = 0; i < scopes.length; i++) {
var scope = exec_state.frame().scope(i);
assertTrue(scope.isScope());
assertEquals(scopes[i], scope.scopeType());
assertScopeMirrorEquals(all_scopes[i], scope);
}
CheckFastAllScopes(scopes, exec_state);
// Get the debug command processor.
var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
// Send a scopes request and check the result.
var json;
var request_json = '{"seq":0,"type":"request","command":"scopes"}';
var response_json = dcp.processDebugJSONRequest(request_json);
var response = JSON.parse(response_json);
assertEquals(scopes.length, response.body.scopes.length);
for (var i = 0; i < scopes.length; i++) {
assertEquals(i, response.body.scopes[i].index);
assertEquals(scopes[i], response.body.scopes[i].type);
if (scopes[i] == debug.ScopeType.Local ||
scopes[i] == debug.ScopeType.Script ||
scopes[i] == debug.ScopeType.Closure) {
assertTrue(response.body.scopes[i].object.ref < 0);
} else {
assertTrue(response.body.scopes[i].object.ref >= 0);
}
var found = false;
for (var j = 0; j < response.refs.length && !found; j++) {
found = response.refs[j].handle == response.body.scopes[i].object.ref;
}
assertTrue(found, "Scope object " + response.body.scopes[i].object.ref + " not found");
}
}
function CheckScopeDoesNotHave(properties, number, exec_state) {
var scope = exec_state.frame().scope(number);
for (var p of properties) {
var property_mirror = scope.scopeObject().property(p);
assertTrue(property_mirror.isUndefined(), 'property ' + p + ' found in scope');
}
}
// Check that the scope contains at least minimum_content. For functions just
// check that there is a function.
function CheckScopeContent(minimum_content, number, exec_state) {
var scope = exec_state.frame().scope(number);
var minimum_count = 0;
for (var p in minimum_content) {
var property_mirror = scope.scopeObject().property(p);
assertFalse(property_mirror.isUndefined(), 'property ' + p + ' not found in scope');
if (typeof(minimum_content[p]) === 'function') {
assertTrue(property_mirror.value().isFunction());
} else {
assertEquals(minimum_content[p], property_mirror.value().value(), 'property ' + p + ' has unexpected value');
}
minimum_count++;
}
// 'arguments' and might be exposed in the local and closure scope. Just
// ignore this.
var scope_size = scope.scopeObject().properties().length;
if (!scope.scopeObject().property('arguments').isUndefined()) {
scope_size--;
}
// Ditto for 'this'.
if (!scope.scopeObject().property('this').isUndefined()) {
scope_size--;
}
// Temporary variables introduced by the parser have not been materialized.
assertTrue(scope.scopeObject().property('').isUndefined());
if (scope_size < minimum_count) {
print('Names found in scope:');
var names = scope.scopeObject().propertyNames();
for (var i = 0; i < names.length; i++) {
print(names[i]);
}
}
assertTrue(scope_size >= minimum_count);
// Get the debug command processor.
var dcp = exec_state.debugCommandProcessor("unspecified_running_state");
// Send a scope request for information on a single scope and check the
// result.
var request_json = '{"seq":0,"type":"request","command":"scope","arguments":{"number":';
request_json += scope.scopeIndex();
request_json += '}}';
var response_json = dcp.processDebugJSONRequest(request_json);
var response = JSON.parse(response_json);
assertEquals(scope.scopeType(), response.body.type);
assertEquals(number, response.body.index);
if (scope.scopeType() == debug.ScopeType.Local ||
scope.scopeType() == debug.ScopeType.Script ||
scope.scopeType() == debug.ScopeType.Closure) {
assertTrue(response.body.object.ref < 0);
} else {
assertTrue(response.body.object.ref >= 0);
}
var found = false;
for (var i = 0; i < response.refs.length && !found; i++) {
found = response.refs[i].handle == response.body.object.ref;
}
assertTrue(found, "Scope object " + response.body.object.ref + " not found");
}
////////////////////////////////////////////////////////////////////////////////
// Actual tests.
////////////////////////////////////////////////////////////////////////////////
BeginTest();
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent(
{local_var: undefined, exported_var: undefined, imported_var: undefined},
0, exec_state);
CheckScopeDoesNotHave(
["doesnotexist", "local_let", "exported_let", "imported_let"],
0, exec_state);
};
debugger;
EndTest();
let local_let = 1;
var local_var = 2;
export let exported_let = 3;
export var exported_var = 4;
import {exported_let as imported_let} from "modules-debug-scopes2.js";
import {exported_var as imported_var} from "modules-debug-scopes2.js";
BeginTest();
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent(
{local_let: 1, local_var: 2, exported_let: 3, exported_var: 4,
imported_let: 3, imported_var: 4}, 0, exec_state);
};
debugger;
EndTest();
local_let += 10;
local_var += 10;
exported_let += 10;
exported_var += 10;
BeginTest();
listener_delegate = function(exec_state) {
CheckScopeChain([debug.ScopeType.Module,
debug.ScopeType.Script,
debug.ScopeType.Global], exec_state);
CheckScopeContent(
{local_let: 11, local_var: 12, exported_let: 13, exported_var: 14,
imported_let: 13, imported_var: 14}, 0, exec_state);
};
debugger;
EndTest();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment