This repository was archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 747
Expand file tree
/
Copy pathwasm.js
More file actions
142 lines (128 loc) · 3.08 KB
/
Copy pathwasm.js
File metadata and controls
142 lines (128 loc) · 3.08 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* @flow */
import { BinaryReader } from "wasmparser/dist/WasmParser";
import { WasmDisassembler } from "wasmparser/dist/WasmDis";
type WasmState = {
lines: Array<number>,
offsets: Array<number>
};
var wasmStates: { [string]: WasmState } = Object.create(null);
/**
* @memberof utils/wasm
* @static
*/
function getWasmText(sourceId: string, data: Uint8Array) {
let parser = new BinaryReader();
parser.setData(data.buffer, 0, data.length);
let dis = new WasmDisassembler();
dis.addOffsets = true;
let done = dis.disassembleChunk(parser);
let result = dis.getResult();
if (result.lines.length === 0) {
result = { lines: ["No luck with wast conversion"], offsets: [0], done };
}
let offsets = result.offsets,
lines = [];
for (let i = 0; i < offsets.length; i++) {
lines[offsets[i]] = i;
}
wasmStates[sourceId] = { offsets, lines };
return { lines: result.lines, done: result.done };
}
/**
* @memberof utils/wasm
* @static
*/
function getWasmLineNumberFormatter(sourceId: string) {
const codeOf0 = 48,
codeOfA = 65;
let buffer = [
codeOf0,
codeOf0,
codeOf0,
codeOf0,
codeOf0,
codeOf0,
codeOf0,
codeOf0
];
let last0 = 7;
return function(number: number) {
let offset = lineToWasmOffset(sourceId, number - 1);
if (offset == undefined) {
return "";
}
let i = 7;
for (let n = offset; n !== 0 && i >= 0; n >>= 4, i--) {
let 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);
};
}
/**
* @memberof utils/wasm
* @static
*/
function isWasm(sourceId: string) {
return sourceId in wasmStates;
}
/**
* @memberof utils/wasm
* @static
*/
function lineToWasmOffset(sourceId: string, number: number): ?number {
let wasmState = wasmStates[sourceId];
if (!wasmState) {
return undefined;
}
let offset = wasmState.offsets[number];
while (offset === undefined && number > 0) {
offset = wasmState.offsets[--number];
}
return offset;
}
/**
* @memberof utils/wasm
* @static
*/
function wasmOffsetToLine(sourceId: string, offset: number): ?number {
let wasmState = wasmStates[sourceId];
if (!wasmState) {
return undefined;
}
return wasmState.lines[offset];
}
/**
* @memberof utils/wasm
* @static
*/
function clearWasmStates() {
wasmStates = Object.create(null);
}
function renderWasmText(sourceId: string, { binary }: Object) {
// binary does not survive as Uint8Array, converting from string
let data = new Uint8Array(binary.length);
for (let i = 0; i < data.length; i++) {
data[i] = binary.charCodeAt(i);
}
let { lines } = getWasmText(sourceId, data);
const MAX_LINES = 100000;
if (lines.length > MAX_LINES) {
lines.splice(MAX_LINES, lines.length - MAX_LINES);
lines.push(";; .... text is truncated due to the size");
}
return lines;
}
export {
getWasmText,
getWasmLineNumberFormatter,
isWasm,
lineToWasmOffset,
wasmOffsetToLine,
clearWasmStates,
renderWasmText
};