Fix error in rest parameter length optimization (#3573)

If there aren’t enough arguments to get to the offset index, we would
return an negative length.
This commit is contained in:
Justin Ridgewell
2016-07-12 21:12:45 -04:00
committed by Henry Zhu
parent 57ef3ea8eb
commit 823ffbd87c
4 changed files with 25 additions and 20 deletions

View File

@@ -13,10 +13,14 @@ let buildRest = template(`
}
`);
let loadRest = template(`
let restIndex = template(`
ARGUMENTS.length <= INDEX ? undefined : ARGUMENTS[INDEX]
`);
let restLength = template(`
ARGUMENTS.length <= OFFSET ? 0 : ARGUMENTS.length - OFFSET
`);
let memberExpressionOptimisationVisitor = {
Scope(path, state) {
// check if this scope has a local binding that will shadow the rest parameter
@@ -155,21 +159,18 @@ function optimiseIndexGetter(path, argsId, offset) {
index = t.binaryExpression("+", path.parent.property, t.numericLiteral(offset));
}
path.parentPath.replaceWith(loadRest({
path.parentPath.replaceWith(restIndex({
ARGUMENTS: argsId,
INDEX: index,
}));
}
function optimiseLengthGetter(path, argsLengthExpression, argsId, offset) {
function optimiseLengthGetter(path, argsId, offset) {
if (offset) {
path.parentPath.replaceWith(
t.binaryExpression(
"-",
argsLengthExpression,
t.numericLiteral(offset),
)
);
path.parentPath.replaceWith(restLength({
ARGUMENTS: argsId,
OFFSET: t.numericLiteral(offset),
}));
} else {
path.replaceWith(argsId);
}
@@ -183,10 +184,6 @@ export let visitor = {
let rest = node.params.pop().argument;
let argsId = t.identifier("arguments");
let argsLengthExpression = t.memberExpression(
argsId,
t.identifier("length"),
);
// otherwise `arguments` will be remapped in arrow functions
argsId._shadowedFunctionLiteral = path;
@@ -230,7 +227,7 @@ export let visitor = {
optimiseIndexGetter(path, argsId, state.offset);
break;
case "lengthGetter":
optimiseLengthGetter(path, argsLengthExpression, argsId, state.offset);
optimiseLengthGetter(path, argsId, state.offset);
break;
default:
path.replaceWith(argsId);

View File

@@ -0,0 +1,8 @@
var length = function (a, b, ...items) {
return items.length;
};
assert.equal(length(), 0);
assert.equal(length(1), 0);
assert.equal(length(1, 2), 0);
assert.equal(length(1, 2, 3), 1);

View File

@@ -1,6 +1,6 @@
var t = function (f) {
arguments.length <= 1 ? undefined : arguments[1];
arguments.length <= arguments.length - 1 - 1 + 1 ? undefined : arguments[arguments.length - 1 - 1 + 1];
arguments.length <= (arguments.length <= 1 ? 0 : arguments.length - 1) - 1 + 1 ? undefined : arguments[(arguments.length <= 1 ? 0 : arguments.length - 1) - 1 + 1];
};
function t(f) {
@@ -11,4 +11,4 @@ function t(f) {
items;
items[0];
items[items.length - 1];
}
}

View File

@@ -63,11 +63,11 @@ var b = function () {
};
var b = function (foo) {
return (arguments.length - 1) * 2;
return (arguments.length <= 1 ? 0 : arguments.length - 1) * 2;
};
var b = function (foo, baz) {
return arguments.length - 2;
return arguments.length <= 2 ? 0 : arguments.length - 2;
};
function x() {
@@ -193,4 +193,4 @@ function postfixDecrement() {
}
rest[0]--;
}
}