diff --git a/ReactCommon/hermes/inspector/chrome/Connection.cpp b/ReactCommon/hermes/inspector/chrome/Connection.cpp index 13460fa6b42..1f8e109b657 100644 --- a/ReactCommon/hermes/inspector/chrome/Connection.cpp +++ b/ReactCommon/hermes/inspector/chrome/Connection.cpp @@ -595,6 +595,10 @@ Connection::Impl::makePropsFromScope( std::pair 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 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)); } diff --git a/ReactCommon/hermes/inspector/chrome/MessageConverters.cpp b/ReactCommon/hermes/inspector/chrome/MessageConverters.cpp index 7b1aabb788a..288cc333992 100644 --- a/ReactCommon/hermes/inspector/chrome/MessageConverters.cpp +++ b/ReactCommon/hermes/inspector/chrome/MessageConverters.cpp @@ -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(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(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)); diff --git a/ReactCommon/hermes/inspector/chrome/tests/ConnectionTests.cpp b/ReactCommon/hermes/inspector/chrome/tests/ConnectionTests.cpp index 1d9f8a89190..efdd72290ea 100644 --- a/ReactCommon/hermes/inspector/chrome/tests/ConnectionTests.cpp +++ b/ReactCommon/hermes/inspector/chrome/tests/ConnectionTests.cpp @@ -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;