From 3ee13cae02174e565ca40462d7b15c4974265871 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 23 Apr 2020 23:50:53 +0100 Subject: improve `compress` (#3814) - avoid identifier overflow through consecutive API calls - simplify `reduce_vars` - enhance `unsafe` `evaluate` --- lib/scope.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) (limited to 'lib/scope.js') diff --git a/lib/scope.js b/lib/scope.js index ad33c5e0..adbfe4ac 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -43,23 +43,21 @@ "use strict"; -function SymbolDef(scope, orig, init) { +function SymbolDef(id, scope, orig, init) { + this.eliminated = 0; + this.global = false; + this.id = id; + this.init = init; + this.lambda = orig instanceof AST_SymbolLambda; + this.mangled_name = null; this.name = orig.name; this.orig = [ orig ]; - this.init = init; - this.eliminated = 0; - this.scope = scope; this.references = []; this.replaced = 0; - this.global = false; - this.mangled_name = null; + this.scope = scope; this.undeclared = false; - this.id = SymbolDef.next_id++; - this.lambda = orig instanceof AST_SymbolLambda; } -SymbolDef.next_id = 1; - SymbolDef.prototype = { unmangleable: function(options) { return this.global && !options.toplevel @@ -151,6 +149,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) { scope.def_variable(node).defun = defun; } }); + self.next_def_id = 0; self.walk(tw); // pass 2: find back references and eval @@ -240,12 +239,18 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) { } }); +AST_Scope.DEFMETHOD("make_def", function(orig, init) { + var top = this; + while (top.parent_scope) top = top.parent_scope; + return new SymbolDef(++top.next_def_id, this, orig, init); +}); + AST_Toplevel.DEFMETHOD("def_global", function(node) { var globals = this.globals, name = node.name; if (globals.has(name)) { return globals.get(name); } else { - var g = new SymbolDef(this, node); + var g = this.make_def(node); g.undeclared = true; g.global = true; globals.set(name, g); @@ -310,7 +315,7 @@ AST_Scope.DEFMETHOD("def_variable", function(symbol, init) { def.orig.push(symbol); if (def.init instanceof AST_Function) def.init = init; } else { - def = new SymbolDef(this, symbol, init); + def = this.make_def(symbol, init); this.variables.set(symbol.name, def); def.global = !this.parent_scope; } -- cgit v1.2.3