mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Allow enumerating local variables
Summary: Our current lazy compilation parent scope issue caused us to only have a single lexical scope, which was taken to be the global scope. This effectively hid all local variables. Additionally, the variables were not marked as enumerable, so Chrome didn't show them. As further improvements, 'this' is now included, and we more correctly tag parent scopes as closures. Changelog: [Internal] Reviewed By: avp Differential Revision: D18671527 fbshipit-source-id: cbbf9fbd319e433b9f681bd23e4ad7b4bb4a3d74
This commit is contained in:
1 parent
a69c18cfbc
commit
33f82c569c
3 files changed
+52
-17
No files matched your search
@@ -595,6 +595,10 @@ Connection::Impl::makePropsFromScope(
|
||||
std::pair<uint32_t, uint32_t> frameAndScopeIndex,
|
||||
const std::string &objectGroup,
|
||||
const debugger::ProgramState &state) {
|
||||
// Chrome represents variables in a scope as properties on a dummy object.
|
||||
// We don't instantiate such dummy objects, we just pretended to have one.
|
||||
// Chrome has now asked for its properties, so it's time to synthesize
|
||||
// descriptions of the properties that the dummy object would have had.
|
||||
std::vector<m::runtime::PropertyDescriptor> result;
|
||||
|
||||
uint32_t frameIndex = frameAndScopeIndex.first;
|
||||
@@ -602,6 +606,19 @@ Connection::Impl::makePropsFromScope(
|
||||
debugger::LexicalInfo lexicalInfo = state.getLexicalInfo(frameIndex);
|
||||
uint32_t varCount = lexicalInfo.getVariablesCountInScope(scopeIndex);
|
||||
|
||||
// If this is the frame's local scope, include 'this'.
|
||||
if (scopeIndex == 0) {
|
||||
auto varInfo = state.getVariableInfoForThis(frameIndex);
|
||||
m::runtime::PropertyDescriptor desc;
|
||||
desc.name = varInfo.name;
|
||||
desc.value = m::runtime::makeRemoteObject(
|
||||
getRuntime(), varInfo.value, objTable_, objectGroup);
|
||||
// Chrome only shows enumerable properties.
|
||||
desc.enumerable = true;
|
||||
result.emplace_back(std::move(desc));
|
||||
}
|
||||
|
||||
// Then add each of the variables in this lexical scope.
|
||||
for (uint32_t varIndex = 0; varIndex < varCount; varIndex++) {
|
||||
debugger::VariableInfo varInfo =
|
||||
state.getVariableInfo(frameIndex, scopeIndex, varIndex);
|
||||
@@ -610,6 +627,7 @@ Connection::Impl::makePropsFromScope(
|
||||
desc.name = varInfo.name;
|
||||
desc.value = m::runtime::makeRemoteObject(
|
||||
getRuntime(), varInfo.value, objTable_, objectGroup);
|
||||
desc.enumerable = true;
|
||||
|
||||
result.emplace_back(std::move(desc));
|
||||
}
|
||||
|
||||
@@ -68,21 +68,38 @@ m::debugger::CallFrame m::debugger::makeCallFrame(
|
||||
result.location = makeLocation(callFrameInfo.location);
|
||||
|
||||
uint32_t scopeCount = lexicalInfo.getScopesCount();
|
||||
for (uint32_t scopeIndex = 0; scopeIndex < scopeCount; scopeIndex++) {
|
||||
|
||||
// First we have our local scope (unless we're in the global function)
|
||||
if (scopeCount > 1) {
|
||||
m::debugger::Scope scope;
|
||||
scope.type = "local";
|
||||
scope.object.objectId = objTable.addScope(
|
||||
std::make_pair(callFrameIndex, 0), BacktraceObjectGroup);
|
||||
scope.object.type = "object";
|
||||
scope.object.className = "Object";
|
||||
result.scopeChain.emplace_back(std::move(scope));
|
||||
}
|
||||
|
||||
// Then we have zero or more parent closure scopes
|
||||
for (uint32_t scopeIndex = 1; scopeIndex < scopeCount - 1; scopeIndex++) {
|
||||
m::debugger::Scope scope;
|
||||
|
||||
if (scopeIndex == scopeCount - 1) {
|
||||
scope.type = "global";
|
||||
scope.name = "Global Scope";
|
||||
scope.object.objectId =
|
||||
objTable.addValue(runtime.global(), BacktraceObjectGroup);
|
||||
} else {
|
||||
scope.type = "local";
|
||||
scope.name = "Scope " + folly::to<std::string>(scopeIndex);
|
||||
scope.object.objectId = objTable.addScope(
|
||||
std::make_pair(callFrameIndex, scopeIndex), BacktraceObjectGroup);
|
||||
}
|
||||
scope.type = "closure";
|
||||
// TODO: Get the parent closure's name
|
||||
scope.name = folly::to<std::string>(scopeIndex);
|
||||
scope.object.objectId = objTable.addScope(
|
||||
std::make_pair(callFrameIndex, scopeIndex), BacktraceObjectGroup);
|
||||
scope.object.type = "object";
|
||||
scope.object.className = "Object";
|
||||
result.scopeChain.emplace_back(std::move(scope));
|
||||
}
|
||||
|
||||
// Finally, we always have the global scope
|
||||
{
|
||||
m::debugger::Scope scope;
|
||||
scope.type = "global";
|
||||
scope.object.objectId =
|
||||
objTable.addValue(runtime.global(), BacktraceObjectGroup);
|
||||
scope.object.type = "object";
|
||||
scope.object.className = "Object";
|
||||
result.scopeChain.emplace_back(std::move(scope));
|
||||
|
||||
@@ -1377,7 +1377,8 @@ TEST(ConnectionTests, testGetProperties) {
|
||||
conn,
|
||||
msgId++,
|
||||
scopeObjId,
|
||||
{{"num", PropInfo("number").setValue(123)},
|
||||
{{"this", PropInfo("undefined")},
|
||||
{"num", PropInfo("number").setValue(123)},
|
||||
{"obj", PropInfo("object")},
|
||||
{"arr", PropInfo("object").setSubtype("array")},
|
||||
{"bar", PropInfo("function")}});
|
||||
@@ -1458,7 +1459,7 @@ TEST(ConnectionTests, testGetPropertiesOnlyOwnProperties) {
|
||||
conn,
|
||||
msgId++,
|
||||
scopeObject.objectId.value(),
|
||||
{{"obj", PropInfo("object")}, {"protoObject", PropInfo("object")}});
|
||||
{{"this", PropInfo("undefined")}, {"obj", PropInfo("object")}, {"protoObject", PropInfo("object")}});
|
||||
EXPECT_EQ(scopeChildren.count("obj"), 1);
|
||||
std::string objId = scopeChildren.at("obj");
|
||||
|
||||
@@ -1815,14 +1816,14 @@ TEST(ConnectionTests, testScopeVariables) {
|
||||
EXPECT_EQ(scopeChain.size(), 2);
|
||||
|
||||
// [2] inspect local scope
|
||||
EXPECT_EQ(scopeChain.at(0).name, "Scope 0");
|
||||
EXPECT_EQ(scopeChain.at(0).type, "local");
|
||||
auto localScopeObject = scopeChain.at(0).object;
|
||||
auto localScopeObjectChildren = expectProps(
|
||||
conn,
|
||||
msgId++,
|
||||
localScopeObject.objectId.value(),
|
||||
{{"localString", PropInfo("string").setValue("local-string")},
|
||||
{{"this", PropInfo("undefined")},
|
||||
{"localString", PropInfo("string").setValue("local-string")},
|
||||
{"localObject", PropInfo("object")}});
|
||||
auto localObjectId = localScopeObjectChildren.at("localObject");
|
||||
expectProps(
|
||||
@@ -1838,7 +1839,6 @@ TEST(ConnectionTests, testScopeVariables) {
|
||||
// in our test code and we can't use expectProps() method here.
|
||||
// As a workaround we create a Map of properties and check that
|
||||
// those global properties that we have defined are in the map.
|
||||
EXPECT_EQ(scopeChain.at(1).name, "Global Scope");
|
||||
EXPECT_EQ(scopeChain.at(1).type, "global");
|
||||
auto globalScopeObject = scopeChain.at(1).object;
|
||||
m::runtime::GetPropertiesRequest req;
|
||||
|
||||
Reference in new issue
Block a user