Commit 2bdd0feb authored by dgozman's avatar dgozman Committed by Commit bot

[inspector] Store interger in context embedder data instead of a string.

This is to improve the performance of common operation of
extracting contextId or contextGroupId out of Context.

BUG=none

Review-Url: https://codereview.chromium.org/2558913004
Cr-Commit-Position: refs/heads/master@{#41657}
parent bb753b6d
...@@ -8983,14 +8983,12 @@ MaybeLocal<String> debug::Script::SourceMappingURL() const { ...@@ -8983,14 +8983,12 @@ MaybeLocal<String> debug::Script::SourceMappingURL() const {
handle_scope.CloseAndEscape(i::Handle<i::String>::cast(value))); handle_scope.CloseAndEscape(i::Handle<i::String>::cast(value)));
} }
MaybeLocal<String> debug::Script::ContextData() const { MaybeLocal<Value> debug::Script::ContextData() const {
i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate(); i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
i::HandleScope handle_scope(isolate); i::HandleScope handle_scope(isolate);
i::Handle<i::Script> script = Utils::OpenHandle(this); i::Handle<i::Script> script = Utils::OpenHandle(this);
i::Handle<i::Object> value(script->context_data(), isolate); i::Handle<i::Object> value(script->context_data(), isolate);
if (!value->IsString()) return MaybeLocal<String>(); return Utils::ToLocal(handle_scope.CloseAndEscape(value));
return Utils::ToLocal(
handle_scope.CloseAndEscape(i::Handle<i::String>::cast(value)));
} }
MaybeLocal<String> debug::Script::Source() const { MaybeLocal<String> debug::Script::Source() const {
......
...@@ -154,7 +154,7 @@ class Script { ...@@ -154,7 +154,7 @@ class Script {
MaybeLocal<String> Name() const; MaybeLocal<String> Name() const;
MaybeLocal<String> SourceURL() const; MaybeLocal<String> SourceURL() const;
MaybeLocal<String> SourceMappingURL() const; MaybeLocal<String> SourceMappingURL() const;
MaybeLocal<String> ContextData() const; MaybeLocal<Value> ContextData() const;
MaybeLocal<String> Source() const; MaybeLocal<String> Source() const;
bool IsWasm() const; bool IsWasm() const;
bool GetPossibleBreakpoints(const debug::Location& start, bool GetPossibleBreakpoints(const debug::Location& start,
......
...@@ -143,20 +143,6 @@ DebuggerScript.getCollectionEntries = function(object) ...@@ -143,20 +143,6 @@ DebuggerScript.getCollectionEntries = function(object)
} }
} }
/**
* @param {string|undefined} contextData
* @return {number}
*/
DebuggerScript._executionContextId = function(contextData)
{
if (!contextData)
return 0;
var match = contextData.match(/^[^,]*,([^,]*),.*$/);
if (!match)
return 0;
return parseInt(match[1], 10) || 0;
}
/** /**
* @param {!ExecutionState} execState * @param {!ExecutionState} execState
* @param {!BreakpointInfo} info * @param {!BreakpointInfo} info
...@@ -485,12 +471,9 @@ DebuggerScript._frameMirrorToJSCallFrame = function(frameMirror) ...@@ -485,12 +471,9 @@ DebuggerScript._frameMirrorToJSCallFrame = function(frameMirror)
function contextId() function contextId()
{ {
var mirror = ensureFuncMirror(); var mirror = ensureFuncMirror();
// Old V8 do not have context() function on these objects
if (!mirror.context)
return DebuggerScript._executionContextId(mirror.script().value().context_data);
var context = mirror.context(); var context = mirror.context();
if (context) if (context && context.data())
return DebuggerScript._executionContextId(context.data()); return Number(context.data());
return 0; return 0;
} }
......
...@@ -32,6 +32,7 @@ var JavaScriptCallFrameDetails; ...@@ -32,6 +32,7 @@ var JavaScriptCallFrameDetails;
sourceID: function():(number), sourceID: function():(number),
line: function():number, line: function():number,
column: function():number, column: function():number,
contextId: function():number,
thisObject: !Object, thisObject: !Object,
evaluate: function(string):*, evaluate: function(string):*,
restart: function():undefined, restart: function():undefined,
......
...@@ -41,10 +41,12 @@ InspectedContext::InspectedContext(V8InspectorImpl* inspector, ...@@ -41,10 +41,12 @@ InspectedContext::InspectedContext(V8InspectorImpl* inspector,
m_humanReadableName(toString16(info.humanReadableName)), m_humanReadableName(toString16(info.humanReadableName)),
m_auxData(toString16(info.auxData)), m_auxData(toString16(info.auxData)),
m_reported(false) { m_reported(false) {
v8::Isolate* isolate = m_inspector->isolate();
info.context->SetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex),
v8::Int32::New(isolate, contextId));
m_context.SetWeak(&m_context, &clearContext, m_context.SetWeak(&m_context, &clearContext,
v8::WeakCallbackType::kParameter); v8::WeakCallbackType::kParameter);
v8::Isolate* isolate = m_inspector->isolate();
v8::Local<v8::Object> global = info.context->Global(); v8::Local<v8::Object> global = info.context->Global();
v8::Local<v8::Object> console = v8::Local<v8::Object> console =
V8Console::createConsole(this, info.hasMemoryOnConsole); V8Console::createConsole(this, info.hasMemoryOnConsole);
...@@ -65,6 +67,14 @@ InspectedContext::~InspectedContext() { ...@@ -65,6 +67,14 @@ InspectedContext::~InspectedContext() {
} }
} }
// static
int InspectedContext::contextId(v8::Local<v8::Context> context) {
v8::Local<v8::Value> data =
context->GetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex));
if (data.IsEmpty() || !data->IsInt32()) return 0;
return static_cast<int>(data.As<v8::Int32>()->Value());
}
v8::Local<v8::Context> InspectedContext::context() const { v8::Local<v8::Context> InspectedContext::context() const {
return m_context.Get(isolate()); return m_context.Get(isolate());
} }
......
...@@ -21,6 +21,8 @@ class InspectedContext { ...@@ -21,6 +21,8 @@ class InspectedContext {
public: public:
~InspectedContext(); ~InspectedContext();
static int contextId(v8::Local<v8::Context>);
v8::Local<v8::Context> context() const; v8::Local<v8::Context> context() const;
int contextId() const { return m_contextId; } int contextId() const { return m_contextId; }
int contextGroupId() const { return m_contextGroupId; } int contextGroupId() const { return m_contextGroupId; }
......
...@@ -1050,10 +1050,17 @@ void V8DebuggerAgentImpl::didParseSource( ...@@ -1050,10 +1050,17 @@ void V8DebuggerAgentImpl::didParseSource(
if (!success) if (!success)
script->setSourceMappingURL(findSourceMapURL(scriptSource, false)); script->setSourceMappingURL(findSourceMapURL(scriptSource, false));
int contextId = script->executionContextId();
int contextGroupId = m_inspector->contextGroupId(contextId);
InspectedContext* inspected =
m_inspector->getContext(contextGroupId, contextId);
std::unique_ptr<protocol::DictionaryValue> executionContextAuxData; std::unique_ptr<protocol::DictionaryValue> executionContextAuxData;
if (!script->executionContextAuxData().isEmpty()) if (inspected) {
// Script reused between different groups/sessions can have a stale
// execution context id.
executionContextAuxData = protocol::DictionaryValue::cast( executionContextAuxData = protocol::DictionaryValue::cast(
protocol::StringUtil::parseJSON(script->executionContextAuxData())); protocol::StringUtil::parseJSON(inspected->auxData()));
}
bool isLiveEdit = script->isLiveEdit(); bool isLiveEdit = script->isLiveEdit();
bool hasSourceURL = script->hasSourceURL(); bool hasSourceURL = script->hasSourceURL();
String16 scriptId = script->scriptId(); String16 scriptId = script->scriptId();
...@@ -1073,17 +1080,15 @@ void V8DebuggerAgentImpl::didParseSource( ...@@ -1073,17 +1080,15 @@ void V8DebuggerAgentImpl::didParseSource(
if (success) if (success)
m_frontend.scriptParsed( m_frontend.scriptParsed(
scriptId, scriptURL, scriptRef->startLine(), scriptRef->startColumn(), scriptId, scriptURL, scriptRef->startLine(), scriptRef->startColumn(),
scriptRef->endLine(), scriptRef->endColumn(), scriptRef->endLine(), scriptRef->endColumn(), contextId,
scriptRef->executionContextId(), scriptRef->hash(m_isolate), scriptRef->hash(m_isolate), std::move(executionContextAuxDataParam),
std::move(executionContextAuxDataParam), isLiveEditParam, isLiveEditParam, std::move(sourceMapURLParam), hasSourceURLParam);
std::move(sourceMapURLParam), hasSourceURLParam);
else else
m_frontend.scriptFailedToParse( m_frontend.scriptFailedToParse(
scriptId, scriptURL, scriptRef->startLine(), scriptRef->startColumn(), scriptId, scriptURL, scriptRef->startLine(), scriptRef->startColumn(),
scriptRef->endLine(), scriptRef->endColumn(), scriptRef->endLine(), scriptRef->endColumn(), contextId,
scriptRef->executionContextId(), scriptRef->hash(m_isolate), scriptRef->hash(m_isolate), std::move(executionContextAuxDataParam),
std::move(executionContextAuxDataParam), std::move(sourceMapURLParam), std::move(sourceMapURLParam), hasSourceURLParam);
hasSourceURLParam);
if (scriptURL.isEmpty() || !success) return; if (scriptURL.isEmpty() || !success) return;
...@@ -1150,7 +1155,7 @@ V8DebuggerAgentImpl::SkipPauseRequest V8DebuggerAgentImpl::didPause( ...@@ -1150,7 +1155,7 @@ V8DebuggerAgentImpl::SkipPauseRequest V8DebuggerAgentImpl::didPause(
if (!exception.IsEmpty()) { if (!exception.IsEmpty()) {
InjectedScript* injectedScript = nullptr; InjectedScript* injectedScript = nullptr;
m_session->findInjectedScript(V8Debugger::contextId(context), m_session->findInjectedScript(InspectedContext::contextId(context),
injectedScript); injectedScript);
if (injectedScript) { if (injectedScript) {
m_breakReason = m_breakReason =
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "src/inspector/v8-debugger-script.h" #include "src/inspector/v8-debugger-script.h"
#include "src/inspector/inspected-context.h"
#include "src/inspector/string-util.h" #include "src/inspector/string-util.h"
namespace v8_inspector { namespace v8_inspector {
...@@ -98,20 +99,10 @@ class ActualScript : public V8DebuggerScript { ...@@ -98,20 +99,10 @@ class ActualScript : public V8DebuggerScript {
m_endColumn = m_startColumn; m_endColumn = m_startColumn;
} }
if (script->ContextData().ToLocal(&tmp)) { v8::Local<v8::Value> contextData;
String16 contextData = toProtocolString(tmp); if (script->ContextData().ToLocal(&contextData) && contextData->IsInt32()) {
size_t firstComma = contextData.find(",", 0); m_executionContextId =
size_t secondComma = firstComma != String16::kNotFound static_cast<int>(contextData.As<v8::Int32>()->Value());
? contextData.find(",", firstComma + 1)
: String16::kNotFound;
if (secondComma != String16::kNotFound) {
String16 executionContextId =
contextData.substring(firstComma + 1, secondComma - firstComma - 1);
bool isOk = false;
m_executionContextId = executionContextId.toInteger(&isOk);
if (!isOk) m_executionContextId = 0;
m_executionContextAuxData = contextData.substring(secondComma + 1);
}
} }
if (script->Source().ToLocal(&tmp)) { if (script->Source().ToLocal(&tmp)) {
...@@ -129,10 +120,6 @@ class ActualScript : public V8DebuggerScript { ...@@ -129,10 +120,6 @@ class ActualScript : public V8DebuggerScript {
bool isLiveEdit() const override { return m_isLiveEdit; } bool isLiveEdit() const override { return m_isLiveEdit; }
const String16& executionContextAuxData() const override {
return m_executionContextAuxData;
}
const String16& sourceMappingURL() const override { const String16& sourceMappingURL() const override {
return m_sourceMappingURL; return m_sourceMappingURL;
} }
...@@ -171,7 +158,6 @@ class ActualScript : public V8DebuggerScript { ...@@ -171,7 +158,6 @@ class ActualScript : public V8DebuggerScript {
String16 m_sourceMappingURL; String16 m_sourceMappingURL;
v8::Global<v8::String> m_sourceObj; v8::Global<v8::String> m_sourceObj;
String16 m_executionContextAuxData;
bool m_isLiveEdit = false; bool m_isLiveEdit = false;
v8::Global<v8::debug::Script> m_script; v8::Global<v8::debug::Script> m_script;
}; };
...@@ -199,9 +185,6 @@ class WasmVirtualScript : public V8DebuggerScript { ...@@ -199,9 +185,6 @@ class WasmVirtualScript : public V8DebuggerScript {
} }
const String16& sourceMappingURL() const override { return emptyString(); } const String16& sourceMappingURL() const override { return emptyString(); }
const String16& executionContextAuxData() const override {
return emptyString();
}
bool isLiveEdit() const override { return false; } bool isLiveEdit() const override { return false; }
void setSourceMappingURL(const String16&) override {} void setSourceMappingURL(const String16&) override {}
......
...@@ -62,7 +62,6 @@ class V8DebuggerScript { ...@@ -62,7 +62,6 @@ class V8DebuggerScript {
int endLine() const { return m_endLine; } int endLine() const { return m_endLine; }
int endColumn() const { return m_endColumn; } int endColumn() const { return m_endColumn; }
int executionContextId() const { return m_executionContextId; } int executionContextId() const { return m_executionContextId; }
virtual const String16& executionContextAuxData() const = 0;
virtual bool isLiveEdit() const = 0; virtual bool isLiveEdit() const = 0;
void setSourceURL(const String16&); void setSourceURL(const String16&);
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "src/inspector/v8-debugger.h" #include "src/inspector/v8-debugger.h"
#include "src/inspector/debugger-script.h" #include "src/inspector/debugger-script.h"
#include "src/inspector/inspected-context.h"
#include "src/inspector/protocol/Protocol.h" #include "src/inspector/protocol/Protocol.h"
#include "src/inspector/script-breakpoint.h" #include "src/inspector/script-breakpoint.h"
#include "src/inspector/string-util.h" #include "src/inspector/string-util.h"
...@@ -50,7 +51,6 @@ v8::MaybeLocal<v8::Value> V8Debugger::callDebuggerMethod( ...@@ -50,7 +51,6 @@ v8::MaybeLocal<v8::Value> V8Debugger::callDebuggerMethod(
V8Debugger::V8Debugger(v8::Isolate* isolate, V8InspectorImpl* inspector) V8Debugger::V8Debugger(v8::Isolate* isolate, V8InspectorImpl* inspector)
: m_isolate(isolate), : m_isolate(isolate),
m_inspector(inspector), m_inspector(inspector),
m_lastContextId(0),
m_enableCount(0), m_enableCount(0),
m_breakpointsActivated(true), m_breakpointsActivated(true),
m_runningNestedMessageLoop(false), m_runningNestedMessageLoop(false),
...@@ -86,47 +86,20 @@ void V8Debugger::disable() { ...@@ -86,47 +86,20 @@ void V8Debugger::disable() {
bool V8Debugger::enabled() const { return !m_debuggerScript.IsEmpty(); } bool V8Debugger::enabled() const { return !m_debuggerScript.IsEmpty(); }
// static
int V8Debugger::contextId(v8::Local<v8::Context> context) {
v8::Local<v8::Value> data =
context->GetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex));
if (data.IsEmpty() || !data->IsString()) return 0;
String16 dataString = toProtocolString(data.As<v8::String>());
if (dataString.isEmpty()) return 0;
size_t commaPos = dataString.find(",");
if (commaPos == String16::kNotFound) return 0;
size_t commaPos2 = dataString.find(",", commaPos + 1);
if (commaPos2 == String16::kNotFound) return 0;
return dataString.substring(commaPos + 1, commaPos2 - commaPos - 1)
.toInteger();
}
// static
int V8Debugger::getGroupId(v8::Local<v8::Context> context) {
v8::Local<v8::Value> data =
context->GetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex));
if (data.IsEmpty() || !data->IsString()) return 0;
String16 dataString = toProtocolString(data.As<v8::String>());
if (dataString.isEmpty()) return 0;
size_t commaPos = dataString.find(",");
if (commaPos == String16::kNotFound) return 0;
return dataString.substring(0, commaPos).toInteger();
}
void V8Debugger::getCompiledScripts( void V8Debugger::getCompiledScripts(
int contextGroupId, int contextGroupId,
std::vector<std::unique_ptr<V8DebuggerScript>>& result) { std::vector<std::unique_ptr<V8DebuggerScript>>& result) {
v8::HandleScope scope(m_isolate); v8::HandleScope scope(m_isolate);
v8::PersistentValueVector<v8::debug::Script> scripts(m_isolate); v8::PersistentValueVector<v8::debug::Script> scripts(m_isolate);
v8::debug::GetLoadedScripts(m_isolate, scripts); v8::debug::GetLoadedScripts(m_isolate, scripts);
String16 contextPrefix = String16::fromInteger(contextGroupId) + ",";
for (size_t i = 0; i < scripts.Size(); ++i) { for (size_t i = 0; i < scripts.Size(); ++i) {
v8::Local<v8::debug::Script> script = scripts.Get(i); v8::Local<v8::debug::Script> script = scripts.Get(i);
if (!script->WasCompiled()) continue; if (!script->WasCompiled()) continue;
v8::Local<v8::String> v8ContextData; v8::Local<v8::Value> contextData;
if (!script->ContextData().ToLocal(&v8ContextData)) continue; if (!script->ContextData().ToLocal(&contextData) || !contextData->IsInt32())
String16 contextData = toProtocolString(v8ContextData); continue;
if (contextData.find(contextPrefix) != 0) continue; int contextId = static_cast<int>(contextData.As<v8::Int32>()->Value());
if (m_inspector->contextGroupId(contextId) != contextGroupId) continue;
result.push_back(V8DebuggerScript::Create(m_isolate, script, false)); result.push_back(V8DebuggerScript::Create(m_isolate, script, false));
} }
} }
...@@ -483,8 +456,8 @@ void V8Debugger::handleProgramBreak(v8::Local<v8::Context> pausedContext, ...@@ -483,8 +456,8 @@ void V8Debugger::handleProgramBreak(v8::Local<v8::Context> pausedContext,
// Don't allow nested breaks. // Don't allow nested breaks.
if (m_runningNestedMessageLoop) return; if (m_runningNestedMessageLoop) return;
V8DebuggerAgentImpl* agent = V8DebuggerAgentImpl* agent = m_inspector->enabledDebuggerAgentForGroup(
m_inspector->enabledDebuggerAgentForGroup(getGroupId(pausedContext)); m_inspector->contextGroupId(pausedContext));
if (!agent) return; if (!agent) return;
std::vector<String16> breakpointIds; std::vector<String16> breakpointIds;
...@@ -505,12 +478,12 @@ void V8Debugger::handleProgramBreak(v8::Local<v8::Context> pausedContext, ...@@ -505,12 +478,12 @@ void V8Debugger::handleProgramBreak(v8::Local<v8::Context> pausedContext,
pausedContext, exception, breakpointIds, isPromiseRejection, isUncaught); pausedContext, exception, breakpointIds, isPromiseRejection, isUncaught);
if (result == V8DebuggerAgentImpl::RequestNoSkip) { if (result == V8DebuggerAgentImpl::RequestNoSkip) {
m_runningNestedMessageLoop = true; m_runningNestedMessageLoop = true;
int groupId = getGroupId(pausedContext); int groupId = m_inspector->contextGroupId(pausedContext);
DCHECK(groupId); DCHECK(groupId);
m_inspector->client()->runMessageLoopOnPause(groupId); m_inspector->client()->runMessageLoopOnPause(groupId);
// The agent may have been removed in the nested loop. // The agent may have been removed in the nested loop.
agent = agent = m_inspector->enabledDebuggerAgentForGroup(
m_inspector->enabledDebuggerAgentForGroup(getGroupId(pausedContext)); m_inspector->contextGroupId(pausedContext));
if (agent) agent->didContinue(); if (agent) agent->didContinue();
m_runningNestedMessageLoop = false; m_runningNestedMessageLoop = false;
} }
...@@ -566,8 +539,8 @@ void V8Debugger::handleV8DebugEvent( ...@@ -566,8 +539,8 @@ void V8Debugger::handleV8DebugEvent(
return; return;
} }
V8DebuggerAgentImpl* agent = V8DebuggerAgentImpl* agent = m_inspector->enabledDebuggerAgentForGroup(
m_inspector->enabledDebuggerAgentForGroup(getGroupId(eventContext)); m_inspector->contextGroupId(eventContext));
if (!agent) return; if (!agent) return;
v8::HandleScope scope(m_isolate); v8::HandleScope scope(m_isolate);
...@@ -884,23 +857,13 @@ bool V8Debugger::isPaused() { return !m_pausedContext.IsEmpty(); } ...@@ -884,23 +857,13 @@ bool V8Debugger::isPaused() { return !m_pausedContext.IsEmpty(); }
std::unique_ptr<V8StackTraceImpl> V8Debugger::createStackTrace( std::unique_ptr<V8StackTraceImpl> V8Debugger::createStackTrace(
v8::Local<v8::StackTrace> stackTrace) { v8::Local<v8::StackTrace> stackTrace) {
int contextGroupId = int contextGroupId =
m_isolate->InContext() ? getGroupId(m_isolate->GetCurrentContext()) : 0; m_isolate->InContext()
? m_inspector->contextGroupId(m_isolate->GetCurrentContext())
: 0;
return V8StackTraceImpl::create(this, contextGroupId, stackTrace, return V8StackTraceImpl::create(this, contextGroupId, stackTrace,
V8StackTraceImpl::maxCallStackSizeToCapture); V8StackTraceImpl::maxCallStackSizeToCapture);
} }
int V8Debugger::markContext(const V8ContextInfo& info) {
DCHECK(info.context->GetIsolate() == m_isolate);
int contextId = ++m_lastContextId;
String16 debugData = String16::fromInteger(info.contextGroupId) + "," +
String16::fromInteger(contextId) + "," +
toString16(info.auxData);
v8::Context::Scope contextScope(info.context);
info.context->SetEmbedderData(static_cast<int>(v8::Context::kDebugIdIndex),
toV8String(m_isolate, debugData));
return contextId;
}
void V8Debugger::setAsyncCallStackDepth(V8DebuggerAgentImpl* agent, int depth) { void V8Debugger::setAsyncCallStackDepth(V8DebuggerAgentImpl* agent, int depth) {
if (depth <= 0) if (depth <= 0)
m_maxAsyncCallStackDepthMap.erase(agent); m_maxAsyncCallStackDepthMap.erase(agent);
...@@ -929,7 +892,9 @@ void V8Debugger::asyncTaskScheduled(const String16& taskName, void* task, ...@@ -929,7 +892,9 @@ void V8Debugger::asyncTaskScheduled(const String16& taskName, void* task,
if (!m_maxAsyncCallStackDepth) return; if (!m_maxAsyncCallStackDepth) return;
v8::HandleScope scope(m_isolate); v8::HandleScope scope(m_isolate);
int contextGroupId = int contextGroupId =
m_isolate->InContext() ? getGroupId(m_isolate->GetCurrentContext()) : 0; m_isolate->InContext()
? m_inspector->contextGroupId(m_isolate->GetCurrentContext())
: 0;
std::unique_ptr<V8StackTraceImpl> chain = V8StackTraceImpl::capture( std::unique_ptr<V8StackTraceImpl> chain = V8StackTraceImpl::capture(
this, contextGroupId, V8StackTraceImpl::maxCallStackSizeToCapture, this, contextGroupId, V8StackTraceImpl::maxCallStackSizeToCapture,
taskName); taskName);
...@@ -996,7 +961,8 @@ std::unique_ptr<V8StackTraceImpl> V8Debugger::captureStackTrace( ...@@ -996,7 +961,8 @@ std::unique_ptr<V8StackTraceImpl> V8Debugger::captureStackTrace(
if (!m_isolate->InContext()) return nullptr; if (!m_isolate->InContext()) return nullptr;
v8::HandleScope handles(m_isolate); v8::HandleScope handles(m_isolate);
int contextGroupId = getGroupId(m_isolate->GetCurrentContext()); int contextGroupId =
m_inspector->contextGroupId(m_isolate->GetCurrentContext());
if (!contextGroupId) return nullptr; if (!contextGroupId) return nullptr;
size_t stackSize = size_t stackSize =
......
...@@ -31,10 +31,6 @@ class V8Debugger { ...@@ -31,10 +31,6 @@ class V8Debugger {
V8Debugger(v8::Isolate*, V8InspectorImpl*); V8Debugger(v8::Isolate*, V8InspectorImpl*);
~V8Debugger(); ~V8Debugger();
static int contextId(v8::Local<v8::Context>);
static int getGroupId(v8::Local<v8::Context>);
int markContext(const V8ContextInfo&);
bool enabled() const; bool enabled() const;
String16 setBreakpoint(const ScriptBreakpoint&, int* actualLineNumber, String16 setBreakpoint(const ScriptBreakpoint&, int* actualLineNumber,
...@@ -142,7 +138,6 @@ class V8Debugger { ...@@ -142,7 +138,6 @@ class V8Debugger {
v8::Isolate* m_isolate; v8::Isolate* m_isolate;
V8InspectorImpl* m_inspector; V8InspectorImpl* m_inspector;
int m_lastContextId;
int m_enableCount; int m_enableCount;
bool m_breakpointsActivated; bool m_breakpointsActivated;
v8::Global<v8::Object> m_debuggerScript; v8::Global<v8::Object> m_debuggerScript;
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#include "src/inspector/v8-function-call.h" #include "src/inspector/v8-function-call.h"
#include "src/inspector/inspected-context.h"
#include "src/inspector/string-util.h" #include "src/inspector/string-util.h"
#include "src/inspector/v8-debugger.h" #include "src/inspector/v8-debugger.h"
#include "src/inspector/v8-inspector-impl.h" #include "src/inspector/v8-inspector-impl.h"
...@@ -89,7 +90,7 @@ v8::Local<v8::Value> V8FunctionCall::callWithoutExceptionHandling() { ...@@ -89,7 +90,7 @@ v8::Local<v8::Value> V8FunctionCall::callWithoutExceptionHandling() {
DCHECK(!info[i].IsEmpty()); DCHECK(!info[i].IsEmpty());
} }
int contextGroupId = V8Debugger::getGroupId(m_context); int contextGroupId = m_inspector->contextGroupId(m_context);
if (contextGroupId) { if (contextGroupId) {
m_inspector->client()->muteMetrics(contextGroupId); m_inspector->client()->muteMetrics(contextGroupId);
m_inspector->muteExceptions(contextGroupId); m_inspector->muteExceptions(contextGroupId);
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#include "src/inspector/v8-heap-profiler-agent-impl.h" #include "src/inspector/v8-heap-profiler-agent-impl.h"
#include "src/inspector/injected-script.h" #include "src/inspector/injected-script.h"
#include "src/inspector/inspected-context.h"
#include "src/inspector/protocol/Protocol.h" #include "src/inspector/protocol/Protocol.h"
#include "src/inspector/string-util.h" #include "src/inspector/string-util.h"
#include "src/inspector/v8-debugger.h" #include "src/inspector/v8-debugger.h"
...@@ -55,7 +56,7 @@ class GlobalObjectNameResolver final ...@@ -55,7 +56,7 @@ class GlobalObjectNameResolver final
const char* GetName(v8::Local<v8::Object> object) override { const char* GetName(v8::Local<v8::Object> object) override {
InspectedContext* context = m_session->inspector()->getContext( InspectedContext* context = m_session->inspector()->getContext(
m_session->contextGroupId(), m_session->contextGroupId(),
V8Debugger::contextId(object->CreationContext())); InspectedContext::contextId(object->CreationContext()));
if (!context) return ""; if (!context) return "";
String16 name = context->origin(); String16 name = context->origin();
size_t length = name.length(); size_t length = name.length();
......
...@@ -54,10 +54,21 @@ V8InspectorImpl::V8InspectorImpl(v8::Isolate* isolate, ...@@ -54,10 +54,21 @@ V8InspectorImpl::V8InspectorImpl(v8::Isolate* isolate,
m_client(client), m_client(client),
m_debugger(new V8Debugger(isolate, this)), m_debugger(new V8Debugger(isolate, this)),
m_capturingStackTracesCount(0), m_capturingStackTracesCount(0),
m_lastExceptionId(0) {} m_lastExceptionId(0),
m_lastContextId(0) {}
V8InspectorImpl::~V8InspectorImpl() {} V8InspectorImpl::~V8InspectorImpl() {}
int V8InspectorImpl::contextGroupId(v8::Local<v8::Context> context) {
return contextGroupId(InspectedContext::contextId(context));
}
int V8InspectorImpl::contextGroupId(int contextId) {
protocol::HashMap<int, int>::iterator it =
m_contextIdToGroupIdMap.find(contextId);
return it != m_contextIdToGroupIdMap.end() ? it->second : 0;
}
V8DebuggerAgentImpl* V8InspectorImpl::enabledDebuggerAgentForGroup( V8DebuggerAgentImpl* V8InspectorImpl::enabledDebuggerAgentForGroup(
int contextGroupId) { int contextGroupId) {
V8InspectorSessionImpl* session = sessionForContextGroup(contextGroupId); V8InspectorSessionImpl* session = sessionForContextGroup(contextGroupId);
...@@ -83,7 +94,7 @@ v8::MaybeLocal<v8::Value> V8InspectorImpl::runCompiledScript( ...@@ -83,7 +94,7 @@ v8::MaybeLocal<v8::Value> V8InspectorImpl::runCompiledScript(
v8::Local<v8::Context> context, v8::Local<v8::Script> script) { v8::Local<v8::Context> context, v8::Local<v8::Script> script) {
v8::MicrotasksScope microtasksScope(m_isolate, v8::MicrotasksScope microtasksScope(m_isolate,
v8::MicrotasksScope::kRunMicrotasks); v8::MicrotasksScope::kRunMicrotasks);
int groupId = V8Debugger::getGroupId(context); int groupId = contextGroupId(context);
if (V8DebuggerAgentImpl* agent = enabledDebuggerAgentForGroup(groupId)) if (V8DebuggerAgentImpl* agent = enabledDebuggerAgentForGroup(groupId))
agent->willExecuteScript(script->GetUnboundScript()->GetId()); agent->willExecuteScript(script->GetUnboundScript()->GetId());
v8::MaybeLocal<v8::Value> result = script->Run(context); v8::MaybeLocal<v8::Value> result = script->Run(context);
...@@ -113,7 +124,7 @@ v8::MaybeLocal<v8::Value> V8InspectorImpl::callFunction( ...@@ -113,7 +124,7 @@ v8::MaybeLocal<v8::Value> V8InspectorImpl::callFunction(
v8::Local<v8::Value> receiver, int argc, v8::Local<v8::Value> info[], v8::Local<v8::Value> receiver, int argc, v8::Local<v8::Value> info[],
v8::MicrotasksScope::Type runMicrotasks) { v8::MicrotasksScope::Type runMicrotasks) {
v8::MicrotasksScope microtasksScope(m_isolate, runMicrotasks); v8::MicrotasksScope microtasksScope(m_isolate, runMicrotasks);
int groupId = V8Debugger::getGroupId(context); int groupId = contextGroupId(context);
if (V8DebuggerAgentImpl* agent = enabledDebuggerAgentForGroup(groupId)) if (V8DebuggerAgentImpl* agent = enabledDebuggerAgentForGroup(groupId))
agent->willExecuteScript(function->ScriptId()); agent->willExecuteScript(function->ScriptId());
v8::MaybeLocal<v8::Value> result = v8::MaybeLocal<v8::Value> result =
...@@ -226,7 +237,9 @@ InspectedContext* V8InspectorImpl::getContext(int groupId, ...@@ -226,7 +237,9 @@ InspectedContext* V8InspectorImpl::getContext(int groupId,
} }
void V8InspectorImpl::contextCreated(const V8ContextInfo& info) { void V8InspectorImpl::contextCreated(const V8ContextInfo& info) {
int contextId = m_debugger->markContext(info); int contextId = ++m_lastContextId;
InspectedContext* context = new InspectedContext(this, info, contextId);
m_contextIdToGroupIdMap[contextId] = info.contextGroupId;
ContextsByGroupMap::iterator contextIt = m_contexts.find(info.contextGroupId); ContextsByGroupMap::iterator contextIt = m_contexts.find(info.contextGroupId);
if (contextIt == m_contexts.end()) if (contextIt == m_contexts.end())
...@@ -235,11 +248,9 @@ void V8InspectorImpl::contextCreated(const V8ContextInfo& info) { ...@@ -235,11 +248,9 @@ void V8InspectorImpl::contextCreated(const V8ContextInfo& info) {
info.contextGroupId, info.contextGroupId,
std::unique_ptr<ContextByIdMap>(new ContextByIdMap()))) std::unique_ptr<ContextByIdMap>(new ContextByIdMap())))
.first; .first;
const auto& contextById = contextIt->second; const auto& contextById = contextIt->second;
DCHECK(contextById->find(contextId) == contextById->cend()); DCHECK(contextById->find(contextId) == contextById->cend());
InspectedContext* context = new InspectedContext(this, info, contextId);
(*contextById)[contextId].reset(context); (*contextById)[contextId].reset(context);
SessionMap::iterator sessionIt = m_sessions.find(info.contextGroupId); SessionMap::iterator sessionIt = m_sessions.find(info.contextGroupId);
if (sessionIt != m_sessions.end()) if (sessionIt != m_sessions.end())
...@@ -247,22 +258,22 @@ void V8InspectorImpl::contextCreated(const V8ContextInfo& info) { ...@@ -247,22 +258,22 @@ void V8InspectorImpl::contextCreated(const V8ContextInfo& info) {
} }
void V8InspectorImpl::contextDestroyed(v8::Local<v8::Context> context) { void V8InspectorImpl::contextDestroyed(v8::Local<v8::Context> context) {
int contextId = V8Debugger::contextId(context); int contextId = InspectedContext::contextId(context);
int contextGroupId = V8Debugger::getGroupId(context); int groupId = contextGroupId(context);
m_contextIdToGroupIdMap.erase(contextId);
ConsoleStorageMap::iterator storageIt = ConsoleStorageMap::iterator storageIt = m_consoleStorageMap.find(groupId);
m_consoleStorageMap.find(contextGroupId);
if (storageIt != m_consoleStorageMap.end()) if (storageIt != m_consoleStorageMap.end())
storageIt->second->contextDestroyed(contextId); storageIt->second->contextDestroyed(contextId);
InspectedContext* inspectedContext = getContext(contextGroupId, contextId); InspectedContext* inspectedContext = getContext(groupId, contextId);
if (!inspectedContext) return; if (!inspectedContext) return;
SessionMap::iterator iter = m_sessions.find(contextGroupId); SessionMap::iterator iter = m_sessions.find(groupId);
if (iter != m_sessions.end()) if (iter != m_sessions.end())
iter->second->runtimeAgent()->reportExecutionContextDestroyed( iter->second->runtimeAgent()->reportExecutionContextDestroyed(
inspectedContext); inspectedContext);
discardInspectedContext(contextGroupId, contextId); discardInspectedContext(groupId, contextId);
} }
void V8InspectorImpl::resetContextGroup(int contextGroupId) { void V8InspectorImpl::resetContextGroup(int contextGroupId) {
...@@ -277,14 +288,16 @@ void V8InspectorImpl::resetContextGroup(int contextGroupId) { ...@@ -277,14 +288,16 @@ void V8InspectorImpl::resetContextGroup(int contextGroupId) {
void V8InspectorImpl::willExecuteScript(v8::Local<v8::Context> context, void V8InspectorImpl::willExecuteScript(v8::Local<v8::Context> context,
int scriptId) { int scriptId) {
if (V8DebuggerAgentImpl* agent = if (V8DebuggerAgentImpl* agent =
enabledDebuggerAgentForGroup(V8Debugger::getGroupId(context))) enabledDebuggerAgentForGroup(contextGroupId(context))) {
agent->willExecuteScript(scriptId); agent->willExecuteScript(scriptId);
}
} }
void V8InspectorImpl::didExecuteScript(v8::Local<v8::Context> context) { void V8InspectorImpl::didExecuteScript(v8::Local<v8::Context> context) {
if (V8DebuggerAgentImpl* agent = if (V8DebuggerAgentImpl* agent =
enabledDebuggerAgentForGroup(V8Debugger::getGroupId(context))) enabledDebuggerAgentForGroup(contextGroupId(context))) {
agent->didExecuteScript(); agent->didExecuteScript();
}
} }
void V8InspectorImpl::idleStarted() { void V8InspectorImpl::idleStarted() {
...@@ -304,8 +317,8 @@ unsigned V8InspectorImpl::exceptionThrown( ...@@ -304,8 +317,8 @@ unsigned V8InspectorImpl::exceptionThrown(
v8::Local<v8::Value> exception, const StringView& detailedMessage, v8::Local<v8::Value> exception, const StringView& detailedMessage,
const StringView& url, unsigned lineNumber, unsigned columnNumber, const StringView& url, unsigned lineNumber, unsigned columnNumber,
std::unique_ptr<V8StackTrace> stackTrace, int scriptId) { std::unique_ptr<V8StackTrace> stackTrace, int scriptId) {
int contextGroupId = V8Debugger::getGroupId(context); int groupId = contextGroupId(context);
if (!contextGroupId || m_muteExceptionsMap[contextGroupId]) return 0; if (!groupId || m_muteExceptionsMap[groupId]) return 0;
std::unique_ptr<V8StackTraceImpl> stackTraceImpl( std::unique_ptr<V8StackTraceImpl> stackTraceImpl(
static_cast<V8StackTraceImpl*>(stackTrace.release())); static_cast<V8StackTraceImpl*>(stackTrace.release()));
unsigned exceptionId = nextExceptionId(); unsigned exceptionId = nextExceptionId();
...@@ -314,23 +327,21 @@ unsigned V8InspectorImpl::exceptionThrown( ...@@ -314,23 +327,21 @@ unsigned V8InspectorImpl::exceptionThrown(
m_client->currentTimeMS(), toString16(detailedMessage), m_client->currentTimeMS(), toString16(detailedMessage),
toString16(url), lineNumber, columnNumber, std::move(stackTraceImpl), toString16(url), lineNumber, columnNumber, std::move(stackTraceImpl),
scriptId, m_isolate, toString16(message), scriptId, m_isolate, toString16(message),
V8Debugger::contextId(context), exception, exceptionId); InspectedContext::contextId(context), exception, exceptionId);
ensureConsoleMessageStorage(contextGroupId) ensureConsoleMessageStorage(groupId)->addMessage(std::move(consoleMessage));
->addMessage(std::move(consoleMessage));
return exceptionId; return exceptionId;
} }
void V8InspectorImpl::exceptionRevoked(v8::Local<v8::Context> context, void V8InspectorImpl::exceptionRevoked(v8::Local<v8::Context> context,
unsigned exceptionId, unsigned exceptionId,
const StringView& message) { const StringView& message) {
int contextGroupId = V8Debugger::getGroupId(context); int groupId = contextGroupId(context);
if (!contextGroupId) return; if (!groupId) return;
std::unique_ptr<V8ConsoleMessage> consoleMessage = std::unique_ptr<V8ConsoleMessage> consoleMessage =
V8ConsoleMessage::createForRevokedException( V8ConsoleMessage::createForRevokedException(
m_client->currentTimeMS(), toString16(message), exceptionId); m_client->currentTimeMS(), toString16(message), exceptionId);
ensureConsoleMessageStorage(contextGroupId) ensureConsoleMessageStorage(groupId)->addMessage(std::move(consoleMessage));
->addMessage(std::move(consoleMessage));
} }
std::unique_ptr<V8StackTrace> V8InspectorImpl::captureStackTrace( std::unique_ptr<V8StackTrace> V8InspectorImpl::captureStackTrace(
......
...@@ -58,6 +58,8 @@ class V8InspectorImpl : public V8Inspector { ...@@ -58,6 +58,8 @@ class V8InspectorImpl : public V8Inspector {
v8::Isolate* isolate() const { return m_isolate; } v8::Isolate* isolate() const { return m_isolate; }
V8InspectorClient* client() { return m_client; } V8InspectorClient* client() { return m_client; }
V8Debugger* debugger() { return m_debugger.get(); } V8Debugger* debugger() { return m_debugger.get(); }
int contextGroupId(v8::Local<v8::Context>);
int contextGroupId(int contextId);
v8::MaybeLocal<v8::Value> runCompiledScript(v8::Local<v8::Context>, v8::MaybeLocal<v8::Value> runCompiledScript(v8::Local<v8::Context>,
v8::Local<v8::Script>); v8::Local<v8::Script>);
...@@ -136,6 +138,7 @@ class V8InspectorImpl : public V8Inspector { ...@@ -136,6 +138,7 @@ class V8InspectorImpl : public V8Inspector {
v8::Global<v8::Context> m_regexContext; v8::Global<v8::Context> m_regexContext;
int m_capturingStackTracesCount; int m_capturingStackTracesCount;
unsigned m_lastExceptionId; unsigned m_lastExceptionId;
int m_lastContextId;
using MuteExceptionsMap = protocol::HashMap<int, int>; using MuteExceptionsMap = protocol::HashMap<int, int>;
MuteExceptionsMap m_muteExceptionsMap; MuteExceptionsMap m_muteExceptionsMap;
...@@ -151,6 +154,8 @@ class V8InspectorImpl : public V8Inspector { ...@@ -151,6 +154,8 @@ class V8InspectorImpl : public V8Inspector {
protocol::HashMap<int, std::unique_ptr<V8ConsoleMessageStorage>>; protocol::HashMap<int, std::unique_ptr<V8ConsoleMessageStorage>>;
ConsoleStorageMap m_consoleStorageMap; ConsoleStorageMap m_consoleStorageMap;
protocol::HashMap<int, int> m_contextIdToGroupIdMap;
DISALLOW_COPY_AND_ASSIGN(V8InspectorImpl); DISALLOW_COPY_AND_ASSIGN(V8InspectorImpl);
}; };
......
...@@ -295,7 +295,7 @@ V8InspectorSessionImpl::wrapObject(v8::Local<v8::Context> context, ...@@ -295,7 +295,7 @@ V8InspectorSessionImpl::wrapObject(v8::Local<v8::Context> context,
const String16& groupName, const String16& groupName,
bool generatePreview) { bool generatePreview) {
InjectedScript* injectedScript = nullptr; InjectedScript* injectedScript = nullptr;
findInjectedScript(V8Debugger::contextId(context), injectedScript); findInjectedScript(InspectedContext::contextId(context), injectedScript);
if (!injectedScript) return nullptr; if (!injectedScript) return nullptr;
std::unique_ptr<protocol::Runtime::RemoteObject> result; std::unique_ptr<protocol::Runtime::RemoteObject> result;
injectedScript->wrapObject(value, groupName, false, generatePreview, &result); injectedScript->wrapObject(value, groupName, false, generatePreview, &result);
...@@ -307,7 +307,7 @@ V8InspectorSessionImpl::wrapTable(v8::Local<v8::Context> context, ...@@ -307,7 +307,7 @@ V8InspectorSessionImpl::wrapTable(v8::Local<v8::Context> context,
v8::Local<v8::Value> table, v8::Local<v8::Value> table,
v8::Local<v8::Value> columns) { v8::Local<v8::Value> columns) {
InjectedScript* injectedScript = nullptr; InjectedScript* injectedScript = nullptr;
findInjectedScript(V8Debugger::contextId(context), injectedScript); findInjectedScript(InspectedContext::contextId(context), injectedScript);
if (!injectedScript) return nullptr; if (!injectedScript) return nullptr;
return injectedScript->wrapTable(table, columns); return injectedScript->wrapTable(table, columns);
} }
......
...@@ -241,7 +241,7 @@ Response ensureContext(V8InspectorImpl* inspector, int contextGroupId, ...@@ -241,7 +241,7 @@ Response ensureContext(V8InspectorImpl* inspector, int contextGroupId,
inspector->client()->ensureDefaultContextInGroup(contextGroupId); inspector->client()->ensureDefaultContextInGroup(contextGroupId);
if (defaultContext.IsEmpty()) if (defaultContext.IsEmpty())
return Response::Error("Cannot find default execution context"); return Response::Error("Cannot find default execution context");
*contextId = V8Debugger::contextId(defaultContext); *contextId = InspectedContext::contextId(defaultContext);
} }
return Response::OK(); return Response::OK();
} }
......
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