11cb0ef41Sopenharmony_ci// Copyright 2017 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciimport { SourceResolver } from "../src/source-resolver";
61cb0ef41Sopenharmony_ciimport { SelectionBroker } from "../src/selection-broker";
71cb0ef41Sopenharmony_ciimport { DisassemblyView } from "../src/disassembly-view";
81cb0ef41Sopenharmony_ciimport { GraphMultiView } from "../src/graphmultiview";
91cb0ef41Sopenharmony_ciimport { CodeMode, CodeView } from "../src/code-view";
101cb0ef41Sopenharmony_ciimport { Tabs } from "../src/tabs";
111cb0ef41Sopenharmony_ciimport { Resizer } from "../src/resizer";
121cb0ef41Sopenharmony_ciimport * as C from "../src/constants";
131cb0ef41Sopenharmony_ciimport { InfoView } from "./info-view";
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciwindow.onload = function () {
161cb0ef41Sopenharmony_ci  let multiview: GraphMultiView = null;
171cb0ef41Sopenharmony_ci  let disassemblyView: DisassemblyView = null;
181cb0ef41Sopenharmony_ci  let sourceViews: Array<CodeView> = [];
191cb0ef41Sopenharmony_ci  let selectionBroker: SelectionBroker = null;
201cb0ef41Sopenharmony_ci  let sourceResolver: SourceResolver = null;
211cb0ef41Sopenharmony_ci  const resizer = new Resizer(panesUpdatedCallback, 75, 75);
221cb0ef41Sopenharmony_ci  const sourceTabsContainer = document.getElementById(C.SOURCE_PANE_ID);
231cb0ef41Sopenharmony_ci  const sourceTabs = new Tabs(sourceTabsContainer);
241cb0ef41Sopenharmony_ci  sourceTabs.addTab("&#x2b;").classList.add("last-tab", "persistent-tab");
251cb0ef41Sopenharmony_ci  const disassemblyTabsContainer = document.getElementById(C.GENERATED_PANE_ID);
261cb0ef41Sopenharmony_ci  const disassemblyTabs = new Tabs(disassemblyTabsContainer);
271cb0ef41Sopenharmony_ci  disassemblyTabs.addTab("&#x2b;").classList.add("last-tab", "persistent-tab");
281cb0ef41Sopenharmony_ci  const [infoTab, infoContainer] = sourceTabs.addTabAndContent("Info");
291cb0ef41Sopenharmony_ci  infoTab.classList.add("persistent-tab");
301cb0ef41Sopenharmony_ci  infoContainer.classList.add("viewpane", "scrollable");
311cb0ef41Sopenharmony_ci  const infoView = new InfoView(infoContainer);
321cb0ef41Sopenharmony_ci  infoView.show();
331cb0ef41Sopenharmony_ci  sourceTabs.activateTab(infoTab);
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  function panesUpdatedCallback() {
361cb0ef41Sopenharmony_ci    if (multiview) multiview.onresize();
371cb0ef41Sopenharmony_ci  }
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci  function loadFile(txtRes: string) {
401cb0ef41Sopenharmony_ci    sourceTabs.clearTabsAndContent();
411cb0ef41Sopenharmony_ci    disassemblyTabs.clearTabsAndContent();
421cb0ef41Sopenharmony_ci    // If the JSON isn't properly terminated, assume compiler crashed and
431cb0ef41Sopenharmony_ci    // add best-guess empty termination
441cb0ef41Sopenharmony_ci    if (txtRes[txtRes.length - 2] == ',') {
451cb0ef41Sopenharmony_ci      txtRes += '{"name":"disassembly","type":"disassembly","data":""}]}';
461cb0ef41Sopenharmony_ci    }
471cb0ef41Sopenharmony_ci    try {
481cb0ef41Sopenharmony_ci      sourceViews.forEach(sv => sv.hide());
491cb0ef41Sopenharmony_ci      if (multiview) multiview.hide();
501cb0ef41Sopenharmony_ci      multiview = null;
511cb0ef41Sopenharmony_ci      document.getElementById("ranges").innerHTML = '';
521cb0ef41Sopenharmony_ci      document.getElementById('ranges').style.visibility = "hidden";
531cb0ef41Sopenharmony_ci      document.getElementById('show-hide-ranges').style.visibility = "hidden";
541cb0ef41Sopenharmony_ci      if (disassemblyView) disassemblyView.hide();
551cb0ef41Sopenharmony_ci      sourceViews = [];
561cb0ef41Sopenharmony_ci      sourceResolver = new SourceResolver();
571cb0ef41Sopenharmony_ci      selectionBroker = new SelectionBroker(sourceResolver);
581cb0ef41Sopenharmony_ci
591cb0ef41Sopenharmony_ci      const jsonObj = JSON.parse(txtRes);
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci      let fnc = null;
621cb0ef41Sopenharmony_ci      // Backwards compatibility.
631cb0ef41Sopenharmony_ci      if (typeof jsonObj.function == 'string') {
641cb0ef41Sopenharmony_ci        fnc = {
651cb0ef41Sopenharmony_ci          functionName: fnc,
661cb0ef41Sopenharmony_ci          sourceId: -1,
671cb0ef41Sopenharmony_ci          startPosition: jsonObj.sourcePosition,
681cb0ef41Sopenharmony_ci          endPosition: jsonObj.sourcePosition + jsonObj.source.length,
691cb0ef41Sopenharmony_ci          sourceText: jsonObj.source,
701cb0ef41Sopenharmony_ci          backwardsCompatibility: true
711cb0ef41Sopenharmony_ci        };
721cb0ef41Sopenharmony_ci      } else {
731cb0ef41Sopenharmony_ci        fnc = Object.assign(jsonObj.function, { backwardsCompatibility: false });
741cb0ef41Sopenharmony_ci      }
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci      sourceResolver.setInlinings(jsonObj.inlinings);
771cb0ef41Sopenharmony_ci      sourceResolver.setSourceLineToBytecodePosition(jsonObj.sourceLineToBytecodePosition);
781cb0ef41Sopenharmony_ci      sourceResolver.setSources(jsonObj.sources, fnc);
791cb0ef41Sopenharmony_ci      sourceResolver.setNodePositionMap(jsonObj.nodePositions);
801cb0ef41Sopenharmony_ci      sourceResolver.parsePhases(jsonObj.phases);
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_ci      const [sourceTab, sourceContainer] = sourceTabs.addTabAndContent("Source");
831cb0ef41Sopenharmony_ci      sourceContainer.classList.add("viewpane", "scrollable");
841cb0ef41Sopenharmony_ci      sourceTabs.activateTab(sourceTab);
851cb0ef41Sopenharmony_ci      const sourceView = new CodeView(sourceContainer, selectionBroker, sourceResolver, fnc, CodeMode.MAIN_SOURCE);
861cb0ef41Sopenharmony_ci      sourceView.show();
871cb0ef41Sopenharmony_ci      sourceViews.push(sourceView);
881cb0ef41Sopenharmony_ci
891cb0ef41Sopenharmony_ci      sourceResolver.forEachSource(source => {
901cb0ef41Sopenharmony_ci        const sourceView = new CodeView(sourceContainer, selectionBroker, sourceResolver, source, CodeMode.INLINED_SOURCE);
911cb0ef41Sopenharmony_ci        sourceView.show();
921cb0ef41Sopenharmony_ci        sourceViews.push(sourceView);
931cb0ef41Sopenharmony_ci      });
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci      const [disassemblyTab, disassemblyContainer] = disassemblyTabs.addTabAndContent("Disassembly");
961cb0ef41Sopenharmony_ci      disassemblyContainer.classList.add("viewpane", "scrollable");
971cb0ef41Sopenharmony_ci      disassemblyTabs.activateTab(disassemblyTab);
981cb0ef41Sopenharmony_ci      disassemblyView = new DisassemblyView(disassemblyContainer, selectionBroker);
991cb0ef41Sopenharmony_ci      disassemblyView.initializeCode(fnc.sourceText);
1001cb0ef41Sopenharmony_ci      if (sourceResolver.disassemblyPhase) {
1011cb0ef41Sopenharmony_ci        disassemblyView.initializePerfProfile(jsonObj.eventCounts);
1021cb0ef41Sopenharmony_ci        disassemblyView.showContent(sourceResolver.disassemblyPhase.data);
1031cb0ef41Sopenharmony_ci        disassemblyView.show();
1041cb0ef41Sopenharmony_ci      }
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci      multiview = new GraphMultiView(C.INTERMEDIATE_PANE_ID, selectionBroker, sourceResolver);
1071cb0ef41Sopenharmony_ci      multiview.show();
1081cb0ef41Sopenharmony_ci    } catch (err) {
1091cb0ef41Sopenharmony_ci      if (window.confirm("Error: Exception during load of TurboFan JSON file:\n" +
1101cb0ef41Sopenharmony_ci        "error: " + err.message + "\nDo you want to clear session storage?")) {
1111cb0ef41Sopenharmony_ci        window.sessionStorage.clear();
1121cb0ef41Sopenharmony_ci      }
1131cb0ef41Sopenharmony_ci      return;
1141cb0ef41Sopenharmony_ci    }
1151cb0ef41Sopenharmony_ci  }
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci  function initializeUploadHandlers() {
1181cb0ef41Sopenharmony_ci    // The <input> form #upload-helper with type file can't be a picture.
1191cb0ef41Sopenharmony_ci    // We hence keep it hidden, and forward the click from the picture
1201cb0ef41Sopenharmony_ci    // button #upload.
1211cb0ef41Sopenharmony_ci    document.getElementById("upload").addEventListener("click", e => {
1221cb0ef41Sopenharmony_ci      document.getElementById("upload-helper").click();
1231cb0ef41Sopenharmony_ci      e.stopPropagation();
1241cb0ef41Sopenharmony_ci    });
1251cb0ef41Sopenharmony_ci    document.getElementById("upload-helper").addEventListener("change",
1261cb0ef41Sopenharmony_ci      function (this: HTMLInputElement) {
1271cb0ef41Sopenharmony_ci        const uploadFile = this.files && this.files[0];
1281cb0ef41Sopenharmony_ci        if (uploadFile) {
1291cb0ef41Sopenharmony_ci          const filereader = new FileReader();
1301cb0ef41Sopenharmony_ci          filereader.onload = () => {
1311cb0ef41Sopenharmony_ci            const txtRes = filereader.result;
1321cb0ef41Sopenharmony_ci            if (typeof txtRes == 'string') {
1331cb0ef41Sopenharmony_ci              loadFile(txtRes);
1341cb0ef41Sopenharmony_ci            }
1351cb0ef41Sopenharmony_ci          };
1361cb0ef41Sopenharmony_ci          filereader.readAsText(uploadFile);
1371cb0ef41Sopenharmony_ci        }
1381cb0ef41Sopenharmony_ci      }
1391cb0ef41Sopenharmony_ci    );
1401cb0ef41Sopenharmony_ci    window.addEventListener("keydown", (e: KeyboardEvent) => {
1411cb0ef41Sopenharmony_ci      if (e.keyCode == 76 && e.ctrlKey) { // CTRL + L
1421cb0ef41Sopenharmony_ci        document.getElementById("upload-helper").click();
1431cb0ef41Sopenharmony_ci        e.stopPropagation();
1441cb0ef41Sopenharmony_ci        e.preventDefault();
1451cb0ef41Sopenharmony_ci      }
1461cb0ef41Sopenharmony_ci    });
1471cb0ef41Sopenharmony_ci  }
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci  initializeUploadHandlers();
1501cb0ef41Sopenharmony_ci  resizer.updatePanes();
1511cb0ef41Sopenharmony_ci};
152