diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /devtools/client/shared/source-map-loader/wasm-dwarf/convertToJSON.js | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/client/shared/source-map-loader/wasm-dwarf/convertToJSON.js')
-rw-r--r-- | devtools/client/shared/source-map-loader/wasm-dwarf/convertToJSON.js | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/devtools/client/shared/source-map-loader/wasm-dwarf/convertToJSON.js b/devtools/client/shared/source-map-loader/wasm-dwarf/convertToJSON.js new file mode 100644 index 0000000000..9faae5c901 --- /dev/null +++ b/devtools/client/shared/source-map-loader/wasm-dwarf/convertToJSON.js @@ -0,0 +1,66 @@ +/* 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*/ + +"use strict"; + +const { + getDwarfToWasmData, +} = require("resource://devtools/client/shared/source-map-loader/wasm-dwarf/wasmAsset.js"); + +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; + const success = 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 (!success) { + throw new Error("Unable to convert from DWARF sections"); + } + 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) { + // Note: We don't 'await' here because it could mean that multiple + // calls to 'convertToJSON' could cause multiple fetches to be started. + cachedWasmModule = cachedWasmModule || loadConverterModule(); + + return convertDwarf(buffer, await cachedWasmModule); +} + +async function loadConverterModule() { + const wasm = await getDwarfToWasmData(); + const imports = {}; + const { instance } = await WebAssembly.instantiate(wasm, imports); + return instance; +} + +module.exports = { + convertToJSON, +}; |