Repository navigation
Expand file tree
/
Copy pathdocumentation.html
More file actions
678 lines (634 loc) · 23.6 KB
/
Copy pathdocumentation.html
File metadata and controls
678 lines (634 loc) · 23.6 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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" type="image/svg+xml" href="./logo.svg" />
<title>Documentation — Java Memory Playground</title>
<meta
name="description"
content="How to read and build stack and heap diagrams with the Java Memory Playground: steps, the garbage collector, the teacher's playground, sharing, and embedding."
/>
<style>
:root {
/* The OpenPatch palette, from the branding repository. */
--green: #007864;
--deep-forest: #004c45;
--mint: #b5e3d8;
--coal: #242428;
--charcoal: #3c3c3c;
--quicksilver: #a4a4a4;
--silver: #d6d6d6;
--whitesmoke: #f5f5f5;
--white: #ffffff;
--bg: var(--white);
--fg: var(--coal);
--muted: var(--charcoal);
--rule: var(--silver);
--accent: var(--green);
--code-bg: var(--whitesmoke);
--note-bg: var(--mint);
--note-rule: var(--green);
--max: 46rem;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: var(--coal);
--fg: var(--whitesmoke);
--muted: var(--quicksilver);
--rule: var(--charcoal);
/* The brand green is too dark to read on Coal, so on a dark ground
the accent is Fresh Mint, which is the same hue lightened. */
--accent: var(--mint);
--code-bg: #1b1b1f;
--note-bg: var(--deep-forest);
--note-rule: var(--mint);
}
}
* {
box-sizing: border-box;
}
body {
margin: 0;
padding: 0 1.25rem 5rem;
background: var(--bg);
color: var(--fg);
font-family: Inter, system-ui, -apple-system, "Segoe UI", Helvetica,
Arial, sans-serif;
line-height: 1.65;
font-size: 17px;
}
header,
main,
footer {
max-width: var(--max);
margin: 0 auto;
}
header {
padding: 3rem 0 1.5rem;
border-bottom: 1px solid var(--rule);
}
h1 {
font-size: 2rem;
line-height: 1.2;
margin: 0 0 0.5rem;
}
header p {
color: var(--muted);
margin: 0 0 1.25rem;
}
h2 {
font-size: 1.4rem;
margin: 3rem 0 0.75rem;
padding-top: 0.75rem;
border-top: 1px solid var(--rule);
}
h3 {
font-size: 1.1rem;
margin: 2rem 0 0.5rem;
}
a {
color: var(--accent);
}
code {
background: var(--code-bg);
border-radius: 4px;
padding: 0.1em 0.35em;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 0.9em;
}
pre {
background: var(--code-bg);
border-radius: 8px;
padding: 1rem;
overflow-x: auto;
}
pre code {
background: none;
padding: 0;
font-size: 0.85em;
line-height: 1.5;
}
.table-wrap {
overflow-x: auto;
}
table {
border-collapse: collapse;
width: 100%;
margin: 1rem 0;
font-size: 0.94em;
}
th,
td {
text-align: left;
padding: 0.5rem 0.7rem;
border-bottom: 1px solid var(--rule);
vertical-align: top;
}
th {
font-weight: 600;
white-space: nowrap;
}
td code {
white-space: nowrap;
}
.note {
background: var(--note-bg);
border-left: 3px solid var(--note-rule);
border-radius: 0 6px 6px 0;
padding: 0.85rem 1rem;
margin: 1.25rem 0;
}
.note p {
margin: 0;
}
.toc {
margin: 1.5rem 0 0;
padding: 0;
list-style: none;
columns: 2;
column-gap: 2rem;
font-size: 0.95em;
}
.toc li {
margin: 0 0 0.35rem;
break-inside: avoid;
}
@media (max-width: 34rem) {
.toc {
columns: 1;
}
}
.back {
display: inline-block;
text-decoration: none;
font-weight: 600;
}
footer {
margin-top: 4rem;
padding-top: 1.5rem;
border-top: 1px solid var(--rule);
color: var(--muted);
font-size: 0.92em;
}
footer a {
margin-right: 1rem;
}
</style>
</head>
<body>
<header>
<a class="back" href="./">← Back to the playground</a>
<h1>Java Memory Playground</h1>
<p>
Draw what a Java program has in memory: the call stack on one side, the
objects on the heap on the other, and the references between them.
Nothing here runs your code — you build the picture, which is what makes
it a thinking tool rather than a debugger.
</p>
<ul class="toc">
<li><a href="#reading">Reading a diagram</a></li>
<li><a href="#building">Building one</a></li>
<li><a href="#values">Values and references</a></li>
<li><a href="#steps">Steps and traces</a></li>
<li><a href="#gc">The garbage collector</a></li>
<li><a href="#edit">The teacher's playground</a></li>
<li><a href="#classes">Classes from Java source</a></li>
<li><a href="#options">Options</a></li>
<li><a href="#shortcuts">Keyboard shortcuts</a></li>
<li><a href="#sharing">Sharing a diagram</a></li>
<li><a href="#embedding">Embedding</a></li>
<li><a href="#files">.jmp files and VS Code</a></li>
</ul>
</header>
<main>
<h2 id="reading">Reading a diagram</h2>
<p>
A diagram has two kinds of box. A <strong>stack frame</strong> is one
method call, labelled with its depth and name, holding that call's local
variables. An <strong>object</strong> is one instance on the heap,
labelled <code>:ClassName</code> and holding its fields.
</p>
<p>
An arrow is a <strong>reference</strong>. It leaves the exact variable
or field that holds it, so you can see which field of which object
points where. A variable with no arrow holds <code>null</code>.
</p>
<p>
Frames stack in call order, and only the frame on top can return — the
<code>return</code> button on the others is disabled, because a call in
the middle of the stack cannot finish before the ones above it. That
restriction is the lesson, so the button stays visible rather than
disappearing.
</p>
<h2 id="building">Building one</h2>
<p>
The palette in the top left corner is the source of new things. Drag one
onto the canvas:
</p>
<ul>
<li>
<strong>new <em>ClassName</em></strong> — an instance of one of your
configured classes, with a field for each attribute.
</li>
<li>
<strong>new Array</strong> — pick a length and an element type. The
<em>length</em> field is then fixed, as it is in Java: the elements
can be edited, the number of them cannot.
</li>
<li>
<strong>Call Method</strong> — a new frame on top of the stack.
</li>
<li>
<strong>Declare Global Variable</strong> — a variable belonging to no
frame. Java has no globals; this is scaffolding for the lessons that
come before methods do.
</li>
</ul>
<p>
To make a reference, drag from the handle on the right of a variable or
field to the object it should point at. Drag the arrow away again to set
it back to <code>null</code>. Inside a frame,
<strong>Declare Local Variable</strong> adds a variable, and
<code>return</code> pops the frame — along with the references it held,
which is often what makes something garbage.
</p>
<h2 id="values">Values and references</h2>
<p>
Primitive fields are edited in place: a number field for the numeric
types, a checkbox for <code>boolean</code>. Reference fields get a
handle to drag an arrow from instead.
</p>
<p>
A <code>String</code> is shown inside the thing that references it, in
quotes, rather than as its own box on the heap. A diagram about a linked
list should not sprout a box for every name. When the point of the
lesson <em>is</em> that a String is an object like any other —
<code>==</code> against <code>.equals()</code>, the string pool — turn
<code>inlineStrings</code> off and it gets a box and an arrow like
everything else. The object is there either way; only the drawing
changes.
</p>
<h2 id="steps">Steps and traces</h2>
<p>
A diagram can be a single picture or a sequence of them. The bar along
the bottom moves between steps, and each step can carry a label saying
what happens there — <em>head = new Node(1)</em>. It is one strip: the
dots for the trace, then the step’s label, then what you can do
about it.
</p>
<p>
<strong>Add step</strong> duplicates the current step and continues from
it, so a trace is built by changing one thing at a time. What changed
since the previous step is marked, which is usually the whole point of a
trace; <code>hideStepChanges</code> turns that off.
</p>
<p>
A step can be marked as an <strong>exercise</strong>. The student is
handed the previous step and builds this one themselves, then checks
their answer against what the step actually contains.
</p>
<p>
Beside the label, a step can carry a <strong>hint</strong>. It is not on
screen to begin with — a hint that is always visible is part of the
task, and the exercise is then a different one. The student presses
<strong>Hint</strong> when they want it, which puts a rung between
trying again and <strong>Show solution</strong>. Write the hint as the
nudge you would give over a shoulder, not as the answer in prose.
</p>
<p>
A step can also carry <strong>statements</strong> — lines such as
<code>neu.next = current.next</code> that the reader runs by pressing
them, in whatever order they choose. Each one is evaluated against the
diagram as it stands, so the right-hand side is read from the picture
rather than from the text: run the two lines of an insertion the wrong
way round and the new node ends up pointing at itself, with the rest of
the list greyed out behind it. A numbered log keeps the order that was
chosen, and <strong>Start over</strong> puts the diagram back to before
the first line ran. Dragging edges still works — the lines are another
way in, not a replacement. Authors write them beside the label,
separated by semicolons.
</p>
<h2 id="gc">The garbage collector</h2>
<p>
<strong>Run Garbage Collector</strong>, under the palette, removes every
object that cannot be reached from a variable or a stack frame.
Unreachable objects are already tinted before you press it, so you can
see what is about to go. It sits with the palette because the two are
the same kind of thing: one puts objects on the canvas, the other takes
them away.
</p>
<p>
With the <code>gcPrediction</code> option on, you commit first: click
the objects you believe are unreachable, then
<strong>Check and collect</strong> scores what you found, what you
missed, and what you marked that was still reachable. Committing to an
answer before seeing it is where the learning is.
</p>
<h2 id="edit">The teacher's playground — the <code>?edit</code> URL</h2>
<p>
There are two playgrounds, and which one you get is decided by the URL.
</p>
<div class="table-wrap">
<table>
<thead>
<tr>
<th>URL</th>
<th>What you get</th>
</tr>
</thead>
<tbody>
<tr>
<td>the app’s address</td>
<td>
The student's playground: the whole diagram, every edit, and the
steps of a trace to walk through.
</td>
</tr>
<tr>
<td>the same, with <code>?edit</code></td>
<td>
The teacher's: the same, plus class configuration and step
authoring.
</td>
</tr>
</tbody>
</table>
</div>
<p>
<code>/edit</code> as a path works too, where the host has a rewrite
rule for it. The query flag <code>?edit</code> always works, because the
app is served as static files.
</p>
<div class="note">
<p>
The split is about which tools are on screen, not about what a student
is allowed to touch. A student still builds objects, connects
references, walks the steps and runs the collector. What the teacher's
playground adds is deciding <em>which classes exist</em> and
<em>what the steps are</em> — the setup for an exercise, not the
working of it.
</p>
</div>
<p>
The diagram itself lives in the part of the URL after the
<code>#</code>, and the mode is deliberately kept out of it. That means
one diagram has both an editing link and a working link: take a link you
saved from the teacher's playground, drop the <code>?edit</code>, and
you have the student's view of the same picture.
</p>
<h2 id="classes">Classes from Java source</h2>
<p>
The teacher's playground asks for your classes instead of making you
describe each field through a dialog. Paste the Java:
</p>
<pre><code>class Node {
int value;
Node next;
}</code></pre>
<p>
Only the structure is read — the class names, and the name and type of
each field. Method bodies are skipped and
<strong>nothing is executed</strong>. Records work too, and their
components are read as fields.
</p>
<p>
Applying classes reaches every step of the diagram, so when a change
would cost something already drawn, you are told what before it happens:
which field is about to be deleted from how many objects, and which
objects belong to a class that no longer exists. A field that holds
nothing — an untouched <code>int</code> still at <code>0</code>, a
reference still <code>null</code> — is not counted, because losing it
costs nothing.
</p>
<h2 id="options">Options</h2>
<p>
Options are part of the diagram, so they travel with a shared link. The
configuration view has presets — <em>References only</em>,
<em>With the stack</em>, <em>Everything</em> — for the common shapes of
lesson. The individual switches:
</p>
<div class="table-wrap">
<table>
<thead>
<tr>
<th>Option</th>
<th>Default</th>
<th>Effect</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>hideSidebar</code></td>
<td>off</td>
<td>Hide the palette, for a diagram meant only to be read.</td>
</tr>
<tr>
<td><code>hideCallMethod</code></td>
<td>off</td>
<td>Drop <em>Call Method</em> from the palette.</td>
</tr>
<tr>
<td><code>hideDeclareGlobalVariable</code></td>
<td><strong>on</strong></td>
<td>
Drop <em>Declare Global Variable</em>. On by default, because
Java has no globals.
</td>
</tr>
<tr>
<td><code>hideNewArray</code></td>
<td>off</td>
<td>Drop <em>new Array</em>.</td>
</tr>
<tr>
<td><code>disableGarbageCollector</code></td>
<td>off</td>
<td>No collector button, and no tinting of unreachable objects.</td>
</tr>
<tr>
<td><code>gcPrediction</code></td>
<td>off</td>
<td>Ask which objects are unreachable before collecting them.</td>
</tr>
<tr>
<td><code>createNewOnEdgeDrop</code></td>
<td>off</td>
<td>
Dropping an arrow on empty canvas creates the object it points
at.
</td>
</tr>
<tr>
<td><code>inlineStrings</code></td>
<td><strong>on</strong></td>
<td>
Draw a <code>String</code> inside its holder instead of as its
own heap box.
</td>
</tr>
<tr>
<td><code>hideSteps</code></td>
<td>off</td>
<td>Hide the step bar, for a lesson about one picture.</td>
</tr>
<tr>
<td><code>hideStepChanges</code></td>
<td>off</td>
<td>Stop marking what a step changed since the one before it.</td>
</tr>
</tbody>
</table>
</div>
<h2 id="shortcuts">Keyboard shortcuts</h2>
<div class="table-wrap">
<table>
<thead>
<tr>
<th>Shortcut</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr><td><code>Ctrl/Cmd + S</code></td><td>Save</td></tr>
<tr><td><code>Ctrl/Cmd + Z</code></td><td>Undo</td></tr>
<tr><td><code>Ctrl/Cmd + Y</code></td><td>Redo</td></tr>
<tr><td><code>Ctrl/Cmd + ,</code></td><td>Toggle the configuration view</td></tr>
<tr><td><code>Ctrl/Cmd + +</code></td><td>Zoom in</td></tr>
<tr><td><code>Ctrl/Cmd + -</code></td><td>Zoom out</td></tr>
<tr><td><code>Ctrl/Cmd + 0</code></td><td>Reset the zoom</td></tr>
<tr><td><code>Shift + !</code></td><td>Fit the diagram to the window</td></tr>
</tbody>
</table>
</div>
<p>
A shortcut is never taken from someone typing into a field. In an
embedded playground the bindings can be changed, or unbound, through the
<code>keyBindings</code> attribute.
</p>
<h2 id="sharing">Sharing a diagram</h2>
<p>
<strong>Save (URL)</strong> writes the whole diagram — classes, options,
every step — into the address bar, compressed and encoded after the
<code>#</code>. The link is the document: send it to a class, put it in
a worksheet, or bookmark it. There is no account and nothing is stored
on a server.
</p>
<p>
Because the state is in the fragment, it never reaches the server, and a
very large diagram makes a very long link. <strong>Download (PNG)</strong>
exports the picture, and with more than one step,
<strong>Download all steps</strong> gives one image containing the whole
trace for a printed worksheet.
</p>
<p>
<strong>B&W</strong> switches the whole playground to black and
white, and the downloads follow. It is not a filter over the colours:
the heap turns white, a stack frame grey, and everything the diagram was
saying in colour is said again in line style: an unreachable object is
the box with a dotted border, and what a step changed is dashed —
the ring around a node, or the reference itself. Photocopy it and it
still reads.
</p>
<h2 id="embedding">Embedding</h2>
<p>
The playground is published as a framework-agnostic web component. Two
elements are defined, matching the two playgrounds:
</p>
<pre><code><script src="path/to/index.umd.js"></script>
<!-- the student's -->
<java-memory-playground id="playground"></java-memory-playground>
<!-- the teacher's -->
<java-memory-playground-editor></java-memory-playground-editor>
<script>
const playground = document.getElementById("playground");
playground.setAttribute("memory", JSON.stringify(diagram));
playground.addEventListener("change", (e) => console.log(e.detail));
</script></code></pre>
<p>
The bundle is published as
<code>@java-memory-playground/web-component</code>. An
embedded playground never touches the URL of its host page, and several
of them can share one page. Both elements take the same attributes:</p>
<div class="table-wrap">
<table>
<thead>
<tr>
<th>Attribute</th>
<th>Type</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>memory</code></td>
<td>string</td>
<td>The diagram, as JSON or as a saved link's fragment.</td>
</tr>
<tr>
<td><code>options</code></td>
<td>JSON</td>
<td>The options above, overriding what the diagram carries.</td>
</tr>
<tr>
<td><code>language</code></td>
<td>string</td>
<td>
<code>en</code> or <code>de</code>. Follows the browser
otherwise.
</td>
</tr>
<tr>
<td><code>step</code></td>
<td>number</td>
<td>
Which step to show, counted from zero, so prose around the
diagram can walk a reader through a trace.
</td>
</tr>
<tr>
<td><code>persistence</code></td>
<td>boolean</td>
<td>
Off for an embedded playground: it must not take over the URL of
the page hosting it.
</td>
</tr>
<tr>
<td><code>keyBindings</code></td>
<td>JSON</td>
<td>Overrides for the shortcuts above.</td>
</tr>
</tbody>
</table>
</div>
<p>
It reports back with two events: <code>change</code>, carrying the whole
diagram when the user saves, and <code>stepchange</code>, carrying the
step index whenever the shown step moves.
</p>
<p>
For a React application, the component is also published on its own as
<code>@java-memory-playground/java-memory-playground</code>.
</p>
<h2 id="files">.jmp files and VS Code</h2>
<p>
A <code>.jmp</code> file is exactly the JSON that a shared link carries,
so a diagram moves between the app, an embedded playground and a file
unchanged. It is an ordinary text file: diffable, reviewable, and fine
to keep in a repository next to the code it illustrates.
</p>
<p>
<strong>Java Memory Playground Studio</strong> opens those files in
VS Code as diagrams rather than as JSON, with the editor's own undo and
save. <em>Show Source</em> switches to the JSON and
<em>Show Diagram</em> switches back.
</p>
</main>
<footer>
<a href="./">Playground</a>
<a href="https://github.com/openpatch/java-memory-playground">GitHub</a>
<a href="https://openpatch.org">OpenPatch</a>
</footer>
</body>
</html>