OLD | NEW |
---|---|
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/inspector/v8-debugger-script.h" | 5 #include "src/inspector/v8-debugger-script.h" |
6 | 6 |
7 #include "src/inspector/inspected-context.h" | 7 #include "src/inspector/inspected-context.h" |
8 #include "src/inspector/search-util.h" | |
8 #include "src/inspector/string-util.h" | 9 #include "src/inspector/string-util.h" |
9 #include "src/inspector/wasm-translation.h" | 10 #include "src/inspector/wasm-translation.h" |
10 | 11 |
11 namespace v8_inspector { | 12 namespace v8_inspector { |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
15 const char hexDigits[17] = "0123456789ABCDEF"; | 16 const char hexDigits[17] = "0123456789ABCDEF"; |
16 | 17 |
17 void appendUnsignedAsHex(uint64_t number, String16Builder* destination) { | 18 void appendUnsignedAsHex(uint64_t number, String16Builder* destination) { |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 m_script.Reset(m_isolate, script); | 148 m_script.Reset(m_isolate, script); |
148 } | 149 } |
149 | 150 |
150 bool isLiveEdit() const override { return m_isLiveEdit; } | 151 bool isLiveEdit() const override { return m_isLiveEdit; } |
151 bool isModule() const override { return m_isModule; } | 152 bool isModule() const override { return m_isModule; } |
152 | 153 |
153 const String16& sourceMappingURL() const override { | 154 const String16& sourceMappingURL() const override { |
154 return m_sourceMappingURL; | 155 return m_sourceMappingURL; |
155 } | 156 } |
156 | 157 |
157 String16 source(v8::Isolate* isolate) const override { | 158 String16 source() const override { |
159 v8::HandleScope scope(m_isolate); | |
158 if (!m_sourceObj.IsEmpty()) | 160 if (!m_sourceObj.IsEmpty()) |
159 return toProtocolString(m_sourceObj.Get(isolate)); | 161 return toProtocolString(m_sourceObj.Get(m_isolate)); |
160 return V8DebuggerScript::source(isolate); | 162 return V8DebuggerScript::source(); |
161 } | 163 } |
162 | 164 |
163 void setSourceMappingURL(const String16& sourceMappingURL) override { | 165 void setSourceMappingURL(const String16& sourceMappingURL) override { |
164 m_sourceMappingURL = sourceMappingURL; | 166 m_sourceMappingURL = sourceMappingURL; |
165 } | 167 } |
166 | 168 |
167 void setSource(v8::Local<v8::String> source) override { | 169 void setSource(v8::Local<v8::String> source) override { |
168 m_source = String16(); | 170 m_lineEndings.reset(); |
169 m_sourceObj.Reset(m_isolate, source); | 171 m_sourceObj.Reset(m_isolate, source); |
170 m_hash = String16(); | 172 m_hash = String16(); |
171 } | 173 } |
172 | 174 |
173 bool getPossibleBreakpoints( | 175 bool getPossibleBreakpoints( |
174 const v8::debug::Location& start, const v8::debug::Location& end, | 176 const v8::debug::Location& start, const v8::debug::Location& end, |
175 std::vector<v8::debug::Location>* locations) override { | 177 std::vector<v8::debug::Location>* locations) override { |
176 v8::HandleScope scope(m_isolate); | 178 v8::HandleScope scope(m_isolate); |
177 v8::Local<v8::debug::Script> script = m_script.Get(m_isolate); | 179 v8::Local<v8::debug::Script> script = m_script.Get(m_isolate); |
178 return script->GetPossibleBreakpoints(start, end, locations); | 180 return script->GetPossibleBreakpoints(start, end, locations); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
261 private: | 263 private: |
262 static const String16& emptyString() { | 264 static const String16& emptyString() { |
263 static const String16 singleEmptyString; | 265 static const String16 singleEmptyString; |
264 return singleEmptyString; | 266 return singleEmptyString; |
265 } | 267 } |
266 | 268 |
267 v8::Global<v8::debug::WasmScript> m_script; | 269 v8::Global<v8::debug::WasmScript> m_script; |
268 WasmTranslation* m_wasmTranslation; | 270 WasmTranslation* m_wasmTranslation; |
269 }; | 271 }; |
270 | 272 |
273 std::unique_ptr<std::vector<size_t>> lineEndings(const String16& text) { | |
274 std::unique_ptr<std::vector<size_t>> result(new std::vector<size_t>()); | |
275 | |
276 const String16 lineEndString = "\n"; | |
277 size_t start = 0; | |
278 while (start < text.length()) { | |
279 size_t lineEnd = text.find(lineEndString, start); | |
280 if (lineEnd == String16::kNotFound) break; | |
281 | |
282 result->push_back(lineEnd); | |
283 start = lineEnd + 1; | |
284 } | |
285 result->push_back(text.length()); | |
286 | |
287 return result; | |
288 } | |
289 | |
271 } // namespace | 290 } // namespace |
272 | 291 |
273 std::unique_ptr<V8DebuggerScript> V8DebuggerScript::Create( | 292 std::unique_ptr<V8DebuggerScript> V8DebuggerScript::Create( |
274 v8::Isolate* isolate, v8::Local<v8::debug::Script> scriptObj, | 293 v8::Isolate* isolate, v8::Local<v8::debug::Script> scriptObj, |
275 bool isLiveEdit) { | 294 bool isLiveEdit) { |
276 return std::unique_ptr<ActualScript>( | 295 return std::unique_ptr<ActualScript>( |
277 new ActualScript(isolate, scriptObj, isLiveEdit)); | 296 new ActualScript(isolate, scriptObj, isLiveEdit)); |
278 } | 297 } |
279 | 298 |
280 std::unique_ptr<V8DebuggerScript> V8DebuggerScript::CreateWasm( | 299 std::unique_ptr<V8DebuggerScript> V8DebuggerScript::CreateWasm( |
281 v8::Isolate* isolate, WasmTranslation* wasmTranslation, | 300 v8::Isolate* isolate, WasmTranslation* wasmTranslation, |
282 v8::Local<v8::debug::WasmScript> underlyingScript, String16 id, | 301 v8::Local<v8::debug::WasmScript> underlyingScript, String16 id, |
283 String16 url, String16 source) { | 302 String16 url, String16 source) { |
284 return std::unique_ptr<WasmVirtualScript>( | 303 return std::unique_ptr<WasmVirtualScript>( |
285 new WasmVirtualScript(isolate, wasmTranslation, underlyingScript, | 304 new WasmVirtualScript(isolate, wasmTranslation, underlyingScript, |
286 std::move(id), std::move(url), std::move(source))); | 305 std::move(id), std::move(url), std::move(source))); |
287 } | 306 } |
288 | 307 |
289 V8DebuggerScript::V8DebuggerScript(v8::Isolate* isolate, String16 id, | 308 V8DebuggerScript::V8DebuggerScript(v8::Isolate* isolate, String16 id, |
290 String16 url) | 309 String16 url) |
291 : m_id(std::move(id)), m_url(std::move(url)), m_isolate(isolate) {} | 310 : m_id(std::move(id)), m_url(std::move(url)), m_isolate(isolate) {} |
292 | 311 |
293 V8DebuggerScript::~V8DebuggerScript() {} | 312 V8DebuggerScript::~V8DebuggerScript() {} |
294 | 313 |
295 const String16& V8DebuggerScript::sourceURL() const { | 314 const String16& V8DebuggerScript::sourceURL() const { |
296 return m_sourceURL.isEmpty() ? m_url : m_sourceURL; | 315 return m_sourceURL.isEmpty() ? m_url : m_sourceURL; |
297 } | 316 } |
298 | 317 |
299 const String16& V8DebuggerScript::hash(v8::Isolate* isolate) const { | 318 const String16& V8DebuggerScript::hash() const { |
300 if (m_hash.isEmpty()) m_hash = calculateHash(source(isolate)); | 319 if (m_hash.isEmpty()) m_hash = calculateHash(source()); |
301 DCHECK(!m_hash.isEmpty()); | 320 DCHECK(!m_hash.isEmpty()); |
302 return m_hash; | 321 return m_hash; |
303 } | 322 } |
304 | 323 |
305 void V8DebuggerScript::setSourceURL(const String16& sourceURL) { | 324 void V8DebuggerScript::setSourceURL(const String16& sourceURL) { |
306 m_sourceURL = sourceURL; | 325 m_sourceURL = sourceURL; |
307 } | 326 } |
308 | 327 |
328 String16 V8DebuggerScript::lineAt(int lineNumberWithOffset, int maxLength) { | |
329 String16 sourceStr = source(); | |
330 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
| |
331 int lineNumber = lineNumberWithOffset - m_startLine; | |
332 if (lineNumber >= static_cast<int>(m_lineEndings->size()) || lineNumber < 0) | |
333 return String16(); | |
334 size_t lineEnd = m_lineEndings->at(lineNumber); | |
335 size_t lineStart = lineNumber > 0 ? m_lineEndings->at(lineNumber - 1) + 1 : 0; | |
336 String16 line = sourceStr.substring(lineStart, lineEnd - lineStart); | |
337 if (line.length() && line[line.length() - 1] == '\r') | |
338 line = line.substring(0, line.length() - 1); | |
339 if (maxLength) line = line.substring(0, maxLength); | |
340 return line; | |
341 } | |
342 | |
309 } // namespace v8_inspector | 343 } // namespace v8_inspector |
OLD | NEW |