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: Alexei Filippov <alph@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55286}
This commit is contained in:
Alexey Kozyatinskiy 2018-08-21 11:18:19 -07:00 committed by Commit Bot
parent a6938128f4
commit e987606a8a
3 changed files with 48 additions and 22 deletions

View File

@ -160,9 +160,8 @@ String16 breakpointHint(const V8DebuggerScript& script, int lineNumber,
int columnNumber) {
int offset = script.offset(lineNumber, columnNumber);
if (offset == V8DebuggerScript::kNoOffset) return String16();
const String16& source = script.source();
String16 hint =
source.substring(offset, kBreakpointHintMaxLength).stripWhiteSpace();
script.source(offset, kBreakpointHintMaxLength).stripWhiteSpace();
for (size_t i = 0; i < hint.length(); ++i) {
if (hint[i] == '\r' || hint[i] == '\n' || hint[i] == ';') {
return hint.substring(0, i);
@ -183,8 +182,8 @@ void adjustBreakpointLocation(const V8DebuggerScript& script,
intptr_t searchRegionOffset = std::max(
sourceOffset - kBreakpointHintMaxSearchOffset, static_cast<intptr_t>(0));
size_t offset = sourceOffset - searchRegionOffset;
String16 searchArea = script.source().substring(
searchRegionOffset, offset + kBreakpointHintMaxSearchOffset);
String16 searchArea = script.source(searchRegionOffset,
offset + kBreakpointHintMaxSearchOffset);
size_t nextMatch = searchArea.find(hint, offset);
size_t prevMatch = searchArea.reverseFind(hint, offset);
@ -837,7 +836,7 @@ Response V8DebuggerAgentImpl::searchInContent(
return Response::Error("No script for id: " + scriptId);
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),
optionalIsRegex.fromMaybe(false));
*results = protocol::Array<protocol::Debugger::SearchMatch>::create();
@ -927,7 +926,7 @@ Response V8DebuggerAgentImpl::getScriptSource(const String16& scriptId,
ScriptsMap::iterator it = m_scripts.find(scriptId);
if (it == m_scripts.end())
return Response::Error("No script for id: " + scriptId);
*scriptSource = it->second->source();
*scriptSource = it->second->source(0);
return Response::OK();
}
@ -1434,8 +1433,7 @@ void V8DebuggerAgentImpl::didParseSource(
scriptRef->endLine(), scriptRef->endColumn(), contextId,
scriptRef->hash(), std::move(executionContextAuxDataParam),
isLiveEditParam, std::move(sourceMapURLParam), hasSourceURLParam,
isModuleParam, static_cast<int>(scriptRef->source().length()),
std::move(stackTrace));
isModuleParam, scriptRef->length(), std::move(stackTrace));
}
} else {
m_frontend.scriptFailedToParse(
@ -1443,7 +1441,7 @@ void V8DebuggerAgentImpl::didParseSource(
scriptRef->endLine(), scriptRef->endColumn(), contextId,
scriptRef->hash(), std::move(executionContextAuxDataParam),
std::move(sourceMapURLParam), hasSourceURLParam, isModuleParam,
static_cast<int>(scriptRef->source().length()), std::move(stackTrace));
scriptRef->length(), std::move(stackTrace));
}
if (!success) {

View File

@ -20,7 +20,7 @@ const char kGlobalDebuggerScriptHandleLabel[] = "DevTools debugger";
// Multiplikation in
// eingeschränkten Branchingprogrammmodellen" by Woelfe.
// 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,
0x81ABE279};
static uint64_t random[] = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476,
@ -34,9 +34,14 @@ String16 calculateHash(const String16& str) {
const size_t hashesSize = arraysize(hashes);
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;
size_t sizeInBytes = sizeof(UChar) * str.length();
data = reinterpret_cast<const uint32_t*>(str.characters16());
size_t sizeInBytes = sizeof(UChar) * written;
data = reinterpret_cast<const uint32_t*>(buffer.get());
for (size_t i = 0; i < sizeInBytes / 4; ++i) {
uint32_t d = v8::internal::ReadUnalignedUInt32(
reinterpret_cast<v8::internal::Address>(data + i));
@ -121,12 +126,29 @@ class ActualScript : public V8DebuggerScript {
bool isLiveEdit() const override { return m_isLiveEdit; }
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 startColumn() const override { return m_startColumn; }
int endLine() const override { return m_endLine; }
int endColumn() const override { return m_endColumn; }
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 {
return m_sourceMappingURL;
@ -213,7 +235,13 @@ class ActualScript : public V8DebuggerScript {
}
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());
return m_hash;
}
@ -264,10 +292,6 @@ class ActualScript : public V8DebuggerScript {
USE(script->ContextId().To(&m_executionContextId));
if (script->Source().ToLocal(&tmp)) {
m_source = toProtocolString(m_isolate, tmp);
}
m_isModule = script->IsModule();
m_script.Reset(m_isolate, script);
@ -277,7 +301,6 @@ class ActualScript : public V8DebuggerScript {
String16 m_sourceMappingURL;
bool m_isLiveEdit = false;
bool m_isModule = false;
String16 m_source;
mutable String16 m_hash;
int m_startLine = 0;
int m_startColumn = 0;
@ -309,8 +332,9 @@ class WasmVirtualScript : public V8DebuggerScript {
UNREACHABLE();
}
bool isSourceLoadedLazily() const override { return true; }
const String16& source() const override {
return m_wasmTranslation->GetSource(m_id, m_functionIndex);
String16 source(size_t pos, size_t len) const override {
return m_wasmTranslation->GetSource(m_id, m_functionIndex)
.substring(pos, len);
}
int startLine() const override {
return m_wasmTranslation->GetStartLine(m_id, m_functionIndex);
@ -324,6 +348,9 @@ class WasmVirtualScript : public V8DebuggerScript {
int endColumn() const override {
return m_wasmTranslation->GetEndColumn(m_id, m_functionIndex);
}
int length() const override {
return static_cast<int>(source(0, UINT_MAX).length());
}
bool getPossibleBreakpoints(
const v8::debug::Location& start, const v8::debug::Location& end,

View File

@ -60,7 +60,7 @@ class V8DebuggerScript {
const String16& sourceURL() const { return m_url; }
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 int startLine() const = 0;
virtual int startColumn() const = 0;
@ -70,6 +70,7 @@ class V8DebuggerScript {
virtual bool isLiveEdit() const = 0;
virtual bool isModule() const = 0;
virtual bool isSourceLoadedLazily() const = 0;
virtual int length() const = 0;
void setSourceURL(const String16&);
virtual void setSourceMappingURL(const String16&) = 0;