Skip to content

Commit efc6b4a

Browse files
committed
fix: make code clearer
1 parent a41d67d commit efc6b4a

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

packages/SwingSet/src/controller.js

+21-21
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import { insistStorageAPI } from './storageAPI';
2626
import { insistCapData } from './capdata';
2727
import { parseVatSlot } from './parseVatSlots';
2828

29+
// FIXME: Put this somewhere better.
30+
process.on('unhandledRejection', e => console.log('unhandledRejection', e));
31+
2932
const ADMIN_DEVICE_PATH = require.resolve('./kernel/vatAdmin/vatAdmin-src');
3033
const ADMIN_VAT_PATH = require.resolve('./kernel/vatAdmin/vatAdminWrapper');
3134

@@ -197,32 +200,29 @@ function realmRegisterEndOfCrank(fn) {
197200
{ registerEndOfCrank },
198201
);
199202

200-
return src => {
201-
// FIXME: Note that this replaceGlobalMeter endowment is not any
202-
// worse than before metering existed. However, it probably is
203-
// only necessary to be added to the kernel, rather than all
204-
// static vats once we add metering support to the dynamic vat
205-
// implementation.
206-
// FIXME: Same for registerEndOfCrank.
207-
208-
// Cope with ESM problems.
209-
src = src.replace(/(_[a-zA-Z0-9]{3}\u200d\.e)/g, 'eval');
210-
211-
// Support both getExport and nestedEvaluate module format.
203+
return (src, filePrefix = '/SwingSet-bundled-source') => {
212204
const nestedEvaluate = source =>
213205
s.evaluate(source, {
206+
// Support both getExport and nestedEvaluate module format.
214207
require: r,
208+
nestedEvaluate,
209+
210+
// FIXME: Note that this replaceGlobalMeter endowment is not any
211+
// worse than before metering existed. However, it probably is
212+
// only necessary to be added to the kernel, rather than all
213+
// static vats once we add metering support to the dynamic vat
214+
// implementation.
215+
// FIXME: Same for registerEndOfCrank.
215216
registerEndOfCrank: realmRegisterEndOfCrank,
216217
replaceGlobalMeter,
217-
nestedEvaluate,
218218
});
219-
return nestedEvaluate(src)().default;
219+
return nestedEvaluate(src)(filePrefix).default;
220220
};
221221
}
222222

223223
function buildSESKernel(sesEvaluator, endowments) {
224224
const kernelSource = getKernelSource();
225-
const buildKernel = sesEvaluator(kernelSource);
225+
const buildKernel = sesEvaluator(kernelSource, '/SwingSet-kernel');
226226
return buildKernel(endowments);
227227
}
228228

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

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

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

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

packages/bundle-source/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ The advantage of this format is that it helps preserve the filenames within
5454
the bundle in the event of any stack traces.
5555

5656
Also, the toplevel `getExport(filePrefix = "/bundled-source")` accepts an
57-
optional `filePrefix` argument in order to help sanitize stack
58-
traces (which is prepended to relative paths for the bundled files).
57+
optional `filePrefix` argument (which is prepended to relative paths for the
58+
bundled files) in order to help give context to stack traces.

packages/bundle-source/src/index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ return module.exports;
119119
throw Error('need to override nestedEvaluate');
120120
};
121121
function computeExports(filename, powers) {
122-
const { require: myRequire, _log } = powers;
122+
const { require: systemRequire, _log } = powers;
123123
// This captures the endowed require.
124124
const match = filename.match(/^(.*)\/[^\/]+$/);
125125
const thisdir = match ? match[1] : '.';
@@ -171,7 +171,14 @@ return module.exports;
171171
const code = createEvalString(filename);
172172
if (!code) {
173173
// log('missing code for', filename, sourceBundle);
174-
return myRequire(filename);
174+
if (systemRequire) {
175+
return systemRequire(filename);
176+
}
177+
throw Error(
178+
`require(${JSON.stringify(
179+
filename,
180+
)}) failed; no toplevel require endowment`,
181+
);
175182
}
176183

177184
// log('evaluating', typeof nestedEvaluate, code);

0 commit comments

Comments
 (0)