From 49d1271909d8dc61a4b27d28caf1587565586168 Mon Sep 17 00:00:00 2001 From: Michael FIG Date: Thu, 13 Aug 2020 12:50:53 -0600 Subject: [PATCH] refactor: inherit HandledPromise from install-ses --- packages/SwingSet/src/controller.js | 3 --- packages/SwingSet/src/kernel/liveSlots.js | 3 +-- .../src/kernel/vatManager/nodeWorkerSupervisor.js | 2 -- .../src/kernel/vatManager/subprocessSupervisor.js | 2 -- packages/captp/lib/captp.js | 2 +- packages/cosmic-swingset/lib/ag-solo/vats/repl.js | 3 +-- packages/cosmic-swingset/test/captp-fixture.js | 4 ++-- packages/eventual-send/README.md | 2 +- packages/eventual-send/src/E.js | 2 +- packages/eventual-send/src/index.js | 6 +++--- packages/eventual-send/test/get-hp.js | 5 +++++ packages/install-ses/package.json | 1 + packages/produce-promise/src/producePromise.js | 10 +++++----- packages/promise-kit/src/promiseKit.js | 10 +++++----- packages/spawner/src/contractHost.js | 14 +------------- packages/zoe/src/contractSupport/zoeHelpers.js | 4 ++-- packages/zoe/src/evalContractCode.js | 5 ++--- 17 files changed, 31 insertions(+), 47 deletions(-) create mode 100644 packages/eventual-send/test/get-hp.js diff --git a/packages/SwingSet/src/controller.js b/packages/SwingSet/src/controller.js index cae53373544..eff717ce9b0 100644 --- a/packages/SwingSet/src/controller.js +++ b/packages/SwingSet/src/controller.js @@ -14,7 +14,6 @@ import { isTamed, tameMetering } from '@agoric/tame-metering'; import bundleSource from '@agoric/bundle-source'; import { importBundle } from '@agoric/import-bundle'; import { initSwingStore } from '@agoric/swing-store-simple'; -import { HandledPromise } from '@agoric/eventual-send'; import { makeMeteringTransformer } from '@agoric/transform-metering'; import { makeTransform } from '@agoric/transform-eventual-send'; @@ -258,7 +257,6 @@ export async function buildVatController( endowments: { console: makeConsole(`${debugPrefix}SwingSet:kernel`), require: kernelRequire, - HandledPromise, }, }); const buildKernel = kernelNS.default; @@ -292,7 +290,6 @@ export async function buildVatController( function makeVatEndowments(consoleTag) { return harden({ console: makeConsole(`${debugPrefix}SwingSet:${consoleTag}`), - HandledPromise, // re2 is a RegExp work-a-like that disables backtracking expressions for // safer memory consumption RegExp: re2, diff --git a/packages/SwingSet/src/kernel/liveSlots.js b/packages/SwingSet/src/kernel/liveSlots.js index 6bc9cf22cc5..20de8c7887d 100644 --- a/packages/SwingSet/src/kernel/liveSlots.js +++ b/packages/SwingSet/src/kernel/liveSlots.js @@ -1,6 +1,5 @@ -/* global harden */ +/* global harden HandledPromise */ -import { HandledPromise } from '@agoric/eventual-send'; import { QCLASS, Remotable, diff --git a/packages/SwingSet/src/kernel/vatManager/nodeWorkerSupervisor.js b/packages/SwingSet/src/kernel/vatManager/nodeWorkerSupervisor.js index 3cbd6288c7e..ae1a6b4ae1e 100644 --- a/packages/SwingSet/src/kernel/vatManager/nodeWorkerSupervisor.js +++ b/packages/SwingSet/src/kernel/vatManager/nodeWorkerSupervisor.js @@ -8,7 +8,6 @@ import anylogger from 'anylogger'; import { assert } from '@agoric/assert'; import { importBundle } from '@agoric/import-bundle'; import { Remotable, getInterfaceOf } from '@agoric/marshal'; -import { HandledPromise } from '@agoric/eventual-send'; import { waitUntilQuiescent } from '../../waitUntilQuiescent'; import { makeLiveSlots } from '../liveSlots'; @@ -77,7 +76,6 @@ parentPort.on('message', ([type, ...margs]) => { const [bundle, vatParameters] = margs; const endowments = { console: makeConsole(`SwingSet:vatWorker`), - HandledPromise, }; importBundle(bundle, { endowments }).then(vatNS => { workerLog(`got vatNS:`, Object.keys(vatNS).join(',')); diff --git a/packages/SwingSet/src/kernel/vatManager/subprocessSupervisor.js b/packages/SwingSet/src/kernel/vatManager/subprocessSupervisor.js index 977a52faeda..739dd333a14 100644 --- a/packages/SwingSet/src/kernel/vatManager/subprocessSupervisor.js +++ b/packages/SwingSet/src/kernel/vatManager/subprocessSupervisor.js @@ -9,7 +9,6 @@ import Netstring from 'netstring-stream'; import { assert } from '@agoric/assert'; import { importBundle } from '@agoric/import-bundle'; import { Remotable, getInterfaceOf } from '@agoric/marshal'; -import { HandledPromise } from '@agoric/eventual-send'; import { waitUntilQuiescent } from '../../waitUntilQuiescent'; import { makeLiveSlots } from '../liveSlots'; @@ -92,7 +91,6 @@ fromParent.on('data', data => { const [bundle, vatParameters] = margs; const endowments = { console: makeConsole(`SwingSet:vatWorker`), - HandledPromise, }; importBundle(bundle, { endowments }).then(vatNS => { workerLog(`got vatNS:`, Object.keys(vatNS).join(',')); diff --git a/packages/captp/lib/captp.js b/packages/captp/lib/captp.js index 281a9584a21..ebf40ad71ae 100644 --- a/packages/captp/lib/captp.js +++ b/packages/captp/lib/captp.js @@ -3,7 +3,7 @@ // This logic was mostly lifted from @agoric/swingset-vat liveSlots.js // Defects in it are mfig's fault. import { Remotable, makeMarshal, QCLASS } from '@agoric/marshal'; -import { E, HandledPromise } from '@agoric/eventual-send'; +import { E, HandledPromise } from '@agoric/eventual-send/shim'; import { isPromise } from '@agoric/promise-kit'; export { E, HandledPromise }; diff --git a/packages/cosmic-swingset/lib/ag-solo/vats/repl.js b/packages/cosmic-swingset/lib/ag-solo/vats/repl.js index 52e42615b83..8e1b4eb371b 100644 --- a/packages/cosmic-swingset/lib/ag-solo/vats/repl.js +++ b/packages/cosmic-swingset/lib/ag-solo/vats/repl.js @@ -1,7 +1,7 @@ /* global harden Compartment */ import { isPromise } from '@agoric/promise-kit'; -import { E, HandledPromise } from '@agoric/eventual-send'; +import { E } from '@agoric/eventual-send'; import makeUIAgentMakers from './ui-agent'; @@ -158,7 +158,6 @@ export function getReplHandler(replObjects, send, vatPowers) { getInterfaceOf, console: replConsole, E, - HandledPromise, commands, history, harden, diff --git a/packages/cosmic-swingset/test/captp-fixture.js b/packages/cosmic-swingset/test/captp-fixture.js index 500bdf13511..87070ebdb92 100644 --- a/packages/cosmic-swingset/test/captp-fixture.js +++ b/packages/cosmic-swingset/test/captp-fixture.js @@ -1,6 +1,6 @@ import { spawn } from 'child_process'; import WebSocket from 'ws'; -import { makeCapTP, E, HandledPromise } from '@agoric/captp'; +import { makeCapTP, E } from '@agoric/captp'; const PORT = 8000; @@ -93,7 +93,7 @@ export function makeFixture() { }); } - return new HandledPromise((resolve, reject) => { + return new Promise((resolve, reject) => { cp.addListener('exit', code => { if (!expectedToExit) { // Display all our output. diff --git a/packages/eventual-send/README.md b/packages/eventual-send/README.md index 9b9e98a035b..8e594052f5e 100644 --- a/packages/eventual-send/README.md +++ b/packages/eventual-send/README.md @@ -11,7 +11,7 @@ Create a HandledPromise class to implement the eventual-send API. This API is u > Note: If you're writing an application, you probably don't want to use this package directly. You'll want to use the eventual-send `~.` operator (tildot) provided in [SES](https://github.com/Agoric/SES) or other platforms. -After importing `@agoric/eventual-send`, the exported `HandledPromise` class can be used as described in `test/test.js`. +After importing `@agoric/eventual-send/shim`, the global `HandledPromise` class can be used as described in `test/test.js`. [circleci-svg]: https://circleci.com/gh/Agoric/eventual-send.svg?style=svg [circleci-url]: https://circleci.com/gh/Agoric/eventual-send diff --git a/packages/eventual-send/src/E.js b/packages/eventual-send/src/E.js index 10a1c48efc5..437714c87a7 100644 --- a/packages/eventual-send/src/E.js +++ b/packages/eventual-send/src/E.js @@ -71,5 +71,5 @@ export default function makeE(HandledPromise) { E.when = (x, onfulfilled = undefined, onrejected = undefined) => HandledPromise.resolve(x).then(onfulfilled, onrejected); - return harden(E); + return Object.freeze(E); } diff --git a/packages/eventual-send/src/index.js b/packages/eventual-send/src/index.js index ac2098cc433..8d06cce4a1e 100644 --- a/packages/eventual-send/src/index.js +++ b/packages/eventual-send/src/index.js @@ -316,7 +316,7 @@ export function makeHandledPromise(Promise) { ); } - const staticMethods = harden({ + const staticMethods = { get(target, key) { return handle(target, 'get', key); }, @@ -354,7 +354,7 @@ export function makeHandledPromise(Promise) { promiseResolve().then(_ => new HandledPromise(executeThen)), ); }, - }); + }; defineProperties(HandledPromise, getOwnPropertyDescriptors(staticMethods)); @@ -458,5 +458,5 @@ export function makeHandledPromise(Promise) { }; promiseResolve = Promise.resolve.bind(Promise); - return harden(HandledPromise); + return HandledPromise; } diff --git a/packages/eventual-send/test/get-hp.js b/packages/eventual-send/test/get-hp.js new file mode 100644 index 00000000000..9a857a5c3fd --- /dev/null +++ b/packages/eventual-send/test/get-hp.js @@ -0,0 +1,5 @@ +import makeE from '../src/E'; +import makeHandledPromise from '../src/handled-promise'; + +export const HandledPromise = makeHandledPromise(Promise); +export const E = makeE(HandledPromise); diff --git a/packages/install-ses/package.json b/packages/install-ses/package.json index 59b269897d4..c8f2f20c99c 100644 --- a/packages/install-ses/package.json +++ b/packages/install-ses/package.json @@ -15,6 +15,7 @@ "tape": "^4.13.2" }, "dependencies": { + "@agoric/eventual-send": "^0.9.3", "ses": "^0.10.0" }, "files": [ diff --git a/packages/produce-promise/src/producePromise.js b/packages/produce-promise/src/producePromise.js index 1124117377c..b7a83426160 100644 --- a/packages/produce-promise/src/producePromise.js +++ b/packages/produce-promise/src/producePromise.js @@ -1,10 +1,10 @@ -/* global harden */ +/* global harden globalThis */ // @ts-check // eslint-disable-next-line spaced-comment /// -import { HandledPromise } from '@agoric/eventual-send'; +const BestPromise = globalThis.HandledPromise || Promise; /** * @template T @@ -20,7 +20,7 @@ import { HandledPromise } from '@agoric/eventual-send'; const NOOP_INITIALIZER = harden(_ => {}); /** - * producePromise() builds a HandledPromise object, and returns a record + * producePromise() builds a Promise, and returns a record * containing the promise itself, as well as separate facets for resolving * and rejecting it. * @@ -34,7 +34,7 @@ export function producePromise() { /** @type {(reason: any) => void} */ let rej = NOOP_INITIALIZER; - const p = new HandledPromise((resolve, reject) => { + const p = new BestPromise((resolve, reject) => { res = resolve; rej = reject; }); @@ -68,6 +68,6 @@ harden(producePromise); * @returns {maybePromise is Promise} Whether it is a promise */ export function isPromise(maybePromise) { - return HandledPromise.resolve(maybePromise) === maybePromise; + return BestPromise.resolve(maybePromise) === maybePromise; } harden(isPromise); diff --git a/packages/promise-kit/src/promiseKit.js b/packages/promise-kit/src/promiseKit.js index b5d2920abb1..fa65829ce06 100644 --- a/packages/promise-kit/src/promiseKit.js +++ b/packages/promise-kit/src/promiseKit.js @@ -1,10 +1,10 @@ -/* global harden */ +/* global harden globalThis */ // @ts-check // eslint-disable-next-line spaced-comment /// -import { HandledPromise } from '@agoric/eventual-send'; +const BestPromise = globalThis.HandledPromise || Promise; /** * @template T @@ -29,7 +29,7 @@ import { HandledPromise } from '@agoric/eventual-send'; const NOOP_INITIALIZER = harden(_ => {}); /** - * makePromiseKit() builds a HandledPromise object, and returns a record + * makePromiseKit() builds a Promise object, and returns a record * containing the promise itself, as well as separate facets for resolving * and rejecting it. * @@ -42,7 +42,7 @@ export function makePromiseKit() { /** @type {(reason: any) => void} */ let rej = NOOP_INITIALIZER; - const p = new HandledPromise((resolve, reject) => { + const p = new BestPromise((resolve, reject) => { res = resolve; rej = reject; }); @@ -75,6 +75,6 @@ harden(makePromiseKit); * @returns {maybePromise is Promise} Whether it is a promise */ export function isPromise(maybePromise) { - return HandledPromise.resolve(maybePromise) === maybePromise; + return BestPromise.resolve(maybePromise) === maybePromise; } harden(isPromise); diff --git a/packages/spawner/src/contractHost.js b/packages/spawner/src/contractHost.js index b043522dd95..d36fb6e2702 100644 --- a/packages/spawner/src/contractHost.js +++ b/packages/spawner/src/contractHost.js @@ -1,6 +1,6 @@ // Copyright (C) 2019 Agoric, under Apache License 2.0 -/* global harden */ +/* global harden HandledPromise */ import Nat from '@agoric/nat'; import { importBundle } from '@agoric/import-bundle'; @@ -9,7 +9,6 @@ import { assert, details } from '@agoric/assert'; import { allComparable } from '@agoric/same-structure'; import makeIssuerKit from '@agoric/ertp'; import { makePromiseKit } from '@agoric/promise-kit'; -import { E, HandledPromise } from '@agoric/eventual-send'; export { makeCollect } from './makeCollect'; @@ -48,19 +47,8 @@ function makeContractHost(vatPowers, additionalEndowments = {}) { }); } - // TODO: this should really have console and HandledPromise. We need - // 'require' until we change the environment definition (and - // bundle-source's "externals" list) to get 'harden' from a global, not an - // import, and then change the nestedEvaluate format to stop needing - // 'require' even though nobody calls it. The bundles we install here - // should be standalone, with no remaining require() calls. Probably. const defaultEndowments = { console, - E, - harden, - Nat, - makePromiseKit, - HandledPromise, }; // note: support for check functions was removed during warner's diff --git a/packages/zoe/src/contractSupport/zoeHelpers.js b/packages/zoe/src/contractSupport/zoeHelpers.js index fa3816f4150..8877c64f793 100644 --- a/packages/zoe/src/contractSupport/zoeHelpers.js +++ b/packages/zoe/src/contractSupport/zoeHelpers.js @@ -2,7 +2,7 @@ import { assert, details } from '@agoric/assert'; import { sameStructure } from '@agoric/same-structure'; -import { E, HandledPromise } from '@agoric/eventual-send'; +import { E } from '@agoric/eventual-send'; import { satisfiesWant, isOfferSafe } from '../offerSafety'; import '../../exported'; @@ -362,7 +362,7 @@ export const makeZoeHelpers = zcf => { * @returns {Promise} */ makeEmptyOffer: () => - new HandledPromise(resolve => { + new Promise(resolve => { const invite = zcf.makeInvitation( offerHandle => resolve(offerHandle), 'empty offer', diff --git a/packages/zoe/src/evalContractCode.js b/packages/zoe/src/evalContractCode.js index 2bdc322f051..3b712f98bda 100644 --- a/packages/zoe/src/evalContractCode.js +++ b/packages/zoe/src/evalContractCode.js @@ -1,3 +1,4 @@ +/* global HandledPromise */ import Nat from '@agoric/nat'; import makeIssuerKit from '@agoric/ertp'; @@ -5,7 +6,6 @@ import { assert } from '@agoric/assert'; import { makePromiseKit } from '@agoric/promise-kit'; import { sameStructure } from '@agoric/same-structure'; import { importBundle } from '@agoric/import-bundle'; -import { HandledPromise } from '@agoric/eventual-send'; const evalContractBundle = (bundle, additionalEndowments = {}) => { // Make the console more verbose. @@ -14,7 +14,7 @@ const evalContractBundle = (bundle, additionalEndowments = {}) => { log: console.info, }; - // TODO: this should really only be console and HandledPromise + // TODO: this should really only be console const defaultEndowments = { assert, console: louderConsole, @@ -23,7 +23,6 @@ const evalContractBundle = (bundle, additionalEndowments = {}) => { makeIssuerKit, makePromiseKit, sameStructure, - HandledPromise, }; const fullEndowments = Object.create(null, {