Skip to content

Commit

Permalink
Minor typing and other cleanups extracted from #3795 (#3851)
Browse files Browse the repository at this point in the history
  • Loading branch information
erights authored Sep 19, 2021
1 parent 90a456f commit c50dc5d
Show file tree
Hide file tree
Showing 27 changed files with 124 additions and 159 deletions.
5 changes: 0 additions & 5 deletions packages/ERTP/src/types.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// eslint-disable-next-line spaced-comment
/// <reference types="ses"/>

/**
* @template T
* @typedef {import('@agoric/promise-kit').ERef<T>} ERef
*/

/**
* @typedef {import('@agoric/marshal').InterfaceSpec} InterfaceSpec
* @typedef {import('@agoric/marshal').GetInterfaceOf} GetInterfaceOf
Expand Down
4 changes: 0 additions & 4 deletions packages/SwingSet/src/vats/network/internal-types.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/SwingSet/src/vats/network/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { toBytes } from './bytes.js';

import '@agoric/store/exported.js';
import './types.js';
import './internal-types.js';

/**
* Compatibility note: this must match what our peers use,
Expand Down
1 change: 0 additions & 1 deletion packages/SwingSet/src/vats/network/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { makeNetworkProtocol, ENDPOINT_SEPARATOR } from './network.js';

import '@agoric/store/exported.js';
import './types.js';
import './internal-types.js';

/**
* @typedef {Object} Router A delimited string router implementation
Expand Down
6 changes: 4 additions & 2 deletions packages/SwingSet/src/vats/network/types.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

/**
* @typedef {string|Buffer|ArrayBuffer} Data
* @typedef {string} Bytes
Expand All @@ -22,7 +24,7 @@
* @typedef {Object} Port A port that has been bound to a protocol
* @property {() => Endpoint} getLocalAddress Get the locally bound name of this port
* @property {(acceptHandler: ListenHandler) => Promise<void>} addListener Begin accepting incoming connections
* @property {(remote: Endpoint, connectionHandler: ConnectionHandler = {}) => Promise<Connection>} connect Make an outbound connection
* @property {(remote: Endpoint, connectionHandler?: ConnectionHandler) => Promise<Connection>} connect Make an outbound connection
* @property {(acceptHandler: ListenHandler) => Promise<void>} removeListener Remove the currently-bound listener
* @property {() => void} revoke Deallocate the port entirely, removing all listeners and closing all active connections
*/
Expand All @@ -48,7 +50,7 @@
* @typedef {Object} ConnectionHandler A handler for a given Connection
* @property {(connection: Connection, localAddr: Endpoint, remoteAddr: Endpoint, c: ConnectionHandler) => void} [onOpen] The connection has been opened
* @property {(connection: Connection, packetBytes: Bytes, c: ConnectionHandler) => Promise<Data>} [onReceive] The connection received a packet
* @property {(connection: Connection, reason?: CloseReason, c: ConnectionHandler) => Promise<void>} [onClose] The connection has been closed
* @property {(connection: Connection, reason?: CloseReason, c?: ConnectionHandler) => Promise<void>} [onClose] The connection has been closed
*
* @typedef {any?} CloseReason The reason a connection was closed
*/
Expand Down
6 changes: 4 additions & 2 deletions packages/dapp-svelte-wallet/api/src/internal-types.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

/**
* @typedef {Object} PursesAddedState
* @property {Purse} purse
Expand All @@ -13,7 +15,7 @@
* @typedef {Object} PurseActions
* @property {(receiverP: ERef<{ receive: (payment: Payment) => void }>, valueToSend: Value) => Promise<void>} send
* @property {(payment: Payment) => Promise<Amount>} receive
* @property {(payment: Payment, amount: Amount=) => Promise<Amount>} deposit
* @property {(payment: Payment, amount?: Amount) => Promise<Amount>} deposit
*/

/**
Expand Down Expand Up @@ -73,7 +75,7 @@
* @property {string=} issuerBoardId
*
* @typedef {Object} PaymentActions
* @property {(purseOrPetname: (Purse | Petname)=) => Promise<Value>} deposit
* @property {(purseOrPetname?: (Purse | Petname)) => Promise<Value>} deposit
* @property {() => Promise<boolean>} refresh
* @property {() => Promise<boolean>} getAmountOf
*/
Expand Down
5 changes: 0 additions & 5 deletions packages/dapp-svelte-wallet/api/src/lib-wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ import '@agoric/zoe/exported.js';
import './internal-types.js';
import './types.js';

/**
* @template T
* @typedef {import('@agoric/promise-kit').PromiseRecord<T>} PromiseRecord
*/

// does nothing
const noActionStateChangeHandler = _newState => {};

Expand Down
5 changes: 1 addition & 4 deletions packages/dapp-svelte-wallet/api/src/types.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/**
* @template T
* @typedef {import('@agoric/promise-kit').ERef<T>} ERef
*/
// @ts-check

