Skip to content

Commit

Permalink
fix: track endo 1382 fix to 6570
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Dec 2, 2022
1 parent d092592 commit c953ba3
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions packages/internal/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import { isPromise } from '@endo/promise-kit';

/** @typedef {import('@endo/marshal/src/types').Remotable} Remotable */

const { getPrototypeOf, create, entries, fromEntries } = Object;
const {
getPrototypeOf,
create,
entries,
fromEntries,
getOwnPropertyDescriptors,
} = Object;
const { ownKeys, apply } = Reflect;

const { details: X, quote: q } = assert;
Expand Down Expand Up @@ -144,28 +150,45 @@ export const applyLabelingError = (func, args, label = undefined) => {
harden(applyLabelingError);

/**
* @param {unknown} a
* @param {unknown} b
* Prioritize symbols as earlier than strings.
*
* @param {string|symbol} a
* @param {string|symbol} b
* @returns {-1 | 0 | 1}
*/
const compareStringified = (a, b) => {
const left = String(a);
const right = String(b);
// eslint-disable-next-line no-nested-ternary
return left < right ? -1 : left > right ? 1 : 0;
if (typeof a === typeof b) {
const left = String(a);
const right = String(b);
// eslint-disable-next-line no-nested-ternary
return left < right ? -1 : left > right ? 1 : 0;
}
if (typeof a === 'symbol') {
assert(typeof b === 'string');
return -1;
}
assert(typeof a === 'string');
assert(typeof b === 'symbol');
return 1;
};

/**
* @param {Record<string | symbol, unknown>} obj
* TODO Consolidate with the `getMethodNames` in `@endo/eventual-send`
*
* @param {any} obj
* @returns {(string|symbol)[]}
*/
export const getMethodNames = obj => {
const result = [];
let result = [];
while (obj !== null && obj !== Object.prototype) {
const mNames = ownKeys(obj).filter(name => typeof obj[name] === 'function');
const descs = getOwnPropertyDescriptors(obj); // be tolerant of non-objects
const mNames = ownKeys(descs).filter(
name => typeof obj[name] === 'function',
);
result.push(...mNames);
obj = getPrototypeOf(obj);
obj = isObject(obj) ? getPrototypeOf(obj) : null;
}
result = [...new Set(result)]; // dedup
result.sort(compareStringified);
return harden(result);
};
Expand Down

0 comments on commit c953ba3

Please sign in to comment.