tor-browser

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

wasm.js (3674B)


      1 const readWasm = require("../lib/read-wasm");
      2 
      3 /**
      4 * Provide the JIT with a nice shape / hidden class.
      5 */
      6 function Mapping() {
      7  this.generatedLine = 0;
      8  this.generatedColumn = 0;
      9  this.lastGeneratedColumn = null;
     10  this.source = null;
     11  this.originalLine = null;
     12  this.originalColumn = null;
     13  this.name = null;
     14 }
     15 
     16 let cachedWasm = null;
     17 
     18 module.exports = function wasm() {
     19  if (cachedWasm) {
     20    return cachedWasm;
     21  }
     22 
     23  const callbackStack = [];
     24 
     25  cachedWasm = readWasm()
     26    .then(buffer => {
     27      return WebAssembly.instantiate(buffer, {
     28        env: {
     29          mapping_callback(
     30            generatedLine,
     31            generatedColumn,
     32 
     33            hasLastGeneratedColumn,
     34            lastGeneratedColumn,
     35 
     36            hasOriginal,
     37            source,
     38            originalLine,
     39            originalColumn,
     40 
     41            hasName,
     42            name
     43          ) {
     44            const mapping = new Mapping();
     45            // JS uses 1-based line numbers, wasm uses 0-based.
     46            mapping.generatedLine = generatedLine + 1;
     47            mapping.generatedColumn = generatedColumn;
     48 
     49            if (hasLastGeneratedColumn) {
     50              // JS uses inclusive last generated column, wasm uses exclusive.
     51              mapping.lastGeneratedColumn = lastGeneratedColumn - 1;
     52            }
     53 
     54            if (hasOriginal) {
     55              mapping.source = source;
     56              // JS uses 1-based line numbers, wasm uses 0-based.
     57              mapping.originalLine = originalLine + 1;
     58              mapping.originalColumn = originalColumn;
     59 
     60              if (hasName) {
     61                mapping.name = name;
     62              }
     63            }
     64 
     65            callbackStack[callbackStack.length - 1](mapping);
     66          },
     67 
     68          start_all_generated_locations_for() {
     69            console.time("all_generated_locations_for");
     70          },
     71          end_all_generated_locations_for() {
     72            console.timeEnd("all_generated_locations_for");
     73          },
     74 
     75          start_compute_column_spans() {
     76            console.time("compute_column_spans");
     77          },
     78          end_compute_column_spans() {
     79            console.timeEnd("compute_column_spans");
     80          },
     81 
     82          start_generated_location_for() {
     83            console.time("generated_location_for");
     84          },
     85          end_generated_location_for() {
     86            console.timeEnd("generated_location_for");
     87          },
     88 
     89          start_original_location_for() {
     90            console.time("original_location_for");
     91          },
     92          end_original_location_for() {
     93            console.timeEnd("original_location_for");
     94          },
     95 
     96          start_parse_mappings() {
     97            console.time("parse_mappings");
     98          },
     99          end_parse_mappings() {
    100            console.timeEnd("parse_mappings");
    101          },
    102 
    103          start_sort_by_generated_location() {
    104            console.time("sort_by_generated_location");
    105          },
    106          end_sort_by_generated_location() {
    107            console.timeEnd("sort_by_generated_location");
    108          },
    109 
    110          start_sort_by_original_location() {
    111            console.time("sort_by_original_location");
    112          },
    113          end_sort_by_original_location() {
    114            console.timeEnd("sort_by_original_location");
    115          },
    116        },
    117      });
    118    })
    119    .then(Wasm => {
    120      return {
    121        exports: Wasm.instance.exports,
    122        withMappingCallback: (mappingCallback, f) => {
    123          callbackStack.push(mappingCallback);
    124          try {
    125            f();
    126          } finally {
    127            callbackStack.pop();
    128          }
    129        },
    130      };
    131    })
    132    .then(null, e => {
    133      cachedWasm = null;
    134      throw e;
    135    });
    136 
    137  return cachedWasm;
    138 };