extend function inlining safety checks (#2430)

This commit is contained in:
Alex Lam S.L
2017-11-05 06:18:45 +08:00
committed by GitHub
parent a8aa28a7a6
commit 5b4b07e9a7
3 changed files with 167 additions and 27 deletions

View File

@@ -319,7 +319,7 @@ merge(Compressor.prototype, {
if (value instanceof AST_Lambda) { if (value instanceof AST_Lambda) {
d.single_use = d.scope === node.scope d.single_use = d.scope === node.scope
&& !(d.orig[0] instanceof AST_SymbolFunarg) && !(d.orig[0] instanceof AST_SymbolFunarg)
|| value.is_constant_expression(); || value.is_constant_expression(node.scope);
} else { } else {
d.single_use = d.scope === node.scope d.single_use = d.scope === node.scope
&& loop_ids[d.id] === in_loop && loop_ids[d.id] === in_loop
@@ -385,7 +385,7 @@ merge(Compressor.prototype, {
mark(d, true); mark(d, true);
if (unused && d.references.length == 1) { if (unused && d.references.length == 1) {
d.single_use = d.scope === d.references[0].scope 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; var save_ids = safe_ids;
@@ -2176,18 +2176,22 @@ merge(Compressor.prototype, {
} }
def(AST_Node, return_false); def(AST_Node, return_false);
def(AST_Constant, return_true); def(AST_Constant, return_true);
def(AST_Lambda, function(){ def(AST_Lambda, function(scope){
var self = this; var self = this;
var result = true; var result = true;
self.walk(new TreeWalker(function(node) { self.walk(new TreeWalker(function(node) {
if (!result) return true; if (!result) return true;
if (node instanceof AST_SymbolRef) { if (node instanceof AST_SymbolRef) {
var def = node.definition(); var def = node.definition();
if (self.enclosed.indexOf(def) >= 0 if (member(def, self.enclosed)
&& self.variables.get(def.name) !== def) { && !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; result = false;
return true;
} }
return true;
} }
})); }));
return result; return result;
@@ -2330,7 +2334,10 @@ merge(Compressor.prototype, {
}); });
return true; 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); if (node instanceof AST_Assign) node.right.walk(tw);
return true; return true;
} }
@@ -2403,8 +2410,10 @@ merge(Compressor.prototype, {
} }
} }
if (drop_funcs && node instanceof AST_Defun && node !== self) { 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)); 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 make_node(AST_EmptyStatement, node);
} }
return node; return node;
@@ -2426,7 +2435,7 @@ merge(Compressor.prototype, {
if (var_defs.length > 1 && !def.value) { if (var_defs.length > 1 && !def.value) {
compressor.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name)); compressor.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name));
remove(var_defs, def); remove(var_defs, def);
remove(sym.orig, def.name); drop_decl(sym, def.name);
return; return;
} }
} }
@@ -2459,7 +2468,7 @@ merge(Compressor.prototype, {
} else { } else {
compressor[def.name.unreferenced() ? "warn" : "info"]("Dropping unused variable {name} [{file}:{line},{col}]", template(def.name)); 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) { if (head.length == 0 && tail.length == 1 && tail[0].name instanceof AST_SymbolVar) {
@@ -2468,7 +2477,7 @@ merge(Compressor.prototype, {
var def = tail.pop(); var def = tail.pop();
compressor.warn("Converting duplicated definition of variable {name} to assignment [{file}:{line},{col}]", template(def.name)); compressor.warn("Converting duplicated definition of variable {name} to assignment [{file}:{line},{col}]", template(def.name));
remove(var_defs, def); 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, { side_effects.unshift(make_node(AST_Assign, def, {
operator: "=", operator: "=",
left: make_node(AST_SymbolRef, def.name, def.name), left: make_node(AST_SymbolRef, def.name, def.name),
@@ -2497,10 +2506,9 @@ merge(Compressor.prototype, {
} }
} }
if (drop_vars) { if (drop_vars) {
var def = assign_as_unused(node); var sym = assign_as_unused(node);
if (def instanceof AST_SymbolRef if (sym instanceof AST_SymbolRef
&& !((def = def.definition()).id in in_use_ids) && !(sym.definition().id in in_use_ids)) {
&& self.variables.get(def.name) === def) {
if (node instanceof AST_Assign) { if (node instanceof AST_Assign) {
return maintain_this_binding(parent, node, node.right.transform(tt)); return maintain_this_binding(parent, node, node.right.transform(tt));
} }
@@ -2551,6 +2559,14 @@ merge(Compressor.prototype, {
col : sym.start.col 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); self.transform(tt);

View File

@@ -1109,11 +1109,11 @@ var_catch_toplevel: {
} }
} }
issue_2105: { issue_2105_1: {
options = { options = {
collapse_vars: true, collapse_vars: true,
inline: true, inline: true,
passes: 3, passes: 2,
reduce_vars: true, reduce_vars: true,
side_effects: true, side_effects: true,
unused: true, unused: true,
@@ -1139,17 +1139,50 @@ issue_2105: {
}); });
} }
expect: { expect: {
(function() { ({
var quux = function() { prop: function() {
console.log;
console.log("PASS"); console.log("PASS");
}; }
return { }).prop();
prop: function() { }
console.log; expect_stdout: "PASS"
quux(); }
issue_2105_2: {
options = {
collapse_vars: true,
inline: true,
passes: 2,
properties: true,
pure_getters: "strict",
reduce_vars: true,
side_effects: true,
unsafe: true,
unused: true,
}
input: {
!function(factory) {
factory();
}( function() {
return function(fn) {
fn()().prop();
}( function() {
function bar() {
var quux = function() {
console.log("PASS");
}, foo = function() {
console.log;
quux();
};
return { prop: foo };
} }
}; return bar;
})().prop(); } );
});
}
expect: {
console.log("PASS");
} }
expect_stdout: "PASS" expect_stdout: "PASS"
} }

View File

@@ -3397,6 +3397,10 @@ issue_2423_1: {
p(); p();
p(); p();
} }
expect_stdout: [
"1",
"1",
]
} }
issue_2423_2: { issue_2423_2: {
@@ -3417,6 +3421,10 @@ issue_2423_2: {
p(); p();
p(); p();
} }
expect_stdout: [
"1",
"1",
]
} }
issue_2423_3: { issue_2423_3: {
@@ -3433,12 +3441,14 @@ issue_2423_3: {
expect: { expect: {
(function() { console.log(function() { return 1; }()); })(); (function() { console.log(function() { return 1; }()); })();
} }
expect_stdout: "1"
} }
issue_2423_4: { issue_2423_4: {
options = { options = {
inline: true, inline: true,
reduce_vars: true, reduce_vars: true,
side_effects: true,
toplevel: true, toplevel: true,
unused: true, unused: true,
} }
@@ -3448,6 +3458,87 @@ issue_2423_4: {
p(); p();
} }
expect: { expect: {
void console.log(1); console.log(1);
} }
expect_stdout: "1"
}
issue_2423_5: {
options = {
inline: true,
passes: 2,
reduce_vars: true,
side_effects: true,
toplevel: true,
unused: true,
}
input: {
function x() {
y();
}
function y() {
console.log(1);
}
function z() {
function y() {
console.log(2);
}
x();
}
z();
z();
}
expect: {
function z() {
console.log(1);
}
z();
z();
}
expect_stdout: [
"1",
"1",
]
}
issue_2423_6: {
options = {
inline: true,
passes: 2,
reduce_vars: true,
side_effects: true,
toplevel: true,
unused: true,
}
input: {
function x() {
y();
}
function y() {
console.log(1);
}
function z() {
function y() {
console.log(2);
}
x();
y();
}
z();
z();
}
expect: {
function z(){
console.log(1);
console.log(2);
}
z();
z();
}
expect_stdout: [
"1",
"2",
"1",
"2",
]
} }