Skip to content

Commit

Permalink
fix(tame-metering): quieter warnings for defineProperty failures
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Dec 19, 2020
1 parent 5ed0310 commit 7575b94
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions packages/tame-metering/src/tame.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export function tameMetering() {

let globalMeter = null;
const definePropertyFailures = new Set();
const frozenPaths = new Set();
const wrapped = new WeakMap();
const setWrapped = (...args) => apply(wmSet, wrapped, args);
const getWrapped = (...args) => apply(wmGet, wrapped, args);
Expand Down Expand Up @@ -228,7 +229,7 @@ export function tameMetering() {
wrap(getPrototypeOf(target), pname && `${pname}.__proto__`),
);

const assignToTarget = (p, desc) => {
const assignToWrapper = (p, desc) => {
let newDesc;
if (constructor && p === 'constructor') {
newDesc = {
Expand All @@ -240,28 +241,35 @@ export function tameMetering() {
} else {
newDesc = wrapDescriptor(desc, pname && `${pname}.${String(p)}`);
}
for (const o of [wrapper, target]) {
try {
defineProperty(o, p, newDesc);
} catch (e) {
// Ignore: TypeError: Cannot redefine property: ...
const path = pname ? `${pname}.${String(p)}` : '*unknown*';
if (!definePropertyFailures.has(path)) {
definePropertyFailures.add(path);
// This is an intentional use of console.log, not a debugging vestige.
console.log(`Cannot meter ${path}`);
}

if (Object.isFrozen(wrapper)) {
if (!frozenPaths.has(pname)) {
frozenPaths.add(pname);
console.log(`Cannot meter frozen ${pname}`);
}
return;
}

try {
defineProperty(wrapper, p, newDesc);
} catch (e) {
// Ignore: TypeError: Cannot redefine property: ...
const path = pname ? `${pname}.${String(p)}` : '*unknown*';
if (!definePropertyFailures.has(path)) {
definePropertyFailures.add(path);
// This is an intentional use of console.log, not a debugging vestige.
console.log(`Cannot meter defined ${path}`);
}
}
};

// Assign the wrapped descriptors to the target.
const tdescs = getOwnPropertyDescriptors(target);
Object.getOwnPropertyNames(tdescs).forEach(p =>
assignToTarget(p, tdescs[p]),
assignToWrapper(p, tdescs[p]),
);
Object.getOwnPropertySymbols(tdescs).forEach(p =>
assignToTarget(p, tdescs[p]),
assignToWrapper(p, tdescs[p]),
);

return wrapper;
Expand Down

0 comments on commit 7575b94

Please sign in to comment.