Skip to content

Commit

Permalink
feat(types): fromCapData is Passable, but unknown is more practical
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed May 2, 2024
1 parent dacd908 commit 5fa54f0
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 15 deletions.
3 changes: 2 additions & 1 deletion packages/captp/src/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ export const makeCapTP = (
epoch,
questionID,
target,
// @ts-expect-error Type 'unknown' is not assignable to type 'Passable<PassableCap, Error>'.
method: serialize(harden([null, args])),
});
return promise;
Expand All @@ -425,6 +426,7 @@ export const makeCapTP = (
epoch,
questionID,
target,
// @ts-expect-error Type 'unknown' is not assignable to type 'Passable<PassableCap, Error>'.
method: serialize(harden([prop, args])),
});
return promise;
Expand Down Expand Up @@ -493,7 +495,6 @@ export const makeCapTP = (
}

// If we imported this slot, mark it as one our peer exported.
// @ts-expect-error map lacks value type
return slotToImported.get(recvSlot.add(slot));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/marshal/src/marshal-stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ harden(stringify);

/**
* @param {string} str
* @returns {Passable}
* @returns {unknown}
*/
const parse = str =>
unserialize(
Expand Down
8 changes: 4 additions & 4 deletions packages/marshal/src/marshal.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ export const makeMarshal = (
};

const makeFullRevive = slots => {
/** @type {Map<number, RemotableObject | Promise>} */
/** @type {Map<number>} */
const valMap = new Map();

/**
* @param {{iface?: string, index: number}} slotData
* @returns {RemotableObject | Promise}
* @returns {PassableCap}
*/
const decodeSlotCommon = slotData => {
const { iface = undefined, index, ...rest } = slotData;
Expand Down Expand Up @@ -346,7 +346,7 @@ export const makeMarshal = (
const makeDecodeSlotFromSmallcaps = prefix => {
/**
* @param {string} stringEncoding
* @param {(e: unknown) => Passable} _decodeRecur
* @param {(e: unknown) => PassableCap} _decodeRecur
* @returns {RemotableObject | Promise}
*/
return (stringEncoding, _decodeRecur) => {
Expand Down Expand Up @@ -404,7 +404,7 @@ export const makeMarshal = (
// which should be considered fixed once we've completed the switch
// to smallcaps.
assertPassable(result);
return result;
return /** @type {PassableCap} */ (result);
};

return harden({
Expand Down
10 changes: 6 additions & 4 deletions packages/marshal/src/types.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
// @ts-check
export {};

/** @import {Passable} from '@endo/pass-style' */
/** @import {Passable, PassableCap} from '@endo/pass-style' */

/**
* @template Slot
* @template {PassableCap} [Value=any]
* @callback ConvertValToSlot
* @param {import("@endo/pass-style").PassableCap} val
* @param {Value} val
* @returns {Slot}
*/

/**
* @template Slot
* @template {PassableCap} [Value=any]
* @callback ConvertSlotToVal
* @param {Slot} slot
* @param {string} [iface]
* @returns {import("@endo/pass-style").PassableCap}
* @returns {Value}
*/

/**
Expand Down Expand Up @@ -88,7 +90,7 @@ export {};
/**
* @template Slot
* @callback ToCapData
* @param {any} val a Passable
* @param {Passable} val
* @returns {CapData<Slot>}
*/

Expand Down
25 changes: 24 additions & 1 deletion packages/marshal/src/types.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import { expectType, expectNotType } from 'tsd';

import type { PrimitiveStyle } from '@endo/pass-style';
import {
Far,
type PrimitiveStyle,
type RemotableObject,
} from '@endo/pass-style';
import { makeMarshal } from './marshal.js';

expectType<PrimitiveStyle>('string');
expectType<PrimitiveStyle>('number');
// @ts-expect-error
expectType<PrimitiveStyle>(1);
// @ts-expect-error
expectType<PrimitiveStyle>('str');

type KCap = RemotableObject & { getKref: () => string; iface: () => string };
const valToSlot = (s: KCap) => s.getKref();
const slotToVal = (s: string) => null as unknown as KCap;
const marshal = makeMarshal(valToSlot, slotToVal);
const cycled = marshal.fromCapData(marshal.toCapData(null as unknown as KCap));
expectType<unknown>(cycled);

const m = makeMarshal();
type SlottedRemotable = { getBoardId: () => string };
const foo = Far('foo');
const foo1 = Far('foo', { getBoardId: () => 'board1' });
const foo2 = Far('foo', { getBoardId: () => 'board2' });
const bar = Far('bar');
const bar1 = Far('bar', { getBoardId: () => 'board1' });
m.toCapData(harden({ o: foo1 }));
m.toCapData(harden({ o: foo2 }));
m.toCapData(harden({ o: bar1 }));
2 changes: 1 addition & 1 deletion packages/pass-style/src/internal-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ export {};
* `assertValid` still needs to be called to determine if it
* actually is valid.
* @property {(candidate: any,
* passStyleOfRecur: PassStyleOf
* passStyleOfRecur: (val: any) => PassStyle
* ) => void} assertValid
*/
3 changes: 0 additions & 3 deletions packages/pass-style/src/passStyleOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,10 @@ const makePassStyleOf = passStyleHelpers => {
}
for (const helper of passStyleHelpers) {
if (helper.canBeValid(inner)) {
// @ts-expect-error XXX
helper.assertValid(inner, passStyleOfRecur);
return helper.styleName;
}
}
// @ts-expect-error XXX
remotableHelper.assertValid(inner, passStyleOfRecur);
return 'remotable';
}
Expand All @@ -182,7 +180,6 @@ const makePassStyleOf = passStyleHelpers => {
Fail`Cannot pass non-frozen objects like ${inner}. Use harden()`;
typeof inner.then !== 'function' ||
Fail`Cannot pass non-promise thenables`;
// @ts-expect-error XXX
remotableHelper.assertValid(inner, passStyleOfRecur);
return 'remotable';
}
Expand Down

0 comments on commit 5fa54f0

Please sign in to comment.