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
// JavaScript that exhausts the engine's heap, for the heap limits to stop.
const grow = (keep) => {
for (;;) {
// Rows are built by pushing: V8 does not deliver termination to a hot
// loop around `new Array(n).fill(value)` for large n.
const row = [];
for (let index = 0; index < 1024; index += 1) {
row.push(index);
}
keep.push(row);
}
};
globalThis.fetchletExports = {
/** Return at once. */
ready() {
return "ready";
},
/** Grow an array of arrays until the heap is exhausted. */
grow() {
grow([]);
},
/** Catch the out-of-memory error, free the memory and carry on. */
swallow() {
const keep = [];
for (;;) {
try {
grow(keep);
} catch {
keep.length = 0;
}
}
},
/** Allocate an ArrayBuffer and report the error's name if it fails. */
buffer(bytes) {
try {
return new ArrayBuffer(bytes).byteLength;
} catch (error) {
return error.name;
}
},
};