-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathviewer.js
488 lines (438 loc) · 15.2 KB
/
viewer.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
var wasmparser = require('wasmparser');
var wasmdis = require('wasmdis');
var sourceMapUtils = require('./source-map-utils.js');
var colors;
var annotators;
function initialize() {
var openButton = document.getElementById('openFile');
openButton.addEventListener('click', openButtonClicked);
var browseInput = document.getElementById('browseFile');
browseInput.addEventListener('change', browseInputChanged);
var dumpRows = document.getElementById('dump').querySelector('.rows');
dumpRows.addEventListener('click', rowClicked);
var text = document.getElementById('text');
text.addEventListener('click', textClicked);
colors = Object.create(null);
colors[wasmparser.BinaryReaderState.BEGIN_SECTION] = 1;
colors[wasmparser.BinaryReaderState.CODE_OPERATOR] = 2;
colors[wasmparser.BinaryReaderState.INIT_EXPRESSION_OPERATOR] = 2;
colors[wasmparser.BinaryReaderState.BEGIN_FUNCTION_BODY] = 3;
colors[wasmparser.BinaryReaderState.BEGIN_WASM] = 4;
colors[wasmparser.BinaryReaderState.EXPORT_SECTION_ENTRY] = 5;
colors[wasmparser.BinaryReaderState.IMPORT_SECTION_ENTRY] = 5;
colors[wasmparser.BinaryReaderState.TYPE_SECTION_ENTRY] = 5;
colors[wasmparser.BinaryReaderState.FUNCTION_SECTION_ENTRY] = 5;
colors[wasmparser.BinaryReaderState.GLOBAL_SECTION_ENTRY] = 6;
colors[wasmparser.BinaryReaderState.MEMORY_SECTION_ENTRY] = 6;
colors[wasmparser.BinaryReaderState.DATA_SECTION_ENTRY] = 6;
colors[wasmparser.BinaryReaderState.TABLE_SECTION_ENTRY] = 6;
colors[wasmparser.BinaryReaderState.ELEMENT_SECTION_ENTRY] = 6;
colors[wasmparser.BinaryReaderState.NAME_SECTION_ENTRY] = 7;
colors[wasmparser.BinaryReaderState.RELOC_SECTION_HEADER] = 3;
colors[wasmparser.BinaryReaderState.RELOC_SECTION_ENTRY] = 7;
annotators = Object.create(null);
annotators[wasmparser.BinaryReaderState.BEGIN_SECTION] = function (result) {
return 'ID: ' + wasmparser.SectionCode[result.id] +
(result.name ? '\nName: ' + wasmparser.bytesToString(result.name) : '');
};
annotators[wasmparser.BinaryReaderState.INIT_EXPRESSION_OPERATOR] =
annotators[wasmparser.BinaryReaderState.CODE_OPERATOR] = function (result) {
return 'Operator: ' + wasmparser.OperatorCode[result.code] + '\n' +
JSON.stringify(result);
};
annotators[wasmparser.BinaryReaderState.TYPE_SECTION_ENTRY] = function (result) {
if (result.form != wasmparser.Type.func) {
return defaultAnnotator(result);
}
var am = Array.prototype.map;
return '(' + am.call(result.params, formatType).join(',') + ') : (' +
am.call(result.returns, formatType).join(',') + ')';
};
annotators[wasmparser.BinaryReaderState.IMPORT_SECTION_ENTRY] = function (result) {
if (result.kind != wasmparser.ExternalKind.Function) {
return defaultAnnotator(result);
}
return JSON.stringify(wasmparser.bytesToString(result.module)) + ' ' +
JSON.stringify(wasmparser.bytesToString(result.field)) + '\n' +
'Type Index: ' + result.funcTypeIndex;
};
annotators[wasmparser.BinaryReaderState.EXPORT_SECTION_ENTRY] = function (result) {
if (result.kind != wasmparser.ExternalKind.Function) {
return defaultAnnotator(result);
}
return JSON.stringify(wasmparser.bytesToString(result.field)) + '\n' +
'Function Index: ' + result.index;
};
annotators[wasmparser.BinaryReaderState.NAME_SECTION_ENTRY] = function (result) {
if (result.type != wasmparser.NameType.Function) {
return "";
}
var names = result.names.slice(0, 20);
return names.map(function (n) {
return n.index + ": " + wasmparser.bytesToString(n.name);
}).join('\n') + (result.names.length > 20 ? '...' : '');
};
annotators[wasmparser.BinaryReaderState.DATA_SECTION_ENTRY_BODY] = function (result) {
return "";
};
annotators[wasmparser.BinaryReaderState.SECTION_RAW_DATA] = function (result) {
return "";
};
calcHexMetrics();
}
var selectedGrp = null;
function flashOffset(offset, from) {
var oldSelection = document.querySelectorAll(".selected");
for (var i = 0; i < oldSelection.length; i++)
oldSelection[i].classList.remove('selected');
selectedGrp = offset;
if (!offset)
return;
var line = document.querySelector(".line[data-offset = '" + offset + "']");
if (line) {
if (from === 'grp')
line.scrollIntoView();
line.classList.add('selected');
}
// FIXME not flashing when no octets rendered
var grps = document.querySelectorAll(".grp[data-offset = '" + offset + "']");
if (grps.length === 0) {
// rows are not cached
document.getElementById("dump").scrollTop = hexMetrics.lineHeight * Math.floor(offset / perRow);
updateView();
grps = document.querySelectorAll(".grp[data-offset = '" + offset + "']");
}
if (grps.length > 0 && from === 'line')
grps[0].scrollIntoView();
for (var i = 0; i < grps.length; i++)
grps[i].classList.add('selected');
}
function rowClicked(e) {
var t = e.target;
while (t !== document.body && !t.classList.contains('grp'))
t = t.parentNode;
if (t === document.body)
return;
var offset = t.dataset.offset;
flashOffset(offset, 'grp');
}
function textClicked(e) {
var t = e.target;
while (t !== document.body && !t.classList.contains('line'))
t = t.parentNode;
if (t === document.body)
return;
var offset = t.dataset.offset;
flashOffset(offset, 'line');
}
function defaultAnnotator(result) {
var s = JSON.stringify(result, null, 2);
if (s.length <= 1024)
return s;
return s.substring(0, 1024) + '...';
}
function formatType(type) {
return wasmparser.Type[type];
}
function openButtonClicked(e) {
var browseInput = document.getElementById('browseFile');
browseInput.click();
}
function browseInputChanged(e) {
var browseInput = document.getElementById('browseFile');
var file = browseInput.files[0];
var fileReader = new FileReader();
fileReader.onload = function (evt) {
var buffer = evt.target.result;
var header = new Uint8Array(buffer, 0, 4);
if (header[0] == 0 && header[1] == 0x61 && header[2] == 0x73 && header[3] == 0x6D)
openWasm(buffer);
else
sourceMapUtils.openMapOrSourceFile(buffer, file.name);
};
fileReader.readAsArrayBuffer(file);
}
var perRow = 0x10;
var content;
function toHex(n, width) {
var s = n.toString(16).toUpperCase();
while (s.length < width)
s = '0' + s;
return s;
}
function annotate(state, result, position) {
var info = [];
info.push('Type: ' + wasmparser.BinaryReaderState[state] +
' @' + toHex(position, 8));
if (annotators[state]) {
info.push(annotators[state].call(null, result));
} else if (result) {
info.push(defaultAnnotator(result));
}
return info.join('\n');
}
var octetColors = [];
function buildColors() {
var reader = new wasmparser.BinaryReader();
reader.setData(content, 0, content.byteLength);
var lastPosition = reader.position;
octetColors.length = 0;
while (reader.read() && reader.state >= 0) {
if (reader.position <= lastPosition)
continue;
octetColors.push({
offset: lastPosition,
length: reader.position - lastPosition,
color: colors[reader.state],
title: annotate(reader.state, reader.result, lastPosition),
});
lastPosition = reader.position;
}
if (lastPosition < reader.position) {
octetColors.push({
offset: lastPosition,
length: content.byteLength - lastPosition,
color: undefined,
title: reader.state < 0 ? reader.error.message : undefined,
});
}
}
function searchOctetColor(octetIndex) {
var i = 0;
while (i < octetColors.length) {
if (octetColors[i].offset <= octetIndex &&
octetIndex < octetColors[i].offset + octetColors[i].length) {
return i;
}
i++;
}
return i;
}
function paintOctets(dump, startRow, endRow) {
buildColumns(dump, startRow, endRow);
var j = searchOctetColor(startRow * perRow);
var item = octetColors[j];
var groupIndex = startRow * perRow - item.offset;
var groupLength = item.length;
for (var r = startRow; r < endRow; r++) {
var currentRow = cachedRows[r];
if (currentRow.querySelector('.grp')) {
for (var skip = perRow; skip >= groupLength - groupIndex;) {
skip -= groupLength - groupIndex;
if (++j >= octetColors.length) break;
groupIndex = 0;
groupLength = octetColors[j].length;
}
groupIndex += skip;
continue;
}
var groupSpan = null;
var octets = currentRow.querySelectorAll('.o');
var itemsCount = Math.min(content.length - r * perRow, perRow);
for (var i = 0; i < itemsCount; i++) {
var octet = octets[i];
if (!groupSpan || groupSpan.parentNode !== octet.parentNode) {
groupSpan = document.createElement('span');
groupSpan.className = 'grp';
if (item.color)
groupSpan.classList.add('c' + item.color);
octet.parentNode.insertBefore(groupSpan, octet);
if (item.title)
groupSpan.title = item.title;
groupSpan.dataset.offset = item.offset;
groupSpan.classList.add('lst');
if (groupSpan.dataset.offset == selectedGrp)
groupSpan.classList.add('selected');
}
groupSpan.appendChild(octet);
if (++groupIndex >= groupLength) {
if (++j >= octetColors.length) break;
item = octetColors[j];
groupIndex = 0;
groupLength = item.length;
groupSpan = null;
}
}
}
}
function readNames(data) {
try {
var parser = new wasmparser.BinaryReader();
parser.setData(data.buffer, 0, data.length);
var namesReader = new wasmdis.NameSectionReader();
namesReader.read(parser);
if (namesReader.hasValidNames()) {
return namesReader.getNameResolver();
}
} catch (_) {
// ignoring errors
}
return null;
}
function disassemble() {
var text = document.getElementById('text');
text.textContent = '';
try {
var nameResolver = readNames(content);
var reader = new wasmparser.BinaryReader();
reader.setData(content, 0, content.byteLength);
var dis = new wasmdis.WasmDisassembler();
dis.addOffsets = true;
dis.skipTypes = false;
if (nameResolver)
dis.nameResolver = nameResolver;
var done = dis.disassembleChunk(reader);
var result = dis.getResult();
result.lines.forEach(function (s, index) {
var line = document.createElement('div');
line.className = 'line';
var offset = result.offsets[index];
// ignoring offset for lines with only '(' and ')'
var i = 0, j = s.length - 1;
while (i < j && s[i] === ' ') i++;
while (i < j && s[j] === ' ') j++;
if (i === j && (s[i] === '(' || s[i] === ')'))
offset = undefined;
line.textContent = s;
if (offset)
line.dataset.offset = offset;
text.appendChild(line);
});
} catch (_) {
// ignoring error
}
}
var cachedRows = [];
var cachedRowsCount = 0;
function buildColumns(dump, startRow, endRow) {
var insertIndex = cachedRows.reduce(function (acc, r, index) {
return r && index < startRow ? acc + 1 : acc;
}, 0);
for (var i = startRow; i < endRow; i++, insertIndex++) {
if (cachedRows[i]) continue;
var addresses = dump.querySelector('.addresses');
var rows = dump.querySelector('.rows');
var asciis = dump.querySelector('.asciis');
var offset = Math.ceil(hexMetrics.lineHeight * i);
var address = document.createElement('div');
address.className = 'address';
address.style.top = offset + "px";
var rowOffset = i * perRow;
address.textContent = "0x" + toHex(rowOffset, 8);
addresses.insertBefore(address, addresses.childNodes[insertIndex] || null);
var row = document.createElement('div');
row.className = 'row';
row.style.top = offset + "px";
rows.insertBefore(row, rows.childNodes[insertIndex] || null);
var ascii = document.createElement('div');
ascii.className = 'ascii';
ascii.style.top = offset + "px";
asciis.insertBefore(ascii, asciis.childNodes[insertIndex] || null);
cachedRows[i] = row;
cachedRowsCount++;
var rowOffset = i * perRow;
var itemsCount = Math.min(content.length - rowOffset, perRow);
row.textContent = '';
var str = '';
for (var j = 0; j < itemsCount; j++) {
var b = content[rowOffset + j];
var octet = document.createElement('span');
octet.className = 'o';
octet.textContent = toHex(b, 2);
row.appendChild(octet);
str += b >= 32 && b < 127 ? String.fromCharCode(b) : '.';
}
ascii.textContent = str;
}
}
function buildHexDump() {
var dumpContentHeight = hexMetrics.lineHeight * Math.ceil(content.length / perRow) + "px";
var dump = document.getElementById('dump');
var addresses = dump.querySelector('.addresses');
addresses.textContent = '';
addresses.style.height = dumpContentHeight;
var rows = dump.querySelector('.rows');
rows.textContent = '';
rows.style.height = dumpContentHeight;
var asciis = dump.querySelector('.asciis');
asciis.textContent = '';
asciis.style.height = dumpContentHeight;
return dump;
}
var updateViewTimeout = null;
function updateViewThrottled(needLayout) {
if (updateViewTimeout)
clearTimeout(updateViewTimeout);
updateViewTimeout = setTimeout(function () {
updateViewTimeout = null;
updateView(needLayout);
}, 100);
}
var hexMetrics = {
lineHeight: 0,
charWidth: 0,
cellMarginWidth: 0,
};
function calcHexMetrics() {
var dump = document.getElementById('dump');
var test = document.createElement('div');
test.className = "measurement";
test.textContent = "00000000";
dump.appendChild(test);
var rect = test.getBoundingClientRect();
var width = rect.width, height = rect.height;
test.className = "measurement-wo-margin";
var widthWOMargin = test.getBoundingClientRect().width;
test.remove();
hexMetrics = {
lineHeight: Math.floor(height),
charWidth: widthWOMargin / 8,
cellMarginWidth: width - widthWOMargin,
};
}
function purgeCachedItems() {
cachedRows.forEach(function (row) { row.remove(); });
cachedRows = [];
}
function layout() {
purgeCachedItems();
calcHexMetrics();
buildHexDump();
}
function updateView(needLayout) {
if (needLayout) layout();
var dump = document.getElementById('dump');
if (cachedRowsCount > 10000) purgeCachedItems();
// Find rows to display, assume all rows have the same height.
var start = Math.max(Math.floor(dump.scrollTop / hexMetrics.lineHeight), 0);
var end = Math.min(Math.ceil((dump.scrollTop + dump.clientHeight) / hexMetrics.lineHeight), Math.ceil(content.length / perRow));
paintOctets(dump, start, end);
}
document.getElementById("dump").addEventListener("scroll", function (e) { updateView(false); });
window.addEventListener("resize", function (e) { updateView(true); });
function openWasm(buffer) {
content = new Uint8Array(buffer);
sourceMap = null;
buildColors();
disassemble();
sourceMapUtils.checkAndLoadDWARF(content);
updateView(true);
}
function loadForURL(url) {
fetch(url).then(function (req) { return req.arrayBuffer(); })
.then(openWasm);
}
initialize();
if (/[?&]api=postmessage/.test(document.location.search)) {
(window.opener || window.parent).postMessage({
type: "wasmexplorer-ready"
}, "*");
window.addEventListener("message", function (e) {
if (e.data.type === "wasmexplorer-load") {
openWasm(e.data.data.buffer);
}
});
document.getElementById('openFile').hidden = true;
} else {
loadForURL('./helloworld2.wasm');
}