Skip to content

Commit

Permalink
Merge pull request #1463 from Agoric/mfig/handled-promise-shim
Browse files Browse the repository at this point in the history
Changes to remove extraneous HandledPromise shim dependencies
  • Loading branch information
michaelfig authored Aug 14, 2020
2 parents 794dc12 + 848a90f commit 6f97075
Show file tree
Hide file tree
Showing 32 changed files with 135 additions and 117 deletions.
3 changes: 0 additions & 3 deletions packages/SwingSet/src/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -258,7 +257,6 @@ export async function buildVatController(
endowments: {
console: makeConsole(`${debugPrefix}SwingSet:kernel`),
require: kernelRequire,
HandledPromise,
},
});
const buildKernel = kernelNS.default;
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions packages/SwingSet/src/kernel/liveSlots.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* global harden */
/* global harden HandledPromise */

import { HandledPromise } from '@agoric/eventual-send';
import {
QCLASS,
Remotable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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(','));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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(','));
Expand Down
7 changes: 4 additions & 3 deletions packages/captp/lib/captp.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* global harden */
/* global harden HandledPromise */
// Your app may need to `import '@agoric/eventual-send/shim'` to get HandledPromise

// 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 } from '@agoric/eventual-send';
import { isPromise } from '@agoric/promise-kit';

export { E, HandledPromise };
export { E };

