Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(ses): cleaner enable-property-overrides #1975

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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