Skip to content

Commit

Permalink
fix: review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Aug 21, 2023
1 parent bdeb4ce commit 0ea5cb5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 34 deletions.
43 changes: 11 additions & 32 deletions packages/exo/src/exo-makers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { objectMap } from '@endo/patterns';

import { defendPrototype, defendPrototypeKit } from './exo-tools.js';

const { Fail, quote: q } = assert;
const { create, seal, freeze, defineProperty, entries, values } = Object;
const { create, seal, freeze, defineProperty, values } = Object;

const { getEnvironmentOption } = makeEnvironmentCaptor(globalThis);
const DEBUG = getEnvironmentOption('DEBUG', '');
Expand Down Expand Up @@ -70,7 +69,7 @@ export const initEmpty = () => emptyRecord;
*/

/**
* @callback GetRevoker
* @callback ReceiveRevoker
* @param {Revoker} revoke
* @returns {void}
*/
Expand All @@ -80,7 +79,7 @@ export const initEmpty = () => emptyRecord;
* @typedef {object} FarClassOptions
* @property {(context: C) => void} [finish]
* @property {StateShape} [stateShape]
* @property {GetRevoker} [getRevoker]
* @property {ReceiveRevoker} [receiveRevoker]
*/

/**
Expand All @@ -101,7 +100,7 @@ export const defineExoClass = (
options = {},
) => {
harden(methods);
const { finish = undefined, getRevoker = undefined } = options;
const { finish = undefined, receiveRevoker = undefined } = options;
/** @type {WeakMap<M,ClassContext<ReturnType<I>, M>>} */
const contextMap = new WeakMap();
const proto = defendPrototype(
Expand Down Expand Up @@ -134,10 +133,10 @@ export const defineExoClass = (
);
};

if (getRevoker) {
if (receiveRevoker) {
const revoke = self => contextMap.delete(self);
harden(revoke);
getRevoker(revoke);
receiveRevoker(revoke);
}

return harden(makeInstance);
Expand All @@ -162,7 +161,7 @@ export const defineExoClassKit = (
options = {},
) => {
harden(methodsKit);
const { finish = undefined, getRevoker = undefined } = options;
const { finish = undefined, receiveRevoker = undefined } = options;
const contextMapKit = objectMap(methodsKit, () => new WeakMap());
const getContextKit = objectMap(
contextMapKit,
Expand Down Expand Up @@ -200,31 +199,11 @@ export const defineExoClassKit = (
return context.facets;
};

if (getRevoker) {
const revoke = aFacet => {
let seenTrue = false;
let facets;
for (const contextMap of values(contextMapKit)) {
if (contextMap.has(aFacet)) {
seenTrue = true;
facets = contextMap.get(aFacet).facets;
break;
}
}
if (!seenTrue) {
return false;
}
// eslint-disable-next-line no-use-before-define
for (const [facetName, facet] of entries(facets)) {
const seen = contextMapKit[facetName].delete(facet);
if (seen === false) {
Fail`internal: inconsistent facet revocation ${q(facetName)}`;
}
}
return seenTrue;
};
if (receiveRevoker) {
const revoke = aFacet =>
values(contextMapKit).some(contextMap => contextMap.delete(aFacet));
harden(revoke);
getRevoker(revoke);
receiveRevoker(revoke);
}

return harden(makeInstanceKit);
Expand Down
6 changes: 4 additions & 2 deletions packages/exo/test/test-revoke-heap-classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ test('test revoke defineExoClass', t => {
},
},
{
getRevoker(r) {
receiveRevoker(r) {
revoke = r;
},
},
Expand Down Expand Up @@ -74,7 +74,7 @@ test('test revoke defineExoClassKit', t => {
},
},
{
getRevoker(r) {
receiveRevoker(r) {
revoke = r;
},
},
Expand All @@ -83,10 +83,12 @@ test('test revoke defineExoClassKit', t => {
t.is(upCounter.incr(5), 8);
t.is(downCounter.decr(), 7);
t.is(revoke(upCounter), true);
t.is(revoke(upCounter), false);
t.throws(() => upCounter.incr(3), {
message:
'"In \\"incr\\" method of (Counter up)" may only be applied to a valid instance: "[Alleged: Counter up]"',
});
t.is(revoke(downCounter), true);
t.is(revoke(downCounter), false);
t.throws(() => downCounter.decr(), {
message:
Expand Down

0 comments on commit 0ea5cb5

Please sign in to comment.