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:
17
lib/ast.js
17
lib/ast.js
@@ -57,9 +57,7 @@ function DEFNODE(type, props, methods, base) {
|
||||
var proto = base && new base;
|
||||
if (proto && proto.initialize || (methods && methods.initialize))
|
||||
code += "this.initialize();";
|
||||
code += " } ";
|
||||
code += "if (!this.$self) this.$self = this;";
|
||||
code += " } ";
|
||||
code += "}}";
|
||||
var ctor = new Function(code)();
|
||||
if (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", {
|
||||
}, null);
|
||||
|
||||
var AST_Node = DEFNODE("Node", "$self start end", {
|
||||
var AST_Node = DEFNODE("Node", "start end", {
|
||||
clone: function() {
|
||||
return new this.CTOR(this);
|
||||
},
|
||||
$documentation: "Base class of all AST nodes",
|
||||
$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",
|
||||
end: "[AST_Token] The last token of this node"
|
||||
},
|
||||
@@ -904,7 +901,7 @@ TreeWalker.prototype = {
|
||||
},
|
||||
in_boolean_context: function() {
|
||||
var stack = this.stack;
|
||||
var i = stack.length, self = stack[--i].$self;
|
||||
var i = stack.length, self = stack[--i];
|
||||
while (i > 0) {
|
||||
var p = stack[--i];
|
||||
if ((p instanceof AST_If && p.condition === self) ||
|
||||
@@ -917,7 +914,7 @@ TreeWalker.prototype = {
|
||||
}
|
||||
if (!(p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||")))
|
||||
return false;
|
||||
self = p.$self;
|
||||
self = p;
|
||||
}
|
||||
},
|
||||
loopcontrol_target: function(label) {
|
||||
@@ -926,15 +923,15 @@ TreeWalker.prototype = {
|
||||
for (var i = stack.length; --i >= 0;) {
|
||||
var x = stack[i];
|
||||
if (x instanceof AST_LabeledStatement && x.label.name == label.name) {
|
||||
return x.body.$self;
|
||||
return x.body;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (var i = stack.length; --i >= 0;) {
|
||||
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) {
|
||||
return (x.body instanceof AST_BlockStatement ? x.body : x).$self;
|
||||
return (x.body instanceof AST_BlockStatement ? x.body : x);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,7 +80,6 @@ merge(Compressor.prototype, {
|
||||
},
|
||||
before: function(node, descend, in_list) {
|
||||
if (node._squeezed) return node;
|
||||
this.stack[this.stack.length - 1] = node = node.clone();
|
||||
if (node instanceof AST_Scope) {
|
||||
node.drop_unused(this);
|
||||
node = node.hoist_declarations(this);
|
||||
@@ -291,7 +290,7 @@ merge(Compressor.prototype, {
|
||||
|
||||
var ab = aborts(stat.body);
|
||||
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;
|
||||
var body = as_statement_array(stat.body).slice(0, -1);
|
||||
stat = stat.clone();
|
||||
@@ -308,7 +307,7 @@ merge(Compressor.prototype, {
|
||||
|
||||
var ab = aborts(stat.alternative);
|
||||
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;
|
||||
stat = stat.clone();
|
||||
stat.body = make_node(AST_BlockStatement, stat.body, {
|
||||
@@ -447,7 +446,6 @@ merge(Compressor.prototype, {
|
||||
stat.walk(new TreeWalker(function(node){
|
||||
if (node instanceof AST_Definitions) {
|
||||
compressor.warn("Declarations in unreachable code! [{file}:{line},{col}]", node.start);
|
||||
node = node.clone();
|
||||
node.remove_initializers();
|
||||
target.push(node);
|
||||
return true;
|
||||
@@ -1187,7 +1185,7 @@ merge(Compressor.prototype, {
|
||||
var last_branch = self.body[self.body.length - 1];
|
||||
if (last_branch) {
|
||||
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();
|
||||
}
|
||||
return self;
|
||||
@@ -1204,11 +1202,7 @@ merge(Compressor.prototype, {
|
||||
});
|
||||
|
||||
AST_Definitions.DEFMETHOD("remove_initializers", function(){
|
||||
this.definitions = this.definitions.map(function(def){
|
||||
def = def.clone();
|
||||
def.value = null;
|
||||
return def;
|
||||
});
|
||||
this.definitions.forEach(function(def){ def.value = null });
|
||||
});
|
||||
|
||||
AST_Definitions.DEFMETHOD("to_assignments", function(){
|
||||
|
||||
Reference in New Issue
Block a user