Skip to content

Commit ba89426

Browse files
committed
bin/vat: add save() to 'shell', load state.json in 'run'
1 parent 20b4e60 commit ba89426

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

bin/vat

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env node
22

3+
const fs = require('fs');
4+
const path = require('path');
35
const process = require('process');
46
const repl = require('repl');
57
require = require('esm')(module);
@@ -41,6 +43,11 @@ async function main() {
4143
};
4244
r.context.run = () => {console.log('run!'); controller.run();};
4345
r.context.step = () => {console.log('step!'); controller.step();};
46+
r.context.save = () => {
47+
fs.writeFileSync(path.resolve(basedir, 'state.json'),
48+
JSON.stringify(controller.getState()));
49+
console.log('state saved to state.json');
50+
};
4451
}
4552
}
4653

src/controller.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import kernelSourceFunc from './bundles/kernel';
1010
import buildKernelNonSES from './kernel/index';
1111
import bundleSource from './build-source-bundle';
1212

13-
export function loadBasedir(basedir, state) {
13+
export function loadBasedir(basedir, stateArg) {
1414
console.log(`= loading config from basedir ${basedir}`);
1515
const vatSources = new Map();
1616
const subs = fs.readdirSync(basedir, { withFileTypes: true });
@@ -37,6 +37,14 @@ export function loadBasedir(basedir, state) {
3737
} catch (e) {
3838
bootstrapIndexJS = undefined;
3939
}
40+
let state;
41+
const stateFile = path.resolve(basedir, 'state.json');
42+
try {
43+
const stateData = fs.readFileSync(stateFile);
44+
state = JSON.parse(stateData);
45+
} catch (e) {
46+
state = stateArg;
47+
}
4048
return harden({ vatSources, bootstrapIndexJS, state });
4149
}
4250

@@ -160,9 +168,11 @@ export async function buildVatController(config, withSES = true, argv = []) {
160168

161169
if (config.state) {
162170
await kernel.loadState(config.state);
171+
console.log(`loadState complete`);
163172
} else if (config.bootstrapIndexJS) {
164173
// we invoke obj[0].bootstrap with an object that contains 'vats' and
165174
// 'argv'.
175+
console.log(`queueing bootstrap()`);
166176
kernel.callBootstrap('_bootstrap', JSON.stringify(argv));
167177
}
168178

0 commit comments

Comments
 (0)