diff --git a/packages/SwingSet/scripts/build-kernel.js b/packages/SwingSet/scripts/build-kernel.js index d5442454a17..62b1a16cef8 100644 --- a/packages/SwingSet/scripts/build-kernel.js +++ b/packages/SwingSet/scripts/build-kernel.js @@ -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); diff --git a/packages/SwingSet/src/controller.js b/packages/SwingSet/src/controller.js index 3f8782bb73b..40f705272cb 100644 --- a/packages/SwingSet/src/controller.js +++ b/packages/SwingSet/src/controller.js @@ -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; }; } @@ -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 { diff --git a/packages/spawner/src/contractHost.js b/packages/spawner/src/contractHost.js index 97abccd53a1..fbfa1b2012d 100644 --- a/packages/spawner/src/contractHost.js +++ b/packages/spawner/src/contractHost.js @@ -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}`, @@ -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. @@ -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; diff --git a/packages/tame-metering/src/ses1.js b/packages/tame-metering/src/ses1.js index 181a0763c94..99555d68a76 100644 --- a/packages/tame-metering/src/ses1.js +++ b/packages/tame-metering/src/ses1.js @@ -23,6 +23,7 @@ const shim = `\ }; })()`; +// Adapt to ESM. export const SES1TameMeteringShim = shim.replace( /_[a-z0-9]{3}\u200d\.g\./gs, '', diff --git a/packages/zoe/src/evalContractCode.js b/packages/zoe/src/evalContractCode.js index 00397f422ef..a157024e643 100644 --- a/packages/zoe/src/evalContractCode.js +++ b/packages/zoe/src/evalContractCode.js @@ -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'; @@ -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', diff --git a/packages/zoe/src/zoe.js b/packages/zoe/src/zoe.js index f44e3b642cf..42568a6b04d 100644 --- a/packages/zoe/src/zoe.js +++ b/packages/zoe/src/zoe.js @@ -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; diff --git a/packages/zoe/test/bundle-source.js b/packages/zoe/test/bundle-source.js new file mode 100644 index 00000000000..d5da8b8cd95 --- /dev/null +++ b/packages/zoe/test/bundle-source.js @@ -0,0 +1,4 @@ +import bundleSource from '@agoric/bundle-source'; + +// Use the new format. +export default src => bundleSource(src, 'nestedEvaluate'); diff --git a/packages/zoe/test/swingsetTests/zoe-metering/test-zoe-metering.js b/packages/zoe/test/swingsetTests/zoe-metering/test-zoe-metering.js index d6c78dd0501..bf379cd2ac3 100644 --- a/packages/zoe/test/swingsetTests/zoe-metering/test-zoe-metering.js +++ b/packages/zoe/test/swingsetTests/zoe-metering/test-zoe-metering.js @@ -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)); diff --git a/packages/zoe/test/swingsetTests/zoe/test-zoe.js b/packages/zoe/test/swingsetTests/zoe/test-zoe.js index c06a8de04b8..d112e8a9a90 100644 --- a/packages/zoe/test/swingsetTests/zoe/test-zoe.js +++ b/packages/zoe/test/swingsetTests/zoe/test-zoe.js @@ -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', diff --git a/packages/zoe/test/unitTests/contracts/test-automaticRefund.js b/packages/zoe/test/unitTests/contracts/test-automaticRefund.js index 392bb33813f..3b543a9b95a 100644 --- a/packages/zoe/test/unitTests/contracts/test-automaticRefund.js +++ b/packages/zoe/test/unitTests/contracts/test-automaticRefund.js @@ -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'; diff --git a/packages/zoe/test/unitTests/contracts/test-autoswap.js b/packages/zoe/test/unitTests/contracts/test-autoswap.js index 5d80335f1fe..5e6bfe958f1 100644 --- a/packages/zoe/test/unitTests/contracts/test-autoswap.js +++ b/packages/zoe/test/unitTests/contracts/test-autoswap.js @@ -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'; diff --git a/packages/zoe/test/unitTests/contracts/test-coveredCall.js b/packages/zoe/test/unitTests/contracts/test-coveredCall.js index 258b2235efa..90d3e650560 100644 --- a/packages/zoe/test/unitTests/contracts/test-coveredCall.js +++ b/packages/zoe/test/unitTests/contracts/test-coveredCall.js @@ -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'; diff --git a/packages/zoe/test/unitTests/contracts/test-myFirstDapp.js b/packages/zoe/test/unitTests/contracts/test-myFirstDapp.js index c3a5cf7e6f5..2a16f9d40d5 100644 --- a/packages/zoe/test/unitTests/contracts/test-myFirstDapp.js +++ b/packages/zoe/test/unitTests/contracts/test-myFirstDapp.js @@ -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'; diff --git a/packages/zoe/test/unitTests/contracts/test-publicAuction.js b/packages/zoe/test/unitTests/contracts/test-publicAuction.js index 6c655abbba7..79f8adb82c0 100644 --- a/packages/zoe/test/unitTests/contracts/test-publicAuction.js +++ b/packages/zoe/test/unitTests/contracts/test-publicAuction.js @@ -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'; diff --git a/packages/zoe/test/unitTests/contracts/test-publicSwap.js b/packages/zoe/test/unitTests/contracts/test-publicSwap.js index 48f3604884b..ba722622b7c 100644 --- a/packages/zoe/test/unitTests/contracts/test-publicSwap.js +++ b/packages/zoe/test/unitTests/contracts/test-publicSwap.js @@ -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'; diff --git a/packages/zoe/test/unitTests/contracts/test-simpleExchange.js b/packages/zoe/test/unitTests/contracts/test-simpleExchange.js index a1f032a3d7a..f26aa0808cb 100644 --- a/packages/zoe/test/unitTests/contracts/test-simpleExchange.js +++ b/packages/zoe/test/unitTests/contracts/test-simpleExchange.js @@ -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';