Skip to content

Commit

Permalink
Merge pull request #8519 from Agoric/ta/liveslots-types
Browse files Browse the repository at this point in the history
fix liveslots types
  • Loading branch information
mergify[bot] authored Nov 11, 2023
2 parents 7244c71 + f7ac76c commit 5dc325b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 14 deletions.
2 changes: 0 additions & 2 deletions packages/SwingSet/src/vats/vattp/vat-vattp.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,13 @@ export function buildRootObject(vatPowers, _vatParams, baggage) {
// TODO: Stop silently creating mailboxes.
// https://github.com/Agoric/agoric-sdk/issues/5824
const { inbound } = provideMailbox(name);
// @ts-expect-error bad typedef
inbound.deliverMessages(newMessages);
},

deliverInboundAck: (name, ack) => {
// TODO: Stop silently creating mailboxes.
// https://github.com/Agoric/agoric-sdk/issues/5824
const { inbound } = provideMailbox(name);
// @ts-expect-error bad typedef
inbound.deliverAck(ack);
},
};
Expand Down
20 changes: 12 additions & 8 deletions packages/swingset-liveslots/src/vatDataTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
WeakMapStore,
WeakSetStore,
} from '@agoric/store';
import type { makeWatchedPromiseManager } from './watchedPromises.js';

// TODO should be moved into @endo/patterns and eventually imported here
// instead of this local definition.
Expand All @@ -25,17 +26,20 @@ export type { MapStore, Pattern };
// onerous.
export type Baggage = MapStore<string, any>;

type WatchedPromisesManager = ReturnType<typeof makeWatchedPromiseManager>;

type Tail<T extends any[]> = T extends [head: any, ...rest: infer Rest]
? Rest
: [];

type MinusContext<
F extends (context, ...rest: any[]) => any,
P extends any[] = Parameters<F>, // P: are the parameters of F
R = ReturnType<F>, // R: the return type of F
> = (...args: Tail<P>) => R;
// used to omit the 'context' parameter
type OmitFirstArg<F> = F extends (x: any, ...args: infer P) => infer R
? (...args: P) => R
: never;

export type KindFacet<O> = { [K in keyof O]: MinusContext<O[K]> };
export type KindFacet<O> = {
[K in keyof O]: OmitFirstArg<O[K]>; // omit the 'context' parameter
};

