aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-11-05 06:18:45 +0800
committerGitHub <noreply@github.com>2017-11-05 06:18:45 +0800
commit5b4b07e9a7d67e593c6ae8d54dc77d174afd25ac (patch)
tree160e150e66c8f05da4d8d35097777ac5c4595b4f /lib
parenta8aa28a7a6c0cb415965d055119956d4333de8fa (diff)
downloadtracifyjs-5b4b07e9a7d67e593c6ae8d54dc77d174afd25ac.tar.gz
tracifyjs-5b4b07e9a7d67e593c6ae8d54dc77d174afd25ac.zip
extend function inlining safety checks (#2430)
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js46
1 files changed, 31 insertions, 15 deletions
diff --git a/lib/compress.js b/lib/compress.js
index ba90f000..454c1666 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -319,7 +319,7 @@ merge(Compressor.prototype, {
if (value instanceof AST_Lambda) {
d.single_use = d.scope === node.scope
&& !(d.orig[0] instanceof AST_SymbolFunarg)
- || value.is_constant_expression();
+ || value.is_constant_expression(node.scope);
} else {
d.single_use = d.scope === node.scope
&& loop_ids[d.id] === in_loop
@@ -385,7 +385,7 @@ merge(Compressor.prototype, {
mark(d, true);
if (unused && d.references.length == 1) {
d.single_use = d.scope === d.references[0].scope
- || node.is_constant_expression();
+ || node.is_constant_expression(d.references[0].scope);
}
}
var save_ids = safe_ids;
@@ -2176,18 +2176,22 @@ merge(Compressor.prototype, {
}
def(AST_Node, return_false);
def(AST_Constant, return_true);
- def(AST_Lambda, function(){
+ def(AST_Lambda, function(scope){
var self = this;
var result = true;
self.walk(new TreeWalker(function(node) {
if (!result) return true;
if (node instanceof AST_SymbolRef) {
var def = node.definition();
- if (self.enclosed.indexOf(def) >= 0
- && self.variables.get(def.name) !== def) {
+ if (member(def, self.enclosed)
+ && !self.variables.has(def.name)) {
+ if (scope) {
+ var scope_def = scope.find_variable(node);
+ if (def.undeclared ? !scope_def : scope_def === def) return true;
+ }
result = false;
- return true;
}
+ return true;
}
}));
return result;
@@ -2330,7 +2334,10 @@ merge(Compressor.prototype, {
});
return true;
}
- if (assign_as_unused(node) instanceof AST_SymbolRef && scope === self) {
+ var sym;
+ if (scope === self
+ && (sym = assign_as_unused(node)) instanceof AST_SymbolRef
+ && self.variables.get(sym.name) === sym.definition()) {
if (node instanceof AST_Assign) node.right.walk(tw);
return true;
}
@@ -2403,8 +2410,10 @@ merge(Compressor.prototype, {
}
}
if (drop_funcs && node instanceof AST_Defun && node !== self) {
- if (!(node.name.definition().id in in_use_ids)) {
+ var def = node.name.definition();
+ if (!(def.id in in_use_ids)) {
compressor[node.name.unreferenced() ? "warn" : "info"]("Dropping unused function {name} [{file}:{line},{col}]", template(node.name));
+ drop_decl(def, node.name);
return make_node(AST_EmptyStatement, node);
}
return node;
@@ -2426,7 +2435,7 @@ merge(Compressor.prototype, {
if (var_defs.length > 1 && !def.value) {
compressor.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name));
remove(var_defs, def);
- remove(sym.orig, def.name);
+ drop_decl(sym, def.name);
return;
}
}
@@ -2459,7 +2468,7 @@ merge(Compressor.prototype, {
} else {
compressor[def.name.unreferenced() ? "warn" : "info"]("Dropping unused variable {name} [{file}:{line},{col}]", template(def.name));
}
- remove(sym.orig, def.name);
+ drop_decl(sym, def.name);
}
});
if (head.length == 0 && tail.length == 1 && tail[0].name instanceof AST_SymbolVar) {
@@ -2468,7 +2477,7 @@ merge(Compressor.prototype, {
var def = tail.pop();
compressor.warn("Converting duplicated definition of variable {name} to assignment [{file}:{line},{col}]", template(def.name));
remove(var_defs, def);
- remove(def.name.definition().orig, def.name);
+ drop_decl(def.name.definition(), def.name);
side_effects.unshift(make_node(AST_Assign, def, {
operator: "=",
left: make_node(AST_SymbolRef, def.name, def.name),
@@ -2497,10 +2506,9 @@ merge(Compressor.prototype, {
}
}
if (drop_vars) {
- var def = assign_as_unused(node);
- if (def instanceof AST_SymbolRef
- && !((def = def.definition()).id in in_use_ids)
- && self.variables.get(def.name) === def) {
+ var sym = assign_as_unused(node);
+ if (sym instanceof AST_SymbolRef
+ && !(sym.definition().id in in_use_ids)) {
if (node instanceof AST_Assign) {
return maintain_this_binding(parent, node, node.right.transform(tt));
}
@@ -2551,6 +2559,14 @@ merge(Compressor.prototype, {
col : sym.start.col
};
}
+
+ function drop_decl(def, decl) {
+ remove(def.orig, decl);
+ if (!def.orig.length) {
+ def.scope.functions.del(def.name);
+ def.scope.variables.del(def.name);
+ }
+ }
}
);
self.transform(tt);