made nodes cache more aggressively, fixing #653

This commit is contained in:
satyr
2010-09-27 17:56:56 +09:00
parent 72c83f5e43
commit 3bba51d5d9
4 changed files with 149 additions and 163 deletions

View File

@@ -1,5 +1,5 @@
(function() {
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IS_STRING, IfNode, InNode, IndexNode, LiteralNode, NUMBER, ObjectNode, OpNode, ParamNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, SIMPLENUM, Scope, SliceNode, SplatNode, SwitchNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, UTILITIES, ValueNode, WhileNode, _ref, compact, del, ends, flatten, include, indexOf, literal, merge, starts, utility;
var AccessorNode, ArrayNode, AssignNode, BaseNode, CallNode, ClassNode, ClosureNode, CodeNode, CommentNode, ExistenceNode, Expressions, ExtendsNode, ForNode, IDENTIFIER, IS_STRING, IfNode, InNode, IndexNode, LiteralNode, NO, NUMBER, ObjectNode, OpNode, ParamNode, ParentheticalNode, PushNode, RangeNode, ReturnNode, SIMPLENUM, Scope, SliceNode, SplatNode, SwitchNode, TAB, TRAILING_WHITESPACE, ThrowNode, TryNode, UTILITIES, ValueNode, WhileNode, YES, _ref, compact, del, ends, flatten, include, indexOf, literal, merge, starts, utility;
var __extends = function(child, parent) {
var ctor = function(){};
ctor.prototype = parent.prototype;
@@ -19,6 +19,12 @@
indexOf = _ref.indexOf;
starts = _ref.starts;
ends = _ref.ends;
YES = function() {
return true;
};
NO = function() {
return false;
};
exports.BaseNode = (function() {
BaseNode = function() {
this.tags = {};
@@ -51,7 +57,7 @@
var compiled, pair, reference;
options || (options = {});
pair = (function() {
if (!(this.containsType(CallNode) || (this instanceof ValueNode && (!(this.base instanceof LiteralNode) || this.hasProperties())))) {
if (!(this.isComplex())) {
return [this, this];
} else if (this instanceof ValueNode && options.assignment) {
return this.cacheIndexes(o);
@@ -154,15 +160,10 @@
BaseNode.prototype.unwrap = function() {
return this;
};
BaseNode.prototype.isStatement = function() {
return false;
};
BaseNode.prototype.isPureStatement = function() {
return false;
};
BaseNode.prototype.topSensitive = function() {
return false;
};
BaseNode.prototype.isStatement = NO;
BaseNode.prototype.isPureStatement = NO;
BaseNode.prototype.isComplex = YES;
BaseNode.prototype.topSensitive = NO;
return BaseNode;
})();
exports.Expressions = (function() {
@@ -174,9 +175,7 @@
__extends(Expressions, BaseNode);
Expressions.prototype["class"] = 'Expressions';
Expressions.prototype.children = ['expressions'];
Expressions.prototype.isStatement = function() {
return true;
};
Expressions.prototype.isStatement = YES;
Expressions.prototype.push = function(node) {
this.expressions.push(node);
return this;
@@ -269,6 +268,7 @@
return this.value === 'break' || this.value === 'continue' || this.value === 'debugger';
};
LiteralNode.prototype.isPureStatement = LiteralNode.prototype.isStatement;
LiteralNode.prototype.isComplex = NO;
LiteralNode.prototype.compileNode = function(o) {
var end, idt;
idt = this.isStatement(o) ? this.idt() : '';
@@ -288,12 +288,8 @@
};
__extends(ReturnNode, BaseNode);
ReturnNode.prototype["class"] = 'ReturnNode';
ReturnNode.prototype.isStatement = function() {
return true;
};
ReturnNode.prototype.isPureStatement = function() {
return true;
};
ReturnNode.prototype.isStatement = YES;
ReturnNode.prototype.isPureStatement = YES;
ReturnNode.prototype.children = ['expression'];
ReturnNode.prototype.makeReturn = function() {
return this;
@@ -341,6 +337,9 @@
ValueNode.prototype.isSplice = function() {
return this.hasProperties() && this.properties[this.properties.length - 1] instanceof SliceNode;
};
ValueNode.prototype.isComplex = function() {
return this.base.isComplex() || this.properties.length;
};
ValueNode.prototype.makeReturn = function() {
return this.hasProperties() ? ValueNode.__super__.makeReturn.call(this) : this.base.makeReturn();
};
@@ -354,29 +353,23 @@
return this.base instanceof LiteralNode && this.base.value.match(NUMBER);
};
ValueNode.prototype.cacheIndexes = function(o) {
var _i, _len, _ref2, copy, i;
var _len, _ref2, _ref3, copy, i, index, indexVar, prop;
copy = new ValueNode(this.base, this.properties.slice(0));
if (this.base instanceof CallNode) {
if (this.base.isComplex()) {
_ref2 = this.base.compileReference(o);
this.base = _ref2[0];
copy.base = _ref2[1];
}
_ref2 = copy.properties;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
(function() {
var _ref3, index, indexVar;
var i = _i;
var prop = _ref2[_i];
if (prop instanceof IndexNode && prop.contains(function(n) {
return n instanceof CallNode;
})) {
_ref3 = prop.index.compileReference(o);
index = _ref3[0];
indexVar = _ref3[1];
this.properties[i] = new IndexNode(index);
return (copy.properties[i] = new IndexNode(indexVar));
}
}).call(this);
for (i = 0, _len = _ref2.length; i < _len; i++) {
prop = _ref2[i];
if (prop instanceof IndexNode && prop.index.isComplex()) {
_ref3 = prop.index.compileReference(o);
index = _ref3[0];
indexVar = _ref3[1];
this.properties[i] = new IndexNode(index);
copy.properties[i] = new IndexNode(indexVar);
}
}
return [this, copy];
};
@@ -384,19 +377,20 @@
return !o.top || this.properties.length ? ValueNode.__super__.compile.call(this, o) : this.base.compile(o);
};
ValueNode.prototype.compileNode = function(o) {
var _i, _len, _ref2, baseline, complete, copy, hasSoak, i, me, only, op, part, prop, props, temp;
var _i, _len, _ref2, baseline, complete, copy, hasSoak, i, me, only, op, part, prevcomp, prop, props, temp;
only = del(o, 'onlyFirst');
op = this.tags.operation;
props = only ? this.properties.slice(0, this.properties.length - 1) : this.properties;
props = only ? this.properties.slice(0, -1) : this.properties;
o.chainRoot || (o.chainRoot = this);
_ref2 = props;
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
prop = _ref2[_i];
if (prop.soakNode) {
hasSoak = true;
break;
}
}
if (hasSoak && this.containsType(CallNode)) {
if (hasSoak && this.isComplex()) {
_ref2 = this.cacheIndexes(o);
me = _ref2[0];
copy = _ref2[1];
@@ -414,19 +408,15 @@
prop = _ref2[i];
this.source = baseline;
if (prop.soakNode) {
if (this.base.containsType(CallNode) && i === 0) {
if (i === 0 && this.base.isComplex()) {
temp = o.scope.freeVariable('ref');
complete = ("(" + (baseline = temp) + " = (" + (complete) + "))");
complete = ("(" + (baseline = temp) + " = (" + (prevcomp = complete) + "))");
}
complete = i === 0 ? ("(typeof " + (complete) + " === \"undefined\" || " + (baseline) + " === null) ? undefined : ") : ("" + (complete) + " == null ? undefined : ");
complete += (baseline += prop.compile(o));
complete = i === 0 && !o.scope.check(complete) ? ("(typeof " + (complete) + " === \"undefined\" || " + (baseline) + " === null)") : ("" + (complete) + " == null");
complete += ' ? undefined : ' + (baseline += prop.compile(o));
} else {
part = prop.compile(o);
if (hasSoak && prop.containsType(CallNode)) {
baseline += copy.properties[i].compile(o);
} else {
baseline += part;
}
baseline += (hasSoak && prop.isComplex() ? copy.properties[i].compile(o) : part);
complete += part;
this.last = part;
}
@@ -443,14 +433,12 @@
};
__extends(CommentNode, BaseNode);
CommentNode.prototype["class"] = 'CommentNode';
CommentNode.prototype.isStatement = function() {
return true;
};
CommentNode.prototype.isStatement = YES;
CommentNode.prototype.makeReturn = function() {
return this;
};
CommentNode.prototype.compileNode = function(o) {
return this.tab + '/*' + this.comment.replace(/\r?\n/g, '\n' + this.tab) + '*/';
return this.tab + '/*' + this.comment.replace(/\n/g, '\n' + this.tab) + '*/';
};
return CommentNode;
})();
@@ -551,7 +539,7 @@
var _i, _len, _ref2, a, b, c, mentionsArgs, meth, obj, temp;
meth = this.meth || this.superReference(o);
obj = this.variable && this.variable.source || 'this';
if (obj.match(/\(/)) {
if (!(IDENTIFIER.test(obj) || NUMBER.test(obj))) {
temp = o.scope.freeVariable('ref');
obj = temp;
meth = ("(" + (temp) + " = " + (this.variable.source) + ")" + (this.variable.last));
@@ -573,7 +561,7 @@
c = o.scope.freeVariable('result');
return "" + (this.first) + "(function() {\n" + (this.idt(1)) + "var ctor = function(){};\n" + (this.idt(1)) + "__extends(ctor, " + (a) + " = " + (meth) + ");\n" + (this.idt(1)) + "return typeof (" + (c) + " = " + (a) + ".apply(" + (b) + " = new ctor, " + (this.compileSplatArguments(o)) + ")) === \"object\" ? " + (c) + " : " + (b) + ";\n" + (this.tab) + "})." + (mentionsArgs ? 'apply(this, arguments)' : 'call(this)') + (this.last);
} else {
return "" + (this.first) + (this.prefix()) + (meth) + ".apply(" + (obj) + ", " + (this.compileSplatArguments(o)) + ")" + (this.last);
return "" + (this.first) + (meth) + ".apply(" + (obj) + ", " + (this.compileSplatArguments(o)) + ")" + (this.last);
}
};
return CallNode;
@@ -613,6 +601,7 @@
namePart = name.match(IS_STRING) ? ("[" + (name) + "]") : ("." + (name));
return this.prototype + namePart;
};
AccessorNode.prototype.isComplex = NO;
return AccessorNode;
})();
exports.IndexNode = (function() {
@@ -631,6 +620,9 @@
prefix = this.proto ? '.prototype' : '';
return "" + (prefix) + "[" + (idx) + "]";
};
IndexNode.prototype.isComplex = function() {
return this.index.isComplex();
};
return IndexNode;
})();
exports.RangeNode = (function() {
@@ -691,7 +683,7 @@
};
RangeNode.prototype.compileSimple = function(o) {
var _ref2, from, idx, step, to;
_ref2 = [parseInt(this.fromNum, 10), parseInt(this.toNum, 10)];
_ref2 = [+this.fromNum, +this.toNum];
from = _ref2[0];
to = _ref2[1];
idx = del(o, 'index');
@@ -700,15 +692,15 @@
return from <= to ? ("" + (idx) + " = " + (from) + "; " + (idx) + " <" + (this.equals) + " " + (to) + "; " + (step || ("" + (idx) + "++"))) : ("" + (idx) + " = " + (from) + "; " + (idx) + " >" + (this.equals) + " " + (to) + "; " + (step || ("" + (idx) + "--")));
};
RangeNode.prototype.compileArray = function(o) {
var _i, _result, body, clause, i, idt, post, pre, range, result, vars;
var _i, _ref2, _ref3, _result, body, clause, i, idt, post, pre, range, result, vars;
idt = this.idt(1);
vars = this.compileVariables(merge(o, {
indent: idt
}));
if (this.fromNum && this.toNum && (Math.abs(+this.fromNum - +this.toNum) <= 20)) {
range = (function() {
_result = [];
for (var _i = +this.fromNum; +this.fromNum <= +this.toNum ? _i <= +this.toNum : _i >= +this.toNum; +this.fromNum <= +this.toNum ? _i += 1 : _i -= 1){ _result.push(_i); }
_result = []; _ref2 = +this.fromNum; _ref3 = +this.toNum;
for (var _i = _ref2; _ref2 <= _ref3 ? _i <= _ref3 : _i >= _ref3; _ref2 <= _ref3 ? _i += 1 : _i -= 1){ _result.push(_i); }
return _result;
}).call(this);
if (this.exclusive) {
@@ -761,9 +753,7 @@
__extends(ObjectNode, BaseNode);
ObjectNode.prototype["class"] = 'ObjectNode';
ObjectNode.prototype.children = ['properties'];
ObjectNode.prototype.topSensitive = function() {
return true;
};
ObjectNode.prototype.topSensitive = YES;
ObjectNode.prototype.compileNode = function(o) {
var _i, _len, _ref2, _result, i, indent, join, lastNoncom, nonComments, obj, prop, props, top;
top = del(o, 'top');
@@ -855,9 +845,7 @@
__extends(ClassNode, BaseNode);
ClassNode.prototype["class"] = 'ClassNode';
ClassNode.prototype.children = ['variable', 'parent', 'properties'];
ClassNode.prototype.isStatement = function() {
return true;
};
ClassNode.prototype.isStatement = YES;
ClassNode.prototype.makeReturn = function() {
this.returns = true;
return this;
@@ -940,12 +928,10 @@
};
__extends(AssignNode, BaseNode);
AssignNode.prototype.PROTO_ASSIGN = /^(\S+)\.prototype/;
AssignNode.prototype.LEADING_DOT = /^\.(prototype\.)?/;
AssignNode.prototype.LEADING_DOT = /^\.(?:prototype\.)?/;
AssignNode.prototype["class"] = 'AssignNode';
AssignNode.prototype.children = ['variable', 'value'];
AssignNode.prototype.topSensitive = function() {
return true;
};
AssignNode.prototype.topSensitive = YES;
AssignNode.prototype.isValue = function() {
return this.variable instanceof ValueNode;
};
@@ -1045,7 +1031,7 @@
from = range.from ? range.from.compile(o) : '0';
to = range.to ? range.to.compile(o) + ' - ' + from + plus : ("" + (name) + ".length");
val = this.value.compile(o);
return "" + (name) + ".splice.apply(" + (name) + ", [" + (from) + ", " + (to) + "].concat(" + (val) + "))";
return "[].splice.apply(" + (name) + ", [" + (from) + ", " + (to) + "].concat(" + (val) + "))";
};
return AssignNode;
})();
@@ -1222,10 +1208,10 @@
prev = args[(last = args.length - 1)];
if (!(arg instanceof SplatNode)) {
if (prev && starts(prev, '[') && ends(prev, ']')) {
args[last] = ("" + (prev.substr(0, prev.length - 1)) + ", " + (code) + "]");
args[last] = ("" + (prev.slice(0, -1)) + ", " + (code) + "]");
continue;
} else if (prev && starts(prev, '.concat([') && ends(prev, '])')) {
args[last] = ("" + (prev.substr(0, prev.length - 2)) + ", " + (code) + "])");
args[last] = ("" + (prev.slice(0, -2)) + ", " + (code) + "])");
continue;
} else {
code = ("[" + (code) + "]");
@@ -1253,9 +1239,7 @@
__extends(WhileNode, BaseNode);
WhileNode.prototype["class"] = 'WhileNode';
WhileNode.prototype.children = ['condition', 'guard', 'body'];
WhileNode.prototype.isStatement = function() {
return true;
};
WhileNode.prototype.isStatement = YES;
WhileNode.prototype.addBody = function(body) {
this.body = body;
return this;
@@ -1335,6 +1319,9 @@
var _ref2;
return (('===' === (_ref2 = this.operator) || '!==' === _ref2)) && !(this.first instanceof OpNode) && !(this.second instanceof OpNode);
};
OpNode.prototype.isComplex = function() {
return this.operator !== '!' || this.first.isComplex();
};
OpNode.prototype.isMutator = function() {
var _ref2;
return ends(this.operator, '=') && !(('===' === (_ref2 = this.operator) || '!==' === _ref2));
@@ -1372,11 +1359,9 @@
OpNode.prototype.compileChain = function(o) {
var _ref2, first, second, shared;
shared = this.first.unwrap().second;
if (shared.containsType(CallNode)) {
_ref2 = shared.compileReference(o);
this.first.second = _ref2[0];
shared = _ref2[1];
}
_ref2 = shared.compileReference(o);
this.first.second = _ref2[0];
shared = _ref2[1];
_ref2 = [this.first.compile(o), this.second.compile(o), shared.compile(o)];
first = _ref2[0];
second = _ref2[1];
@@ -1482,9 +1467,7 @@
__extends(TryNode, BaseNode);
TryNode.prototype["class"] = 'TryNode';
TryNode.prototype.children = ['attempt', 'recovery', 'ensure'];
TryNode.prototype.isStatement = function() {
return true;
};
TryNode.prototype.isStatement = YES;
TryNode.prototype.makeReturn = function() {
if (this.attempt) {
this.attempt = this.attempt.makeReturn();
@@ -1515,9 +1498,7 @@
__extends(ThrowNode, BaseNode);
ThrowNode.prototype["class"] = 'ThrowNode';
ThrowNode.prototype.children = ['expression'];
ThrowNode.prototype.isStatement = function() {
return true;
};
ThrowNode.prototype.isStatement = YES;
ThrowNode.prototype.makeReturn = function() {
return this;
};
@@ -1563,12 +1544,13 @@
ParentheticalNode.prototype.isStatement = function(o) {
return this.expression.isStatement(o);
};
ParentheticalNode.prototype.isComplex = function() {
return this.expression.isComplex();
};
ParentheticalNode.prototype.topSensitive = YES;
ParentheticalNode.prototype.makeReturn = function() {
return this.expression.makeReturn();
};
ParentheticalNode.prototype.topSensitive = function() {
return true;
};
ParentheticalNode.prototype.compileNode = function(o) {
var code, top;
top = del(o, 'top');
@@ -1612,12 +1594,8 @@
__extends(ForNode, BaseNode);
ForNode.prototype["class"] = 'ForNode';
ForNode.prototype.children = ['body', 'source', 'guard'];
ForNode.prototype.isStatement = function() {
return true;
};
ForNode.prototype.topSensitive = function() {
return true;
};
ForNode.prototype.isStatement = YES;
ForNode.prototype.topSensitive = YES;
ForNode.prototype.makeReturn = function() {
this.returns = true;
return this;
@@ -1739,9 +1717,7 @@
__extends(SwitchNode, BaseNode);
SwitchNode.prototype["class"] = 'SwitchNode';
SwitchNode.prototype.children = ['subject', 'cases', 'otherwise'];
SwitchNode.prototype.isStatement = function() {
return true;
};
SwitchNode.prototype.isStatement = YES;
SwitchNode.prototype.makeReturn = function() {
var _i, _len, _ref2, pair;
_ref2 = this.cases;
@@ -1807,9 +1783,7 @@
__extends(IfNode, BaseNode);
IfNode.prototype["class"] = 'IfNode';
IfNode.prototype.children = ['condition', 'body', 'elseBody', 'assigner'];
IfNode.prototype.topSensitive = function() {
return true;
};
IfNode.prototype.topSensitive = YES;
IfNode.prototype.bodyNode = function() {
return this.body == null ? undefined : this.body.unwrap();
};
@@ -1935,8 +1909,8 @@
};
TAB = ' ';
TRAILING_WHITESPACE = /[ \t]+$/gm;
IDENTIFIER = /^[a-zA-Z\$_](\w|\$)*$/;
NUMBER = /^(((\b0(x|X)[0-9a-fA-F]+)|((\b[0-9]+(\.[0-9]+)?|\.[0-9]+)(e[+\-]?[0-9]+)?)))\b$/i;
IDENTIFIER = /^[$A-Za-zA-Z_][$\w]*$/;
NUMBER = /^0x[\da-f]+|^(?:\d+(\.\d+)?|\.\d+)(?:e[+-]?\d+)?$/i;
SIMPLENUM = /^-?\d+$/;
IS_STRING = /^['"]/;
literal = function(name) {