Skip to content

Commit 635e902

Browse files
committed
refactor(SwingSet): Simplify some functions for comprehensibility and interactive debugging
1 parent a068322 commit 635e902

File tree

2 files changed

+38
-51
lines changed

2 files changed

+38
-51
lines changed

packages/SwingSet/src/supervisors/supervisor-helper.js

+9-10
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,15 @@ function makeSupervisorDispatch(dispatch) {
3232
async function dispatchToVat(delivery) {
3333
// the (low-level) vat is responsible for giving up agency, but we still
3434
// protect against exceptions
35-
return Promise.resolve(delivery)
36-
.then(dispatch)
37-
.then(
38-
res => harden(['ok', res, null]),
39-
err => {
40-
// TODO react more thoughtfully, maybe terminate the vat
41-
console.warn(`error during vat dispatch() of ${delivery}`, err);
42-
return harden(['error', `${err}`, null]);
43-
},
44-
);
35+
await null;
36+
try {
37+
const res = await dispatch(delivery);
38+
return harden(['ok', res, null]);
39+
} catch (err) {
40+
// TODO react more thoughtfully, maybe terminate the vat
41+
console.warn(`error during vat dispatch() of ${delivery}`, err);
42+
return harden(['error', `${err}`, null]);
43+
}
4544
}
4645

4746
return harden(dispatchToVat);

packages/SwingSet/tools/run-utils.js

+29-41
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,21 @@ export const makeRunUtils = controller => {
4646
};
4747

4848
/**
49-
* @typedef {import('@endo/eventual-send').EProxy & {
50-
* sendOnly: (presence: unknown) => Record<string, (...args: any) => void>;
51-
* vat: (name: string) => Record<string, (...args: any) => Promise<any>>;
52-
* }} EVProxy
49+
* @typedef {object} EVProxyMethods Given a presence, return a "methods proxy"
50+
* for which every requested property manifests as a method that forwards
51+
* its invocation through the controller to the presence as an invocation of
52+
* an identically-named method with identical arguments (modulo passable
53+
* translation).
54+
* So e.g. `EV(x).m(0)` becomes `controller.queueToVatObject(x, 'm', [0])`.
55+
* @property {(presence: unknown) => Record<string, (...args: any) => void>} sendOnly
56+
* Returns a "methods proxy" for the presence that ignores the result.
57+
* @property {(name: string) => Record<string, (...args: any) => Promise<any>>} vat
58+
* Returns a "methods proxy" for the root object of the specified vat.
59+
* So e.g. `EV.vat('foo').m(0)` becomes
60+
* `controller.queueToVatRoot('foo', 'm', [0])`.
61+
*/
62+
/**
63+
* @typedef {import('@endo/eventual-send').EProxy & EVProxyMethods} EVProxy
5364
*/
5465

5566
// IMPORTANT WARNING TO USERS OF `EV`
@@ -96,50 +107,27 @@ export const makeRunUtils = controller => {
96107
// promise that can remain pending indefinitely, possibly to be settled by a
97108
// future message delivery.
98109

110+
const makeMethodsProxy = (invoker, target, voidResult = false) =>
111+
new Proxy(harden({}), {
112+
get: (_t, method, _rx) => {
113+
const boundMethod = (...args) =>
114+
queueAndRun(() => invoker(target, method, args), voidResult);
115+
return harden(boundMethod);
116+
},
117+
});
118+
99119
/** @type {EVProxy} */
100120
// @ts-expect-error cast, approximate
101121
const EV = Object.assign(
102-
presence =>
103-
new Proxy(harden({}), {
104-
get: (_t, method, _rx) => {
105-
const boundMethod = (...args) =>
106-
queueAndRun(() =>
107-
controller.queueToVatObject(presence, method, args),
108-
);
109-
return harden(boundMethod);
110-
},
111-
}),
122+
presence => makeMethodsProxy(controller.queueToVatObject, presence),
112123
{
113-
vat: vatName =>
114-
new Proxy(harden({}), {
115-
get: (_t, method, _rx) => {
116-
const boundMethod = (...args) =>
117-
queueAndRun(() =>
118-
controller.queueToVatRoot(vatName, method, args),
119-
);
120-
return harden(boundMethod);
121-
},
122-
}),
124+
vat: vatName => makeMethodsProxy(controller.queueToVatRoot, vatName),
123125
sendOnly: presence =>
124-
new Proxy(harden({}), {
125-
get: (_t, method, _rx) => {
126-
const boundMethod = (...args) =>
127-
queueAndRun(
128-
() => controller.queueToVatObject(presence, method, args),
129-
true,
130-
);
131-
return harden(boundMethod);
132-
},
133-
}),
126+
makeMethodsProxy(controller.queueToVatObject, presence, true),
134127
get: presence =>
135128
new Proxy(harden({}), {
136-
get: (_t, pathElement, _rx) =>
137-
queueAndRun(() =>
138-
controller.queueToVatRoot('bootstrap', 'awaitVatObject', [
139-
presence,
140-
[pathElement],
141-
]),
142-
),
129+
get: (_t, key, _rx) =>
130+
EV.vat('bootstrap').awaitVatObject(presence, [key]),
143131
}),
144132
},
145133
);

0 commit comments

Comments
 (0)