Skip to content

Commit

Permalink
bin/vat: add save() to 'shell', load state.json in 'run'
Browse files Browse the repository at this point in the history
  • Loading branch information
warner committed Apr 4, 2019
1 parent 20b4e60 commit ba89426
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
7 changes: 7 additions & 0 deletions bin/vat
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

const fs = require('fs');
const path = require('path');
const process = require('process');
const repl = require('repl');
require = require('esm')(module);
Expand Down Expand Up @@ -41,6 +43,11 @@ async function main() {
};
r.context.run = () => {console.log('run!'); controller.run();};
r.context.step = () => {console.log('step!'); controller.step();};
r.context.save = () => {
fs.writeFileSync(path.resolve(basedir, 'state.json'),
JSON.stringify(controller.getState()));
console.log('state saved to state.json');
};
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import kernelSourceFunc from './bundles/kernel';
import buildKernelNonSES from './kernel/index';
import bundleSource from './build-source-bundle';

export function loadBasedir(basedir, state) {
export function loadBasedir(basedir, stateArg) {
console.log(`= loading config from basedir ${basedir}`);
const vatSources = new Map();
const subs = fs.readdirSync(basedir, { withFileTypes: true });
Expand All @@ -37,6 +37,14 @@ export function loadBasedir(basedir, state) {
} catch (e) {
bootstrapIndexJS = undefined;
}
let state;
const stateFile = path.resolve(basedir, 'state.json');
try {
const stateData = fs.readFileSync(stateFile);
state = JSON.parse(stateData);
} catch (e) {
state = stateArg;
}
return harden({ vatSources, bootstrapIndexJS, state });
}

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

if (config.state) {
await kernel.loadState(config.state);
console.log(`loadState complete`);
} else if (config.bootstrapIndexJS) {
// we invoke obj[0].bootstrap with an object that contains 'vats' and
// 'argv'.
console.log(`queueing bootstrap()`);
kernel.callBootstrap('_bootstrap', JSON.stringify(argv));
}

Expand Down

0 comments on commit ba89426

Please sign in to comment.