Filter out loop statements in operator overloading code, to optimize loop execution speed.

This commit is contained in:
Jürg Lehni 2014-01-05 20:15:57 +01:00
parent 6ad2105110
commit cd7604952c

View file

@ -168,7 +168,17 @@ var PaperScript = Base.exports.PaperScript = (function() {
}
}
switch (node && node.type) {
case 'UnaryExpression':
// -a
if (node.operator in unaryOperators
&& node.argument.type !== 'Literal') {
var arg = getCode(node.argument);
replaceCode(node, '$_("' + node.operator + '", '
+ arg + ')');
}
break;
case 'BinaryExpression':
// a + b, a - b, a / b, a * b, a == b, a % b, ...
if (node.operator in binaryOperators
&& node.left.type !== 'Literal') {
var left = getCode(node.left),
@ -177,22 +187,17 @@ var PaperScript = Base.exports.PaperScript = (function() {
+ '", ' + right + ')');
}
break;
case 'AssignmentExpression':
if (/^.=$/.test(node.operator)
&& node.left.type !== 'Literal') {
var left = getCode(node.left),
right = getCode(node.right);
replaceCode(node, left + ' = _$_(' + left + ', "'
+ node.operator[0] + '", ' + right + ')');
}
break;
case 'UpdateExpression':
// a++, a--
if (!node.prefix && !(parent && (
// Filter out for statements to allow loop increments
// to perform well
parent.type === 'ForStatement'
// We need to filter out parents that are comparison
// operators, e.g. for situations like if (++i < 1),
// as we can't replace that with if (_$_(i, "+", 1) < 1)
// Match any operator beginning with =, !, < and >.
parent.type === 'BinaryExpression'
|| parent.type === 'BinaryExpression'
&& /^[=!<>]/.test(parent.operator)
// array[i++] is a MemberExpression with computed = true
// We can't replace that with array[_$_(i, "+", 1)].
@ -203,12 +208,15 @@ var PaperScript = Base.exports.PaperScript = (function() {
+ node.operator[0] + '", 1)');
}
break;
case 'UnaryExpression':
if (node.operator in unaryOperators
&& node.argument.type !== 'Literal') {
var arg = getCode(node.argument);
replaceCode(node, '$_("' + node.operator + '", '
+ arg + ')');
case 'AssignmentExpression':
/// a += b, a -= b
if (/^.=$/.test(node.operator)
&& node.left.type !== 'Literal'
&& !(parent && parent.type === 'ForStatement')) {
var left = getCode(node.left),
right = getCode(node.right);
replaceCode(node, left + ' = _$_(' + left + ', "'
+ node.operator[0] + '", ' + right + ')');
}
break;
}