Skip to content

Commit

Permalink
feat(nestedEvaluate): support new moduleFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Feb 28, 2020
1 parent f234d49 commit deb8ee7
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 25 deletions.
1 change: 1 addition & 0 deletions packages/SwingSet/scripts/build-kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import bundleSource from '@agoric/bundle-source';
async function main() {
const { source, sourceMap } = await bundleSource(
`${__dirname}/../src/kernel/index.js`,
'nestedEvaluate',
);
const actualSource = `export default ${source}\n${sourceMap}`;
const f = await fs.promises.open('src/bundles/kernel', 'w', 0o644);
Expand Down
23 changes: 17 additions & 6 deletions packages/SwingSet/src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,19 @@ function realmRegisterEndOfCrank(fn) {
// static vats once we add metering support to the dynamic vat
// implementation.
// FIXME: Same for registerEndOfCrank.
return s.evaluate(src, {
require: r,
registerEndOfCrank: realmRegisterEndOfCrank,
replaceGlobalMeter,
})().default;

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

// Support both getExport and nestedEvaluate module format.
const nestedEvaluate = source =>
s.evaluate(source, {
require: r,
registerEndOfCrank: realmRegisterEndOfCrank,
replaceGlobalMeter,
nestedEvaluate,
});
return nestedEvaluate(src)().default;
};
}

