refactor: use Fail template tag
MODIFY
packages/marshal/src/encodePassable.js
packages/marshal/src/encodePassable.js
@@ -15,7 +15,7 @@ import { ErrorHelper } from './helpers/error.js';
/** @template T @typedef {import('./types.js').CopyRecord<T>} CopyRecord */
/** @typedef {import('./types.js').RankCover} RankCover */
-const { details: X, quote: q } = assert;
+const { quote: q, Fail } = assert;
const { fromEntries, is } = Object;
const { ownKeys } = Reflect;
@@ -106,8 +106,7 @@ const encodeBinary64 = n => {
};
const decodeBinary64 = encoded => {
- encoded.startsWith('f') ||
- assert.fail(X`Encoded number expected: ${encoded}`);
+ encoded.startsWith('f') || Fail`Encoded number expected: ${encoded}`;
let bits = BigInt(`0x${encoded.substring(1)}`);
if (encoded[1] < '8') {
bits ^= 0xffffffffffffffffn;
@@ -116,7 +115,7 @@ const decodeBinary64 = encoded => {
}
asBits[0] = bits;
const result = asNumber[0];
- !is(result, -0) || assert.fail(X`Unexpected negative zero: ${encoded}`);
+ !is(result, -0) || Fail`Unexpected negative zero: ${encoded}`;
return result;
};
@@ -168,18 +167,16 @@ const decodeBigInt = encoded => {
let rem = encoded.slice(1);
typePrefix === 'p' ||
typePrefix === 'n' ||
- assert.fail(X`Encoded bigint expected: ${encoded}`);
+ Fail`Encoded bigint expected: ${encoded}`;
const lDigits = rem.search(/[0-9]/) + 1;
- lDigits >= 1 || assert.fail(X`Digit count expected: ${encoded}`);
+ lDigits >= 1 || Fail`Digit count expected: ${encoded}`;
rem = rem.slice(lDigits - 1);
- rem.length >= lDigits ||
- assert.fail(X`Complete digit count expected: ${encoded}`);
+ rem.length >= lDigits || Fail`Complete digit count expected: ${encoded}`;
const snDigits = rem.slice(0, lDigits);
rem = rem.slice(lDigits);
- /^[0-9]+$/.test(snDigits) ||
- assert.fail(X`Decimal digit count expected: ${encoded}`);
+ /^[0-9]+$/.test(snDigits) || Fail`Decimal digit count expected: ${encoded}`;
let nDigits = parseInt(snDigits, 10);
if (typePrefix === 'n') {
// TODO Assert to reject forbidden encodings
@@ -187,10 +184,10 @@ const decodeBigInt = encoded => {
nDigits = 10 ** lDigits - nDigits;
}
- rem.startsWith(':') || assert.fail(X`Separator expected: ${encoded}`);
+ rem.startsWith(':') || Fail`Separator expected: ${encoded}`;
rem = rem.slice(1);
rem.length === nDigits ||
- assert.fail(X`Fixed-length digit sequence expected: ${encoded}`);
+ Fail`Fixed-length digit sequence expected: ${encoded}`;
let n = BigInt(rem);
if (typePrefix === 'n') {
// TODO Assert to reject forbidden encodings
@@ -221,7 +218,7 @@ const encodeArray = (array, encodePassable) => {
};
const decodeArray = (encoded, decodePassable) => {
- encoded.startsWith('[') || assert.fail(X`Encoded array expected: ${encoded}`);
+ encoded.startsWith('[') || Fail`Encoded array expected: ${encoded}`;
const elements = [];
const elemChars = [];
for (let i = 1; i < encoded.length; i += 1) {
@@ -233,19 +230,17 @@ const decodeArray = (encoded, decodePassable) => {
elements.push(element);
} else if (c === '\u0001') {
i += 1;
- i < encoded.length ||
- assert.fail(X`unexpected end of encoding ${encoded}`);
+ i < encoded.length || Fail`unexpected end of encoding ${encoded}`;
const c2 = encoded[i];
c2 === '\u0000' ||
c2 === '\u0001' ||
- assert.fail(X`Unexpected character after u0001 escape: ${c2}`);
+ Fail`Unexpected character after u0001 escape: ${c2}`;
elemChars.push(c2);
} else {
elemChars.push(c);
}
}
- elemChars.length === 0 ||
- assert.fail(X`encoding terminated early: ${encoded}`);
+ elemChars.length === 0 || Fail`encoding terminated early: ${encoded}`;
return harden(elements);
};
@@ -258,15 +253,14 @@ const encodeRecord = (record, encodePassable) => {
const decodeRecord = (encoded, decodePassable) => {
assert(encoded.startsWith('('));
const keysvals = decodeArray(encoded.substring(1), decodePassable);
- keysvals.length === 2 ||
- assert.fail(X`expected keys,values pair: ${encoded}`);
+ keysvals.length === 2 || Fail`expected keys,values pair: ${encoded}`;
const [keys, vals] = keysvals;
(passStyleOf(keys) === 'copyArray' &&
passStyleOf(vals) === 'copyArray' &&
keys.length === vals.length &&
keys.every(key => typeof key === 'string')) ||
- assert.fail(X`not a valid record encoding: ${encoded}`);
+ Fail`not a valid record encoding: ${encoded}`;
const mapEntries = keys.map((key, i) => [key, vals[i]]);
const record = harden(fromEntries(mapEntries));
assertRecord(record, 'decoded record');
@@ -279,11 +273,10 @@ const encodeTagged = (tagged, encodePassable) =>
const decodeTagged = (encoded, decodePassable) => {
assert(encoded.startsWith(':'));
const tagpayload = decodeArray(encoded.substring(1), decodePassable);
- tagpayload.length === 2 ||
- assert.fail(X`expected tag,payload pair: ${encoded}`);
+ tagpayload.length === 2 || Fail`expected tag,payload pair: ${encoded}`;
const [tag, payload] = tagpayload;
passStyleOf(tag) === 'string' ||
- assert.fail(X`not a valid tagged encoding: ${encoded}`);
+ Fail`not a valid tagged encoding: ${encoded}`;
return makeTagged(tag, payload);
};
@@ -315,12 +308,12 @@ const decodeTagged = (encoded, decodePassable) => {
// happens only in agoric-sdk, but not yet in endo. TODO figure out and fix.
// @ts-ignore
export const makeEncodePassable = ({
- encodeRemotable = (rem, _) => assert.fail(X`remotable unexpected: ${rem}`),
- encodePromise = (prom, _) => assert.fail(X`promise unexpected: ${prom}`),
- encodeError = (err, _) => assert.fail(X`error unexpected: ${err}`),
+ encodeRemotable = (rem, _) => Fail`remotable unexpected: ${rem}`,
+ encodePromise = (prom, _) => Fail`promise unexpected: ${prom}`,
+ encodeError = (err, _) => Fail`error unexpected: ${err}`,
} = {}) => {
const encodePassable = passable => {
- if (ErrorHelper.canBeValid(passable, x => x)) {
+ if (ErrorHelper.canBeValid(passable)) {
return encodeError(passable, encodePassable);
}
const passStyle = passStyleOf(passable);
@@ -346,25 +339,19 @@ export const makeEncodePassable = ({
case 'remotable': {
const result = encodeRemotable(passable, encodePassable);
result.startsWith('r') ||
- assert.fail(
- X`internal: Remotable encoding must start with "r": ${result}`,
- );
+ Fail`internal: Remotable encoding must start with "r": ${result}`;
return result;
}
case 'error': {
const result = encodeError(passable, encodePassable);
result.startsWith('!') ||
- assert.fail(
- X`internal: Error encoding must start with "!": ${result}`,
- );
+ Fail`internal: Error encoding must start with "!": ${result}`;
return result;
}
case 'promise': {
const result = encodePromise(passable, encodePassable);
result.startsWith('?') ||
- assert.fail(
- X`internal: Promise encoding must start with "?": ${result}`,
- );
+ Fail`internal: Promise encoding must start with "?": ${result}`;
return result;
}
case 'symbol': {
@@ -380,9 +367,7 @@ export const makeEncodePassable = ({
return encodeTagged(passable, encodePassable);
}
default: {
- assert.fail(
- X`a ${q(passStyle)} cannot be used as a collection passable`,
- );
+ throw Fail`a ${q(passStyle)} cannot be used as a collection passable`;
}
}
};
@@ -418,9 +403,9 @@ harden(makeEncodePassable);
// happens only in agoric-sdk, but not yet in endo. TODO figure out and fix.
// @ts-ignore
export const makeDecodePassable = ({
- decodeRemotable = (rem, _) => assert.fail(X`remotable unexpected: ${rem}`),
- decodePromise = (prom, _) => assert.fail(X`promise unexpected: ${prom}`),
- decodeError = (err, _) => assert.fail(X`error unexpected: ${err}`),
+ decodeRemotable = (rem, _) => Fail`remotable unexpected: ${rem}`,
+ decodePromise = (prom, _) => Fail`promise unexpected: ${prom}`,
+ decodeError = (err, _) => Fail`error unexpected: ${err}`,
} = {}) => {
const decodePassable = encoded => {
switch (encoded.charAt(0)) {
@@ -465,7 +450,7 @@ export const makeDecodePassable = ({
return decodeTagged(encoded, decodePassable);
}
default: {
- assert.fail(X`invalid database key: ${encoded}`);
+ throw Fail`invalid database key: ${encoded}`;
}
}
};
@@ -485,7 +470,7 @@ harden(isEncodedRemotable);
* individually is a valid bigint prefix. `n` for "negative" and `p` for
* "positive". The ordering of these prefixes is the same as the
* rankOrdering of their respective PassStyles. This table is imported by
- * randOrder.js for this purpose.
+ * rankOrder.js for this purpose.
*
* In addition, `|` is the remotable->ordinal mapping prefix:
* This is not used in covers but it is

MODIFY
packages/marshal/src/helpers/copyArray.js
packages/marshal/src/helpers/copyArray.js
@@ -14,7 +14,7 @@ const { isArray, prototype: arrayPrototype } = Array;
export const CopyArrayHelper = harden({
styleName: 'copyArray',
- canBeValid: (candidate, check) =>
+ canBeValid: (candidate, check = undefined) =>
isArray(candidate) ||
(!!check && check(false, X`Array expected: ${candidate}`)),

MODIFY
packages/marshal/src/helpers/copyRecord.js
packages/marshal/src/helpers/copyRecord.js
@@ -17,7 +17,7 @@ const { getPrototypeOf, values, prototype: objectPrototype } = Object;
export const CopyRecordHelper = harden({
styleName: 'copyRecord',
- canBeValid: (candidate, check) => {
+ canBeValid: (candidate, check = undefined) => {
const reject = !!check && (details => check(false, details));
if (getPrototypeOf(candidate) !== objectPrototype) {
return (

MODIFY
packages/marshal/src/helpers/error.js
packages/marshal/src/helpers/error.js
@@ -44,7 +44,7 @@ harden(getErrorConstructor);
export const ErrorHelper = harden({
styleName: 'error',
- canBeValid: (candidate, check) => {
+ canBeValid: (candidate, check = undefined) => {
const reject = !!check && (details => check(false, details));
// TODO: Need a better test than instanceof
if (!(candidate instanceof Error)) {

MODIFY
packages/marshal/src/helpers/remotable.js
packages/marshal/src/helpers/remotable.js
@@ -186,7 +186,7 @@ harden(getInterfaceOf);
export const RemotableHelper = harden({
styleName: 'remotable',
- canBeValid: (candidate, check) => {
+ canBeValid: (candidate, check = undefined) => {
const reject = !!check && (details => check(false, details));
if (!isObject(candidate)) {
return (

MODIFY
packages/marshal/src/helpers/tagged.js
packages/marshal/src/helpers/tagged.js
@@ -19,7 +19,8 @@ const { getOwnPropertyDescriptors } = Object;
export const TaggedHelper = harden({
styleName: 'tagged',
- canBeValid: (candidate, check) => checkPassStyle(candidate, 'tagged', check),
+ canBeValid: (candidate, check = undefined) =>
+ checkPassStyle(candidate, 'tagged', check),
assertValid: (candidate, passStyleOfRecur) => {
checkTagRecord(candidate, 'tagged', assertChecker);

MODIFY
packages/marshal/test/test-encodePassable.js
packages/marshal/test/test-encodePassable.js
@@ -12,7 +12,7 @@ import {
import { compareRank, makeComparatorKit } from '../src/rankOrder.js';
import { sample } from './test-rankOrder.js';
-const { details: X } = assert;
+const { Fail } = assert;
const r2e = new Map();
const e2r = [];
@@ -28,7 +28,7 @@ const encodeRemotable = r => {
};
const decodeRemotable = e => {
- assert(e.startsWith('r'), X`unexpected encoding ${e}`);
+ e.startsWith('r') || Fail`unexpected encoding ${e}`;
const i = Number(BigInt(e.substring(1)));
assert(i >= 0 && i < e2r.length);
return e2r[i];

refactor: use Fail template tag (#1385)