-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.js
More file actions
765 lines (644 loc) · 18.9 KB
/
Copy pathdata.js
File metadata and controls
765 lines (644 loc) · 18.9 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
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
import "./fileWatcher.js";
/** @typedef {object} CodeNode
* @property {string} Name
* @property {string} Description
* @property {string} Value
*
* @property {number} Start absolute position of start
* @property {number} End absolute position of end byte, exclusive
* @property {?string} Ecma Section of ECMA-335 defining this node
* @property {string[]} Errors
*
* @property {?string} LinkPath optional path like FileFormat/Array[2]/Flag
* @property {string} [SelfPath] calculated
* @property {string[]} [ReverseLinks] calculated
* @property {CodeNode} [parent] calculated
* @property {CodeNode[]} Children
*
* @property {HTMLLIElement} [tocLIdom] calculated
*/
/** @type {CodeNode} */
let FileFormat;
function assertThrow(message) {
debugger;
alert(message);
throw message;
}
function $(id) {
return document.getElementById(id);
}
/** @returns {HTMLElement} */
function create(tag, attr) {
const el = document.createElement(tag);
if (attr) {
for (const key in attr) {
el[key] = attr[key];
}
}
return el;
}
// https://stackoverflow.com/a/8023734/771768
// 0 <= h, s, v <= 1
function HSVtoRGB(h, s, v) {
let r, g, b, i, f, p, q, t;
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0:
r = v;
g = t;
b = p;
break;
case 1:
r = q;
g = v;
b = p;
break;
case 2:
r = p;
g = v;
b = t;
break;
case 3:
r = p;
g = q;
b = v;
break;
case 4:
r = t;
g = p;
b = v;
break;
case 5:
r = v;
g = p;
b = q;
break;
}
r = Math.round(r * 255);
g = Math.round(g * 255);
b = Math.round(b * 255);
return "#" + r.toString(16) + g.toString(16) + b.toString(16);
}
function getColor(n) {
return HSVtoRGB(getHue(n), 0.8, 1);
}
function getDimColor(n) {
return HSVtoRGB(getHue(n), 0.3, 1);
}
/**
* Picks colors that don't repeat.
* [0, 1/3, 2/3, 1/6, 3/6, 5/6, 1/12, 3/12, 5/12, ...]
* @param {number} n
*/
function getHue(n) {
if (n < 3) return n / 3;
let denominator = 6;
n = 2 * n + 1 - 6;
while (n >= denominator) {
n -= denominator;
denominator *= 2;
}
return n / denominator;
}
function byteID(i) {
return "byte" + i;
}
function litID(i) {
return "lit" + i;
}
//TODO(PERF) calling this function 100's of thousands of times results in
// a lot of unnecessary delay with style calculations. Instead, should only update if change is needed?
// Updating the color or the cursor causes the restyle cost. Updating half only costs half as much.
function setByte(i, color, cursor) {
const byte = $(byteID(i));
const lit = $(litID(i));
byte.style.backgroundColor = color;
lit.style.backgroundColor = color;
byte.style.cursor = cursor;
lit.style.cursor = cursor;
}
/** @param {CodeNode} node */
function scrollIntoView(node) {
//TODO(PERF) could skipping getBoundingClientRect improve render time?
const first = $(byteID(node.Start));
const last = $(byteID(node.End - 1));
const firstBox = first.getBoundingClientRect();
const lastBox = last.getBoundingClientRect();
// Need to scroll up
if (lastBox.bottom < 0) {
first.scrollIntoView(/* top: */ true);
}
// Need to scroll down
if (firstBox.top > window.innerHeight) {
last.scrollIntoView(/* top: */ false);
}
}
/** @param {HTMLElement} el */
function hideToc(el) {
el.style.visibility = "hidden";
el.style.height = "0";
el.style.lineHeight = "0";
}
/** @param {HTMLElement} el */
function showToc(el) {
el.style.visibility = "";
el.style.height = "";
el.style.lineHeight = "";
}
function searchFilter() {
const text = $("tocSearch").value.toLowerCase();
allTocUL().forEach(hideToc);
allTocLI().forEach(hideToc);
// Unhide all the ToC up the parents
const tocDiv = $("toc");
for (const li of allTocLI()) {
if (!li.textContent.toLowerCase().includes(text)) continue;
let e = li;
while (e !== tocDiv) {
showToc(e);
if (e.previousElementSibling) showToc(e.previousElementSibling);
e = e.parentElement;
}
showToc(e);
}
}
function linkFilter() {
allTocUL().forEach(hideToc);
allTocLI().forEach(hideToc);
for (let i = FileFormat.Start; i < FileFormat.End; ++i) {
setByte(i, "white", "auto");
}
linkFilterHelper(FileFormat, {n: 0});
}
/**
* @param {CodeNode} node
* @param {{n: number}} counter
*/
function linkFilterHelper(node, counter) {
if (node.LinkPath) {
for (let i = node.Start; i < node.End; ++i) {
setByte(i, getColor(counter.n), "pointer");
}
++counter.n;
let e = node.tocLIdom;
let first = true;
while (e !== $("toc")) {
showToc(e);
if (e.previousElementSibling && !first) showToc(e.previousElementSibling);
first = false;
e = e.parentElement;
}
}
for (const ch of node.Children) {
linkFilterHelper(ch, counter);
}
}
/** @param {CodeNode} node */
function setFocusObject(node) {
let hash = "";
for (let hashParent = node; hashParent; hashParent = hashParent.parent) {
hash = hashParent.Name + "/" + hash;
}
window.location.hash = hash.substring(0, hash.length - 1);
}
/** @param {MouseEvent} ev */
function byteOnClick(ev) {
/** @type {string} */
const id = ev.target.id;
const m = /(byte|lit)(\d+)/.exec(id);
if (!m) return true;
const n = Number(m[2]);
if (isLinks()) {
// Clicking on a link should focus the link source
let node = FileFormat;
while (node) {
if (node.LinkPath) {
window.location.hash = node.SelfPath;
return true;
}
node = nodeChild(node, n);
}
} else {
const node = currentNode();
if (node.Start <= n && n < node.End && node.LinkPath) {
window.location.hash = node.LinkPath;
return true;
}
for (let parent = node; parent; parent = parent.parent) {
const child = nodeChild(parent, n);
if (!child) continue;
if (child !== node) {
setFocusObject(child);
}
return true;
}
}
}
/** @param {CodeNode} node */
function setFocus(node) {
allTocUL().forEach(hideToc);
// Unhide all the text elements
allTocLI().forEach(showToc);
// Unhide all the ToC up the parents
const tocDiv = $("toc");
const toc = node.tocLIdom;
let parentUL = toc.parentElement;
while (parentUL !== tocDiv) {
showToc(parentUL);
parentUL = parentUL.parentElement;
}
// Unhide sub-elements
const sib = toc.nextElementSibling;
if (sib) showToc(sib);
allTocLI().forEach(li => {
li.style.textDecoration = "";
li.style.cursor = "";
});
toc.style.textDecoration = "underline";
if (node.LinkPath) {
toc.style.cursor = "pointer";
}
const byteSet = Array(FileFormat.End).fill(false);
setFocusHelper(node, null, byteSet);
for (let i = FileFormat.Start; i < FileFormat.End; ++i) {
if (!byteSet[i]) {
setByte(i, "white", "auto");
}
}
scrollIntoView(node);
drawDetails(node);
}
/** @param {CodeNode} node */
function drawDetails(node) {
const focusDetail = $("focusDetail");
while (focusDetail.firstChild) focusDetail.removeChild(focusDetail.firstChild);
const detailDiv = createBasicDetailsDOM(node);
focusDetail.appendChild(detailDiv);
for (const line of node.Description.split("\n")) {
const p = create("p", {textContent: line});
p.style.margin = 0;
detailDiv.appendChild(p);
}
if (node.ReverseLinks) {
detailDiv.appendChild(create("p", {textContent: "Referenced by:"}));
const ul = create("ul");
for (const revLink of node.ReverseLinks) {
const li = create("li");
let prefix = 0;
for (; prefix < revLink.length && prefix < node.SelfPath.length; ++prefix) {
if (node.SelfPath[prefix] !== revLink[prefix]) break;
// TODO this is buggy:
/*
if
Method[1]/Header/CilOps/Op[0].Value
is a branch that links to
Method[1]/Header/CilOps/Op[1]
then the backref prefix will be
0].Value
*/
}
li.appendChild(
create("a", {
href: "#" + revLink,
textContent: revLink.substring(prefix),
})
);
ul.appendChild(li);
}
detailDiv.appendChild(ul);
}
}
function isLinks() {
return window.location.href.split("#")[1] === "/Links";
}
function onHashChange() {
if (isLinks()) {
linkFilter();
} else {
setFocus(currentNode());
}
}
function currentNode() {
if (isLinks()) {
return FileFormat;
}
const hash = window.location.href.split("#")[1];
return hash ? lookupNode(hash) : FileFormat;
}
/** @param {string} path */
function lookupNode(path) {
const names = path.split("/");
let o = FileFormat;
if (names[0] != "FileFormat") {
// MAYBE when reloading with a new binary, select the first parent node?
assertThrow(`Couldn't find ${names[0]} under ${o.Name}`);
}
names.slice(1).forEach(name => {
const [c] = o.Children.filter(c => name == c.Name);
if (!c) {
assertThrow(`Couldn't find ${name} under ${o.Name}`);
}
o = c;
});
return o;
}
/**
* @param {CodeNode} node
* @param {Number} n
* @returns {?CodeNode}
*/
function nodeChild(node, n) {
for (const child of node.Children) {
if (child.Start <= n && n < child.End) {
return child;
}
}
}
/**
* @param {CodeNode} node
* @param {?CodeNode} currentChild
* @param {boolean[]} byteSet
*/
function setFocusHelper(node, currentChild, byteSet) {
if (node.parent) {
setFocusHelper(node.parent, node, byteSet);
}
const ch = node.Children;
let col;
for (let chI = 0; chI < ch.length; ++chI) {
const cc = ch[chI];
if (cc === currentChild) {
continue;
}
col = currentChild ? getDimColor(chI) : getColor(chI);
for (let i = cc.Start; i < cc.End; ++i) {
setByte(i, col, "zoom-in");
byteSet[i] = true;
}
}
if (!currentChild && !ch.length) {
col = getColor(0); //TODO in-order coloring
const cursor = node.LinkPath ? "pointer" : "auto";
for (let i = node.Start; i < node.End; ++i) {
setByte(i, col, cursor);
byteSet[i] = true;
}
}
}
/** @param {CodeNode} node */
function addParent(node) {
for (const c of node.Children) {
c.parent = node;
addParent(c);
}
}
/** @param {CodeNode} node */
function setSelfPaths(node, prefix) {
if (prefix) prefix += "/";
prefix = prefix || "";
prefix += node.Name;
node.SelfPath = prefix;
for (const c of node.Children) {
setSelfPaths(c, prefix);
}
}
/** @param {CodeNode} node */
function findLinkReferences(node) {
if (node.LinkPath) {
const linked = lookupNode(node.LinkPath);
linked.ReverseLinks = linked.ReverseLinks || [];
linked.ReverseLinks.push(node.SelfPath);
}
node.Children.forEach(findLinkReferences);
}
function drawToc() {
const ul = create("ul"); // MAYBE would nested <details> be simpler? https://developer.mozilla.org/en-US/docs/Web/HTML/Element/details
$("toc").appendChild(ul);
drawTocHelper(FileFormat, ul);
}
/** @param {CodeNode} node */
function drawTocHelper(node, parentUL) {
const onclick = _ => {
if (node == currentNode() && node.LinkPath) {
window.location.hash = node.LinkPath;
} else {
setFocusObject(node);
}
};
const li = create("li", {textContent: node.Name, onclick});
if (node.LinkPath) {
li.style.color = "blue";
}
node.tocLIdom = li;
parentUL.appendChild(li);
const ch = node.Children;
if (ch.length) {
const ul = create("ul");
for (const c of ch) {
drawTocHelper(c, ul);
}
parentUL.appendChild(ul);
}
}
/** @param {CodeNode} node */
function createBasicDetailsDOM(node) {
const details = create("div", {onclick: _ => setFocusObject(node)});
const name = create("p", {textContent: node.Name});
if (node.Ecma) {
name.textContent += " ";
const href = `https://darthwalsh.github.io/ecma-335?section=${node.Ecma}`;
const ecmaLink = create("a", {href, target: "_blank"});
const ecma = create("img", {src: "ecma.png"});
ecma.style.height = "1em";
ecmaLink.appendChild(ecma);
ecmaLink.append(`§${node.Ecma}`);
name.appendChild(ecmaLink);
}
details.appendChild(name);
details.appendChild(create("p", {textContent: node.Value}));
return details;
}
/** @param {CodeNode} node */
function findErrors(node) {
for (const err of node.Errors) {
const errorDiv = createBasicDetailsDOM(node);
$("details").appendChild(errorDiv);
errorDiv.classList.add("error");
errorDiv.appendChild(create("p", {textContent: err}));
}
node.Children.forEach(findErrors);
}
function ToHex(code, width) {
const hexEncodeArray = "0123456789ABCDEF".split("");
let s = "";
for (let i = 0; i < width || code; ++i) {
s = hexEncodeArray[code & 0x0f] + s;
code >>>= 4;
}
return s;
}
// Like ASCII, but the upper range has glyphs included in the font.
const charEncoding =
".αβγδεζηθικλμξπφ" +
"χψωΓΔΞΠΣΦΨΩ♠♥♦♣∞" +
" !\"#$%&'()*+,-./" +
"0123456789:;<=>?" +
"@ABCDEFGHIJKLMNO" +
"PQRSTUVWXYZ[\\]^_" +
"`abcdefghijklmno" +
"pqrstuvwxyz{|}~₪" +
"◦ƒ¨“”♂‡–ˆ‰Š‹Œ♫ެ" +
"฿₱₩‘’♀†₸∫™š›œ♪žß" +
"€¡¢£¤¥¦§◊©ª«¯₹·¸" +
"±°¹²³´…¶≠®º»☼¼¾¿" +
"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ" +
"àáâãäåæçèéêëìíîï" +
"ÐÑÒÓÔÕÖרÙÚÛÜÝÞŸ" +
"ðñòóôõö÷øùúûüýþÿ";
function removeChildren(id) {
const el = $(id);
while (el.firstChild) el.removeChild(el.firstChild);
}
function removeErrorDetails() {
const details = $("details");
while (details.firstElementChild.nextElementSibling)
details.removeChild(details.firstElementChild.nextElementSibling);
}
function allTocUL() {
return $("toc").querySelectorAll("ul");
}
function allTocLI() {
return $("toc").querySelectorAll("li");
}
function cleanupDisplay() {
removeChildren("tocSearch");
removeChildren("toc");
removeChildren("bytes");
removeChildren("focusDetail");
removeErrorDetails();
}
/** @param {ArrayBuffer} buf */
function displayHex(buf) {
const bytes = new Uint8Array(buf);
const div = $("bytes");
const width = 16;
const size = bytes.length;
const rowLabelWidth = ToHex(size - 1).length;
div.appendChild(create("code", {textContent: Array(rowLabelWidth + 1).join("-") + " "}));
for (let i = 0; i < width; i++) {
div.appendChild(create("code", {textContent: ToHex(i, 2) + " "}));
}
div.appendChild(create("br"));
for (let j = 0; j < size; j += width) {
div.appendChild(create("code", {textContent: ToHex(j, rowLabelWidth) + " "}));
for (let i = j; i - j < width; i++) {
div.appendChild(
create("code", {
textContent: ToHex(bytes[i], 2) + " ",
id: byteID(i),
})
);
}
div.appendChild(create("code", {innerHTML: " "}));
for (let i = j; i - j < width; i++) {
const lit = charEncoding[bytes[i]];
div.appendChild(create("code", {textContent: lit, id: litID(i)}));
}
div.appendChild(create("br"));
}
div.onclick = byteOnClick;
}
/** @param {CodeNode} o */
function displayParse(o) {
FileFormat = o;
addParent(FileFormat);
setSelfPaths(FileFormat);
findLinkReferences(FileFormat);
drawToc();
findErrors(FileFormat);
window.onhashchange = onHashChange;
$("tocSearch").oninput = searchFilter;
$("visualLinks").onclick = _ => (window.location.hash = "/Links");
window.onhashchange();
}
async function setupExampleBytes() {
const bytesResponse = await fetch("Program.dat");
const json = await fetch("bytes.json");
const buf = await bytesResponse.arrayBuffer();
displayHex(buf);
displayParse(await json.json());
}
async function setupFromFile(buf) {
displayHex(buf);
const parseUrl = window.location.href.startsWith("http://localhost:5500")
? "http://127.0.0.1:8080"
: "https://dotnetbytes.fly.dev/";
const response = await fetch(parseUrl, {
method: "POST",
body: buf,
headers: {
"Content-Type": "application/x-msdownload",
},
});
displayParse(await response.json());
}
const exampleButton = $("example");
const fileInput = $("fileInput");
/*
If URL query is blank:
- Wait for the user to input a file
- Post file to get JSON form parse
If URL has ?Example
- Get EXE from Program.dat
- Get JSON parse from bytes.json
*/
if (!window.location.href.includes("?Example=true")) {
exampleButton.onclick = () => {
let hash = window.location.href.split("#")[1] || "";
if (hash) {
hash = "#" + hash; //TODO this often causes an error -- go to top?
}
window.location.href = "?Example=true" + hash;
};
exampleButton.value = "Try Example";
fileInput.addEventListener("change", async ev => {
/** @type {ArrayBuffer} */
const buf = await ev.bytes.arrayBuffer();
if (!buf.byteLength) return;
cleanupDisplay();
const dateString = ev.bytes.lastModifiedDate.toLocaleString([], {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
});
//TODO(API) pseudoelement on the file changing componenet?
$("fileTime").innerText = ev.bytes.name + " " + dateString;
fileInput.style.display = "none";
setupFromFile(buf);
});
} else {
exampleButton.onclick = () => {
window.location.href = window.location.href.replace("?Example=true", "");
};
exampleButton.value = "Leave Example";
fileInput.style.display = "none";
setupExampleBytes();
}
//TODO rightPanel width isn't always wide enough for link text i.e. Methods/Method[0]/CilOps/Op[2]/Token/Offset
//TODO(LINK) link targets, using dim? What if both?
//TODO(LINK) visualize link sizes (using onhover to highlight size of target)
//TODO link-references should include name of linking object instead of path
//TODO smart colors (better saturations on reds, etc.) (maybe 0, 120, 240, 60, 180, 300, 30, 90, etc?)
//TODO onhover tooltip to update details div on right
//TODO method ops and stack state visualization before op and links between branches, e.g.
// load 10 |
// load 2.0| I4
// add | I4 R8
// ret | R8
//TODO split monolithic JS file into little libraries
//MAYBE search boxes: X to close, should filter colored bytes
//MAYBE search also looks through name/details (optional check box?) (heuristic to show below?)