summaryrefslogtreecommitdiff
path: root/chromium/v8/src/inspector/wasm-translation.cc
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@qt.io>2018-05-15 10:20:33 +0200
committerAllan Sandfeld Jensen <allan.jensen@qt.io>2018-05-15 10:28:57 +0000
commitd17ea114e5ef69ad5d5d7413280a13e6428098aa (patch)
tree2c01a75df69f30d27b1432467cfe7c1467a498da /chromium/v8/src/inspector/wasm-translation.cc
parent8c5c43c7b138c9b4b0bf56d946e61d3bbc111bec (diff)
downloadqtwebengine-chromium-d17ea114e5ef69ad5d5d7413280a13e6428098aa.tar.gz
BASELINE: Update Chromium to 67.0.3396.47
Change-Id: Idcb1341782e417561a2473eeecc82642dafda5b7 Reviewed-by: Michal Klocek <michal.klocek@qt.io>
Diffstat (limited to 'chromium/v8/src/inspector/wasm-translation.cc')
-rw-r--r--chromium/v8/src/inspector/wasm-translation.cc207
1 files changed, 130 insertions, 77 deletions
diff --git a/chromium/v8/src/inspector/wasm-translation.cc b/chromium/v8/src/inspector/wasm-translation.cc
index 431573d842f..1982f4932ab 100644
--- a/chromium/v8/src/inspector/wasm-translation.cc
+++ b/chromium/v8/src/inspector/wasm-translation.cc
@@ -5,6 +5,7 @@
#include "src/inspector/wasm-translation.h"
#include <algorithm>
+#include <utility>
#include "src/debug/debug-interface.h"
#include "src/inspector/string-util.h"
@@ -15,6 +16,42 @@
namespace v8_inspector {
+using OffsetTable = v8::debug::WasmDisassembly::OffsetTable;
+
+struct WasmSourceInformation {
+ String16 source;
+ int end_line = 0;
+ int end_column = 0;
+
+ OffsetTable offset_table;
+ OffsetTable reverse_offset_table;
+
+ WasmSourceInformation(String16 source, OffsetTable offset_table)
+ : source(std::move(source)), offset_table(std::move(offset_table)) {
+ int num_lines = 0;
+ int last_newline = -1;
+ size_t next_newline = this->source.find('\n', last_newline + 1);
+ while (next_newline != String16::kNotFound) {
+ last_newline = static_cast<int>(next_newline);
+ next_newline = this->source.find('\n', last_newline + 1);
+ ++num_lines;
+ }
+ end_line = num_lines;
+ end_column = static_cast<int>(this->source.length()) - last_newline - 1;
+
+ reverse_offset_table = this->offset_table;
+ // Order by line, column, then byte offset.
+ auto cmp = [](OffsetTable::value_type el1, OffsetTable::value_type el2) {
+ if (el1.line != el2.line) return el1.line < el2.line;
+ if (el1.column != el2.column) return el1.column < el2.column;
+ return el1.byte_offset < el2.byte_offset;
+ };
+ std::sort(reverse_offset_table.begin(), reverse_offset_table.end(), cmp);
+ }
+
+ WasmSourceInformation() = default;
+};
+
class WasmTranslation::TranslatorImpl {
public:
struct TransLocation {
@@ -33,6 +70,10 @@ class WasmTranslation::TranslatorImpl {
virtual void Init(v8::Isolate*, WasmTranslation*, V8DebuggerAgentImpl*) = 0;
virtual void Translate(TransLocation*) = 0;
virtual void TranslateBack(TransLocation*) = 0;
+ virtual const WasmSourceInformation& GetSourceInformation(v8::Isolate*,
+ int index) = 0;
+ virtual const String16 GetHash(v8::Isolate*, int index) = 0;
+
virtual ~TranslatorImpl() {}
class RawTranslator;
@@ -42,14 +83,22 @@ class WasmTranslation::TranslatorImpl {
class WasmTranslation::TranslatorImpl::RawTranslator
: public WasmTranslation::TranslatorImpl {
public:
- void Init(v8::Isolate*, WasmTranslation*, V8DebuggerAgentImpl*) {}
- void Translate(TransLocation*) {}
- void TranslateBack(TransLocation*) {}
+ void Init(v8::Isolate*, WasmTranslation*, V8DebuggerAgentImpl*) override {}
+ void Translate(TransLocation*) override {}
+ void TranslateBack(TransLocation*) override {}
+ const WasmSourceInformation& GetSourceInformation(v8::Isolate*,
+ int index) override {
+ static const WasmSourceInformation singleEmptySourceInformation;
+ return singleEmptySourceInformation;
+ }
+ const String16 GetHash(v8::Isolate*, int index) override {
+ // TODO(herhut): Find useful hash default value.
+ return String16();
+ }
};
class WasmTranslation::TranslatorImpl::DisassemblingTranslator
: public WasmTranslation::TranslatorImpl {
- using OffsetTable = v8::debug::WasmDisassembly::OffsetTable;
public:
DisassemblingTranslator(v8::Isolate* isolate,
@@ -99,48 +148,36 @@ class WasmTranslation::TranslatorImpl::DisassemblingTranslator
}
}
+ static bool LessThan(const v8::debug::WasmDisassemblyOffsetTableEntry& entry,
+ const TransLocation& loc) {
+ return entry.line < loc.line ||
+ (entry.line == loc.line && entry.column < loc.column);
+ }
+
void TranslateBack(TransLocation* loc) override {
- int func_index = GetFunctionIndexFromFakeScriptId(loc->script_id);
- const OffsetTable* reverse_table = GetReverseTable(func_index);
- if (!reverse_table) return;
- DCHECK(!reverse_table->empty());
v8::Isolate* isolate = loc->translation->isolate_;
+ int func_index = GetFunctionIndexFromFakeScriptId(loc->script_id);
+ const OffsetTable& reverse_table = GetReverseTable(isolate, func_index);
+ if (reverse_table.empty()) return;
// Binary search for the given line and column.
- unsigned left = 0; // inclusive
- unsigned right = static_cast<unsigned>(reverse_table->size()); // exclusive
- while (right - left > 1) {
- unsigned mid = (left + right) / 2;
- auto& entry = (*reverse_table)[mid];
- if (entry.line < loc->line ||
- (entry.line == loc->line && entry.column <= loc->column)) {
- left = mid;
- } else {
- right = mid;
- }
- }
+ auto element = std::lower_bound(reverse_table.begin(), reverse_table.end(),
+ *loc, LessThan);
int found_byte_offset = 0;
- // [left] is <= <line,column>, or left==0 and [0] > <line,column>.
- // We are searching for the smallest entry >= <line,column> which is still
- // on the same line. This must be either [left] or [left + 1].
- // If we don't find such an entry, we might have hit the special case of
- // pointing after the last line, which is translated to the end of the
- // function (one byte after the last function byte).
- if ((*reverse_table)[left].line == loc->line &&
- (*reverse_table)[left].column >= loc->column) {
- found_byte_offset = (*reverse_table)[left].byte_offset;
- } else if (left + 1 < reverse_table->size() &&
- (*reverse_table)[left + 1].line == loc->line &&
- (*reverse_table)[left + 1].column >= loc->column) {
- found_byte_offset = (*reverse_table)[left + 1].byte_offset;
- } else if (left == reverse_table->size() - 1 &&
- (*reverse_table)[left].line == loc->line - 1 &&
- loc->column == 0) {
+ // We want an entry on the same line if possible.
+ if (element == reverse_table.end()) {
+ // We did not find an element, so this points after the function.
std::pair<int, int> func_range =
script_.Get(isolate)->GetFunctionRange(func_index);
DCHECK_LE(func_range.first, func_range.second);
found_byte_offset = func_range.second - func_range.first;
+ } else if (element->line == loc->line || element == reverse_table.begin()) {
+ found_byte_offset = element->byte_offset;
+ } else {
+ auto prev = element - 1;
+ DCHECK(prev->line == loc->line);
+ found_byte_offset = prev->byte_offset;
}
loc->script_id = String16::fromInteger(script_.Get(isolate)->Id());
@@ -148,6 +185,31 @@ class WasmTranslation::TranslatorImpl::DisassemblingTranslator
loc->column = found_byte_offset;
}
+ const WasmSourceInformation& GetSourceInformation(v8::Isolate* isolate,
+ int index) override {
+ auto it = source_informations_.find(index);
+ if (it != source_informations_.end()) return it->second;
+ v8::HandleScope scope(isolate);
+ v8::Local<v8::debug::WasmScript> script = script_.Get(isolate);
+ v8::debug::WasmDisassembly disassembly = script->DisassembleFunction(index);
+
+ auto inserted = source_informations_.insert(std::make_pair(
+ index, WasmSourceInformation({disassembly.disassembly.data(),
+ disassembly.disassembly.length()},
+ std::move(disassembly.offset_table))));
+ DCHECK(inserted.second);
+ return inserted.first->second;
+ }
+
+ const String16 GetHash(v8::Isolate* isolate, int index) override {
+ v8::HandleScope scope(isolate);
+ v8::Local<v8::debug::WasmScript> script = script_.Get(isolate);
+ uint32_t hash = script->GetFunctionHash(index);
+ String16Builder builder;
+ builder.appendUnsignedAsHex(hash);
+ return builder.toString();
+ }
+
private:
String16 GetFakeScriptUrl(v8::Isolate* isolate, int func_index) {
v8::Local<v8::debug::WasmScript> script = script_.Get(isolate);
@@ -182,20 +244,10 @@ class WasmTranslation::TranslatorImpl::DisassemblingTranslator
String16 fake_script_id = GetFakeScriptId(underlyingScriptId, func_idx);
String16 fake_script_url = GetFakeScriptUrl(isolate, func_idx);
- v8::Local<v8::debug::WasmScript> script = script_.Get(isolate);
- // TODO(clemensh): Generate disassembly lazily when queried by the frontend.
- v8::debug::WasmDisassembly disassembly =
- script->DisassembleFunction(func_idx);
-
- DCHECK_EQ(0, offset_tables_.count(func_idx));
- offset_tables_.insert(
- std::make_pair(func_idx, std::move(disassembly.offset_table)));
- String16 source(disassembly.disassembly.data(),
- disassembly.disassembly.length());
std::unique_ptr<V8DebuggerScript> fake_script =
- V8DebuggerScript::CreateWasm(isolate, translation, script,
+ V8DebuggerScript::CreateWasm(isolate, translation, script_.Get(isolate),
fake_script_id, std::move(fake_script_url),
- source);
+ func_idx);
translation->AddFakeScript(fake_script->scriptId(), this);
agent->didParseSource(std::move(fake_script), true);
@@ -212,42 +264,19 @@ class WasmTranslation::TranslatorImpl::DisassemblingTranslator
const OffsetTable& GetOffsetTable(const TransLocation* loc) {
int func_index = loc->line;
- auto it = offset_tables_.find(func_index);
- // TODO(clemensh): Once we load disassembly lazily, the offset table
- // might not be there yet. Load it lazily then.
- DCHECK(it != offset_tables_.end());
- return it->second;
+ return GetSourceInformation(loc->translation->isolate_, func_index)
+ .offset_table;
}
- const OffsetTable* GetReverseTable(int func_index) {
- auto it = reverse_tables_.find(func_index);
- if (it != reverse_tables_.end()) return &it->second;
-
- // Find offset table, copy and sort it to get reverse table.
- it = offset_tables_.find(func_index);
- if (it == offset_tables_.end()) return nullptr;
-
- OffsetTable reverse_table = it->second;
- // Order by line, column, then byte offset.
- auto cmp = [](OffsetTable::value_type el1, OffsetTable::value_type el2) {
- if (el1.line != el2.line) return el1.line < el2.line;
- if (el1.column != el2.column) return el1.column < el2.column;
- return el1.byte_offset < el2.byte_offset;
- };
- std::sort(reverse_table.begin(), reverse_table.end(), cmp);
-
- auto inserted = reverse_tables_.insert(
- std::make_pair(func_index, std::move(reverse_table)));
- DCHECK(inserted.second);
- return &inserted.first->second;
+ const OffsetTable& GetReverseTable(v8::Isolate* isolate, int func_index) {
+ return GetSourceInformation(isolate, func_index).reverse_offset_table;
}
v8::Global<v8::debug::WasmScript> script_;
// We assume to only disassemble a subset of the functions, so store them in a
// map instead of an array.
- std::unordered_map<int, const OffsetTable> offset_tables_;
- std::unordered_map<int, const OffsetTable> reverse_tables_;
+ std::unordered_map<int, WasmSourceInformation> source_informations_;
};
WasmTranslation::WasmTranslation(v8::Isolate* isolate)
@@ -280,6 +309,31 @@ void WasmTranslation::Clear() {
fake_scripts_.clear();
}
+const String16& WasmTranslation::GetSource(const String16& script_id,
+ int func_index) {
+ auto it = fake_scripts_.find(script_id);
+ DCHECK_NE(it, fake_scripts_.end());
+ return it->second->GetSourceInformation(isolate_, func_index).source;
+}
+
+int WasmTranslation::GetEndLine(const String16& script_id, int func_index) {
+ auto it = fake_scripts_.find(script_id);
+ DCHECK_NE(it, fake_scripts_.end());
+ return it->second->GetSourceInformation(isolate_, func_index).end_line;
+}
+
+int WasmTranslation::GetEndColumn(const String16& script_id, int func_index) {
+ auto it = fake_scripts_.find(script_id);
+ DCHECK_NE(it, fake_scripts_.end());
+ return it->second->GetSourceInformation(isolate_, func_index).end_column;
+}
+
+String16 WasmTranslation::GetHash(const String16& script_id, int func_index) {
+ auto it = fake_scripts_.find(script_id);
+ DCHECK_NE(it, fake_scripts_.end());
+ return it->second->GetHash(isolate_, func_index);
+}
+
// Translation "forward" (to artificial scripts).
bool WasmTranslation::TranslateWasmScriptLocationToProtocolLocation(
String16* script_id, int* line_number, int* column_number) {
@@ -326,5 +380,4 @@ void WasmTranslation::AddFakeScript(const String16& scriptId,
DCHECK_EQ(0, fake_scripts_.count(scriptId));
fake_scripts_.insert(std::make_pair(scriptId, translator));
}
-
} // namespace v8_inspector