Skip to content

Commit

Permalink
feat(pass-style): Extract passStyleOf and friends from marshal into t…
Browse files Browse the repository at this point in the history
…he new pass-style package (#1439)
  • Loading branch information
erights authored Jan 14, 2023
1 parent c4b3fff commit ccd003c
Show file tree
Hide file tree
Showing 43 changed files with 331 additions and 279 deletions.
10 changes: 10 additions & 0 deletions packages/captp/src/captp.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

// This logic was mostly lifted from @agoric/swingset-vat liveSlots.js
// Defects in it are mfig's fault.
//
// @ts-ignore We actually mean the function, not the type,
// but TS somehow no longer knows that -- following the extraction
// of @endo/pass-style from @endo/marshal.
// We're using @ts-ignore here because TS is inconsistent about whether this
// line is an error. It is not locally, but it is under CI.
import { Remotable, Far, makeMarshal, QCLASS } from '@endo/marshal';
import { E, HandledPromise } from '@endo/eventual-send';
import { isPromise, makePromiseKit } from '@endo/promise-kit';
Expand Down Expand Up @@ -326,6 +332,10 @@ export const makeCapTP = (
}
// A new remote presence
// Use Remotable rather than Far to make a remote from a presence
//
// @ts-expect-error We actually mean the function, not the type,
// but TS somehow no longer knows that -- following the extraction
// of @endo/pass-style from @endo/marshal.
val = Remotable(iface, undefined, pr.resPres());
} else {
// A new promise
Expand Down
2 changes: 1 addition & 1 deletion packages/far/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"homepage": "https://github.com/endojs/endo#readme",
"dependencies": {
"@endo/eventual-send": "^0.16.9",
"@endo/marshal": "^0.8.2"
"@endo/pass-style": "^0.1.0"
},
"devDependencies": {
"@endo/init": "^0.5.53",
Expand Down
2 changes: 1 addition & 1 deletion packages/far/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { E } from '@endo/eventual-send';
export { Far, getInterfaceOf, passStyleOf } from '@endo/marshal';
export { Far, getInterfaceOf, passStyleOf } from '@endo/pass-style';

/**
* @template Primary
Expand Down
38 changes: 7 additions & 31 deletions packages/marshal/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@
export { mapIterable, filterIterable } from './src/helpers/iter-helpers.js';
export {
PASS_STYLE,
isObject,
assertChecker,
getTag,
hasOwnPropertyOf,
} from './src/helpers/passStyle-helpers.js';

export { getErrorConstructor, toPassableError } from './src/helpers/error.js';
export { getInterfaceOf } from './src/helpers/remotable.js';

export {
nameForPassableSymbol,
passableSymbolForName,
} from './src/helpers/symbol.js';

export { passStyleOf, assertPassable } from './src/passStyleOf.js';

export { deeplyFulfilled } from './src/deeplyFulfilled.js';

export { makeTagged } from './src/makeTagged.js';
export { Remotable, Far, ToFarFunction } from './src/make-far.js';

export { QCLASS } from './src/encodeToCapData.js';
export { makeMarshal } from './src/marshal.js';
export { stringify, parse } from './src/marshal-stringify.js';

export { decodeToJustin } from './src/marshal-justin.js';

export {
assertRecord,
assertCopyArray,
assertRemotable,
isRemotable,
isRecord,
isCopyArray,
} from './src/typeGuards.js';

// eslint-disable-next-line import/export
export * from './src/types.js';

// For compatibility, but importers of these should instead import these
// directly from `@endo/pass-style` or (if applicable) `@endo/far`.
// @ts-expect-error TS only complains about this line when checking other
// packages that depend on this one, like marshal. The complaint is about
// repeatedly exported types. Specifically "Remotable".
export * from '@endo/pass-style';
1 change: 1 addition & 0 deletions packages/marshal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"dependencies": {
"@endo/eventual-send": "^0.16.9",
"@endo/nat": "^4.1.24",
"@endo/pass-style": "^0.1.0",
"@endo/promise-kit": "^0.2.53"
},
"devDependencies": {
Expand Down
4 changes: 1 addition & 3 deletions packages/marshal/src/deeplyFulfilled.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import { E } from '@endo/eventual-send';
import { isPromise } from '@endo/promise-kit';
import { getTag, isObject } from './helpers/passStyle-helpers.js';
import { makeTagged } from './makeTagged.js';
import { passStyleOf } from './passStyleOf.js';
import { getTag, isObject, makeTagged, passStyleOf } from '@endo/pass-style';

/** @typedef {import('./types.js').Passable} Passable */
/** @template T @typedef {import('@endo/eventual-send').ERef<T>} ERef */
Expand Down
5 changes: 1 addition & 4 deletions packages/marshal/src/dot-membrane.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
/// <reference types="ses"/>

import { E } from '@endo/eventual-send';
import { isObject } from './helpers/passStyle-helpers.js';
import { getInterfaceOf } from './helpers/remotable.js';
import { Far } from './make-far.js';
import { isObject, getInterfaceOf, Far, passStyleOf } from '@endo/pass-style';
import { makeMarshal } from './marshal.js';
import { passStyleOf } from './passStyleOf.js';

const { fromEntries } = Object;
const { ownKeys } = Reflect;
Expand Down
17 changes: 10 additions & 7 deletions packages/marshal/src/encodePassable.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* eslint-disable no-bitwise */
import { getTag } from './helpers/passStyle-helpers.js';
import { makeTagged } from './makeTagged.js';
import { passStyleOf } from './passStyleOf.js';
import { assertRecord } from './typeGuards.js';
import {
getTag,
makeTagged,
passStyleOf,
assertRecord,
isErrorLike,
nameForPassableSymbol,
passableSymbolForName,
} from './helpers/symbol.js';
import { ErrorHelper } from './helpers/error.js';
} from '@endo/pass-style';

/** @typedef {import('./types.js').PassStyle} PassStyle */
/** @typedef {import('./types.js').Passable} Passable */
Expand Down Expand Up @@ -305,7 +305,7 @@ export const makeEncodePassable = ({
encodeError = (err, _) => Fail`error unexpected: ${err}`,
} = {}) => {
const encodePassable = passable => {
if (ErrorHelper.canBeValid(passable)) {
if (isErrorLike(passable)) {
return encodeError(passable, encodePassable);
}
const passStyle = passStyleOf(passable);
Expand Down Expand Up @@ -470,6 +470,9 @@ harden(isEncodedRemotable);
* prefix used by any cover so that ordinal mapping keys are always outside
* the range of valid collection entry keys.
*/
// @ts-expect-error TS does not understand thst `__proto__;` in this position
// is special syntax. Instead, it complains that the `null` is not a string,
// which would only make sense if this were defining a property.
export const passStylePrefixes = harden({
__proto__: null,
error: '!',
Expand Down
13 changes: 5 additions & 8 deletions packages/marshal/src/encodeToCapData.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@
// encodes to CapData, a JSON-representable data structure, and leaves it to
// the caller (`marshal.js`) to stringify it.

import { passStyleOf } from './passStyleOf.js';

import { ErrorHelper } from './helpers/error.js';
import { makeTagged } from './makeTagged.js';
import {
passStyleOf,
isErrorLike,
makeTagged,
isObject,
getTag,
hasOwnPropertyOf,
} from './helpers/passStyle-helpers.js';
import {
assertPassableSymbol,
nameForPassableSymbol,
passableSymbolForName,
} from './helpers/symbol.js';
} from '@endo/pass-style';

/** @typedef {import('./types.js').Passable} Passable */
/** @typedef {import('./types.js').Encoding} Encoding */
Expand Down Expand Up @@ -249,7 +246,7 @@ export const makeEncodeToCapData = ({
}
};
const encodeToCapData = passable => {
if (ErrorHelper.canBeValid(passable)) {
if (isErrorLike(passable)) {
// We pull out this special case to accommodate errors that are not
// valid Passables. For example, because they're not frozen.
// The special case can only ever apply at the root, and therefore
Expand Down
14 changes: 7 additions & 7 deletions packages/marshal/src/encodeToSmallcaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
// encodes to Smallcaps, a JSON-representable data structure, and leaves it to
// the caller (`marshal.js`) to stringify it.

import { passStyleOf } from './passStyleOf.js';

import { ErrorHelper } from './helpers/error.js';
import { makeTagged } from './makeTagged.js';
import { getTag, hasOwnPropertyOf } from './helpers/passStyle-helpers.js';
import {
passStyleOf,
isErrorLike,
makeTagged,
getTag,
hasOwnPropertyOf,
assertPassableSymbol,
nameForPassableSymbol,
passableSymbolForName,
} from './helpers/symbol.js';
} from '@endo/pass-style';

/** @typedef {import('./types.js').Passable} Passable */
/** @typedef {import('./types.js').Remotable} Remotable */
Expand Down Expand Up @@ -269,7 +269,7 @@ export const makeEncodeToSmallcaps = ({
}
};
const encodeToSmallcaps = passable => {
if (ErrorHelper.canBeValid(passable)) {
if (isErrorLike(passable)) {
// We pull out this special case to accommodate errors that are not
// valid Passables. For example, because they're not frozen.
// The special case can only ever apply at the root, and therefore
Expand Down
12 changes: 8 additions & 4 deletions packages/marshal/src/marshal-justin.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/// <reference types="ses"/>

import { Nat } from '@endo/nat';
import {
getErrorConstructor,
isObject,
passableSymbolForName,
} from '@endo/pass-style';
import { QCLASS } from './encodeToCapData.js';

import { getErrorConstructor } from './helpers/error.js';
import { isObject } from './helpers/passStyle-helpers.js';
import { AtAtPrefixPattern, passableSymbolForName } from './helpers/symbol.js';

/** @typedef {import('./types.js').Encoding} Encoding */
/** @template T @typedef {import('./types.js').CapData<T>} CapData */

Expand Down Expand Up @@ -112,6 +113,9 @@ const makeNoIndenter = () => {
};

const identPattern = /^[a-zA-Z]\w*$/;
harden(identPattern);
const AtAtPrefixPattern = /^@@(.*)$/;
harden(AtAtPrefixPattern);

/**
* @param {Encoding} encoding
Expand Down
20 changes: 14 additions & 6 deletions packages/marshal/src/marshal.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/// <reference types="ses"/>

import { Nat } from '@endo/nat';
import { assertPassable } from './passStyleOf.js';
import {
assertPassable,
getInterfaceOf,
getErrorConstructor,
hasOwnPropertyOf,
} from '@endo/pass-style';

import { getInterfaceOf } from './helpers/remotable.js';
import { getErrorConstructor } from './helpers/error.js';
import {
QCLASS,
makeEncodeToCapData,
Expand All @@ -14,7 +17,6 @@ import {
makeDecodeFromSmallcaps,
makeEncodeToSmallcaps,
} from './encodeToSmallcaps.js';
import { hasOwnPropertyOf } from './helpers/passStyle-helpers.js';

/** @typedef {import('./types.js').MakeMarshalOptions} MakeMarshalOptions */
/** @template Slot @typedef {import('./types.js').ConvertSlotToVal<Slot>} ConvertSlotToVal */
Expand Down Expand Up @@ -99,7 +101,7 @@ export const makeMarshal = (
* Even if an Error is not actually passable, we'd rather send
* it anyway because the diagnostic info carried by the error
* is more valuable than diagnosing why the error isn't
* passable. See comments in ErrorHelper.
* passable. See comments in isErrorLike.
*
* @param {Error} err
* @param {(p: Passable) => unknown} encodeRecur
Expand Down Expand Up @@ -156,7 +158,7 @@ export const makeMarshal = (
* Even if an Error is not actually passable, we'd rather send
* it anyway because the diagnostic info carried by the error
* is more valuable than diagnosing why the error isn't
* passable. See comments in ErrorHelper.
* passable. See comments in isErrorLike.
*
* @param {Error} err
* @param {(p: Passable) => Encoding} encodeRecur
Expand Down Expand Up @@ -323,7 +325,13 @@ export const makeMarshal = (
};

const reviveFromSmallcaps = makeDecodeFromSmallcaps({
// @ts-expect-error This error started after separating pass-style
// out of marshal into its own package. Aside from that, I do not
// understand this error at all.
decodeRemotableFromSmallcaps,
// @ts-expect-error This error started after separating pass-style
// out of marshal into its own package. Aside from that, I do not
// understand this error at all.
decodePromiseFromSmallcaps,
decodeErrorFromSmallcaps,
});
Expand Down
4 changes: 1 addition & 3 deletions packages/marshal/src/rankOrder.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { getTag } from './helpers/passStyle-helpers.js';
import { passStyleOf } from './passStyleOf.js';
import { nameForPassableSymbol } from './helpers/symbol.js';
import { getTag, passStyleOf, nameForPassableSymbol } from '@endo/pass-style';
import {
passStylePrefixes,
recordNames,
Expand Down
Loading

0 comments on commit ccd003c

Please sign in to comment.