remove the $self hack

operations are destructive anyway, so there's no point to clone the nodes in
the transformer.  speed++
This commit is contained in:
Mihai Bazon
2012-10-12 11:07:35 +03:00
parent 731fa9c236
commit 1b6f8d463f
2 changed files with 11 additions and 20 deletions

View File

@@ -57,9 +57,7 @@ function DEFNODE(type, props, methods, base) {
var proto = base && new base; var proto = base && new base;
if (proto && proto.initialize || (methods && methods.initialize)) if (proto && proto.initialize || (methods && methods.initialize))
code += "this.initialize();"; code += "this.initialize();";
code += " } "; code += "}}";
code += "if (!this.$self) this.$self = this;";
code += " } ";
var ctor = new Function(code)(); var ctor = new Function(code)();
if (proto) { if (proto) {
ctor.prototype = proto; ctor.prototype = proto;
@@ -89,13 +87,12 @@ function DEFNODE(type, props, methods, base) {
var AST_Token = DEFNODE("Token", "type value line col pos endpos nlb comments_before file", { var AST_Token = DEFNODE("Token", "type value line col pos endpos nlb comments_before file", {
}, null); }, null);
var AST_Node = DEFNODE("Node", "$self start end", { var AST_Node = DEFNODE("Node", "start end", {
clone: function() { clone: function() {
return new this.CTOR(this); return new this.CTOR(this);
}, },
$documentation: "Base class of all AST nodes", $documentation: "Base class of all AST nodes",
$propdoc: { $propdoc: {
$self: "[AST_Node] Reference to $self. Not modified by clone(). XXX: this could be removed.",
start: "[AST_Token] The first token of this node", start: "[AST_Token] The first token of this node",
end: "[AST_Token] The last token of this node" end: "[AST_Token] The last token of this node"
}, },
@@ -904,7 +901,7 @@ TreeWalker.prototype = {
}, },
in_boolean_context: function() { in_boolean_context: function() {
var stack = this.stack; var stack = this.stack;
var i = stack.length, self = stack[--i].$self; var i = stack.length, self = stack[--i];
while (i > 0) { while (i > 0) {
var p = stack[--i]; var p = stack[--i];
if ((p instanceof AST_If && p.condition === self) || if ((p instanceof AST_If && p.condition === self) ||
@@ -917,7 +914,7 @@ TreeWalker.prototype = {
} }
if (!(p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||"))) if (!(p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||")))
return false; return false;
self = p.$self; self = p;
} }
}, },
loopcontrol_target: function(label) { loopcontrol_target: function(label) {
@@ -926,15 +923,15 @@ TreeWalker.prototype = {
for (var i = stack.length; --i >= 0;) { for (var i = stack.length; --i >= 0;) {
var x = stack[i]; var x = stack[i];
if (x instanceof AST_LabeledStatement && x.label.name == label.name) { if (x instanceof AST_LabeledStatement && x.label.name == label.name) {
return x.body.$self; return x.body;
} }
} }
} else { } else {
for (var i = stack.length; --i >= 0;) { for (var i = stack.length; --i >= 0;) {
var x = stack[i]; var x = stack[i];
if (x instanceof AST_Switch) return x.$self; if (x instanceof AST_Switch) return x;
if (x instanceof AST_For || x instanceof AST_ForIn || x instanceof AST_DWLoop) { if (x instanceof AST_For || x instanceof AST_ForIn || x instanceof AST_DWLoop) {
return (x.body instanceof AST_BlockStatement ? x.body : x).$self; return (x.body instanceof AST_BlockStatement ? x.body : x);
} }
} }
} }

View File

@@ -80,7 +80,6 @@ merge(Compressor.prototype, {
}, },
before: function(node, descend, in_list) { before: function(node, descend, in_list) {
if (node._squeezed) return node; if (node._squeezed) return node;
this.stack[this.stack.length - 1] = node = node.clone();
if (node instanceof AST_Scope) { if (node instanceof AST_Scope) {
node.drop_unused(this); node.drop_unused(this);
node = node.hoist_declarations(this); node = node.hoist_declarations(this);
@@ -291,7 +290,7 @@ merge(Compressor.prototype, {
var ab = aborts(stat.body); var ab = aborts(stat.body);
if (ab && ((ab instanceof AST_Return && !ab.value && in_lambda) if (ab && ((ab instanceof AST_Return && !ab.value && in_lambda)
|| (ab instanceof AST_Continue && self.$self === compressor.loopcontrol_target(ab.label)))) { || (ab instanceof AST_Continue && self === compressor.loopcontrol_target(ab.label)))) {
CHANGED = true; CHANGED = true;
var body = as_statement_array(stat.body).slice(0, -1); var body = as_statement_array(stat.body).slice(0, -1);
stat = stat.clone(); stat = stat.clone();
@@ -308,7 +307,7 @@ merge(Compressor.prototype, {
var ab = aborts(stat.alternative); var ab = aborts(stat.alternative);
if (ab && ((ab instanceof AST_Return && !ab.value && in_lambda) if (ab && ((ab instanceof AST_Return && !ab.value && in_lambda)
|| (ab instanceof AST_Continue && self.$self === compressor.loopcontrol_target(ab.label)))) { || (ab instanceof AST_Continue && self === compressor.loopcontrol_target(ab.label)))) {
CHANGED = true; CHANGED = true;
stat = stat.clone(); stat = stat.clone();
stat.body = make_node(AST_BlockStatement, stat.body, { stat.body = make_node(AST_BlockStatement, stat.body, {
@@ -447,7 +446,6 @@ merge(Compressor.prototype, {
stat.walk(new TreeWalker(function(node){ stat.walk(new TreeWalker(function(node){
if (node instanceof AST_Definitions) { if (node instanceof AST_Definitions) {
compressor.warn("Declarations in unreachable code! [{file}:{line},{col}]", node.start); compressor.warn("Declarations in unreachable code! [{file}:{line},{col}]", node.start);
node = node.clone();
node.remove_initializers(); node.remove_initializers();
target.push(node); target.push(node);
return true; return true;
@@ -1187,7 +1185,7 @@ merge(Compressor.prototype, {
var last_branch = self.body[self.body.length - 1]; var last_branch = self.body[self.body.length - 1];
if (last_branch) { if (last_branch) {
var stat = last_branch.body[last_branch.body.length - 1]; // last statement var stat = last_branch.body[last_branch.body.length - 1]; // last statement
if (stat instanceof AST_Break && compressor.loopcontrol_target(stat.label) === self.$self) if (stat instanceof AST_Break && compressor.loopcontrol_target(stat.label) === self)
last_branch.body.pop(); last_branch.body.pop();
} }
return self; return self;
@@ -1204,11 +1202,7 @@ merge(Compressor.prototype, {
}); });
AST_Definitions.DEFMETHOD("remove_initializers", function(){ AST_Definitions.DEFMETHOD("remove_initializers", function(){
this.definitions = this.definitions.map(function(def){ this.definitions.forEach(function(def){ def.value = null });
def = def.clone();
def.value = null;
return def;
});
}); });
AST_Definitions.DEFMETHOD("to_assignments", function(){ AST_Definitions.DEFMETHOD("to_assignments", function(){