Skip to content

Commit

Permalink
fix: make code clearer
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Feb 28, 2020
1 parent a41d67d commit efc6b4a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
42 changes: 21 additions & 21 deletions packages/SwingSet/src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import { insistStorageAPI } from './storageAPI';
import { insistCapData } from './capdata';
import { parseVatSlot } from './parseVatSlots';

// FIXME: Put this somewhere better.
process.on('unhandledRejection', e => console.log('unhandledRejection', e));

const ADMIN_DEVICE_PATH = require.resolve('./kernel/vatAdmin/vatAdmin-src');
const ADMIN_VAT_PATH = require.resolve('./kernel/vatAdmin/vatAdminWrapper');

Expand Down Expand Up @@ -197,32 +200,29 @@ function realmRegisterEndOfCrank(fn) {
{ registerEndOfCrank },
);

return src => {
// FIXME: Note that this replaceGlobalMeter endowment is not any
// worse than before metering existed. However, it probably is
// only necessary to be added to the kernel, rather than all
// static vats once we add metering support to the dynamic vat
// implementation.
// FIXME: Same for registerEndOfCrank.

// Cope with ESM problems.
src = src.replace(/(_[a-zA-Z0-9]{3}\u200d\.e)/g, 'eval');

// Support both getExport and nestedEvaluate module format.
return (src, filePrefix = '/SwingSet-bundled-source') => {
const nestedEvaluate = source =>
s.evaluate(source, {
// Support both getExport and nestedEvaluate module format.
require: r,
nestedEvaluate,

// FIXME: Note that this replaceGlobalMeter endowment is not any
// worse than before metering existed. However, it probably is
// only necessary to be added to the kernel, rather than all
// static vats once we add metering support to the dynamic vat
// implementation.
// FIXME: Same for registerEndOfCrank.
registerEndOfCrank: realmRegisterEndOfCrank,
replaceGlobalMeter,
nestedEvaluate,
});
return nestedEvaluate(src)().default;
return nestedEvaluate(src)(filePrefix).default;
};
}

function buildSESKernel(sesEvaluator, endowments) {
const kernelSource = getKernelSource();
const buildKernel = sesEvaluator(kernelSource);
const buildKernel = sesEvaluator(kernelSource, '/SwingSet-kernel');
return buildKernel(endowments);
}

Expand Down Expand Up @@ -266,7 +266,7 @@ export async function buildVatController(config, withSES = true, argv = []) {
// Evaluate source to produce a setup function. This binds withSES from the
// enclosing context and evaluates it either in a SES context, or without SES
// by directly calling require().
async function evaluateToSetup(sourceIndex) {
async function evaluateToSetup(sourceIndex, filePrefix = undefined) {
if (!(sourceIndex[0] === '.' || path.isAbsolute(sourceIndex))) {
throw Error(
'sourceIndex must be relative (./foo) or absolute (/foo) not bare (foo)',
Expand All @@ -284,7 +284,7 @@ export async function buildVatController(config, withSES = true, argv = []) {
'nestedEvaluate',
);
const actualSource = `(${source})\n${sourceMap}`;
setup = sesEvaluator(actualSource);
setup = sesEvaluator(actualSource, filePrefix);
} else {
// eslint-disable-next-line global-require,import/no-dynamic-require
setup = require(`${sourceIndex}`).default;
Expand All @@ -298,8 +298,8 @@ export async function buildVatController(config, withSES = true, argv = []) {
setImmediate,
hostStorage,
runEndOfCrank,
vatAdminDevSetup: await evaluateToSetup(ADMIN_DEVICE_PATH),
vatAdminVatSetup: await evaluateToSetup(ADMIN_VAT_PATH),
vatAdminDevSetup: await evaluateToSetup(ADMIN_DEVICE_PATH, '/SwingSet/src'),
vatAdminVatSetup: await evaluateToSetup(ADMIN_VAT_PATH, '/SwingSet/src'),
};

const kernel = withSES
Expand All @@ -308,12 +308,12 @@ export async function buildVatController(config, withSES = true, argv = []) {

async function addGenesisVat(name, sourceIndex, options = {}) {
console.log(`= adding vat '${name}' from ${sourceIndex}`);
const setup = await evaluateToSetup(sourceIndex);
const setup = await evaluateToSetup(sourceIndex, `/SwingSet-vat-${name}`);
kernel.addGenesisVat(name, setup, options);
}

async function addGenesisDevice(name, sourceIndex, endowments) {
const setup = await evaluateToSetup(sourceIndex);
const setup = await evaluateToSetup(sourceIndex, `/SwingSet-dev-${name}`);
kernel.addGenesisDevice(name, setup, endowments);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/bundle-source/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ The advantage of this format is that it helps preserve the filenames within
the bundle in the event of any stack traces.

Also, the toplevel `getExport(filePrefix = "/bundled-source")` accepts an
optional `filePrefix` argument in order to help sanitize stack
traces (which is prepended to relative paths for the bundled files).
optional `filePrefix` argument (which is prepended to relative paths for the
bundled files) in order to help give context to stack traces.
11 changes: 9 additions & 2 deletions packages/bundle-source/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ return module.exports;
throw Error('need to override nestedEvaluate');
};
function computeExports(filename, powers) {
const { require: myRequire, _log } = powers;
const { require: systemRequire, _log } = powers;
// This captures the endowed require.
const match = filename.match(/^(.*)\/[^\/]+$/);
const thisdir = match ? match[1] : '.';
Expand Down Expand Up @@ -171,7 +171,14 @@ return module.exports;
const code = createEvalString(filename);
if (!code) {
// log('missing code for', filename, sourceBundle);
return myRequire(filename);
if (systemRequire) {
return systemRequire(filename);
}
throw Error(
`require(${JSON.stringify(
filename,
)}) failed; no toplevel require endowment`,
);
}

// log('evaluating', typeof nestedEvaluate, code);
Expand Down

0 comments on commit efc6b4a

Please sign in to comment.