-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.mjs
42 lines (32 loc) · 1.02 KB
/
host.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
import { Sandbox } from "./sandbox.mjs"
import fs from 'fs'
import assert from 'assert'
const dummyConsole = {
log: (s) => {
console.log(s)
}
}
const globalThis = { console: dummyConsole };
begin();
resume();
function begin() {
const sandbox = new Sandbox({ globalThis });
const appText = fs.readFileSync('app.js', 'utf-8');
const { incr } = sandbox.evaluateCommonJsModule(appText);
assert.equal(incr(), 1);
assert.equal(incr(), 2);
assert.equal(incr(), 3);
console.log('Saving snapshot');
const snapshot = sandbox.takeSnapshot({ incr });
fs.writeFileSync('snapshot.bin', snapshot);
const { ioJournal, exportList } = JSON.parse(snapshot.toString('utf-8'));
fs.writeFileSync('ioJournal.json', JSON.stringify(ioJournal, null, 2));
}
function resume() {
console.log('Resuming from snapshot');
const snapshot = fs.readFileSync('snapshot.bin');
const { incr } = Sandbox.restoreFromSnapshot(snapshot, { globalThis });
assert.equal(incr(), 4);
assert.equal(incr(), 5);
assert.equal(incr(), 6);
}