Chromium Code Reviews| Index: src/inspector/v8-debugger-script.cc |
| diff --git a/src/inspector/v8-debugger-script.cc b/src/inspector/v8-debugger-script.cc |
| index 200cdc71a26da41ae8bba76d30144ad5a2eca0f7..3522166ef7e0ca79e8cb49766229dbd100a69953 100644 |
| --- a/src/inspector/v8-debugger-script.cc |
| +++ b/src/inspector/v8-debugger-script.cc |
| @@ -5,6 +5,7 @@ |
| #include "src/inspector/v8-debugger-script.h" |
| #include "src/inspector/inspected-context.h" |
| +#include "src/inspector/search-util.h" |
| #include "src/inspector/string-util.h" |
| #include "src/inspector/wasm-translation.h" |
| @@ -154,10 +155,11 @@ class ActualScript : public V8DebuggerScript { |
| return m_sourceMappingURL; |
| } |
| - String16 source(v8::Isolate* isolate) const override { |
| + String16 source() const override { |
| + v8::HandleScope scope(m_isolate); |
| if (!m_sourceObj.IsEmpty()) |
| - return toProtocolString(m_sourceObj.Get(isolate)); |
| - return V8DebuggerScript::source(isolate); |
| + return toProtocolString(m_sourceObj.Get(m_isolate)); |
| + return V8DebuggerScript::source(); |
| } |
| void setSourceMappingURL(const String16& sourceMappingURL) override { |
| @@ -165,7 +167,7 @@ class ActualScript : public V8DebuggerScript { |
| } |
| void setSource(v8::Local<v8::String> source) override { |
| - m_source = String16(); |
| + m_lineEndings.reset(); |
| m_sourceObj.Reset(m_isolate, source); |
| m_hash = String16(); |
| } |
| @@ -268,6 +270,23 @@ class WasmVirtualScript : public V8DebuggerScript { |
| WasmTranslation* m_wasmTranslation; |
| }; |
| +std::unique_ptr<std::vector<size_t>> lineEndings(const String16& text) { |
| + std::unique_ptr<std::vector<size_t>> result(new std::vector<size_t>()); |
| + |
| + const String16 lineEndString = "\n"; |
| + size_t start = 0; |
| + while (start < text.length()) { |
| + size_t lineEnd = text.find(lineEndString, start); |
| + if (lineEnd == String16::kNotFound) break; |
| + |
| + result->push_back(lineEnd); |
| + start = lineEnd + 1; |
| + } |
| + result->push_back(text.length()); |
| + |
| + return result; |
| +} |
| + |
| } // namespace |
| std::unique_ptr<V8DebuggerScript> V8DebuggerScript::Create( |
| @@ -296,8 +315,8 @@ const String16& V8DebuggerScript::sourceURL() const { |
| return m_sourceURL.isEmpty() ? m_url : m_sourceURL; |
| } |
| -const String16& V8DebuggerScript::hash(v8::Isolate* isolate) const { |
| - if (m_hash.isEmpty()) m_hash = calculateHash(source(isolate)); |
| +const String16& V8DebuggerScript::hash() const { |
| + if (m_hash.isEmpty()) m_hash = calculateHash(source()); |
| DCHECK(!m_hash.isEmpty()); |
| return m_hash; |
| } |
| @@ -306,4 +325,19 @@ void V8DebuggerScript::setSourceURL(const String16& sourceURL) { |
| m_sourceURL = sourceURL; |
| } |
| +String16 V8DebuggerScript::lineAt(int lineNumberWithOffset, int maxLength) { |
| + String16 sourceStr = source(); |
| + if (!m_lineEndings) m_lineEndings = lineEndings(sourceStr); |
|
pfeldman
2017/02/07 19:13:05
That's O(N) which hurts us on the 20Mb scripts.
kozy
2017/02/08 01:32:56
V8 couldn't avoid this calculation since it uses l
|
| + int lineNumber = lineNumberWithOffset - m_startLine; |
| + if (lineNumber >= static_cast<int>(m_lineEndings->size()) || lineNumber < 0) |
| + return String16(); |
| + size_t lineEnd = m_lineEndings->at(lineNumber); |
| + size_t lineStart = lineNumber > 0 ? m_lineEndings->at(lineNumber - 1) + 1 : 0; |
| + String16 line = sourceStr.substring(lineStart, lineEnd - lineStart); |
| + if (line.length() && line[line.length() - 1] == '\r') |
| + line = line.substring(0, line.length() - 1); |
| + if (maxLength) line = line.substring(0, maxLength); |
| + return line; |
| +} |
| + |
| } // namespace v8_inspector |