/**
* @typedef {string | string[]} Petname A petname can either be a plain string
Expand Down
5 changes: 0 additions & 5 deletions packages/marshal/src/helpers/copyArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@

import '../types.js';
import './internal-types.js';
/**
* TODO Why do I need these?
*
* @typedef {import('./internal-types.js').PassStyleHelper} PassStyleHelper
*/
import '@agoric/assert/exported.js';
import { assertChecker, checkNormalProperty } from './passStyleHelpers.js';

Expand Down
5 changes: 0 additions & 5 deletions packages/marshal/src/helpers/copyRecord.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ import {

import '../types.js';
import './internal-types.js';
/**
* TODO Why do I need these?
*
* @typedef {import('./internal-types.js').PassStyleHelper} PassStyleHelper
*/
import '@agoric/assert/exported.js';

const { details: X } = assert;
Expand Down
5 changes: 0 additions & 5 deletions packages/marshal/src/helpers/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@

import '../types.js';
import './internal-types.js';
/**
* TODO Why do I need these?
*
* @typedef {import('./internal-types.js').PassStyleHelper} PassStyleHelper
*/
import '@agoric/assert/exported.js';
import { assertChecker } from './passStyleHelpers.js';

Expand Down
9 changes: 0 additions & 9 deletions packages/marshal/src/helpers/internal-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@
// eslint-disable-next-line spaced-comment
/// <reference path="../extra-types.d.ts" />

import '../types.js';

/**
* @callback Checker
* @param {boolean} cond
* @param {Details=} details
* @returns {boolean}
*/

/**
* The PassStyleHelper are only used to make a `passStyleOf` function.
* Thus, it should not depend on an ambient one. Rather, each helper should be
Expand Down
10 changes: 4 additions & 6 deletions packages/marshal/src/helpers/passStyleHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@

import '../types.js';
import './internal-types.js';
/**
* TODO Why do I need these?
*
* @typedef {import('./internal-types.js').Checker} Checker
*/
import '@agoric/assert/exported.js';

const { details: X, quote: q } = assert;
Expand Down Expand Up @@ -106,6 +101,9 @@ export const checkNormalProperty = (
};
harden(checkNormalProperty);

export const getTag = tagRecord => tagRecord[Symbol.toStringTag];
harden(getTag);

/**
* @param {{ [PASS_STYLE]: string }} tagRecord
* @param {PassStyle} passStyle
Expand Down Expand Up @@ -138,7 +136,7 @@ export const checkTagRecord = (tagRecord, passStyle, check = x => x) => {
check,
) &&
check(
typeof tagRecord[Symbol.toStringTag] === 'string',
typeof getTag(tagRecord) === 'string',
X`A [Symbol.toString]-named property must be a string: ${tagRecord}`,
)
);
Expand Down
29 changes: 10 additions & 19 deletions packages/marshal/src/helpers/remotable.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@

import '../types.js';
import './internal-types.js';
/**
* TODO Why do I need these?
*
* @typedef {import('./internal-types.js').PassStyleHelper} PassStyleHelper
* @typedef {import('./internal-types.js').Checker} Checker
*/
import '@agoric/assert/exported.js';
import {
assertChecker,
Expand All @@ -19,6 +13,7 @@ import {
PASS_STYLE,
checkTagRecord,
isObject,
getTag,
} from './passStyleHelpers.js';
import { getEnvironmentOption } from './environment-options.js';

Expand Down Expand Up @@ -49,7 +44,7 @@ export const ALLOW_IMPLICIT_REMOTABLES =

/**
* @param {InterfaceSpec} iface
* @param {Checker} check
* @param {Checker=} check
*/
const checkIface = (iface, check = x => x) => {
return (
Expand Down Expand Up @@ -132,20 +127,16 @@ const checkRemotableProtoOf = (original, check = x => x) => {
}

const {
// @ts-ignore https://github.com/microsoft/TypeScript/issues/1863
[PASS_STYLE]: _passStyleDesc,
// @ts-ignore https://github.com/microsoft/TypeScript/issues/1863
[Symbol.toStringTag]: ifaceDesc,
...restDescs
} = getOwnPropertyDescriptors(proto);
[PASS_STYLE]: _passStyle,
[Symbol.toStringTag]: iface,
...rest
} = proto;

return (
check(
ownKeys(restDescs).length === 0,
X`Unexpected properties on Remotable Proto ${ownKeys(restDescs)}`,
) &&
// @ts-ignore red highlights in vscode but `yarn test` clean.
checkIface(ifaceDesc && ifaceDesc.value, check)
ownKeys(rest).length === 0,
X`Unexpected properties on Remotable Proto ${ownKeys(rest)}`,
) && checkIface(iface, check)
);
};

Expand Down Expand Up @@ -188,7 +179,7 @@ export const getInterfaceOf = val => {
) {
return undefined;
}
return val[Symbol.toStringTag];
return getTag(val);
};
harden(getInterfaceOf);

