Skip to content

Commit

Permalink
Discard setters after calling them with constant values.
Browse files Browse the repository at this point in the history
Closes #134.
  • Loading branch information
benjamn committed May 26, 2017
1 parent f2a2eab commit de193d3
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions lib/runtime/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Ep.addGetters = function (getters, constant) {
! (name in this.getters)) {
this.getters[name] = getter;
getter.constant = constant;
getter.runCount = 0;
}
}
};
Expand Down Expand Up @@ -291,12 +292,27 @@ function forEachSetter(entry, names, callback) {
var keyCount = keys.length;

for (var j = 0; j < keyCount; ++j) {
callSetterIfNecessary(
setters[keys[j]],
name,
getExportByName(entry, name),
callback
);
var key = keys[j];
var value = getExportByName(entry, name);

callSetterIfNecessary(setters[key], name, value, callback);

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.exports[name].
delete setters[key];
}
}
}
}
Expand Down Expand Up @@ -334,8 +350,11 @@ function makeUniqueKey() {
}

function runGetter(entry, name) {
var getter = entry.getters[name];
try {
return entry.getters[name]();
var result = getter();
++getter.runCount;
return result;
} catch (e) {}
return GETTER_ERROR;
}
Expand Down

0 comments on commit de193d3

Please sign in to comment.