Expand Down Expand Up @@ -271,7 +279,10 @@ export async function buildVatController(config, withSES = true, argv = []) {
// (which is expected to initialize some state and export some facetIDs)
let setup;
if (withSES) {
const { source, sourceMap } = await bundleSource(`${sourceIndex}`);
const { source, sourceMap } = await bundleSource(
`${sourceIndex}`,
'nestedEvaluate',
);
const actualSource = `(${source})\n${sourceMap}`;
setup = sesEvaluator(actualSource);
} else {
Expand Down
29 changes: 22 additions & 7 deletions packages/spawner/src/contractHost.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,18 @@ function makeContractHost(E, evaluate, additionalEndowments = {}) {
return meter;
};

const fn = evaluate(functionSrcString, {
...fullEndowments,
getMeter,
});
// Inject the evaluator.
const nestedEvaluate = src => {
const allEndowments = {
...fullEndowments,
getMeter,
nestedEvaluate,
};
// console.log(allEndowments, src);
return evaluate(src, allEndowments);
};

const fn = nestedEvaluate(functionSrcString);
assert(
typeof fn === 'function',
`"${functionSrcString}" must be a string for a function, but produced ${typeof fn}`,
Expand Down Expand Up @@ -140,7 +148,10 @@ function makeContractHost(E, evaluate, additionalEndowments = {}) {
let installation;
if (moduleFormat === 'object') {
installation = extractCheckFunctions(contractSrcs);
} else if (moduleFormat === 'getExport') {
} else if (
moduleFormat === 'getExport' ||
moduleFormat === 'nestedEvaluate'
) {
// We don't support 'check' functions in getExport format,
// because we only do a single evaluate, and the whole
// contract must be metered per-spawn, not per-installation.
Expand All @@ -161,8 +172,12 @@ function makeContractHost(E, evaluate, additionalEndowments = {}) {
function spawn(termsP) {
let startFn;
if (moduleFormat === 'object') {
startFn = evaluateStringToFn(contractSrcs.start);
} else if (moduleFormat === 'getExport') {
startFn = evaluateStringToFn(contractSrcs.start, );
} else if (
moduleFormat === 'getExport' ||
moduleFormat === 'nestedEvaluate'
) {
// We support getExport because it is forward-compatible with nestedEvaluate.
const getExports = evaluateStringToFn(contractSrcs);
const ns = getExports();
startFn = ns.default;
Expand Down
1 change: 1 addition & 0 deletions packages/tame-metering/src/ses1.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const shim = `\
};
})()`;

// Adapt to ESM.
export const SES1TameMeteringShim = shim.replace(
/_[a-z0-9]{3}\u200d\.g\./gs,
'',
Expand Down
6 changes: 4 additions & 2 deletions packages/zoe/src/evalContractCode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* global replaceGlobalMeter, registerEndOfCrank */
import evaluate from '@agoric/evaluate';
import { evaluateProgram } from '@agoric/evaluate';
import Nat from '@agoric/nat';
import harden from '@agoric/harden';

Expand All @@ -12,7 +12,9 @@ import { sameStructure } from '@agoric/same-structure';

const evaluateStringToFn = (functionSrcString, endowments) => {
assert.typeof(functionSrcString, 'string');
const fn = evaluate(functionSrcString, endowments);
const nestedEvaluate = src =>
evaluateProgram(src, { ...endowments, nestedEvaluate });
const fn = nestedEvaluate(`(${functionSrcString})`);
assert.typeof(
fn,
'function',
Expand Down
3 changes: 2 additions & 1 deletion packages/zoe/src/zoe.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,10 @@ const makeZoe = (additionalEndowments = {}) => {
* registering it with Zoe. We have a moduleFormat to allow for
* different future formats without silent failures.
*/
install: (code, moduleFormat = 'getExport') => {
install: (code, moduleFormat = 'nestedEvaluate') => {
let installation;
switch (moduleFormat) {
case 'nestedEvaluate':
case 'getExport': {
installation = evalContractCode(code, additionalEndowments);
break;
Expand Down
4 changes: 4 additions & 0 deletions packages/zoe/test/bundle-source.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import bundleSource from '@agoric/bundle-source';

// Use the new format.
export default src => bundleSource(src, 'nestedEvaluate');
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test } from 'tape-promise/tape';
import { loadBasedir, buildVatController } from '@agoric/swingset-vat';
import path from 'path';
import fs from 'fs';
import bundleSource from '@agoric/bundle-source';
import bundleSource from '../../bundle-source';

// Don't let unhandled promises crash our process.
process.on('unhandledRejection', e => console.log('unhandled rejection', e));
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/test/swingsetTests/zoe/test-zoe.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { test } from 'tape-promise/tape';
import { loadBasedir, buildVatController } from '@agoric/swingset-vat';
import path from 'path';
import fs from 'fs';
import bundleSource from '@agoric/bundle-source';
import bundleSource from '../../bundle-source';

const CONTRACT_FILES = [
'automaticRefund',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from 'tape-promise/tape';
import harden from '@agoric/harden';
import bundleSource from '@agoric/bundle-source';
import bundleSource from '../../bundle-source';

import { makeZoe } from '../../../src/zoe';
import { setup } from '../setupBasicMints';
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/test/unitTests/contracts/test-autoswap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from 'tape-promise/tape';
import harden from '@agoric/harden';
import bundleSource from '@agoric/bundle-source';
import bundleSource from '../../bundle-source';

import { makeZoe } from '../../../src/zoe';
import { setup } from '../setupBasicMints';
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/test/unitTests/contracts/test-coveredCall.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from 'tape-promise/tape';
import harden from '@agoric/harden';
import bundleSource from '@agoric/bundle-source';
import bundleSource from '../../bundle-source';
import { sameStructure } from '@agoric/same-structure';

import buildManualTimer from '../../../tools/manualTimer';
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/test/unitTests/contracts/test-myFirstDapp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from 'tape-promise/tape';
import harden from '@agoric/harden';
import bundleSource from '@agoric/bundle-source';
import bundleSource from '../../bundle-source';

import { makeZoe } from '../../../src/zoe';
import { setup } from '../setupBasicMints';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from 'tape-promise/tape';
import harden from '@agoric/harden';
import bundleSource from '@agoric/bundle-source';
import bundleSource from '../../bundle-source';

import { makeZoe } from '../../../src/zoe';
import { setup } from '../setupBasicMints';
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/test/unitTests/contracts/test-publicSwap.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from 'tape-promise/tape';
import harden from '@agoric/harden';
import bundleSource from '@agoric/bundle-source';
import bundleSource from '../../bundle-source';

import { makeZoe } from '../../../src/zoe';
import { setup } from '../setupBasicMints';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test } from 'tape-promise/tape';
import harden from '@agoric/harden';
import bundleSource from '@agoric/bundle-source';
import bundleSource from '../../bundle-source';

import { makeZoe } from '../../../src/zoe';
import { setup } from '../setupBasicMints';
Expand Down

0 comments on commit deb8ee7

Please sign in to comment.