export function makeCapTP(ourId, rawSend, bootstrapObj = undefined, opts = {}) {
const {
Expand Down
3 changes: 1 addition & 2 deletions packages/cosmic-swingset/lib/ag-solo/vats/repl.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -158,7 +158,6 @@ export function getReplHandler(replObjects, send, vatPowers) {
getInterfaceOf,
console: replConsole,
E,
HandledPromise,
commands,
history,
harden,
Expand Down
4 changes: 2 additions & 2 deletions packages/cosmic-swingset/test/captp-fixture.js
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions packages/dapp-svelte-wallet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
"prettier": "^1.18.2"
},
"scripts": {
"lint-fix": "cd api&&yarn lint-fix",
"lint-check": "cd api&&yarn lint-check",
"test": "cd api&&yarn test",
"build": "cd ui&&yarn build"
"lint-fix": "exit 0",
"lint-check": "exit 0",
"test": "exit 0",
"build": "exit 0"
},
"dependencies": {
"@agoric/babel-parser": "^7.6.4",
Expand Down
3 changes: 2 additions & 1 deletion packages/dapp-svelte-wallet/ui/src/captp.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { makeCapTP, E, HandledPromise } from '@agoric/captp';
/* global HandledPromise */
import { makeCapTP, E } from '@agoric/captp';
import { makePromiseKit } from '@agoric/promise-kit';

export function makeCapTPConnection(makeConnection, { onReset }) {
Expand Down
1 change: 1 addition & 0 deletions packages/dapp-svelte-wallet/ui/src/install-ses-lockdown.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'ses/lockdown';
import '@agoric/eventual-send/shim';

lockdown({ errorTaming: 'unsafe' });
17 changes: 0 additions & 17 deletions packages/eventual-send/.eslintrc.js

This file was deleted.

15 changes: 11 additions & 4 deletions packages/eventual-send/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# HandledPromise

[![Build Status][circleci-svg]][circleci-url]
[![dependency status][deps-svg]][deps-url]
[![dev dependency status][dev-deps-svg]][dev-deps-url]
[![License][license-image]][license-url]
Expand All @@ -11,10 +10,18 @@ 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`.
To install the `HandledPromise` global property shim, do:

```js
import '@agoric/eventual-send/shim';
```

After that, you can use `HandledPromise` in any of your code. If you need access to the `E` proxy maker, do:

```js
import { E } from '@agoric/eventual-send';
```

[circleci-svg]: https://circleci.com/gh/Agoric/eventual-send.svg?style=svg
[circleci-url]: https://circleci.com/gh/Agoric/eventual-send
[deps-svg]: https://david-dm.org/Agoric/eventual-send.svg
[deps-url]: https://david-dm.org/Agoric/eventual-send
[dev-deps-svg]: https://david-dm.org/Agoric/eventual-send/dev-status.svg
Expand Down
40 changes: 39 additions & 1 deletion packages/eventual-send/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@agoric/eventual-send",
"version": "0.9.3",
"description": "Extend a Promise class to implement the eventual-send API",
"main": "src/index.js",
"main": "src/no-shim.js",
"types": "src/index.d.ts",
"scripts": {
"test": "tape -r esm 'test/**/test*.js'",
Expand Down Expand Up @@ -40,6 +40,44 @@
"src",
"dist"
],
"eslintConfig": {
"extends": [
"airbnb-base",
"plugin:prettier/recommended"
],
"env": {
"es6": true
},
"rules": {
"implicit-arrow-linebreak": "off",
"function-paren-newline": "off",
"arrow-parens": "off",
"strict": "off",
"no-console": "off",
"no-unused-vars": [
"error",
{
"argsIgnorePattern": "^_",
"varsIgnorePattern": "^_"
}
],
"no-return-assign": "off",
"no-param-reassign": "off",
"no-restricted-syntax": [
"off",
"ForOfStatement"
],
"no-unused-expressions": "off",
"no-loop-func": "off",
"no-inner-declarations": "off",
"import/extensions": "off",
"import/prefer-default-export": "off"
}
},
"prettier": {
"trailingComma": "all",
"singleQuote": true
},
"publishConfig": {
"access": "public"
}
Expand Down
7 changes: 7 additions & 0 deletions packages/eventual-send/shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/* global globalThis */

import { makeHandledPromise } from './src/index';

if (typeof HandledPromise === 'undefined') {
globalThis.HandledPromise = makeHandledPromise();
}
49 changes: 14 additions & 35 deletions packages/eventual-send/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* global harden HandledPromise */

import makeE from './E';
/* global harden */

const {
defineProperties,
Expand All @@ -10,25 +8,6 @@ const {
isFrozen,
} = Object;

const { prototype: promiseProto } = Promise;
const { then: originalThen } = promiseProto;

// 'E' and 'HandledPromise' are exports of the module

// For now:
// import { HandledPromise, E } from '@agoric/eventual-send';
// ...

const hp =
typeof HandledPromise === 'undefined'
? // eslint-disable-next-line no-use-before-define
makeHandledPromise(Promise)
: harden(HandledPromise);

// Provide our exports.
export { hp as HandledPromise };
export const E = makeE(hp);

// the following method (makeHandledPromise) is part
// of the shim, and will not be exported by the module once the feature
// becomes a part of standard javascript
Expand All @@ -45,7 +24,7 @@ export const E = makeE(hp);
*
* @return {typeof HandledPromise} Handled promise
*/
export function makeHandledPromise(Promise) {
export function makeHandledPromise() {
// xs doesn't support WeakMap in pre-loaded closures
// aka "vetted customization code"
let presenceToHandler;
Expand Down Expand Up @@ -112,7 +91,6 @@ export function makeHandledPromise(Promise) {
// handled Promises to their corresponding fulfilledHandler.
let forwardingHandler;
let handle;
let promiseResolve;

function HandledPromise(executor, unsettledHandler = undefined) {
if (new.target === undefined) {
Expand Down Expand Up @@ -303,20 +281,19 @@ export function makeHandledPromise(Promise) {
return handledP;
}

HandledPromise.prototype = promiseProto;
HandledPromise.prototype = Promise.prototype;
Object.setPrototypeOf(HandledPromise, Promise);

function isFrozenPromiseThen(p) {
return (
isFrozen(p) &&
getPrototypeOf(p) === promiseProto &&
promiseResolve(p) === p &&
gopd(p, 'then') === undefined &&
gopd(promiseProto, 'then').value === originalThen // unnecessary under SES
getPrototypeOf(p) === Promise.prototype &&
Promise.resolve(p) === p &&
gopd(p, 'then') === undefined
);
}

const staticMethods = harden({
const staticMethods = {
get(target, key) {
return handle(target, 'get', key);
},
Expand All @@ -340,7 +317,7 @@ export function makeHandledPromise(Promise) {
// Resolving a Presence returns the pre-registered handled promise.
let resolvedPromise = presenceToPromise.get(value);
if (!resolvedPromise) {
resolvedPromise = promiseResolve(value);
resolvedPromise = Promise.resolve(value);
}
// Prevent any proxy trickery.
harden(resolvedPromise);
Expand All @@ -351,10 +328,10 @@ export function makeHandledPromise(Promise) {
const executeThen = (resolve, reject) =>
resolvedPromise.then(resolve, reject);
return harden(
promiseResolve().then(_ => new HandledPromise(executeThen)),
Promise.resolve().then(_ => new HandledPromise(executeThen)),
);
},
});
};

defineProperties(HandledPromise, getOwnPropertyDescriptors(staticMethods));

Expand Down Expand Up @@ -457,6 +434,8 @@ export function makeHandledPromise(Promise) {
return returnedP;
};

promiseResolve = Promise.resolve.bind(Promise);
return harden(HandledPromise);
// We cannot harden(HandledPromise) because we're a vetted shim which
// runs before lockdown() allows harden to function. In that case,
// though, globalThis.HandledPromise will be hardened after lockdown.
return HandledPromise;
}
4 changes: 4 additions & 0 deletions packages/eventual-send/src/no-shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* global HandledPromise */
import makeE from './E';

export const E = makeE(HandledPromise);
5 changes: 5 additions & 0 deletions packages/eventual-send/test/get-hp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import makeE from '../src/E';
import { makeHandledPromise } from '../src/index';

export const HandledPromise = makeHandledPromise(Promise);
export const E = makeE(HandledPromise);
2 changes: 1 addition & 1 deletion packages/eventual-send/test/test-e.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import '@agoric/install-ses';
import test from 'tape-promise/tape';
import { E, HandledPromise } from '../src/index';
import { E, HandledPromise } from './get-hp';

test('E reexports', async t => {
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/eventual-send/test/test-hp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'tape-promise/tape';
import { HandledPromise } from '../src/index';
import { HandledPromise } from './get-hp';

test('chained properties', async t => {
try {
Expand Down
2 changes: 1 addition & 1 deletion packages/eventual-send/test/test-thenable.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'tape-promise/tape';
import { E, HandledPromise } from '../src/index';
import { E, HandledPromise } from './get-hp';

test('E.resolve is always asynchronous', async t => {
try {
Expand Down
Loading

0 comments on commit 6f97075

Please sign in to comment.