Skip to content

Commit

Permalink
Extract alreadyCalledConstantGetter from inner loop.
Browse files Browse the repository at this point in the history
As promised by my TODO comment.
  • Loading branch information
benjamn committed Jul 13, 2021
1 parent 8021e62 commit 23a6c72
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions lib/runtime/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ function forEachSetter(entry, names, callback) {
var settersByKey = entry.setters[name];
if (!settersByKey) return;

var getter = entry.getters[name];
var alreadyCalledConstantGetter =
typeof getter === "function" &&
// Sometimes a getter function will throw because it's called
// before the variable it's supposed to return has been
// initialized, so we need to know that the getter function has
// run to completion at least once.
getter.runCount > 0 &&
getter.constant;

var value = getExportByName(entry, name);

// Although we may have multiple setter functions with different keys in
Expand All @@ -360,21 +370,13 @@ function forEachSetter(entry, names, callback) {
// Invoke the setter function with the updated value.
callback(setter, name, value);

// TODO Refactor this out of the settersByKey loop, if possible.
var getter = entry.getters[name];
if (typeof getter === "function" &&
// Sometimes a getter function will throw because it's called
// before the variable it's supposed to return has been
// initialized, so we need to know that the getter function has
// run to completion at least once.
getter.runCount > 0 &&
getter.constant) {
// If we happen to know that this getter function has run
// successfully, and will never return a different value, then we
// can forget the corresponding setter, because we've already
// reported that constant value. Note that we can't forget the
// getter, because we need to remember the original value in case
// anyone tampers with entry.module.exports[name].
if (alreadyCalledConstantGetter) {
// If we happen to know this getter function has run successfully
// (getter.runCount > 0), and will never return a different value
// (getter.constant), then we can forget the corresponding setter,
// because we've already reported that constant value. Note that we
// can't forget the getter, because we need to remember the original
// value in case anyone tampers with entry.module.exports[name].
delete settersByKey[key];
}
}
Expand Down

0 comments on commit 23a6c72

Please sign in to comment.