Skip to content

Commit

Permalink
refactor(ses): cleaner enable-property-overrides (#1975)
Browse files Browse the repository at this point in the history
  • Loading branch information
erights authored Jan 16, 2024
1 parent 4a4f9fe commit 1add88a
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions packages/ses/src/enable-property-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,42 +88,50 @@ export default function enablePropertyOverrides(
if ('value' in desc && desc.configurable) {
const { value } = desc;

function getter() {
return value;
}
const isDebug = setHas(debugProperties, prop);

// We use concise method syntax to be `this` sensitive, but still
// omit a prototype property or [[Construct]] behavior.
// @ts-expect-error We know there is an accessor descriptor there
const { get: getter, set: setter } = getOwnPropertyDescriptor(
{
get [prop]() {
return value;
},
set [prop](newValue) {
if (obj === this) {
throw TypeError(
`Cannot assign to read only property '${String(
prop,
)}' of '${path}'`,
);
}
if (objectHasOwnProperty(this, prop)) {
this[prop] = newValue;
} else {
if (isDebug) {
// eslint-disable-next-line @endo/no-polymorphic-call
console.error(TypeError(`Override property ${prop}`));
}
defineProperty(this, prop, {
value: newValue,
writable: true,
enumerable: true,
configurable: true,
});
}
},
},
prop,
);

defineProperty(getter, 'originalValue', {
value,
writable: false,
enumerable: false,
configurable: false,
});

const isDebug = setHas(debugProperties, prop);

function setter(newValue) {
if (obj === this) {
throw TypeError(
`Cannot assign to read only property '${String(
prop,
)}' of '${path}'`,
);
}
if (objectHasOwnProperty(this, prop)) {
this[prop] = newValue;
} else {
if (isDebug) {
// eslint-disable-next-line @endo/no-polymorphic-call
console.error(TypeError(`Override property ${prop}`));
}
defineProperty(this, prop, {
value: newValue,
writable: true,
enumerable: true,
configurable: true,
});
}
}

defineProperty(obj, prop, {
get: getter,
set: setter,
Expand Down

0 comments on commit 1add88a

Please sign in to comment.