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..e7fed32500cceb68ffc7afa8ad7ad736ac28d8bf 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,8 @@ class ActualScript : public V8DebuggerScript { |
} |
void setSource(v8::Local<v8::String> source) override { |
- m_source = String16(); |
+ std::vector<int> empty; |
+ m_lineEndings.swap(empty); |
m_sourceObj.Reset(m_isolate, source); |
m_hash = String16(); |
} |
@@ -184,6 +187,12 @@ class ActualScript : public V8DebuggerScript { |
} |
private: |
+ void calculateLineEndings() override { |
+ if (!m_lineEndings.empty()) return; |
+ v8::HandleScope scope(m_isolate); |
+ m_script.Get(m_isolate)->LineEnds().swap(m_lineEndings); |
+ } |
+ |
String16 GetNameOrSourceUrl(v8::Local<v8::debug::Script> script) { |
v8::Local<v8::String> name; |
if (script->Name().ToLocal(&name) || script->SourceURL().ToLocal(&name)) |
@@ -259,6 +268,20 @@ class WasmVirtualScript : public V8DebuggerScript { |
void resetBlackboxedStateCache() override {} |
private: |
+ void calculateLineEndings() override { |
alph
2017/02/08 02:02:08
ensureLineEndings?
|
+ if (!m_lineEndings.empty()) return; |
+ String16 text = source(); |
+ const String16 lineEndString = "\n"; |
+ size_t start = 0; |
+ while (start < text.length()) { |
+ size_t lineEnd = text.find(lineEndString, start); |
alph
2017/02/08 02:02:08
nit: find(UChar) should be faster.
|
+ if (lineEnd == String16::kNotFound) break; |
+ m_lineEndings.push_back(static_cast<int>(lineEnd)); |
+ start = lineEnd + 1; |
+ } |
+ m_lineEndings.push_back(static_cast<int>(text.length())); |
+ } |
+ |
static const String16& emptyString() { |
static const String16 singleEmptyString; |
return singleEmptyString; |
@@ -296,8 +319,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 +329,19 @@ void V8DebuggerScript::setSourceURL(const String16& sourceURL) { |
m_sourceURL = sourceURL; |
} |
+String16 V8DebuggerScript::lineAt(int lineNumberWithOffset, int maxLength) { |
+ String16 sourceStr = source(); |
+ calculateLineEndings(); |
+ int lineNumber = lineNumberWithOffset - m_startLine; |
+ if (lineNumber >= static_cast<int>(m_lineEndings.size()) || lineNumber < 0) |
alph
2017/02/08 02:02:08
{}
|
+ return String16(); |
+ size_t lineEnd = m_lineEndings[lineNumber]; |
+ size_t lineStart = lineNumber > 0 ? m_lineEndings[lineNumber - 1] + 1 : 0; |
+ String16 line = sourceStr.substring(lineStart, lineEnd - lineStart); |
+ if (line.length() && line[line.length() - 1] == '\r') |
alph
2017/02/08 02:02:08
{}
|
+ line = line.substring(0, line.length() - 1); |
+ if (maxLength) line = line.substring(0, maxLength); |
+ return line; |
+} |
+ |
} // namespace v8_inspector |