export type KindFacets<B> = {
[FacetKey in keyof B]: KindFacet<B[FacetKey]>;
Expand Down Expand Up @@ -169,8 +173,8 @@ export type VatData = {
options?: DefineKindOptions<MultiKindContext<S, B>>,
) => (...args: P) => KindFacets<B>;

providePromiseWatcher: unknown;
watchPromise: unknown;
providePromiseWatcher: WatchedPromisesManager['providePromiseWatcher'];
watchPromise: WatchedPromisesManager['watchPromise'];

makeScalarBigMapStore: <K, V>(
label: string,
Expand Down
6 changes: 5 additions & 1 deletion packages/swingset-liveslots/src/virtualObjectManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ const insistSameCapData = (oldCD, newCD) => {
* recursion if our returned WeakMap/WeakSet wrappers are subsequently installed
* on globalThis.
*
* @returns {object} a new virtual object manager.
* @returns a new virtual object manager.
*
* The virtual object manager allows the creation of persistent objects that do
* not need to occupy memory when they are not in use. It provides five
Expand Down Expand Up @@ -1124,6 +1124,7 @@ export const makeVirtualObjectManager = (
return id;
};

/** @type {import('./vatDataTypes').VatData['defineKind']} */
const defineKind = (tag, init, behavior, options) => {
const kindID = `${allocateExportID()}`;
saveVirtualKindDescriptor(kindID, { kindID, tag });
Expand All @@ -1139,6 +1140,7 @@ export const makeVirtualObjectManager = (
);
};

/** @type {import('./vatDataTypes').VatData['defineKindMulti']} */
const defineKindMulti = (tag, init, behavior, options) => {
const kindID = `${allocateExportID()}`;
saveVirtualKindDescriptor(kindID, { kindID, tag });
Expand Down Expand Up @@ -1178,6 +1180,7 @@ export const makeVirtualObjectManager = (
return kindHandle;
};

/** @type {import('./vatDataTypes').VatData['defineDurableKind']} */
const defineDurableKind = (kindHandle, init, behavior, options) => {
kindHandleToID.has(kindHandle) || Fail`unknown handle ${kindHandle}`;
const kindID = kindHandleToID.get(kindHandle);
Expand All @@ -1200,6 +1203,7 @@ export const makeVirtualObjectManager = (
return maker;
};

/** @type {import('./vatDataTypes').VatData['defineDurableKindMulti']} */
const defineDurableKindMulti = (kindHandle, init, behavior, options) => {
kindHandleToID.has(kindHandle) || Fail`unknown handle ${kindHandle}`;
const kindID = kindHandleToID.get(kindHandle);
Expand Down
6 changes: 6 additions & 0 deletions packages/swingset-liveslots/src/watchedPromises.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ export function makeWatchedPromiseManager({
}
}

/**
*
* @param {Promise} p
* @param {{onFulfilled?: Function, onRejected?: Function}} watcher
* @param {...any} args
*/
function watchPromise(p, watcher, ...args) {
// The following wrapping defers setting up the promise watcher itself to a
// later turn so that if the promise to be watched was the return value from
Expand Down
1 change: 1 addition & 0 deletions packages/swingset-liveslots/test/test-durabilityChecks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ async function runDurabilityCheckTest(t, relaxDurabilityRules) {

const durableHolderKind = makeKindHandle('holder');

/** @param {any} held */
const initHolder = (held = null) => ({ held });
const holderBehavior = {
hold: ({ state }, value) => {
Expand Down
11 changes: 9 additions & 2 deletions packages/swingset-liveslots/tools/setup-vat-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@ const { WeakMap, WeakSet } = globalThis;
let fakeVomKit;

globalThis.VatData = harden({
// @ts-expect-error spread argument for non-rest parameter
defineKind: (...args) => fakeVomKit.vom.defineKind(...args),
// @ts-expect-error spread argument for non-rest parameter
defineKindMulti: (...args) => fakeVomKit.vom.defineKindMulti(...args),
// @ts-expect-error spread argument for non-rest parameter
defineDurableKind: (...args) => fakeVomKit.vom.defineDurableKind(...args),
defineDurableKindMulti: (...args) =>
// @ts-expect-error spread argument for non-rest parameter
fakeVomKit.vom.defineDurableKindMulti(...args),
makeKindHandle: (...args) => fakeVomKit.vom.makeKindHandle(...args),
makeKindHandle: tag => fakeVomKit.vom.makeKindHandle(tag),
canBeDurable: (...args) => fakeVomKit.vom.canBeDurable(...args),
providePromiseWatcher: (...args) =>
fakeVomKit.wpm.providePromiseWatcher(...args),
watchPromise: (...args) => fakeVomKit.wpm.watchPromise(...args),
watchPromise: (p, watcher, ...args) =>
fakeVomKit.wpm.watchPromise(p, watcher, ...args),
makeScalarBigMapStore: (...args) =>
fakeVomKit.cm.makeScalarBigMapStore(...args),
makeScalarBigWeakMapStore: (...args) =>
Expand All @@ -47,7 +52,9 @@ export const reincarnate = (options = {}) => {
WeakSet,
});

// @ts-expect-error toStringTag set imperatively so it doesn't show up in the type
globalThis.WeakMap = fakeVomKit.vom.VirtualObjectAwareWeakMap;
// @ts-expect-error ditto
globalThis.WeakSet = fakeVomKit.vom.VirtualObjectAwareWeakSet;

return { ...options, fakeStore, fakeVomKit };
Expand Down
12 changes: 11 additions & 1 deletion packages/vat-data/src/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
KindFacet,
FunctionsPlusContext,
} from '@agoric/swingset-liveslots';
import { VirtualObjectManager } from '@agoric/swingset-liveslots/src/virtualObjectManager.js';
import {
defineKind,
defineKindMulti,
Expand All @@ -14,6 +15,9 @@ import {
partialAssign,
} from '.';

// for use in assignments below
const anyVal = null as any;

/*
export const makePaymentMaker = (allegedName: string, brand: unknown) => {
const makePayment = defineKind(
Expand Down Expand Up @@ -154,7 +158,13 @@ const someBehavior: FunctionsPlusContext<SomeContext, SomeFacet> = {
return b > context.state.a;
},
};
const someFacet: KindFacet<typeof someBehavior> = null as any;
const someFacet: KindFacet<typeof someBehavior> = anyVal;
// @ts-expect-error
someFacet.gt();
expectType<boolean>(someFacet.gt(1));

const vom: VirtualObjectManager = anyVal;
// @ts-expect-error
vom.missingMethod;
// @ts-expect-error Expected 0-4 arguments but got 5
vom.defineDurableKind(anyVal, anyVal, anyVal, anyVal, 'extra');

0 comments on commit 5dc325b

Please sign in to comment.