add handling for loops when doing rest parameter allocation optimisation

This commit is contained in:
Sebastian McKenzie 2015-07-21 17:51:20 +01:00
parent f6e7b7716b
commit dd225b5b74
3 changed files with 56 additions and 1 deletions

View File

@ -223,7 +223,22 @@ export var visitor = {
} else {
// perform allocation at the lowest common denominator of all references
loop._blockHoist = 1;
this.getEarliestCommonAncestorFrom(state.references).getStatementParent().insertBefore(loop);
var target = this.getEarliestCommonAncestorFrom(state.references).getStatementParent();
// don't perform the allocation inside a loop
var highestLoop;
target.findParent(function (path) {
if (path.isLoop()) {
highestLoop = path;
} else if (path.isFunction()) {
// stop crawling up for functions
return true;
}
});
if (highestLoop) target = highestLoop;
target.insertBefore(loop);
}
}
};

View File

@ -50,3 +50,19 @@ function a(...args) {
console.log("Shouldn't args be from a's scope?", args);
};
}
// loop
function runQueue(queue, ...args) {
for (let i = 0; i < queue.length; i++) {
queue[i](...args)
}
}
// nested loop
function runQueue(queue, ...args) {
if (foo) {
for (let i = 0; i < queue.length; i++) {
queue[i](...args)
}
}
}

View File

@ -77,3 +77,27 @@ function a() {
console.log("Shouldn't args be from a's scope?", args);
};
}
// loop
function runQueue(queue) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
for (var i = 0; i < queue.length; i++) {
queue[i].apply(queue, args);
}
}
// nested loop
function runQueue(queue) {
if (foo) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
for (var i = 0; i < queue.length; i++) {
queue[i].apply(queue, args);
}
}
}