-
Notifications
You must be signed in to change notification settings - Fork 6
/
heap-pane.mjs
110 lines (98 loc) · 3.53 KB
/
heap-pane.mjs
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
import {
renderText
} from "./term-utils.mjs";
import { ScrollableTextPane } from "./scrollable-text-pane.mjs";
import { isHeapRef } from "./language.mjs";
import { inspect } from "util";
export function HeapPane(db, box) {
const self = {
updateDisplay,
get textPane() { return textPane }
};
const log = db.log;
const cache = db.cache;
const textPane = ScrollableTextPane(db, box);
function updateDisplay() {
const heap = db.snapshot.heapMap;
// log.write(`Heap: ${inspect(heap)} \n`);
const lines = [];
for (let heapId in heap) {
const id = heap[heapId];
const object = cache.objectMap.get(id);
//log.write(`Object(${heapId}): ${inspect(object)}\n`);
if (Array.isArray(object)) {
renderArray(heapId, object, lines);
} else if (object instanceof Map) {
renderDictionary(heapId, object, lines);
} else if (typeof object === "string") {
lines.push("*" + heapId);
lines.push(" " + JSON.stringify(object));
} else {
}
}
// log.write(`Display lines: ${JSON.stringify(lines)}\n`);
textPane.updateAllLines(lines);
//renderText(box.left, box.top, box.width, box.height, lines);
}
function renderArray(id, array, lines) {
const displayItems = array.map(displayValue);
lines.push("*" + id);
lines.push("┌" +
displayItems.map(item => "".padEnd(item.length, "─")).join("┬") +
"┐");
if (displayItems.length > 0) {
lines.push(
"│" + displayItems.join("│") + "│");
}
lines.push(
"└" + displayItems.map(item => "".padEnd(item.length, "─")).join("┴") +
"┘");
}
function renderDictionary(id, dict, lines) {
const entries = [];
for (let [key, value] of dict.entries()) {
entries.push([displayValue(key), displayValue(value)]);
}
const column1Width = entries.reduce((width, entry) =>
entry[0].length > width ? entry[0].length : width, 1);
const column2Width = entries.reduce((width, entry) =>
entry[1].length > width ? entry[1].length : width, 1);
lines.push("*" + id);
lines.push(
"┌" + Array(column1Width + 1).join("─") +
"┬" + Array(column2Width + 1).join("─") +
"┐");
for (let i = 0; i < entries.length; i++) {
const entry = entries[i];
lines.push(
"│" + entry[0].padEnd(column1Width, " ") +
"│" + entry[1].padEnd(column2Width, " ") +
"│");
if (i < entries.length - 1) {
lines.push(
"├" + "".padEnd(column1Width, "─") +
"┼" + "".padEnd(column2Width, "─") +
"┤");
}
}
lines.push(
"└" + Array(column1Width + 1).join("─") +
"┴" + Array(column2Width + 1).join("─") +
"┘");
}
return self;
}
function displayValue(value) {
if (value === null) {
return "null";
}else if (isHeapRef(value)) {
return "*" + value.id;
} else if (typeof value === "string") {
return quote(value);
} else {
return String(value);
}
}
function quote(str) {
return '"' + str.replace(/\"/g, '\\"') + '"';
}