From cb6a92892f9a5d66d36c766213c19e274efb9796 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Mon, 25 Dec 2017 01:57:11 +0800 Subject: fix infinite loop during `inline` (#2645) fixes #2644 --- lib/compress.js | 14 ++++++-------- test/compress/functions.js | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 315a18c7..cc360950 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3934,7 +3934,7 @@ merge(Compressor.prototype, { } } if (fn instanceof AST_Function) { - var def, scope, value; + var def, value, scope, level = -1; if (compressor.option("inline") && !fn.uses_arguments && !fn.uses_eval @@ -3946,7 +3946,7 @@ merge(Compressor.prototype, { && fn.is_constant_expression(exp.scope)) && !self.pure && !fn.contains_this() - && (scope = can_flatten_args(fn)) + && can_flatten_args(fn) && (value = flatten_body(stat))) { var expressions = flatten_args(fn, scope); expressions.push(value.clone(true)); @@ -3981,10 +3981,9 @@ merge(Compressor.prototype, { return self; function can_flatten_args(fn) { - var scope, level = 0; var catches = Object.create(null); do { - scope = compressor.parent(level++); + scope = compressor.parent(++level); if (scope instanceof AST_SymbolRef) { scope = scope.fixed_value(); } else if (scope instanceof AST_Catch) { @@ -3998,10 +3997,10 @@ merge(Compressor.prototype, { && !catches[arg.name] && !identifier_atom(arg.name) && !scope.var_names()[arg.name]; - }) && scope; + }); } - function flatten_args(fn, scope) { + function flatten_args(fn) { var decls = []; var expressions = []; for (var len = fn.argnames.length, i = len; --i >= 0;) { @@ -4035,8 +4034,7 @@ merge(Compressor.prototype, { expressions.push(self.args[i]); } if (decls.length) { - for (i = 0; compressor.parent(i) !== scope;) i++; - i = scope.body.indexOf(compressor.parent(i - 1)) + 1; + i = scope.body.indexOf(compressor.parent(level - 1)) + 1; scope.body.splice(i, 0, make_node(AST_Var, fn, { definitions: decls })); diff --git a/test/compress/functions.js b/test/compress/functions.js index 07147663..02b4ab39 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -1423,3 +1423,27 @@ issue_2630_5: { } expect_stdout: "155" } + +recursive_inline: { + options = { + inline: true, + reduce_funcs: true, + reduce_vars: true, + sequences: true, + toplevel: true, + unused: true, + } + input: { + function f() { + h(); + } + function g(a) { + a(); + } + function h(b) { + g(); + if (b) x(); + } + } + expect: {} +} -- cgit v1.2.3