Skip to content

Commit

Permalink
Merge pull request #834 from Agoric/isPromise
Browse files Browse the repository at this point in the history
feat!: Add IsPromise() to the makePromise package
  • Loading branch information
Chris-Hibbert authored Apr 3, 2020
2 parents 64ab4a5 + 26ca972 commit 29b5399
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 84 deletions.
12 changes: 6 additions & 6 deletions packages/SwingSet/src/devices/command.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Nat from '@agoric/nat';
import makePromise from '@agoric/make-promise';
import { makePromise } from '@agoric/make-promise';

export default function buildCommand(broadcastCallback) {
if (!broadcastCallback) {
Expand All @@ -14,10 +14,10 @@ export default function buildCommand(broadcastCallback) {
// deliver the JSON-serializable object to the registered handler, and
// return a promise that fires with a JSON-serializable object in
// response
const { p, res, reject } = makePromise();
const { promise, resolve, reject } = makePromise();
const count = nextCount;
nextCount += 1;
responses.set(count, { res, reject });
responses.set(count, { resolve, reject });
if (!inboundCallback) {
throw new Error(`inboundCommand before registerInboundCallback`);
}
Expand All @@ -26,7 +26,7 @@ export default function buildCommand(broadcastCallback) {
} catch (e) {
console.error(`error running inboundCallback:`, e);
}
return p;
return promise;
}

function sendBroadcast(kBodyString) {
Expand Down Expand Up @@ -54,11 +54,11 @@ export default function buildCommand(broadcastCallback) {
// maybe just ignore it
throw new Error(`unknown response index ${count}`);
}
const { res, reject } = responses.get(count);
const { resolve, reject } = responses.get(count);
if (isReject) {
reject(obj);
} else {
res(obj);
resolve(obj);
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/SwingSet/src/kernel/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import harden from '@agoric/harden';
import { makeMarshal } from '@agoric/marshal';
import evaluateProgram from '@agoric/evaluate';
import { assert, details } from '@agoric/assert';
import makePromise from '@agoric/make-promise';
import { makePromise } from '@agoric/make-promise';
import makeVatManager from './vatManager';
import { makeLiveSlots } from './liveSlots';
import { makeDeviceSlots } from './deviceSlots';
Expand Down Expand Up @@ -105,8 +105,8 @@ export default function buildKernel(kernelEndowments) {
// Node 10 it is higher. So this trick requires Node 11.
// https://jsblog.insiderattack.net/new-changes-to-timers-and-microtasks-from-node-v11-0-0-and-above-68d112743eb3

const { p: queueEmptyP, res } = makePromise();
setImmediate(() => res());
const { promise: queueEmptyP, resolve } = makePromise();
setImmediate(() => resolve());

// protect f() with promise/then
Promise.resolve()
Expand Down
6 changes: 3 additions & 3 deletions packages/SwingSet/src/kernel/vatAdmin/vatAdminWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* device affordances into objects that can be used by code in other vats.
*/
import harden from '@agoric/harden';
import makePromise from '@agoric/make-promise';
import { makePromise } from '@agoric/make-promise';

export default function setup(syscall, state, helpers) {
function build(E, D) {
Expand All @@ -20,7 +20,7 @@ export default function setup(syscall, state, helpers) {
throw Error(`Vat Creation Error: ${error}`);
} else {
const vatPromise = makePromise();
vatIdsToResolvers.set(vatID, vatPromise.res);
vatIdsToResolvers.set(vatID, vatPromise.resolve);
const adminNode = harden({
terminate() {
D(vatAdminNode).terminate(vatID);
Expand All @@ -30,7 +30,7 @@ export default function setup(syscall, state, helpers) {
return D(vatAdminNode).adminStats(vatID);
},
});
return vatPromise.p.then(root => {
return vatPromise.promise.then(root => {
return { adminNode, root };
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/SwingSet/test/basedir-promises-2/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import harden from '@agoric/harden';
import makePromise from '@agoric/make-promise';
import { makePromise } from '@agoric/make-promise';

console.log(`loading bootstrap`);

Expand All @@ -8,7 +8,7 @@ function build(E, log) {
bootstrap(argv, vats) {
const mode = argv[0];
if (mode === 'harden-promise-1') {
const { p: p1 } = makePromise();
const { promise: p1 } = makePromise();
harden(p1);
const allP = [];
// in bug #95, this first call returns a (correctly) frozen Promise,
Expand Down
4 changes: 2 additions & 2 deletions packages/SwingSet/test/basedir-promises/bootstrap.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import harden from '@agoric/harden';
import makePromise from '@agoric/make-promise';
import { makePromise } from '@agoric/make-promise';

console.log(`loading bootstrap`);

Expand Down Expand Up @@ -55,7 +55,7 @@ function build(E, log) {
return 3;
},
});
const { p: p1, res: r1 } = makePromise();
const { promise: p1, resolve: r1 } = makePromise();
console.log(`here1`, Object.isFrozen(p1));
const p2 = E(vats.left).takePromise(p1);
console.log(`here2`);
Expand Down
56 changes: 28 additions & 28 deletions packages/SwingSet/test/message-patterns.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// test.stuff patterns.

import harden from '@agoric/harden';
import makePromise from '@agoric/make-promise';
import { makePromise } from '@agoric/make-promise';

// Exercise a set of increasingly complex object-capability message patterns,
// for testing.
Expand Down Expand Up @@ -233,13 +233,13 @@ export function buildPatterns(E, log) {
// so the resolution is delivered as a separate event.
objA.a50 = async () => {
const ret = await E(b.bob).b50();
const data = await ret.p;
const data = await ret.promise;
log(`a50 done, got ${data}`);
};
const p1 = makePromise();
objB.b50 = async () => {
p1.res('data');
return harden({ p: p1.p });
p1.resolve('data');
return harden({ promise: p1.promise });
};
}
out.a50 = ['a50 done, got data'];
Expand All @@ -250,14 +250,14 @@ export function buildPatterns(E, log) {
// bob returns a (wrapped) promise, then resolves it to a presence
objA.a51 = async () => {
const ret = await E(b.bob).b51();
const bert = await ret.p;
const bert = await ret.promise;
const bert2 = await E(b.bob).b51_2();
log(`a51 done, got ${bert}, match ${bert === bert2} ${bert === b.bert}`);
};
const p1 = makePromise();
objB.b51 = async () => {
p1.res(b.bert);
return harden({ p: p1.p });
p1.resolve(b.bert);
return harden({ promise: p1.promise });
};
objB.b51_2 = async () => {
return b.bert;
Expand All @@ -271,14 +271,14 @@ export function buildPatterns(E, log) {
// bob returns a (wrapped) promise, then resolves it to a new presence
objA.a52 = async () => {
const ret = await E(b.bob).b52();
const bill = await ret.p;
const bill = await ret.promise;
const bill2 = await E(b.bob).b52_2();
log(`a52 done, got ${bill}, match ${bill === bill2}`);
};
const p1 = makePromise();
objB.b52 = async () => {
p1.res(b.bill);
return harden({ p: p1.p });
p1.resolve(b.bill);
return harden({ promise: p1.promise });
};
objB.b52_2 = async () => {
return b.bill;
Expand All @@ -291,13 +291,13 @@ export function buildPatterns(E, log) {
{
objA.a53 = async () => {
const ret = await E(b.bob).b53(a.amy);
const amy2 = await ret.p;
const amy2 = await ret.promise;
log(`a53 done, match ${amy2 === a.amy}`);
};
const p1 = makePromise();
objB.b53 = async amy => {
p1.res(amy);
return harden({ p: p1.p });
p1.resolve(amy);
return harden({ promise: p1.promise });
};
}
out.a53 = ['a53 done, match true'];
Expand All @@ -307,13 +307,13 @@ export function buildPatterns(E, log) {
{
objA.a60 = async () => {
const p1 = makePromise();
const p2 = E(b.bob).b60({ p: p1.p });
p1.res(a.amy);
const p2 = E(b.bob).b60({ promise: p1.promise });
p1.resolve(a.amy);
const amy2 = await p2;
log(`a60 done, match ${amy2 === a.amy}`);
};
objB.b60 = async Pamy => {
const amy = await Pamy.p;
const amy = await Pamy.promise;
return amy;
};
}
Expand All @@ -324,12 +324,12 @@ export function buildPatterns(E, log) {
{
objA.a61 = async () => {
const p1 = Promise.resolve(a.amy);
const p2 = E(b.bob).b61({ p: p1 });
const p2 = E(b.bob).b61({ promise: p1 });
const amy2 = await p2;
log(`a61 done, match ${amy2 === a.amy}`);
};
objB.b61 = async Pamy => {
const amy = await Pamy.p;
const amy = await Pamy.promise;
return amy;
};
}
Expand All @@ -341,15 +341,15 @@ export function buildPatterns(E, log) {
objA.a62 = async () => {
const p2 = await E(b.bob).b62_1();
E(b.bob).b62_2();
const bill = await p2.p;
const bill = await p2.promise;
log(`a62 done, got ${bill}`);
};
const p1 = makePromise();
objB.b62_1 = async () => {
return { p: p1.p };
return { promise: p1.promise };
};
objB.b62_2 = async () => {
p1.res(b.bill);
p1.resolve(b.bill);
};
}
out.a62 = ['a62 done, got [Presence o-52]'];
Expand All @@ -360,17 +360,17 @@ export function buildPatterns(E, log) {
objA.a63 = async () => {
const p2 = await E(b.bob).b63_1(a.amy);
E(b.bob).b63_2();
const amy2 = await p2.p;
const amy2 = await p2.promise;
log(`a63 done, match ${amy2 === a.amy}`);
};
const p1 = makePromise();
let amyOnBob;
objB.b63_1 = async amy2 => {
amyOnBob = amy2;
return { p: p1.p };
return { promise: p1.promise };
};
objB.b63_2 = async () => {
p1.res(amyOnBob);
p1.resolve(amyOnBob);
};
}
out.a63 = ['a63 done, match true'];
Expand Down Expand Up @@ -429,7 +429,7 @@ export function buildPatterns(E, log) {
E(b.bob).b71_resolvex();
};
const p1 = makePromise();
objB.b71_getpx = async () => p1.p;
objB.b71_getpx = async () => p1.promise;
objB.b71_resolvex = async () => {
const x = harden({
pipe1() {
Expand All @@ -448,7 +448,7 @@ export function buildPatterns(E, log) {
return pipe2;
},
});
p1.res(x);
p1.resolve(x);
};
}
out.a71 = ['pipe1', 'pipe2', 'pipe3', 'p1.then', 'p2.then', 'p3.then'];
Expand All @@ -472,7 +472,7 @@ export function buildPatterns(E, log) {
};
const p1 = makePromise();
objB.b72_wait = async () => 0;
objB.b72_getpx = async () => p1.p;
objB.b72_getpx = async () => p1.promise;
objB.b72_resolvex = async () => {
const x = harden({
pipe1() {
Expand All @@ -491,7 +491,7 @@ export function buildPatterns(E, log) {
return pipe2;
},
});
p1.res(x);
p1.resolve(x);
};
}
out.a72 = ['pipe1', 'p1.then', 'pipe2', 'p2.then', 'pipe3', 'p3.then'];
Expand Down
14 changes: 7 additions & 7 deletions packages/SwingSet/test/test-marshal.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 makePromise from '@agoric/make-promise';
import { makePromise } from '@agoric/make-promise';

import { makeMarshaller } from '../src/kernel/liveSlots';

Expand Down Expand Up @@ -106,27 +106,27 @@ test('serialize promise', async t => {
};

const { m } = makeMarshaller(syscall);
const { p, res } = makePromise();
t.deepEqual(m.serialize(p), {
const { promise, resolve } = makePromise();
t.deepEqual(m.serialize(promise), {
body: '{"@qclass":"slot","index":0}',
slots: ['p+5'],
});
// serializer should remember the promise
t.deepEqual(m.serialize(harden(['other stuff', p])), {
t.deepEqual(m.serialize(harden(['other stuff', promise])), {
body: '["other stuff",{"@qclass":"slot","index":0}]',
slots: ['p+5'],
});

// inbound should recognize it and return the promise
t.deepEqual(
m.unserialize({ body: '{"@qclass":"slot","index":0}', slots: ['p+5'] }),
p,
promise,
);

res(5);
resolve(5);
t.deepEqual(log, []);

const { p: pauseP, res: pauseRes } = makePromise();
const { promise: pauseP, resolve: pauseRes } = makePromise();
setImmediate(() => pauseRes());
await pauseP;
t.deepEqual(log, [{ result: 'p+5', data: { body: '5', slots: [] } }]);
Expand Down
12 changes: 6 additions & 6 deletions packages/agoric-cli/lib/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import builtinModules from 'builtin-modules';
import { evaluateProgram } from '@agoric/evaluate';
import { E, HandledPromise, makeCapTP } from '@agoric/captp';
import makePromise from '@agoric/make-promise';
import { makePromise } from '@agoric/make-promise';

import bundleSource from '@agoric/bundle-source';

Expand Down Expand Up @@ -46,7 +46,7 @@ export default async function deployMain(progname, rawArgs, powers, opts) {
dispatch(obj);
} catch (e) {
log.error('server error processing message', data, e);
exit.rej(e);
exit.reject(e);
}
});

Expand Down Expand Up @@ -87,14 +87,14 @@ export default async function deployMain(progname, rawArgs, powers, opts) {

log.info('Done!');
ws.close();
exit.res(0);
exit.resolve(0);
} catch (e) {
exit.rej(e);
exit.reject(e);
}
});
ws.on('close', (_code, _reason) => {
log.debug('connection closed');
exit.res(1);
exit.resolve(1);
});
return exit.p;
return exit.promise;
}
Loading

0 comments on commit 29b5399

Please sign in to comment.