-
Notifications
You must be signed in to change notification settings - Fork 6
/
rich-stack-pane.mjs
363 lines (336 loc) · 13.7 KB
/
rich-stack-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
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
import {
renderText
} from "./term-utils.mjs";
import { ScrollableTextPane } from "./scrollable-text-pane.mjs";
import { isRef, isHeapRef } from "./language.mjs"
import jsonLike from "../json-like/json-like.js";
import $s from "styled_string";
import util from "util";
export function RichStackPane(db, box) {
const self = {
updateDisplay,
get textPane() { return textPane }
};
const inspect = util.inspect;
const log = db.log;
const cache = db.cache;
const textPane = ScrollableTextPane(db, box);
const expandCollapseStates = new Map();
function updateDisplay() {
const lines = [];
let stack = cache.getFunCall(db.snapshot.fun_call_id);
let heapVersion = db.snapshot.heap;
let i = 1;
while (true) {
if (!stack) break;
const fun = cache.getFun(stack.fun_id);
const funName = fun && fun.name || "<unknown>";
lines.push($s(funName + "()", { display: 'underscore' }));
renderLocals(stack.locals, heapVersion, lines);
renderClosureVariables(stack, heapVersion, lines);
renderGlobals(stack.globals, heapVersion, lines);
lines.push(strTimes("─", box.width));
//log.write(JSON.stringify(frame) + ", variables: " + JSON.stringify(variables) + "\n");
stack = stack.parent_id && cache.getFunCall(stack.parent_id);
i += 2;
}
textPane.updateAllLines(lines);
}
function resolve(value, heapVersion, heap) {
if (isHeapRef(value)) {
return cache.getHeapObject(heapVersion, value.id);
} else {
return value;
}
}
function renderLocals(locals, heapVersion, lines) {
const variables = cache.getHeapObject(heapVersion, locals);
variables.forEach((value, key) => {
const prefix = $s(key, {foreground: 'green'}).concat(" = ");
const renderedValue = renderValue(prefix, $s(" "), value, heapVersion, new Set(), [key]);
lines.push(...renderedValue);
});
}
function renderClosureVariables(stack, heapVersion, lines) {
if (stack.closure_cellvars) {
const cellvars = jsonLike.parse(stack.closure_cellvars);
const cellvarsEntries = cellvars.entries();
for (let [key, value] of cellvarsEntries) {
value = resolve(value, heapVersion).get("ob_ref") || null;
const prefix = $s(key, {foreground: 'yellow'}).concat(" = ");
const renderedValue = renderValue(prefix, $s(" "), value, heapVersion, new Set(), [key]);
lines.push(...renderedValue);
}
}
if (stack.closure_freevars) {
const freevars = jsonLike.parse(stack.closure_freevars);
const freevarsEntries = freevars.entries();
for (let [key, value] of freevarsEntries) {
value = resolve(value, heapVersion).get("ob_ref") || null;
const prefix = $s(key, {foreground: 'yellow'}).concat(" = ");
const renderedValue = renderValue(prefix, $s(" "), value, heapVersion, new Set(), [key]);
lines.push(...renderedValue);
}
}
}
function renderGlobals(globals, heapVersion, lines) {
globals = cache.getHeapObject(heapVersion, globals);
if (!globals) {
return;
}
let entries = Array.from(globals.entries());
entries = entries.filter(([key, value]) => !resolve(key, heapVersion).startsWith("__"));
entries.sort((one, other) => {
let oneKey = resolve(one[0], heapVersion);
let otherKey = resolve(other[0], heapVersion);
if (oneKey > otherKey) {
return 1;
} else if (oneKey === otherKey) {
return 0;
} else {
return -1;
}
});
for (let [key, value] of entries) {
key = resolve(key, heapVersion);
const prefix = $s(key, {foreground: 'cyan'}).concat(" = ");
// log.write(`rendering global ${inspect(key)} ${inspect(value)}\n`);
const renderedValue = renderValue(prefix, $s(" "), value, heapVersion, new Set(), [key]);
lines.push(...renderedValue);
}
}
function renderValue(prefix, indent, value, heapVersion, visited, path) {
// log.write(`renderValue(${prefix}, ${inspect(value)})\n`);
const localVisited = new Set(visited);
const oneLine = renderValueOneLine(value, heapVersion, localVisited);
if (indent.length + prefix.length + oneLine.length < box.width) {
for (let id of localVisited) {
visited.add(id);
}
return [$s(indent).concat(prefix).concat(oneLine)];
} else {
return renderValueMultiLine(prefix, indent, value, heapVersion, visited, path);
}
}
function stringify(value) {
if (value === null || value === undefined) {
return "None";
} else if (value === true) {
return "True";
} else if (value === false) {
return "False";
} else {
return JSON.stringify(value);
}
}
function renderValueOneLine(value, heapVersion, visited) {
let object;
if (isHeapRef(value)) {
const ref = value;
const refId = ref.id;
const oid = cache.heapLookup(heapVersion, refId);
if (!oid) {
return `^${refId}`;
}
object = cache.getObject(oid);
if (object === undefined) {
return `*!${oid}`;
}
// log.write(`heap: ${inspect(heap)}`);
if (visited.has(refId) && (typeof object !== "string")) {
return "*" + refId;
}
visited.add(refId);
} else {
object = value;
}
if (typeof object === "string" || typeof object === "number" || typeof object === "boolean" || object === null || object === undefined) {
return stringify(object);
} else if (Array.isArray(object)) {
let outputs = object.map((item) => renderValueOneLine(item, heapVersion, visited));
let tag = object.__tag__;
let output;
if (tag === "tuple") {
output = "(" + outputs.join(", ") + ")";
} else if (tag === "set") {
output = "{" + outputs.join(", ") + "}";
} else {
output = "[" + outputs.join(", ") + "]";
if (tag) {
output = `<${object.__tag__}>${output}`;
}
}
return output;
} else if (object instanceof Map){
let keys = Array.from(object.keys());
let output;
let tag = object.__tag__;
if (keys.length === 1 && keys[0] === "__dict__") {
const dict = object.get("__dict__");
return renderValueOneLine(dict, heapVersion, visited);
}
if (tag === "cell") {
const obRef = object.get("ob_ref");
return renderValueOneLine(obRef, heapVersion, visited);
}
let fun_id;
if (tag === "function") {
fun_id = object.get("fun_id");
object = object.get("closure_freevars") || new Map();
}
let outputs = [];
object.forEach((value, key) => {
if (isHeapRef(key)) {
const realKey = cache.getHeapObject(heapVersion, key.id);
if (typeof realKey === "string") {
if (realKey.startsWith("__")) {
return;
}
}
}
const keyDisplay = renderValueOneLine(key, heapVersion, visited);
const valueDisplay = renderValueOneLine(value, heapVersion, visited);
// log.write(`value: ${inspect(value)}, valueDisplay: ${inspect(valueDisplay)}\n`);
outputs.push(keyDisplay + ": " + valueDisplay);
});
output = "{" + outputs.join(", ") + "}";
if (tag === "type") {
tag = "class";
}
if (tag) {
if (output === "{}") {
output = "";
}
if (tag === "function") {
// get function name
const fun = cache.getFun(fun_id);
if (fun) {
output = `<function ${fun.name}>${output}`;
} else {
output = `<function>${output}`;
}
} else {
output = `<${tag}>${output}`;
}
}
return output;
} else {
throw new Error("Unsupported type: " + inspect(object));
}
}
function renderValueMultiLine(prefix, indent, value, heapVersion, visited, path) {
// log.write(`RenderValueMultiline(${inspect(path)}, ${inspect(value)})\n`);
let object;
let childPath;
if (isHeapRef(value)) {
const ref = value;
const refId = ref.id;
childPath = [...path, refId];
const oid = cache.heapLookup(heapVersion, refId);
if (!oid) {
return `^${refId}`;
}
object = cache.getObject(oid);
if (visited.has(refId) && (typeof object !== "string")) {
return "*" + refId;
}
visited.add(refId);
} else {
object = value;
childPath = path;
}
let lines = [];
if (typeof object === "string" || typeof object === "number" ||
typeof object === "boolean" || object === null || object === undefined) {
lines.push($s(indent).concat(prefix).concat(stringify(object)));
} else if (Array.isArray(object)) {
let begin;
let end;
let tag = object.__tag__;
if (tag === "tuple") {
begin = $s("( ");
end = ")";
} else if (tag === "set") {
begin = $s("{ ");
end = "}";
} else {
begin = $s("[ ");
end = "]";
if (tag) {
begin = $s(`<${tag}>`).concat(begin);
}
}
lines.push($s(indent).concat(prefix).concat(begin));
for (let i = 0; i < object.length; i++) {
let item = object[i];
lines.push(...
renderValue(
$s(""), $s(indent).concat(" "),
item, heapVersion, visited, childPath)
);
if (i !== object.length - 1) {
lines[lines.length - 1] += ", ";
}
}
lines.push($s(indent).concat(end));
} else if (object instanceof Map) {
let tag = object.__tag__;
if (tag === "type") {
tag = "class";
}
let fun_id;
if (tag === "function") {
fun_id = object.get("fun_id");
object = object.get("closure_freevars") || new Map();
} else if (tag === "cell") {
object = object.get("ob_ref");
lines.push(...renderValue($s(prefix), indent, object, heapVersion, visited, childPath));
return lines;
}
let keys = Array.from(object.keys());
if (keys.length === 1 && keys[0] === "__dict__") {
const dict = object.get("__dict__");
const display = renderValue($s(prefix).concat(`<${tag}>`), indent, dict, heapVersion, visited, childPath);
lines.push(...display);
return lines;
}
let begin = "{";
if (tag === "function") {
const fun = cache.getFun(fun_id);
begin = `<${tag} ${fun.name}>${begin}`;
} else if (tag) {
begin = `<${tag}>${begin}`;
}
lines.push($s(indent).concat(prefix).concat(begin));
for (let i = 0; i < keys.length; i++) {
let key = keys[i];
if (isHeapRef(key)) {
const realKey = cache.getHeapObject(heapVersion, key.id);
if (typeof realKey === "string") {
if (realKey.startsWith("__")) {
continue;
}
}
}
const keyDisplay = renderValue($s(""), "", key, heapVersion, visited, childPath);
lines.push(...keyDisplay.slice(0, keyDisplay.length - 1));
const keyDisplayLastLine = keyDisplay[keyDisplay.length - 1];
let value = object.get(key);
const valueDisplay = renderValue(
keyDisplayLastLine + ": ", indent + " ", value, heapVersion, visited, childPath);
lines.push(...valueDisplay);
if (i !== keys.length - 1) {
lines[lines.length - 1] += ", ";
}
}
lines.push(indent + "}");
} else {
throw new Error("Unsupported type: " + inspect(object));
}
return lines;
}
return self;
}
function strTimes(str, num) {
return Array(num + 1).join(str);
}