Skip to content

Commit

Permalink
fix(exo): methods might be non-enumerable
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Sep 26, 2023
1 parent aebbea2 commit 4146964
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions packages/exo/src/exo-tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {

const { quote: q, Fail } = assert;
const { apply, ownKeys } = Reflect;
const { defineProperties, fromEntries } = Object;
const { defineProperties, fromEntries, getOwnPropertyDescriptors, create } =
Object;

/**
* A method guard, for inclusion in an interface guard, that enforces only that
Expand Down Expand Up @@ -285,12 +286,19 @@ export const defendPrototype = (
interfaceGuard = undefined,
) => {
const prototype = {};
if (hasOwnPropertyOf(behaviorMethods, 'constructor')) {
// By ignoring any method named "constructor", we can use a
// class.prototype as a behaviorMethods.
const { constructor: _, ...methods } = behaviorMethods;
// @ts-expect-error TS misses that hasOwn check makes this safe
behaviorMethods = harden(methods);
if (
hasOwnPropertyOf(behaviorMethods, 'constructor') &&
typeof behaviorMethods.constructor === 'function' &&
hasOwnPropertyOf(behaviorMethods.constructor, 'prototype')
) {
// By ignoring any method named "constructor" that might be a
// class, we can use a class.prototype as a behaviorMethods.
// TODO: Because this looks only at own properties, it is
// currently useful only on base classes. Better would be
// for it to deal with subclasses as well.
const { constructor: _, ...methodDescs } =
getOwnPropertyDescriptors(behaviorMethods);
behaviorMethods = create(Object.prototype, methodDescs);
}
/** @type {Record<PropertyKey, MethodGuard> | undefined} */
let methodGuards;
Expand Down

0 comments on commit 4146964

Please sign in to comment.