Skip to content

Commit

Permalink
fix(eventual-send): Update the API throughout agoric-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Dec 10, 2019
1 parent cf8d50c commit 97fc1e7
Show file tree
Hide file tree
Showing 24 changed files with 83 additions and 108 deletions.
4 changes: 2 additions & 2 deletions packages/ERTP/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
},
"homepage": "https://github.com/Agoric/ertp#readme",
"dependencies": {
"@agoric/evaluate": "^2.1.3",
"@agoric/eventual-send": "^0.4.5",
"@agoric/evaluate": "^2.2.0",
"@agoric/eventual-send": "^0.5.0",
"@agoric/harden": "^0.0.4",
"@agoric/marshal": "^0.1.1",
"@agoric/nat": "^2.0.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/SwingSet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
},
"dependencies": {
"@agoric/bundle-source": "^1.0.2",
"@agoric/default-evaluate-options": "^0.2.5",
"@agoric/evaluate": "^2.1.2",
"@agoric/eventual-send": "^0.4.0",
"@agoric/default-evaluate-options": "^0.3.0",
"@agoric/evaluate": "^2.2.0",
"@agoric/eventual-send": "^0.5.0",
"@agoric/harden": "^0.0.4",
"@agoric/marshal": "^0.1.1",
"@agoric/nat": "^2.0.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/SwingSet/src/kernel/liveSlots.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import harden from '@agoric/harden';
import Nat from '@agoric/nat';
import { E } from '@agoric/eventual-send';
import { E, HandledPromise } from '@agoric/eventual-send';
import { QCLASS, mustPassByPresence, makeMarshal } from '@agoric/marshal';
import { insist } from '../insist';
import { insistVatType, makeVatSlot, parseVatSlot } from '../parseVatSlots';
Expand All @@ -23,15 +23,15 @@ function build(syscall, _state, makeRoot, forVatID) {
function makeQueued(slot) {
/* eslint-disable no-use-before-define */
const handler = {
POST(_o, prop, args) {
applyMethod(_o, prop, args) {
// Support: o~.[prop](...args) remote method invocation
return queueMessage(slot, prop, args);
},
};
/* eslint-enable no-use-before-define */

const pr = {};
pr.p = Promise.makeHandled((res, rej, resolveWithPresence) => {
pr.p = new HandledPromise((res, rej, resolveWithPresence) => {
pr.rej = rej;
pr.resPres = () => resolveWithPresence(handler);
pr.res = res;
Expand Down
1 change: 0 additions & 1 deletion packages/SwingSet/test/test-liveslots.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { test } from 'tape-promise/tape';
import harden from '@agoric/harden';
// eslint-disable-next-line no-unused-vars
import evaluateExpr from '@agoric/evaluate'; // to get Promise.makeHandled
import { buildStorageInMemory } from '../src/hostStorage';
import buildKernel from '../src/kernel/index';
import { makeLiveSlots } from '../src/kernel/liveSlots';
Expand Down
1 change: 0 additions & 1 deletion packages/SwingSet/test/test-marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { test } from 'tape-promise/tape';
import harden from '@agoric/harden';
import { makeMarshal, mustPassByPresence } from '@agoric/marshal';
import '@agoric/eventual-send'; // for side-effect: ensure HandledPromise is installed

import { makeMarshaller } from '../src/kernel/liveSlots';
import makePromise from '../src/makePromise';
Expand Down
22 changes: 12 additions & 10 deletions packages/agoric-cli/lib/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ const sendJSON = (ws, obj) => {
if (ws.readyState !== ws.OPEN) {
return;
}
//console.log('sending', obj);
// console.log('sending', obj);
ws.send(JSON.stringify(obj));
};

export default async function deployMain(progname, rawArgs, priv) {
const { console, error } = priv;
const { _: args, hostport } = parseArgs(rawArgs, {
default: {
hostport: '127.0.0.1:8000'
hostport: '127.0.0.1:8000',
},
});

Expand Down Expand Up @@ -70,22 +70,24 @@ export default async function deployMain(progname, rawArgs, priv) {

for (const arg of args) {
const moduleFile = path.resolve(process.cwd(), arg);
const pathResolve = (...args) => path.resolve(path.dirname(moduleFile), ...args);
const pathResolve = (...resArgs) =>
path.resolve(path.dirname(moduleFile), ...resArgs);
console.log('running', moduleFile);
const { source, sourceMap } = await bundleSource(moduleFile);

const actualSource = `(${source}\n)\n${sourceMap}`;
const mainNS = evaluateProgram(actualSource, { require })();
const main = mainNS.default;
if (typeof main !== 'function') {
console.error(`${moduleFile} does not have an export default function main`);
continue;
console.error(
`${moduleFile} does not have an export default function main`,
);
} else {
await main(bootC.P, {
bundleSource: file => bundleSource(pathResolve(file)),
pathResolve,
});
}

await main(bootC.P, {
bundleSource: file => bundleSource(pathResolve(file)),
pathResolve,
});
}

console.error('Done!');
Expand Down
4 changes: 2 additions & 2 deletions packages/agoric-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
},
"dependencies": {
"@agoric/bundle-source": "^1.0.3",
"@agoric/captp": "^1.0.2",
"@agoric/captp": "^1.1.0",
"@agoric/evaluate": "^2.1.3",
"@agoric/eventual-send": "^0.4.4",
"@agoric/eventual-send": "^0.5.0",
"chalk": "^2.4.2",
"esm": "^3.2.5",
"minimist": "^1.2.0",
Expand Down
35 changes: 17 additions & 18 deletions packages/captp/lib/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
function makeQuestion() {
lastQuestionID += 1;
const questionID = lastQuestionID;
// eslint-disable-next-line no-use-before-define
const pr = makeRemote(questionID);
questions.set(questionID, pr);
return [questionID, pr];
Expand Down Expand Up @@ -171,19 +172,18 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
: HandledPromise.get(val, prop);
answers.set(questionID, hp);
hp.then(res =>
send({
type: 'CTP_RETURN',
answerID: questionID,
result: serialize(harden(res)),
}),
)
.catch(rej =>
send({
type: 'CTP_RETURN',
answerID: questionID,
exception: serialize(harden(rej)),
}),
);
send({
type: 'CTP_RETURN',
answerID: questionID,
result: serialize(harden(res)),
}),
).catch(rej =>
send({
type: 'CTP_RETURN',
answerID: questionID,
exception: serialize(harden(rej)),
}),
);
},
CTP_RETURN(obj) {
const { result, exception, answerID } = obj;
Expand All @@ -206,7 +206,7 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
imports.delete(promiseID);
},
CTP_ABORT(obj) {
const {exception} = obj;
const { exception } = obj;
unplug = true;
for (const pr of questions.values()) {
pr.rej(exception);
Expand All @@ -229,10 +229,6 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
};
harden(handler);

// Abort a connection.
const abort = exception =>
dispatch({ type: 'CTP_ABORT', exception });

// Return a dispatch function.
const dispatch = obj => {
if (unplug) {
Expand All @@ -246,5 +242,8 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
return false;
};

// Abort a connection.
const abort = exception => dispatch({ type: 'CTP_ABORT', exception });

return harden({ abort, dispatch, getBootstrap });
}
2 changes: 1 addition & 1 deletion packages/captp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"tape-promise": "^4.0.0"
},
"dependencies": {
"@agoric/eventual-send": "^0.4.4",
"@agoric/eventual-send": "^0.5.0",
"@agoric/harden": "^0.0.4",
"@agoric/marshal": "^0.1.1",
"@agoric/nat": "^2.0.1",
Expand Down
1 change: 0 additions & 1 deletion packages/cosmic-swingset/lib/ag-solo/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import readlines from 'n-readlines';
// import connect from 'lotion-connect';
// import harden from '@agoric/harden';
// import djson from 'deterministic-json';
// import maybeExtendPromise from '@agoric/transform-bang';

import {
loadBasedir,
Expand Down
6 changes: 3 additions & 3 deletions packages/default-evaluate-options/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@agoric/default-evaluate-options",
"version": "0.2.7",
"version": "0.3.0",
"description": "Agoric default evaluation options for Realms and SES",
"main": "dist/default-evaluate-options.cjs.js",
"module": "dist/default-evaluate-options.esm.js",
Expand All @@ -23,8 +23,8 @@
},
"dependencies": {
"@agoric/babel-parser": "^7.6.4",
"@agoric/eventual-send": "^0.4.4",
"@agoric/transform-eventual-send": "^1.0.3",
"@agoric/eventual-send": "^0.5.0",
"@agoric/transform-eventual-send": "^1.0.4",
"@babel/generator": "^7.6.4",
"esm": "^3.2.5"
},
Expand Down
5 changes: 1 addition & 4 deletions packages/default-evaluate-options/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ export default [
format: 'cjs',
},
],
plugins: [
resolve(),
commonjs({include: /node_modules/}),
],
plugins: [resolve(), commonjs({ include: /node_modules/ })],
},
];
7 changes: 2 additions & 5 deletions packages/default-evaluate-options/src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { parse } from '@agoric/babel-parser';
import generate from '@babel/generator';
import makeEventualSendTransformer from '@agoric/transform-eventual-send';
import { maybeExtendPromise, makeHandledPromise } from '@agoric/eventual-send';
import { makeHandledPromise } from '@agoric/eventual-send';

const shims = [
`(${maybeExtendPromise})(Promise)`,
`this.HandledPromise = (${makeHandledPromise})(Promise)`,
];
const shims = [`this.HandledPromise = (${makeHandledPromise})(Promise)`];
const transforms = [...makeEventualSendTransformer(parse, generate)];

export default function makeDefaultEvaluateOptions() {
Expand Down
2 changes: 1 addition & 1 deletion packages/default-evaluate-options/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('sanity', async t => {
);
const sourceState = options.transforms.reduce(
(ss, transform) => (transform.rewrite ? transform.rewrite(ss) : ss),
{ src: `foo~.bar` },
{ src: `foo~.bar`, endowments: {} },
);

t.equal(
Expand Down
4 changes: 2 additions & 2 deletions packages/evaluate/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@agoric/evaluate",
"version": "2.1.3",
"version": "2.2.0",
"description": "(unsafe) three-argument evaluator function",
"main": "src/main.js",
"module": "src/index.js",
Expand All @@ -18,7 +18,7 @@
"tape-promise": "^4.0.0"
},
"dependencies": {
"@agoric/default-evaluate-options": "^0.2.7",
"@agoric/default-evaluate-options": "^0.3.0",
"esm": "^3.2.5"
},
"author": "Agoric",
Expand Down
2 changes: 1 addition & 1 deletion packages/eventual-send/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# MaybeExtendPromise
# HandledPromise

[![Build Status][circleci-svg]][circleci-url]
[![dependency status][deps-svg]][deps-url]
Expand Down
6 changes: 3 additions & 3 deletions packages/eventual-send/integration-test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ For more information on how widely each tool is used, see the [2018 State of JS
Note: The actual commands can be found in the `test_integration` job in the CircleCI config (`.circlci/config.yml`).

We start with the unit tests that we already have in `test/test.js` of the main directory. Then, we transform the test file into a few files that will be used by the bundlers:
* a ES6 modules version that replaces the maybeExtendPromise local import in the test file (`import { maybeExtendPromise } from '../src/index';`) with an import of the package (`@agoric/eventual-send`)
* a ES6 modules version that replaces the HandledPromise local import in the test file (`import { HandledPromise } from '../src/index';`) with an import of the package (`@agoric/eventual-send`)
* a CommonJS version that does the same replacement (this will be used by browserify, which cannot process ES6 modules)
* a CommonJS version that removes all maybeExtendPromise imports in favor of a global 'maybeExtendPromise' object that will be put in place by a previous `<script>` tag during the tests (this will be used by the unpkg/umd tests).
* a CommonJS version that removes all HandledPromise imports in favor of a global 'HandledPromise' object that will be put in place by a previous `<script>` tag during the tests (this will be used by the unpkg/umd tests).

### Webpack

Expand All @@ -34,7 +34,7 @@ We take the Common JS version of the test that we created and bundle it with bro

### Rollup

Rollup is a little trickier. If we try to use rollup to bundle the tests and maybeExtendPromise in the same way that we do with Webpack and Browserify, we have major difficulties. Based on the errors, the documentation suggests using three plugins (`rollup-plugin-node-resolve`, `rollup-plugin-commonjs`, `rollup-plugin-node-builtins`), but it still fails. So instead, we divide up the work - Browserify creates the test file from our Tape tests and Rollup creates the bundle that includes maybeExtendPromise. Rollup creates an IIFE that creates maybeExtendPromise as a global variable, and that is used by the browser version of the Tape tests.
Rollup is a little trickier. If we try to use rollup to bundle the tests and HandledPromise in the same way that we do with Webpack and Browserify, we have major difficulties. Based on the errors, the documentation suggests using three plugins (`rollup-plugin-node-resolve`, `rollup-plugin-commonjs`, `rollup-plugin-node-builtins`), but it still fails. So instead, we divide up the work - Browserify creates the test file from our Tape tests and Rollup creates the bundle that includes HandledPromise. Rollup creates an IIFE that creates HandledPromise as a global variable, and that is used by the browser version of the Tape tests.

### Parcel

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const runBrowserTests = async indexFile => {
};

const testBundler = (bundlerName, indexFile) => {
test(`maybeExtendPromise works with ${bundlerName}`, t => {
test(`HandledPromise works with ${bundlerName}`, t => {
runBrowserTests(indexFile).then(({ numTests, numPass }) => {
t.notEqual(numTests, undefined);
t.equal(numTests, numPass);
Expand Down
4 changes: 4 additions & 0 deletions packages/eventual-send/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';

export default [
{
input: 'src/index.js',
Expand All @@ -16,5 +19,6 @@ export default [
format: 'cjs',
},
],
plugins: [resolve(), commonjs({ include: /node_modules/ })],
},
];
27 changes: 13 additions & 14 deletions packages/eventual-send/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@ import makeE from './E';
// import { HandledPromise, E } from '@agoric/eventual-send';
// ...

// eslint-disable-next-line import/no-mutable-exports
let hp;
if (typeof HandledPromise === 'undefined') {
// Export a fresh shim.
// eslint-disable-next-line no-use-before-define
hp = makeHandledPromise();
} else {
// Reuse the global or endowed HandledPromise.
hp = HandledPromise;
}
const hp =
typeof HandledPromise === 'undefined'
? // eslint-disable-next-line no-use-before-define
makeHandledPromise(Promise)
: HandledPromise;

// Provide our exports.
export { hp as HandledPromise };
Expand All @@ -39,7 +34,7 @@ export const E = makeE(hp);
*
* @return {typeof HandledPromise} Handled promise
*/
function makeHandledPromise() {
export function makeHandledPromise(Promise) {
const harden = (typeof SES !== 'undefined' && SES.harden) || Object.freeze;

// xs doesn't support WeakMap in pre-loaded closures
Expand All @@ -59,7 +54,7 @@ function makeHandledPromise() {
// handled Promises to their corresponding fulfilledHandler.
let forwardingHandler;
let handle;
let baseResolve;
let handledPromiseResolve;

class HandledPromise extends Promise {
static get(target, key) {
Expand Down Expand Up @@ -93,7 +88,11 @@ function makeHandledPromise() {
if (handledPromise) {
return handledPromise;
}
return baseResolve(value);
const basePromise = Promise.resolve(value);
if (basePromise === value) {
return value;
}
return handledPromiseResolve(value);
}

constructor(executor, unfulfilledHandler = undefined) {
Expand Down Expand Up @@ -331,6 +330,6 @@ function makeHandledPromise() {
return new HandledPromise(executor);
};

baseResolve = Promise.resolve.bind(HandledPromise);
handledPromiseResolve = Promise.resolve.bind(HandledPromise);
return harden(HandledPromise);
}
Loading

0 comments on commit 97fc1e7

Please sign in to comment.