2020-08-28 14:41:56 +00:00
|
|
|
// Copyright 2020 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2020-11-03 08:01:33 +00:00
|
|
|
import {FocusEvent, SelectionEvent} from './events.mjs';
|
|
|
|
import {delay, DOM, formatBytes, V8CustomElement} from './helper.mjs';
|
|
|
|
import {IcLogEntry} from './log/ic.mjs';
|
|
|
|
import {MapLogEntry} from './log/map.mjs';
|
|
|
|
|
|
|
|
DOM.defineCustomElement('source-panel',
|
|
|
|
(templateText) =>
|
|
|
|
class SourcePanel extends V8CustomElement {
|
|
|
|
_selectedSourcePositions = [];
|
|
|
|
_sourcePositionsToMarkNodes;
|
|
|
|
_scripts = [];
|
|
|
|
_script;
|
|
|
|
constructor() {
|
|
|
|
super(templateText);
|
|
|
|
this.scriptDropdown.addEventListener(
|
|
|
|
'change', e => this._handleSelectScript(e));
|
|
|
|
}
|
2020-11-02 09:27:28 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get script() {
|
|
|
|
return this.$('#script');
|
|
|
|
}
|
2020-11-02 09:27:28 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get scriptNode() {
|
|
|
|
return this.$('.scriptNode');
|
|
|
|
}
|
2020-11-02 09:27:28 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
set script(script) {
|
|
|
|
if (this._script === script) return;
|
|
|
|
this._script = script;
|
|
|
|
this._renderSourcePanel();
|
|
|
|
this._updateScriptDropdownSelection();
|
|
|
|
}
|
2020-11-02 09:27:28 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
set selectedSourcePositions(sourcePositions) {
|
|
|
|
this._selectedSourcePositions = sourcePositions;
|
|
|
|
// TODO: highlight multiple scripts
|
|
|
|
this.script = sourcePositions[0]?.script;
|
|
|
|
this._focusSelectedMarkers();
|
|
|
|
}
|
2020-11-02 09:27:28 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
set data(scripts) {
|
|
|
|
this._scripts = scripts;
|
|
|
|
this._initializeScriptDropdown();
|
|
|
|
}
|
2020-11-02 09:27:28 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
get scriptDropdown() {
|
|
|
|
return this.$('#script-dropdown');
|
|
|
|
}
|
2020-11-02 09:27:28 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
_initializeScriptDropdown() {
|
|
|
|
this._scripts.sort((a, b) => a.name.localeCompare(b.name));
|
|
|
|
let select = this.scriptDropdown;
|
|
|
|
select.options.length = 0;
|
|
|
|
for (const script of this._scripts) {
|
|
|
|
const option = document.createElement('option');
|
|
|
|
const size = formatBytes(script.source.length);
|
|
|
|
option.text = `${script.name} (id=${script.id} size=${size})`;
|
|
|
|
option.script = script;
|
|
|
|
select.add(option);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_updateScriptDropdownSelection() {
|
|
|
|
this.scriptDropdown.selectedIndex =
|
|
|
|
this._script ? this._scripts.indexOf(this._script) : -1;
|
|
|
|
}
|
2020-09-23 10:30:17 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
async _renderSourcePanel() {
|
|
|
|
let scriptNode;
|
|
|
|
if (this._script) {
|
|
|
|
await delay(1);
|
|
|
|
const builder =
|
|
|
|
new LineBuilder(this, this._script, this._selectedSourcePositions);
|
|
|
|
scriptNode = builder.createScriptNode();
|
|
|
|
this._sourcePositionsToMarkNodes = builder.sourcePositionToMarkers;
|
|
|
|
} else {
|
|
|
|
scriptNode = document.createElement('pre');
|
|
|
|
this._selectedMarkNodes = undefined;
|
|
|
|
}
|
|
|
|
const oldScriptNode = this.script.childNodes[1];
|
|
|
|
this.script.replaceChild(scriptNode, oldScriptNode);
|
|
|
|
}
|
2020-09-23 10:30:17 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
async _focusSelectedMarkers() {
|
|
|
|
await delay(100);
|
|
|
|
// Remove all marked nodes.
|
|
|
|
for (let markNode of this._sourcePositionsToMarkNodes.values()) {
|
|
|
|
markNode.className = '';
|
|
|
|
}
|
|
|
|
for (let sourcePosition of this._selectedSourcePositions) {
|
|
|
|
this._sourcePositionsToMarkNodes.get(sourcePosition).className = 'marked';
|
|
|
|
}
|
|
|
|
const sourcePosition = this._selectedSourcePositions[0];
|
|
|
|
if (!sourcePosition) return;
|
|
|
|
const markNode = this._sourcePositionsToMarkNodes.get(sourcePosition);
|
|
|
|
markNode.scrollIntoView(
|
|
|
|
{behavior: 'smooth', block: 'nearest', inline: 'center'});
|
|
|
|
}
|
2020-11-02 09:27:28 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
_handleSelectScript(e) {
|
|
|
|
const option =
|
|
|
|
this.scriptDropdown.options[this.scriptDropdown.selectedIndex];
|
|
|
|
this.script = option.script;
|
|
|
|
this.selectLogEntries(this._script.entries());
|
|
|
|
}
|
2020-09-23 10:30:17 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
handleSourcePositionClick(e) {
|
|
|
|
this.selectLogEntries(e.target.sourcePosition.entries)
|
|
|
|
}
|
2020-11-02 09:27:28 +00:00
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
selectLogEntries(logEntries) {
|
|
|
|
let icLogEntries = [];
|
|
|
|
let mapLogEntries = [];
|
|
|
|
for (const entry of logEntries) {
|
|
|
|
if (entry instanceof MapLogEntry) {
|
|
|
|
mapLogEntries.push(entry);
|
|
|
|
} else if (entry instanceof IcLogEntry) {
|
|
|
|
icLogEntries.push(entry);
|
2020-08-28 14:41:56 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-03 08:01:33 +00:00
|
|
|
if (icLogEntries.length > 0) {
|
|
|
|
this.dispatchEvent(new SelectionEvent(icLogEntries));
|
|
|
|
}
|
|
|
|
if (mapLogEntries.length > 0) {
|
|
|
|
this.dispatchEvent(new SelectionEvent(mapLogEntries));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-09-23 10:30:17 +00:00
|
|
|
|
|
|
|
class SourcePositionIterator {
|
2020-10-19 10:45:42 +00:00
|
|
|
_entries;
|
|
|
|
_index = 0;
|
2020-09-23 10:30:17 +00:00
|
|
|
constructor(sourcePositions) {
|
2020-10-19 10:45:42 +00:00
|
|
|
this._entries = sourcePositions;
|
2020-09-23 10:30:17 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
* forLine(lineIndex) {
|
2020-11-02 09:27:28 +00:00
|
|
|
this._findStart(lineIndex);
|
2020-11-03 08:01:33 +00:00
|
|
|
while (!this._done() && this._current().line === lineIndex) {
|
2020-10-19 10:45:42 +00:00
|
|
|
yield this._current();
|
|
|
|
this._next();
|
2020-09-23 10:30:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 09:27:28 +00:00
|
|
|
_findStart(lineIndex) {
|
2020-11-03 08:01:33 +00:00
|
|
|
while (!this._done() && this._current().line < lineIndex) {
|
2020-11-02 09:27:28 +00:00
|
|
|
this._next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-19 10:45:42 +00:00
|
|
|
_current() {
|
|
|
|
return this._entries[this._index];
|
2020-09-23 10:30:17 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 10:45:42 +00:00
|
|
|
_done() {
|
|
|
|
return this._index + 1 >= this._entries.length;
|
2020-09-23 10:30:17 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 10:45:42 +00:00
|
|
|
_next() {
|
|
|
|
this._index++;
|
2020-09-23 10:30:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 08:01:33 +00:00
|
|
|
function* lineIterator(source) {
|
2020-09-23 10:30:17 +00:00
|
|
|
let current = 0;
|
|
|
|
let line = 1;
|
2020-11-03 08:01:33 +00:00
|
|
|
while (current < source.length) {
|
|
|
|
const next = source.indexOf('\n', current);
|
2020-09-23 10:30:17 +00:00
|
|
|
if (next === -1) break;
|
|
|
|
yield [line, source.substring(current, next)];
|
|
|
|
line++;
|
|
|
|
current = next + 1;
|
|
|
|
}
|
|
|
|
if (current < source.length) yield [line, source.substring(current)];
|
|
|
|
}
|
|
|
|
|
|
|
|
class LineBuilder {
|
2020-11-02 09:27:28 +00:00
|
|
|
_script;
|
|
|
|
_clickHandler;
|
|
|
|
_sourcePositions;
|
|
|
|
_selection;
|
|
|
|
_sourcePositionToMarkers = new Map();
|
2020-09-23 10:30:17 +00:00
|
|
|
|
2020-11-02 09:27:28 +00:00
|
|
|
constructor(panel, script, highlightPositions) {
|
2020-10-19 10:45:42 +00:00
|
|
|
this._script = script;
|
2020-11-02 09:27:28 +00:00
|
|
|
this._selection = new Set(highlightPositions);
|
2020-10-19 10:45:42 +00:00
|
|
|
this._clickHandler = panel.handleSourcePositionClick.bind(panel);
|
2020-09-23 10:30:17 +00:00
|
|
|
// TODO: sort on script finalization.
|
2020-11-10 11:47:40 +00:00
|
|
|
script.sourcePositions.sort((a, b) => {
|
|
|
|
if (a.line === b.line) return a.column - b.column;
|
|
|
|
return a.line - b.line;
|
|
|
|
});
|
|
|
|
this._sourcePositions = new SourcePositionIterator(script.sourcePositions);
|
2020-11-02 09:27:28 +00:00
|
|
|
}
|
2020-09-23 10:30:17 +00:00
|
|
|
|
2020-11-02 09:27:28 +00:00
|
|
|
get sourcePositionToMarkers() {
|
|
|
|
return this._sourcePositionToMarkers;
|
2020-09-23 10:30:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
createScriptNode() {
|
2020-11-03 08:01:33 +00:00
|
|
|
const scriptNode = document.createElement('pre');
|
2020-09-23 10:30:17 +00:00
|
|
|
scriptNode.classList.add('scriptNode');
|
2020-10-19 10:45:42 +00:00
|
|
|
for (let [lineIndex, line] of lineIterator(this._script.source)) {
|
|
|
|
scriptNode.appendChild(this._createLineNode(lineIndex, line));
|
2020-09-23 10:30:17 +00:00
|
|
|
}
|
|
|
|
return scriptNode;
|
|
|
|
}
|
|
|
|
|
2020-10-19 10:45:42 +00:00
|
|
|
_createLineNode(lineIndex, line) {
|
2020-11-03 08:01:33 +00:00
|
|
|
const lineNode = document.createElement('span');
|
|
|
|
let columnIndex = 0;
|
2020-10-19 10:45:42 +00:00
|
|
|
for (const sourcePosition of this._sourcePositions.forLine(lineIndex)) {
|
2020-09-23 10:30:17 +00:00
|
|
|
const nextColumnIndex = sourcePosition.column - 1;
|
2020-11-03 08:01:33 +00:00
|
|
|
lineNode.appendChild(document.createTextNode(
|
|
|
|
line.substring(columnIndex, nextColumnIndex)));
|
2020-09-23 10:30:17 +00:00
|
|
|
columnIndex = nextColumnIndex;
|
|
|
|
|
|
|
|
lineNode.appendChild(
|
2020-10-19 10:45:42 +00:00
|
|
|
this._createMarkerNode(line[columnIndex], sourcePosition));
|
2020-09-23 10:30:17 +00:00
|
|
|
columnIndex++;
|
|
|
|
}
|
|
|
|
lineNode.appendChild(
|
2020-11-03 08:01:33 +00:00
|
|
|
document.createTextNode(line.substring(columnIndex) + '\n'));
|
2020-09-23 10:30:17 +00:00
|
|
|
return lineNode;
|
|
|
|
}
|
|
|
|
|
2020-10-19 10:45:42 +00:00
|
|
|
_createMarkerNode(text, sourcePosition) {
|
2020-11-03 08:01:33 +00:00
|
|
|
const marker = document.createElement('mark');
|
2020-11-02 09:27:28 +00:00
|
|
|
this._sourcePositionToMarkers.set(sourcePosition, marker);
|
2020-09-23 10:30:17 +00:00
|
|
|
marker.textContent = text;
|
|
|
|
marker.sourcePosition = sourcePosition;
|
2020-10-19 10:45:42 +00:00
|
|
|
marker.onclick = this._clickHandler;
|
2020-09-23 10:30:17 +00:00
|
|
|
return marker;
|
|
|
|
}
|
|
|
|
}
|