tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

dbg.js (2319B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
      4 
      5 import { prefs, asyncStore, features } from "./prefs";
      6 
      7 function getThreadFront(dbg) {
      8  return dbg.targetCommand.targetFront.threadFront;
      9 }
     10 
     11 function findSource(dbg, url) {
     12  const sources = dbg.selectors.getSourceList();
     13  return sources.find(s => (s.url || "").includes(url));
     14 }
     15 
     16 function findSources(dbg, url) {
     17  const sources = dbg.selectors.getSourceList();
     18  return sources.filter(s => (s.url || "").includes(url));
     19 }
     20 
     21 function evaluate(dbg, expression) {
     22  return dbg.client.evaluate(expression);
     23 }
     24 
     25 function bindSelectors(obj) {
     26  return Object.keys(obj.selectors).reduce((bound, selector) => {
     27    bound[selector] = (a, b, c) =>
     28      obj.selectors[selector](obj.store.getState(), a, b, c);
     29    return bound;
     30  }, {});
     31 }
     32 
     33 function formatMappedLocation(mappedLocation) {
     34  const { location, generatedLocation } = mappedLocation;
     35  return {
     36    original: `(${location.line}, ${location.column})`,
     37    generated: `(${generatedLocation.line}, ${generatedLocation.column})`,
     38  };
     39 }
     40 
     41 function formatMappedLocations(locations) {
     42  return console.table(locations.map(loc => formatMappedLocation(loc)));
     43 }
     44 
     45 function formatSelectedColumnBreakpoints(dbg) {
     46  const positions = dbg.selectors.getBreakpointPositionsForSource(
     47    dbg.selectors.getSelectedSource().id
     48  );
     49 
     50  return formatMappedLocations(positions);
     51 }
     52 
     53 const diff = (a, b) => Object.keys(a).filter(key => !Object.is(a[key], b[key]));
     54 
     55 export function setupHelper(obj) {
     56  const selectors = bindSelectors(obj);
     57  const dbg = {
     58    ...obj,
     59    selectors,
     60    prefs,
     61    asyncStore,
     62    features,
     63 
     64    helpers: {
     65      findSource: url => findSource(dbg, url),
     66      findSources: url => findSources(dbg, url),
     67      evaluate: expression => evaluate(dbg, expression),
     68      dumpThread: () => getThreadFront(dbg).dumpThread(),
     69    },
     70    formatters: {
     71      mappedLocations: locations => formatMappedLocations(locations),
     72      mappedLocation: location => formatMappedLocation(location),
     73      selectedColumnBreakpoints: () => formatSelectedColumnBreakpoints(dbg),
     74    },
     75    _telemetry: {
     76      events: {},
     77    },
     78    diff,
     79  };
     80 
     81  window.dbg = dbg;
     82 }