forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertToJSON.js
More file actions
55 lines (50 loc) · 1.63 KB
/
Copy pathconvertToJSON.js
File metadata and controls
55 lines (50 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
/* eslint camelcase: 0*/
let cachedWasmModule;
let utf8Decoder;
function convertDwarf(wasm, instance) {
const { memory, alloc_mem, free_mem, convert_dwarf } = instance.exports;
const wasmPtr = alloc_mem(wasm.byteLength);
new Uint8Array(memory.buffer, wasmPtr, wasm.byteLength).set(
new Uint8Array(wasm)
);
const resultPtr = alloc_mem(12);
const enableXScopes = true;
convert_dwarf(
wasmPtr,
wasm.byteLength,
resultPtr,
resultPtr + 4,
enableXScopes
);
free_mem(wasmPtr);
const resultView = new DataView(memory.buffer, resultPtr, 12);
const outputPtr = resultView.getUint32(0, true),
outputLen = resultView.getUint32(4, true);
free_mem(resultPtr);
if (!utf8Decoder) {
utf8Decoder = new TextDecoder("utf-8");
}
const output = utf8Decoder.decode(
new Uint8Array(memory.buffer, outputPtr, outputLen)
);
free_mem(outputPtr);
return output;
}
async function convertToJSON(buffer: ArrayBuffer): any {
if (!cachedWasmModule) {
const isFirefoxPanel =
typeof location !== "undefined" && location.protocol === "resource:";
const wasmPath = `${isFirefoxPanel ? "." : "/wasm"}/dwarf_to_json.wasm`;
const wasm = await (await fetch(wasmPath)).arrayBuffer();
const imports = {};
const { instance } = await WebAssembly.instantiate(wasm, imports);
cachedWasmModule = instance;
}
return convertDwarf(buffer, cachedWasmModule);
}
module.exports = {
convertToJSON
};