tor-browser

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

convertToJSON.js (1896B)


      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 /* eslint camelcase: 0*/
      6 
      7 "use strict";
      8 
      9 const {
     10  getDwarfToWasmData,
     11 } = require("resource://devtools/client/shared/source-map-loader/wasm-dwarf/wasmAsset.js");
     12 
     13 let cachedWasmModule;
     14 let utf8Decoder;
     15 
     16 function convertDwarf(wasm, instance) {
     17  const { memory, alloc_mem, free_mem, convert_dwarf } = instance.exports;
     18  const wasmPtr = alloc_mem(wasm.byteLength);
     19  new Uint8Array(memory.buffer, wasmPtr, wasm.byteLength).set(
     20    new Uint8Array(wasm)
     21  );
     22  const resultPtr = alloc_mem(12);
     23  const enableXScopes = true;
     24  const success = convert_dwarf(
     25    wasmPtr,
     26    wasm.byteLength,
     27    resultPtr,
     28    resultPtr + 4,
     29    enableXScopes
     30  );
     31  free_mem(wasmPtr);
     32  const resultView = new DataView(memory.buffer, resultPtr, 12);
     33  const outputPtr = resultView.getUint32(0, true),
     34    outputLen = resultView.getUint32(4, true);
     35  free_mem(resultPtr);
     36  if (!success) {
     37    throw new Error("Unable to convert from DWARF sections");
     38  }
     39  if (!utf8Decoder) {
     40    utf8Decoder = new TextDecoder("utf-8");
     41  }
     42  const output = utf8Decoder.decode(
     43    new Uint8Array(memory.buffer, outputPtr, outputLen)
     44  );
     45  free_mem(outputPtr);
     46  return output;
     47 }
     48 
     49 async function convertToJSON(buffer) {
     50  // Note: We don't 'await' here because it could mean that multiple
     51  // calls to 'convertToJSON' could cause multiple fetches to be started.
     52  cachedWasmModule = cachedWasmModule || loadConverterModule();
     53 
     54  return convertDwarf(buffer, await cachedWasmModule);
     55 }
     56 
     57 async function loadConverterModule() {
     58  const wasm = await getDwarfToWasmData();
     59  const imports = {};
     60  const { instance } = await WebAssembly.instantiate(wasm, imports);
     61  return instance;
     62 }
     63 
     64 module.exports = {
     65  convertToJSON,
     66 };