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/sourceeditor/wasm.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/sourceeditor/wasm.js')
-rw-r--r-- | devtools/client/shared/sourceeditor/wasm.js | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/devtools/client/shared/sourceeditor/wasm.js b/devtools/client/shared/sourceeditor/wasm.js new file mode 100644 index 0000000000..ed3ef4df63 --- /dev/null +++ b/devtools/client/shared/sourceeditor/wasm.js @@ -0,0 +1,93 @@ +/* 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/. */ + +"use strict"; + +const wasmparser = require("resource://devtools/client/shared/vendor/WasmParser.js"); +const wasmdis = require("resource://devtools/client/shared/vendor/WasmDis.js"); + +const wasmStates = new WeakMap(); + +function getWasmText(subject, data) { + const parser = new wasmparser.BinaryReader(); + parser.setData(data.buffer, 0, data.length); + const dis = new wasmdis.WasmDisassembler(); + dis.addOffsets = true; + const done = dis.disassembleChunk(parser); + let result = dis.getResult(); + if (result.lines.length === 0) { + result = { lines: ["No luck with wast conversion"], offsets: [0], done }; + } + + const offsets = result.offsets, + lines = []; + for (let i = 0; i < offsets.length; i++) { + lines[offsets[i]] = i; + } + + wasmStates.set(subject, { offsets, lines }); + + return { lines: result.lines, done: result.done }; +} + +function getWasmLineNumberFormatter(subject) { + const codeOf0 = 48, + codeOfA = 65; + const buffer = [ + codeOf0, + codeOf0, + codeOf0, + codeOf0, + codeOf0, + codeOf0, + codeOf0, + codeOf0, + ]; + let last0 = 7; + return function (number) { + const offset = lineToWasmOffset(subject, number - 1); + if (offset === undefined) { + return ""; + } + let i = 7; + for (let n = offset | 0; n !== 0 && i >= 0; n >>= 4, i--) { + const nibble = n & 15; + buffer[i] = nibble < 10 ? codeOf0 + nibble : codeOfA - 10 + nibble; + } + for (let j = i; j > last0; j--) { + buffer[j] = codeOf0; + } + last0 = i; + return String.fromCharCode.apply(null, buffer); + }; +} + +function isWasm(subject) { + return wasmStates.has(subject); +} + +function lineToWasmOffset(subject, number) { + const wasmState = wasmStates.get(subject); + if (!wasmState) { + return undefined; + } + let offset = wasmState.offsets[number]; + while (offset === undefined && number > 0) { + offset = wasmState.offsets[--number]; + } + return offset; +} + +function wasmOffsetToLine(subject, offset) { + const wasmState = wasmStates.get(subject); + return wasmState.lines[offset]; +} + +module.exports = { + getWasmText, + getWasmLineNumberFormatter, + isWasm, + lineToWasmOffset, + wasmOffsetToLine, +}; |