diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-04-23 23:50:53 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-24 06:50:53 +0800 |
commit | 3ee13cae02174e565ca40462d7b15c4974265871 (patch) | |
tree | 1a7a9b09da324ff3cb238ac101637696e02cb781 /lib/scope.js | |
parent | 99cf3a38c53d4a8e4ac937962df4a0fdc600c070 (diff) | |
download | tracifyjs-3ee13cae02174e565ca40462d7b15c4974265871.tar.gz tracifyjs-3ee13cae02174e565ca40462d7b15c4974265871.zip |
improve `compress` (#3814)
- avoid identifier overflow through consecutive API calls
- simplify `reduce_vars`
- enhance `unsafe` `evaluate`
Diffstat (limited to 'lib/scope.js')
-rw-r--r-- | lib/scope.js | 29 |
1 files changed, 17 insertions, 12 deletions
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; } |