Commit e987606a authored by Alexey Kozyatinskiy's avatar Alexey Kozyatinskiy Committed by Commit Bot

inspector: do not convert and store String16 for script source

We need script source for:
- calculating hash to report as part of scriptParsed event,
- reporting it as response on getScriptSource request,
- searching inside as response on searchInContent request,
- breakpoints hints.

In all cases there is no need to store source on inspector side.

R=alph@chromium.org

Bug: chromium:873865,v8:7731
Cq-Include-Trybots: luci.chromium.try:linux_chromium_headless_rel;master.tryserver.blink:linux_trusty_blink_rel
Change-Id: Ice24ddc72cfff36fb9a2dff2d7c4543defe3f668
Reviewed-on: https://chromium-review.googlesource.com/1182603
Commit-Queue: Aleksey Kozyatinskiy <kozyatinskiy@chromium.org>
Reviewed-by: 's avatarAlexei Filippov <alph@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55286}
parent a6938128
...@@ -160,9 +160,8 @@ String16 breakpointHint(const V8DebuggerScript& script, int lineNumber, ...@@ -160,9 +160,8 @@ String16 breakpointHint(const V8DebuggerScript& script, int lineNumber,
int columnNumber) { int columnNumber) {
int offset = script.offset(lineNumber, columnNumber); int offset = script.offset(lineNumber, columnNumber);
if (offset == V8DebuggerScript::kNoOffset) return String16(); if (offset == V8DebuggerScript::kNoOffset) return String16();
const String16& source = script.source();
String16 hint = String16 hint =
source.substring(offset, kBreakpointHintMaxLength).stripWhiteSpace(); script.source(offset, kBreakpointHintMaxLength).stripWhiteSpace();
for (size_t i = 0; i < hint.length(); ++i) { for (size_t i = 0; i < hint.length(); ++i) {
if (hint[i] == '\r' || hint[i] == '\n' || hint[i] == ';') { if (hint[i] == '\r' || hint[i] == '\n' || hint[i] == ';') {
return hint.substring(0, i); return hint.substring(0, i);
...@@ -183,8 +182,8 @@ void adjustBreakpointLocation(const V8DebuggerScript& script, ...@@ -183,8 +182,8 @@ void adjustBreakpointLocation(const V8DebuggerScript& script,
intptr_t searchRegionOffset = std::max( intptr_t searchRegionOffset = std::max(
sourceOffset - kBreakpointHintMaxSearchOffset, static_cast<intptr_t>(0)); sourceOffset - kBreakpointHintMaxSearchOffset, static_cast<intptr_t>(0));
size_t offset = sourceOffset - searchRegionOffset; size_t offset = sourceOffset - searchRegionOffset;
String16 searchArea = script.source().substring( String16 searchArea = script.source(searchRegionOffset,
searchRegionOffset, offset + kBreakpointHintMaxSearchOffset); offset + kBreakpointHintMaxSearchOffset);
size_t nextMatch = searchArea.find(hint, offset); size_t nextMatch = searchArea.find(hint, offset);
size_t prevMatch = searchArea.reverseFind(hint, offset); size_t prevMatch = searchArea.reverseFind(hint, offset);
...@@ -837,7 +836,7 @@ Response V8DebuggerAgentImpl::searchInContent( ...@@ -837,7 +836,7 @@ Response V8DebuggerAgentImpl::searchInContent(
return Response::Error("No script for id: " + scriptId); return Response::Error("No script for id: " + scriptId);
std::vector<std::unique_ptr<protocol::Debugger::SearchMatch>> matches = std::vector<std::unique_ptr<protocol::Debugger::SearchMatch>> matches =
searchInTextByLinesImpl(m_session, it->second->source(), query, searchInTextByLinesImpl(m_session, it->second->source(0), query,
optionalCaseSensitive.fromMaybe(false), optionalCaseSensitive.fromMaybe(false),
optionalIsRegex.fromMaybe(false)); optionalIsRegex.fromMaybe(false));
*results = protocol::Array<protocol::Debugger::SearchMatch>::create(); *results = protocol::Array<protocol::Debugger::SearchMatch>::create();
...@@ -927,7 +926,7 @@ Response V8DebuggerAgentImpl::getScriptSource(const String16& scriptId, ...@@ -927,7 +926,7 @@ Response V8DebuggerAgentImpl::getScriptSource(const String16& scriptId,
ScriptsMap::iterator it = m_scripts.find(scriptId); ScriptsMap::iterator it = m_scripts.find(scriptId);
if (it == m_scripts.end()) if (it == m_scripts.end())
return Response::Error("No script for id: " + scriptId); return Response::Error("No script for id: " + scriptId);
*scriptSource = it->second->source(); *scriptSource = it->second->source(0);
return Response::OK(); return Response::OK();
} }
...@@ -1434,8 +1433,7 @@ void V8DebuggerAgentImpl::didParseSource( ...@@ -1434,8 +1433,7 @@ void V8DebuggerAgentImpl::didParseSource(
scriptRef->endLine(), scriptRef->endColumn(), contextId, scriptRef->endLine(), scriptRef->endColumn(), contextId,
scriptRef->hash(), std::move(executionContextAuxDataParam), scriptRef->hash(), std::move(executionContextAuxDataParam),
isLiveEditParam, std::move(sourceMapURLParam), hasSourceURLParam, isLiveEditParam, std::move(sourceMapURLParam), hasSourceURLParam,
isModuleParam, static_cast<int>(scriptRef->source().length()), isModuleParam, scriptRef->length(), std::move(stackTrace));
std::move(stackTrace));
} }
} else { } else {
m_frontend.scriptFailedToParse( m_frontend.scriptFailedToParse(
...@@ -1443,7 +1441,7 @@ void V8DebuggerAgentImpl::didParseSource( ...@@ -1443,7 +1441,7 @@ void V8DebuggerAgentImpl::didParseSource(
scriptRef->endLine(), scriptRef->endColumn(), contextId, scriptRef->endLine(), scriptRef->endColumn(), contextId,
scriptRef->hash(), std::move(executionContextAuxDataParam), scriptRef->hash(), std::move(executionContextAuxDataParam),
std::move(sourceMapURLParam), hasSourceURLParam, isModuleParam, std::move(sourceMapURLParam), hasSourceURLParam, isModuleParam,
static_cast<int>(scriptRef->source().length()), std::move(stackTrace)); scriptRef->length(), std::move(stackTrace));
} }
if (!success) { if (!success) {
......
...@@ -20,7 +20,7 @@ const char kGlobalDebuggerScriptHandleLabel[] = "DevTools debugger"; ...@@ -20,7 +20,7 @@ const char kGlobalDebuggerScriptHandleLabel[] = "DevTools debugger";
// Multiplikation in // Multiplikation in
// eingeschränkten Branchingprogrammmodellen" by Woelfe. // eingeschränkten Branchingprogrammmodellen" by Woelfe.
// http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000 // http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000
String16 calculateHash(const String16& str) { String16 calculateHash(v8::Isolate* isolate, v8::Local<v8::String> source) {
static uint64_t prime[] = {0x3FB75161, 0xAB1F4E4F, 0x82675BC5, 0xCD924D35, static uint64_t prime[] = {0x3FB75161, 0xAB1F4E4F, 0x82675BC5, 0xCD924D35,
0x81ABE279}; 0x81ABE279};
static uint64_t random[] = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, static uint64_t random[] = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476,
...@@ -34,9 +34,14 @@ String16 calculateHash(const String16& str) { ...@@ -34,9 +34,14 @@ String16 calculateHash(const String16& str) {
const size_t hashesSize = arraysize(hashes); const size_t hashesSize = arraysize(hashes);
size_t current = 0; size_t current = 0;
std::unique_ptr<UChar[]> buffer(new UChar[source->Length()]);
int written = source->Write(
isolate, reinterpret_cast<uint16_t*>(buffer.get()), 0, source->Length());
const uint32_t* data = nullptr; const uint32_t* data = nullptr;
size_t sizeInBytes = sizeof(UChar) * str.length(); size_t sizeInBytes = sizeof(UChar) * written;
data = reinterpret_cast<const uint32_t*>(str.characters16()); data = reinterpret_cast<const uint32_t*>(buffer.get());
for (size_t i = 0; i < sizeInBytes / 4; ++i) { for (size_t i = 0; i < sizeInBytes / 4; ++i) {
uint32_t d = v8::internal::ReadUnalignedUInt32( uint32_t d = v8::internal::ReadUnalignedUInt32(
reinterpret_cast<v8::internal::Address>(data + i)); reinterpret_cast<v8::internal::Address>(data + i));
...@@ -121,12 +126,29 @@ class ActualScript : public V8DebuggerScript { ...@@ -121,12 +126,29 @@ class ActualScript : public V8DebuggerScript {
bool isLiveEdit() const override { return m_isLiveEdit; } bool isLiveEdit() const override { return m_isLiveEdit; }
bool isModule() const override { return m_isModule; } bool isModule() const override { return m_isModule; }
const String16& source() const override { return m_source; } String16 source(size_t pos, size_t len) const override {
v8::HandleScope scope(m_isolate);
v8::Local<v8::String> v8Source;
if (!script()->Source().ToLocal(&v8Source)) return String16();
if (pos >= static_cast<size_t>(v8Source->Length())) return String16();
size_t substringLength =
std::min(len, static_cast<size_t>(v8Source->Length()) - pos);
std::unique_ptr<UChar[]> buffer(new UChar[substringLength]);
v8Source->Write(m_isolate, reinterpret_cast<uint16_t*>(buffer.get()),
static_cast<int>(pos), static_cast<int>(substringLength));
return String16(buffer.get(), substringLength);
}
int startLine() const override { return m_startLine; } int startLine() const override { return m_startLine; }
int startColumn() const override { return m_startColumn; } int startColumn() const override { return m_startColumn; }
int endLine() const override { return m_endLine; } int endLine() const override { return m_endLine; }
int endColumn() const override { return m_endColumn; } int endColumn() const override { return m_endColumn; }
bool isSourceLoadedLazily() const override { return false; } bool isSourceLoadedLazily() const override { return false; }
int length() const override {
v8::HandleScope scope(m_isolate);
v8::Local<v8::String> v8Source;
if (!script()->Source().ToLocal(&v8Source)) return 0;
return v8Source->Length();
}
const String16& sourceMappingURL() const override { const String16& sourceMappingURL() const override {
return m_sourceMappingURL; return m_sourceMappingURL;
...@@ -213,7 +235,13 @@ class ActualScript : public V8DebuggerScript { ...@@ -213,7 +235,13 @@ class ActualScript : public V8DebuggerScript {
} }
const String16& hash() const override { const String16& hash() const override {
if (m_hash.isEmpty()) m_hash = calculateHash(source()); if (m_hash.isEmpty()) {
v8::HandleScope scope(m_isolate);
v8::Local<v8::String> v8Source;
if (script()->Source().ToLocal(&v8Source)) {
m_hash = calculateHash(m_isolate, v8Source);
}
}
DCHECK(!m_hash.isEmpty()); DCHECK(!m_hash.isEmpty());
return m_hash; return m_hash;
} }
...@@ -264,10 +292,6 @@ class ActualScript : public V8DebuggerScript { ...@@ -264,10 +292,6 @@ class ActualScript : public V8DebuggerScript {
USE(script->ContextId().To(&m_executionContextId)); USE(script->ContextId().To(&m_executionContextId));
if (script->Source().ToLocal(&tmp)) {
m_source = toProtocolString(m_isolate, tmp);
}
m_isModule = script->IsModule(); m_isModule = script->IsModule();
m_script.Reset(m_isolate, script); m_script.Reset(m_isolate, script);
...@@ -277,7 +301,6 @@ class ActualScript : public V8DebuggerScript { ...@@ -277,7 +301,6 @@ class ActualScript : public V8DebuggerScript {
String16 m_sourceMappingURL; String16 m_sourceMappingURL;
bool m_isLiveEdit = false; bool m_isLiveEdit = false;
bool m_isModule = false; bool m_isModule = false;
String16 m_source;
mutable String16 m_hash; mutable String16 m_hash;
int m_startLine = 0; int m_startLine = 0;
int m_startColumn = 0; int m_startColumn = 0;
...@@ -309,8 +332,9 @@ class WasmVirtualScript : public V8DebuggerScript { ...@@ -309,8 +332,9 @@ class WasmVirtualScript : public V8DebuggerScript {
UNREACHABLE(); UNREACHABLE();
} }
bool isSourceLoadedLazily() const override { return true; } bool isSourceLoadedLazily() const override { return true; }
const String16& source() const override { String16 source(size_t pos, size_t len) const override {
return m_wasmTranslation->GetSource(m_id, m_functionIndex); return m_wasmTranslation->GetSource(m_id, m_functionIndex)
.substring(pos, len);
} }
int startLine() const override { int startLine() const override {
return m_wasmTranslation->GetStartLine(m_id, m_functionIndex); return m_wasmTranslation->GetStartLine(m_id, m_functionIndex);
...@@ -324,6 +348,9 @@ class WasmVirtualScript : public V8DebuggerScript { ...@@ -324,6 +348,9 @@ class WasmVirtualScript : public V8DebuggerScript {
int endColumn() const override { int endColumn() const override {
return m_wasmTranslation->GetEndColumn(m_id, m_functionIndex); return m_wasmTranslation->GetEndColumn(m_id, m_functionIndex);
} }
int length() const override {
return static_cast<int>(source(0, UINT_MAX).length());
}
bool getPossibleBreakpoints( bool getPossibleBreakpoints(
const v8::debug::Location& start, const v8::debug::Location& end, const v8::debug::Location& start, const v8::debug::Location& end,
......
...@@ -60,7 +60,7 @@ class V8DebuggerScript { ...@@ -60,7 +60,7 @@ class V8DebuggerScript {
const String16& sourceURL() const { return m_url; } const String16& sourceURL() const { return m_url; }
virtual const String16& sourceMappingURL() const = 0; virtual const String16& sourceMappingURL() const = 0;
virtual const String16& source() const = 0; virtual String16 source(size_t pos, size_t len = UINT_MAX) const = 0;
virtual const String16& hash() const = 0; virtual const String16& hash() const = 0;
virtual int startLine() const = 0; virtual int startLine() const = 0;
virtual int startColumn() const = 0; virtual int startColumn() const = 0;
...@@ -70,6 +70,7 @@ class V8DebuggerScript { ...@@ -70,6 +70,7 @@ class V8DebuggerScript {
virtual bool isLiveEdit() const = 0; virtual bool isLiveEdit() const = 0;
virtual bool isModule() const = 0; virtual bool isModule() const = 0;
virtual bool isSourceLoadedLazily() const = 0; virtual bool isSourceLoadedLazily() const = 0;
virtual int length() const = 0;
void setSourceURL(const String16&); void setSourceURL(const String16&);
virtual void setSourceMappingURL(const String16&) = 0; virtual void setSourceMappingURL(const String16&) = 0;
......
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