Expand Down
9 changes: 1 addition & 8 deletions packages/marshal/src/marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export function makeMarshal(
X`invalid qclass typeof ${q(typeof qclass)}`,
);
assert(!isArray(rawTree));
// Switching on `encoded[QCLASS]` (or anything less direct, like
// Switching on `rawTree[QCLASS]` (or anything less direct, like
// `qclass`) does not discriminate rawTree in typescript@4.2.3 and
// earlier.
switch (rawTree['@qclass']) {
Expand Down Expand Up @@ -403,13 +403,6 @@ export function makeMarshal(

default: {
assert(
// @ts-ignore Should be at-ts-expect-error, but see
// https://github.com/Agoric/agoric-sdk/issues/3840
//
// This value indeed violates the current types.
// We test for it to give a more informative diagnostic if we
// receive it from a counterparty using an older version of the
// protocol.
qclass !== 'ibid',
X`The protocol no longer supports ibid encoding: ${rawTree}.`,
);
Expand Down
32 changes: 15 additions & 17 deletions packages/marshal/src/passStyleOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@ const { details: X, quote: q } = assert;
const { ownKeys } = Reflect;
const { isFrozen } = Object;

// TODO Why do I need to import the type this time? I still don't have a model.
/**
* @typedef {import('./helpers/internal-types.js').PassStyleHelper} PassStyleHelper
*/

/**
* @param {PassStyleHelper[]} passStyleHelper The passStyleHelper to register,
* @param {PassStyleHelper[]} passStyleHelpers The passStyleHelpers to register,
* in priority order.
* NOTE These must all be "trusted",
* complete, and non-colliding. `makePassStyleOf` may *assume* that each helper
Expand All @@ -33,24 +28,28 @@ const { isFrozen } = Object;
* accidents.
* @returns {{passStyleOf: PassStyleOf, HelperTable: any}}
*/
const makePassStyleOfKit = passStyleHelper => {
const makePassStyleOfKit = passStyleHelpers => {
const HelperTable = {
__proto__: null,
copyArray: undefined,
copyRecord: undefined,
error: undefined,
remotable: undefined,
};
for (const helper of passStyleHelper) {
for (const helper of passStyleHelpers) {
const { styleName } = helper;
assert(styleName in HelperTable);
assert.equal(HelperTable[styleName], undefined);
assert(styleName in HelperTable, X`Unrecognized helper: ${q(styleName)}`);
assert.equal(
HelperTable[styleName],
undefined,
X`conflicting helpers for ${q(styleName)}`,
);
HelperTable[styleName] = helper;
}
for (const styleName of ownKeys(HelperTable)) {
assert(
HelperTable[styleName] !== undefined,
X`missing helper for ${styleName}`,
X`missing helper for ${q(styleName)}`,
);
}
harden(HelperTable);
Expand All @@ -69,7 +68,7 @@ const makePassStyleOfKit = passStyleHelper => {
*
* @type {WeakMap<Passable, PassStyle>}
*/
const passStyleOfCache = new WeakMap();
const passStyleMemo = new WeakMap();

/**
* @type {PassStyleOf}
Expand All @@ -86,9 +85,9 @@ const makePassStyleOfKit = passStyleHelper => {
const passStyleOfRecur = inner => {
const innerIsObject = isObject(inner);
if (innerIsObject) {
if (passStyleOfCache.has(inner)) {
if (passStyleMemo.has(inner)) {
// @ts-ignore TypeScript doesn't know that `get` after `has` is safe
return passStyleOfCache.get(inner);
return passStyleMemo.get(inner);
}
assert(
!inProgress.has(inner),
Expand All @@ -99,7 +98,7 @@ const makePassStyleOfKit = passStyleHelper => {
// eslint-disable-next-line no-use-before-define
const passStyle = passStyleOfInternal(inner);
if (innerIsObject) {
passStyleOfCache.set(inner, passStyle);
passStyleMemo.set(inner, passStyle);
inProgress.delete(inner);
}
return passStyle;
Expand Down Expand Up @@ -145,7 +144,7 @@ const makePassStyleOfKit = passStyleHelper => {
helper.assertValid(inner, passStyleOfRecur);
return /** @type {PassStyle} */ (passStyleTag);
}
for (const helper of passStyleHelper) {
for (const helper of passStyleHelpers) {
if (helper.canBeValid(inner)) {
helper.assertValid(inner, passStyleOfRecur);
return helper.styleName;
Expand Down Expand Up @@ -183,7 +182,6 @@ const { passStyleOf, HelperTable } = makePassStyleOfKit([
ErrorHelper,
RemotableHelper,
]);

export { passStyleOf };

export const everyPassableChild = (passable, fn) => {
Expand Down
Loading

0 comments on commit c50dc5d

Please sign in to comment.