tidy up various interfaces (#4066)

This commit is contained in:
Alex Lam S.L
2020-08-23 21:39:38 +01:00
committed by GitHub
parent af1b2f30c9
commit 7dc61cdc89
5 changed files with 168 additions and 167 deletions

View File

@@ -409,16 +409,16 @@ var AST_With = DEFNODE("With", "expression", {
/* -----[ scope and functions ]----- */ /* -----[ scope and functions ]----- */
var AST_Scope = DEFNODE("Scope", "variables functions uses_with uses_eval parent_scope enclosed cname", { var AST_Scope = DEFNODE("Scope", "cname enclosed uses_eval uses_with parent_scope functions variables make_def", {
$documentation: "Base class for all statements introducing a lexical scope", $documentation: "Base class for all statements introducing a lexical scope",
$propdoc: { $propdoc: {
variables: "[Object/S] a map of name -> SymbolDef for all variables/functions defined in this scope",
functions: "[Object/S] like `variables`, but only lists function declarations",
uses_with: "[boolean/S] tells whether this scope uses the `with` statement",
uses_eval: "[boolean/S] tells whether this scope contains a direct call to the global `eval`",
parent_scope: "[AST_Scope?/S] link to the parent scope",
enclosed: "[SymbolDef*/S] a list of all symbol definitions that are accessed from this scope or any subscopes",
cname: "[integer/S] current index for mangling variables (used internally by the mangler)", cname: "[integer/S] current index for mangling variables (used internally by the mangler)",
enclosed: "[SymbolDef*/S] a list of all symbol definitions that are accessed from this scope or any subscopes",
uses_eval: "[boolean/S] tells whether this scope contains a direct call to the global `eval`",
uses_with: "[boolean/S] tells whether this scope uses the `with` statement",
parent_scope: "[AST_Scope?/S] link to the parent scope",
functions: "[Object/S] like `variables`, but only lists function declarations",
variables: "[Object/S] a map of name -> SymbolDef for all variables/functions defined in this scope",
}, },
clone: function(deep) { clone: function(deep) {
var node = this._clone(deep); var node = this._clone(deep);

View File

@@ -3152,11 +3152,11 @@ merge(Compressor.prototype, {
return this.expression._find_defs(compressor, "." + this.property + suffix); return this.expression._find_defs(compressor, "." + this.property + suffix);
}); });
def(AST_SymbolDeclaration, function(compressor) { def(AST_SymbolDeclaration, function(compressor) {
if (!this.global()) return; if (!this.definition().global) return;
if (HOP(compressor.option("global_defs"), this.name)) warn(this); if (HOP(compressor.option("global_defs"), this.name)) warn(this);
}); });
def(AST_SymbolRef, function(compressor, suffix) { def(AST_SymbolRef, function(compressor, suffix) {
if (!this.global()) return; if (!this.definition().global) return;
var defines = compressor.option("global_defs"); var defines = compressor.option("global_defs");
var name = this.name + suffix; var name = this.name + suffix;
if (HOP(defines, name)) return to_node(defines[name], this); if (HOP(defines, name)) return to_node(defines[name], this);
@@ -4700,7 +4700,7 @@ merge(Compressor.prototype, {
}); });
function log(sym, text, props) { function log(sym, text, props) {
AST_Node[sym.unreferenced() ? "warn" : "info"](text, props); AST_Node[sym.definition().references.length > 0 ? "info" : "warn"](text, props);
} }
function template(sym) { function template(sym) {
@@ -6395,7 +6395,7 @@ merge(Compressor.prototype, {
} }
})); }));
var code = OutputStream(); var code = OutputStream();
AST_BlockStatement.prototype._codegen.call(fun, fun, code); AST_BlockStatement.prototype._codegen.call(fun, code);
self.args = [ self.args = [
make_node(AST_String, self, { make_node(AST_String, self, {
value: fun.argnames.map(function(arg) { value: fun.argnames.map(function(arg) {

View File

@@ -630,13 +630,7 @@ function OutputStream(options) {
var use_asm = false; var use_asm = false;
AST_Node.DEFMETHOD("print", function(stream, force_parens) { AST_Node.DEFMETHOD("print", function(stream, force_parens) {
var self = this, generator = self._codegen; var self = this;
function doit() {
stream.prepend_comments(self);
self.add_source_map(stream);
generator(self, stream);
stream.append_comments(self);
}
stream.push_node(self); stream.push_node(self);
if (force_parens || self.needs_parens(stream)) { if (force_parens || self.needs_parens(stream)) {
stream.with_parens(doit); stream.with_parens(doit);
@@ -644,9 +638,14 @@ function OutputStream(options) {
doit(); doit();
} }
stream.pop_node(); stream.pop_node();
});
AST_Node.DEFMETHOD("_print", AST_Node.prototype.print);
function doit() {
stream.prepend_comments(self);
self.add_source_map(stream);
self._codegen(stream);
stream.append_comments(self);
}
});
AST_Node.DEFMETHOD("print_to_string", function(options) { AST_Node.DEFMETHOD("print_to_string", function(options) {
var s = OutputStream(options); var s = OutputStream(options);
this.print(s); this.print(s);
@@ -813,9 +812,9 @@ function OutputStream(options) {
/* -----[ PRINTERS ]----- */ /* -----[ PRINTERS ]----- */
DEFPRINT(AST_Directive, function(self, output) { DEFPRINT(AST_Directive, function(output) {
var quote = self.quote; var quote = this.quote;
var value = self.value; var value = this.value;
switch (output.option("quote_style")) { switch (output.option("quote_style")) {
case 0: case 0:
case 2: case 2:
@@ -828,7 +827,7 @@ function OutputStream(options) {
output.print(quote + value + quote); output.print(quote + value + quote);
output.semicolon(); output.semicolon();
}); });
DEFPRINT(AST_Debugger, function(self, output) { DEFPRINT(AST_Debugger, function(output) {
output.print("debugger"); output.print("debugger");
output.semicolon(); output.semicolon();
}); });
@@ -864,21 +863,21 @@ function OutputStream(options) {
force_statement(this.body, output); force_statement(this.body, output);
}); });
DEFPRINT(AST_Statement, function(self, output) { DEFPRINT(AST_Statement, function(output) {
self.body.print(output); this.body.print(output);
output.semicolon(); output.semicolon();
}); });
DEFPRINT(AST_Toplevel, function(self, output) { DEFPRINT(AST_Toplevel, function(output) {
display_body(self.body, true, output, true); display_body(this.body, true, output, true);
output.print(""); output.print("");
}); });
DEFPRINT(AST_LabeledStatement, function(self, output) { DEFPRINT(AST_LabeledStatement, function(output) {
self.label.print(output); this.label.print(output);
output.colon(); output.colon();
self.body.print(output); this.body.print(output);
}); });
DEFPRINT(AST_SimpleStatement, function(self, output) { DEFPRINT(AST_SimpleStatement, function(output) {
self.body.print(output); this.body.print(output);
output.semicolon(); output.semicolon();
}); });
function print_braced_empty(self, output) { function print_braced_empty(self, output) {
@@ -895,13 +894,14 @@ function OutputStream(options) {
}); });
} else print_braced_empty(self, output); } else print_braced_empty(self, output);
} }
DEFPRINT(AST_BlockStatement, function(self, output) { DEFPRINT(AST_BlockStatement, function(output) {
print_braced(self, output); print_braced(this, output);
}); });
DEFPRINT(AST_EmptyStatement, function(self, output) { DEFPRINT(AST_EmptyStatement, function(output) {
output.semicolon(); output.semicolon();
}); });
DEFPRINT(AST_Do, function(self, output) { DEFPRINT(AST_Do, function(output) {
var self = this;
output.print("do"); output.print("do");
output.space(); output.space();
make_block(self.body, output); make_block(self.body, output);
@@ -913,7 +913,8 @@ function OutputStream(options) {
}); });
output.semicolon(); output.semicolon();
}); });
DEFPRINT(AST_While, function(self, output) { DEFPRINT(AST_While, function(output) {
var self = this;
output.print("while"); output.print("while");
output.space(); output.space();
output.with_parens(function() { output.with_parens(function() {
@@ -922,7 +923,8 @@ function OutputStream(options) {
output.space(); output.space();
self._do_print_body(output); self._do_print_body(output);
}); });
DEFPRINT(AST_For, function(self, output) { DEFPRINT(AST_For, function(output) {
var self = this;
output.print("for"); output.print("for");
output.space(); output.space();
output.with_parens(function() { output.with_parens(function() {
@@ -951,7 +953,8 @@ function OutputStream(options) {
output.space(); output.space();
self._do_print_body(output); self._do_print_body(output);
}); });
DEFPRINT(AST_ForIn, function(self, output) { DEFPRINT(AST_ForIn, function(output) {
var self = this;
output.print("for"); output.print("for");
output.space(); output.space();
output.with_parens(function() { output.with_parens(function() {
@@ -964,7 +967,8 @@ function OutputStream(options) {
output.space(); output.space();
self._do_print_body(output); self._do_print_body(output);
}); });
DEFPRINT(AST_With, function(self, output) { DEFPRINT(AST_With, function(output) {
var self = this;
output.print("with"); output.print("with");
output.space(); output.space();
output.with_parens(function() { output.with_parens(function() {
@@ -975,7 +979,7 @@ function OutputStream(options) {
}); });
/* -----[ functions ]----- */ /* -----[ functions ]----- */
AST_Lambda.DEFMETHOD("_do_print", function(output, nokeyword) { DEFPRINT(AST_Lambda, function(output, nokeyword) {
var self = this; var self = this;
if (!nokeyword) { if (!nokeyword) {
output.print("function"); output.print("function");
@@ -993,32 +997,23 @@ function OutputStream(options) {
output.space(); output.space();
print_braced(self, output, true); print_braced(self, output, true);
}); });
DEFPRINT(AST_Lambda, function(self, output) {
self._do_print(output);
});
/* -----[ jumps ]----- */ /* -----[ jumps ]----- */
function print_jump(output, kind, target) { function print_jump(kind, prop) {
output.print(kind); return function(output) {
if (target) { output.print(kind);
output.space(); var target = this[prop];
target.print(output); if (target) {
} output.space();
output.semicolon(); target.print(output);
}
output.semicolon();
};
} }
DEFPRINT(AST_Return, print_jump("return", "value"));
DEFPRINT(AST_Return, function(self, output) { DEFPRINT(AST_Throw, print_jump("throw", "value"));
print_jump(output, "return", self.value); DEFPRINT(AST_Break, print_jump("break", "label"));
}); DEFPRINT(AST_Continue, print_jump("continue", "label"));
DEFPRINT(AST_Throw, function(self, output) {
print_jump(output, "throw", self.value);
});
DEFPRINT(AST_Break, function(self, output) {
print_jump(output, "break", self.label);
});
DEFPRINT(AST_Continue, function(self, output) {
print_jump(output, "continue", self.label);
});
/* -----[ if ]----- */ /* -----[ if ]----- */
function make_then(self, output) { function make_then(self, output) {
@@ -1047,7 +1042,8 @@ function OutputStream(options) {
} }
force_statement(self.body, output); force_statement(self.body, output);
} }
DEFPRINT(AST_If, function(self, output) { DEFPRINT(AST_If, function(output) {
var self = this;
output.print("if"); output.print("if");
output.space(); output.space();
output.with_parens(function() { output.with_parens(function() {
@@ -1069,7 +1065,8 @@ function OutputStream(options) {
}); });
/* -----[ switch ]----- */ /* -----[ switch ]----- */
DEFPRINT(AST_Switch, function(self, output) { DEFPRINT(AST_Switch, function(output) {
var self = this;
output.print("switch"); output.print("switch");
output.space(); output.space();
output.with_parens(function() { output.with_parens(function() {
@@ -1087,28 +1084,30 @@ function OutputStream(options) {
}); });
}); });
}); });
AST_SwitchBranch.DEFMETHOD("_do_print_body", function(output) { function print_branch_body(self, output) {
output.newline(); output.newline();
this.body.forEach(function(stmt) { self.body.forEach(function(stmt) {
output.indent(); output.indent();
stmt.print(output); stmt.print(output);
output.newline(); output.newline();
}); });
}); }
DEFPRINT(AST_Default, function(self, output) { DEFPRINT(AST_Default, function(output) {
output.print("default:"); output.print("default:");
self._do_print_body(output); print_branch_body(this, output);
}); });
DEFPRINT(AST_Case, function(self, output) { DEFPRINT(AST_Case, function(output) {
var self = this;
output.print("case"); output.print("case");
output.space(); output.space();
self.expression.print(output); self.expression.print(output);
output.print(":"); output.print(":");
self._do_print_body(output); print_branch_body(self, output);
}); });
/* -----[ exceptions ]----- */ /* -----[ exceptions ]----- */
DEFPRINT(AST_Try, function(self, output) { DEFPRINT(AST_Try, function(output) {
var self = this;
output.print("try"); output.print("try");
output.space(); output.space();
print_braced(self, output); print_braced(self, output);
@@ -1121,7 +1120,8 @@ function OutputStream(options) {
self.bfinally.print(output); self.bfinally.print(output);
} }
}); });
DEFPRINT(AST_Catch, function(self, output) { DEFPRINT(AST_Catch, function(output) {
var self = this;
output.print("catch"); output.print("catch");
output.space(); output.space();
output.with_parens(function() { output.with_parens(function() {
@@ -1130,13 +1130,14 @@ function OutputStream(options) {
output.space(); output.space();
print_braced(self, output); print_braced(self, output);
}); });
DEFPRINT(AST_Finally, function(self, output) { DEFPRINT(AST_Finally, function(output) {
output.print("finally"); output.print("finally");
output.space(); output.space();
print_braced(self, output); print_braced(this, output);
}); });
DEFPRINT(AST_Var, function(self, output) { DEFPRINT(AST_Var, function(output) {
var self = this;
output.print("var"); output.print("var");
output.space(); output.space();
self.definitions.forEach(function(def, i) { self.definitions.forEach(function(def, i) {
@@ -1161,7 +1162,8 @@ function OutputStream(options) {
node.print(output, parens); node.print(output, parens);
} }
DEFPRINT(AST_VarDef, function(self, output) { DEFPRINT(AST_VarDef, function(output) {
var self = this;
self.name.print(output); self.name.print(output);
if (self.value) { if (self.value) {
output.space(); output.space();
@@ -1185,18 +1187,19 @@ function OutputStream(options) {
}); });
}); });
} }
DEFPRINT(AST_Call, function(self, output) { DEFPRINT(AST_Call, function(output) {
self.expression.print(output); this.expression.print(output);
print_call_args(self, output); print_call_args(this, output);
}); });
DEFPRINT(AST_New, function(self, output) { DEFPRINT(AST_New, function(output) {
var self = this;
output.print("new"); output.print("new");
output.space(); output.space();
self.expression.print(output); self.expression.print(output);
if (need_constructor_parens(self, output)) print_call_args(self, output); if (need_constructor_parens(self, output)) print_call_args(self, output);
}); });
DEFPRINT(AST_Sequence, function(self, output) { DEFPRINT(AST_Sequence, function(output) {
self.expressions.forEach(function(node, index) { this.expressions.forEach(function(node, index) {
if (index > 0) { if (index > 0) {
output.comma(); output.comma();
if (output.should_break()) { if (output.should_break()) {
@@ -1207,7 +1210,8 @@ function OutputStream(options) {
node.print(output); node.print(output);
}); });
}); });
DEFPRINT(AST_Dot, function(self, output) { DEFPRINT(AST_Dot, function(output) {
var self = this;
var expr = self.expression; var expr = self.expression;
expr.print(output); expr.print(output);
var prop = self.property; var prop = self.property;
@@ -1228,35 +1232,38 @@ function OutputStream(options) {
output.print_name(prop); output.print_name(prop);
} }
}); });
DEFPRINT(AST_Sub, function(self, output) { DEFPRINT(AST_Sub, function(output) {
self.expression.print(output); this.expression.print(output);
output.print("["); output.print("[");
self.property.print(output); this.property.print(output);
output.print("]"); output.print("]");
}); });
DEFPRINT(AST_UnaryPrefix, function(self, output) { DEFPRINT(AST_UnaryPrefix, function(output) {
var op = self.operator; var op = this.operator;
var exp = this.expression;
output.print(op); output.print(op);
if (/^[a-z]/i.test(op) if (/^[a-z]/i.test(op)
|| (/[+-]$/.test(op) || (/[+-]$/.test(op)
&& self.expression instanceof AST_UnaryPrefix && exp instanceof AST_UnaryPrefix
&& /^[+-]/.test(self.expression.operator))) { && /^[+-]/.test(exp.operator))) {
output.space(); output.space();
} }
self.expression.print(output); exp.print(output);
}); });
DEFPRINT(AST_UnaryPostfix, function(self, output) { DEFPRINT(AST_UnaryPostfix, function(output) {
self.expression.print(output); this.expression.print(output);
output.print(self.operator); output.print(this.operator);
}); });
DEFPRINT(AST_Binary, function(self, output) { DEFPRINT(AST_Binary, function(output) {
var self = this;
self.left.print(output); self.left.print(output);
output.space(); output.space();
output.print(self.operator); output.print(self.operator);
output.space(); output.space();
self.right.print(output); self.right.print(output);
}); });
DEFPRINT(AST_Conditional, function(self, output) { DEFPRINT(AST_Conditional, function(output) {
var self = this;
self.condition.print(output); self.condition.print(output);
output.space(); output.space();
output.print("?"); output.print("?");
@@ -1268,10 +1275,10 @@ function OutputStream(options) {
}); });
/* -----[ literals ]----- */ /* -----[ literals ]----- */
DEFPRINT(AST_Array, function(self, output) { DEFPRINT(AST_Array, function(output) {
output.with_square(function() { var a = this.elements, len = a.length;
var a = self.elements, len = a.length; output.with_square(len > 0 ? function() {
if (len > 0) output.space(); output.space();
a.forEach(function(exp, i) { a.forEach(function(exp, i) {
if (i) output.comma(); if (i) output.comma();
exp.print(output); exp.print(output);
@@ -1281,12 +1288,13 @@ function OutputStream(options) {
if (i === len - 1 && exp instanceof AST_Hole) if (i === len - 1 && exp instanceof AST_Hole)
output.comma(); output.comma();
}); });
if (len > 0) output.space(); output.space();
}); } : noop);
}); });
DEFPRINT(AST_Object, function(self, output) { DEFPRINT(AST_Object, function(output) {
if (self.properties.length > 0) output.with_block(function() { var props = this.properties;
self.properties.forEach(function(prop, i) { if (props.length > 0) output.with_block(function() {
props.forEach(function(prop, i) {
if (i) { if (i) {
output.print(","); output.print(",");
output.newline(); output.newline();
@@ -1296,7 +1304,7 @@ function OutputStream(options) {
}); });
output.newline(); output.newline();
}); });
else print_braced_empty(self, output); else print_braced_empty(this, output);
}); });
function print_property_name(key, quote, output) { function print_property_name(key, quote, output) {
@@ -1315,47 +1323,48 @@ function OutputStream(options) {
} }
} }
DEFPRINT(AST_ObjectKeyVal, function(self, output) { DEFPRINT(AST_ObjectKeyVal, function(output) {
var self = this;
print_property_name(self.key, self.quote, output); print_property_name(self.key, self.quote, output);
output.colon(); output.colon();
self.value.print(output); self.value.print(output);
}); });
AST_ObjectProperty.DEFMETHOD("_print_getter_setter", function(type, output) { function print_accessor(type) {
output.print(type); return function(output) {
output.space(); var self = this;
print_property_name(this.key.name, this.quote, output); output.print(type);
this.value._do_print(output, true); output.space();
}); print_property_name(self.key.name, self.quote, output);
DEFPRINT(AST_ObjectSetter, function(self, output) { self.value._codegen(output, true);
self._print_getter_setter("set", output); };
}); }
DEFPRINT(AST_ObjectGetter, function(self, output) { DEFPRINT(AST_ObjectGetter, print_accessor("get"));
self._print_getter_setter("get", output); DEFPRINT(AST_ObjectSetter, print_accessor("set"));
}); DEFPRINT(AST_Symbol, function(output) {
DEFPRINT(AST_Symbol, function(self, output) { var def = this.definition();
var def = self.definition(); output.print_name(def && def.mangled_name || this.name);
output.print_name(def && def.mangled_name || self.name);
}); });
DEFPRINT(AST_Hole, noop); DEFPRINT(AST_Hole, noop);
DEFPRINT(AST_This, function(self, output) { DEFPRINT(AST_This, function(output) {
output.print("this"); output.print("this");
}); });
DEFPRINT(AST_Constant, function(self, output) { DEFPRINT(AST_Constant, function(output) {
output.print(self.value); output.print(this.value);
}); });
DEFPRINT(AST_String, function(self, output) { DEFPRINT(AST_String, function(output) {
output.print_string(self.value, self.quote); output.print_string(this.value, this.quote);
}); });
DEFPRINT(AST_Number, function(self, output) { DEFPRINT(AST_Number, function(output) {
if (use_asm && self.start && self.start.raw != null) { var start = this.start;
output.print(self.start.raw); if (use_asm && start && start.raw != null) {
output.print(start.raw);
} else { } else {
output.print(make_num(self.value)); output.print(make_num(this.value));
} }
}); });
DEFPRINT(AST_RegExp, function(self, output) { DEFPRINT(AST_RegExp, function(output) {
var regexp = self.value; var regexp = this.value;
var str = regexp.toString(); var str = regexp.toString();
var end = str.lastIndexOf("/"); var end = str.lastIndexOf("/");
if (regexp.raw_source) { if (regexp.raw_source) {
@@ -1389,7 +1398,7 @@ function OutputStream(options) {
} }
})); }));
var p = output.parent(); var p = output.parent();
if (p instanceof AST_Binary && /^in/.test(p.operator) && p.left === self) if (p instanceof AST_Binary && /^in/.test(p.operator) && p.left === this)
output.print(" "); output.print(" ");
}); });

View File

@@ -110,7 +110,6 @@ function mangle_properties(ast, options) {
cache: null, cache: null,
debug: false, debug: false,
keep_quoted: false, keep_quoted: false,
only_cache: false,
regex: null, regex: null,
reserved: null, reserved: null,
}, true); }, true);
@@ -213,7 +212,6 @@ function mangle_properties(ast, options) {
function can_mangle(name) { function can_mangle(name) {
if (unmangleable[name]) return false; if (unmangleable[name]) return false;
if (options.only_cache) return cache.has(name);
if (/^-?[0-9]+(\.[0-9]+)?(e[+-][0-9]+)?$/.test(name)) return false; if (/^-?[0-9]+(\.[0-9]+)?(e[+-][0-9]+)?$/.test(name)) return false;
return true; return true;
} }

View File

@@ -96,8 +96,9 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
// pass 1: setup scope chaining and handle definitions // pass 1: setup scope chaining and handle definitions
var self = this; var self = this;
var scope = self.parent_scope = null;
var defun = null; var defun = null;
var next_def_id = 0;
var scope = self.parent_scope = null;
var tw = new TreeWalker(function(node, descend) { var tw = new TreeWalker(function(node, descend) {
if (node instanceof AST_Catch) { if (node instanceof AST_Catch) {
var save_scope = scope; var save_scope = scope;
@@ -149,7 +150,9 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) {
scope.def_variable(node).defun = defun; scope.def_variable(node).defun = defun;
} }
}); });
self.next_def_id = 0; self.make_def = function(orig, init) {
return new SymbolDef(++next_def_id, this, orig, init);
};
self.walk(tw); self.walk(tw);
// pass 2: find back references and eval // pass 2: find back references and eval
@@ -240,12 +243,6 @@ 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) { AST_Toplevel.DEFMETHOD("def_global", function(node) {
var globals = this.globals, name = node.name; var globals = this.globals, name = node.name;
if (globals.has(name)) { if (globals.has(name)) {
@@ -259,23 +256,28 @@ AST_Toplevel.DEFMETHOD("def_global", function(node) {
} }
}); });
function init_scope_vars(scope, parent) {
scope.cname = -1; // the current index for mangling functions/variables
scope.enclosed = []; // variables from this or outer scope(s) that are referenced from this or inner scopes
scope.uses_eval = false; // will be set to true if this or nested scope uses the global `eval`
scope.uses_with = false; // will be set to true if this or some nested scope uses the `with` statement
scope.parent_scope = parent; // the parent scope (null if this is the top level)
scope.functions = new Dictionary(); // map name to AST_SymbolDefun (functions defined in this scope)
scope.variables = new Dictionary(); // map name to AST_SymbolVar (variables defined in this scope; includes functions)
if (parent) scope.make_def = parent.make_def; // top-level tracking of SymbolDef instances
}
AST_Scope.DEFMETHOD("init_scope_vars", function(parent_scope) { AST_Scope.DEFMETHOD("init_scope_vars", function(parent_scope) {
this.variables = new Dictionary(); // map name to AST_SymbolVar (variables defined in this scope; includes functions) init_scope_vars(this, parent_scope);
this.functions = new Dictionary(); // map name to AST_SymbolDefun (functions defined in this scope)
this.uses_with = false; // will be set to true if this or some nested scope uses the `with` statement
this.uses_eval = false; // will be set to true if this or nested scope uses the global `eval`
this.parent_scope = parent_scope; // the parent scope
this.enclosed = []; // a list of variables from this or outer scope(s) that are referenced from this or inner scopes
this.cname = -1; // the current index for mangling functions/variables
}); });
AST_Lambda.DEFMETHOD("init_scope_vars", function() { AST_Lambda.DEFMETHOD("init_scope_vars", function(parent_scope) {
AST_Scope.prototype.init_scope_vars.apply(this, arguments); init_scope_vars(this, parent_scope);
this.uses_arguments = false; this.uses_arguments = false;
this.def_variable(new AST_SymbolFunarg({ this.def_variable(new AST_SymbolFunarg({
name: "arguments", name: "arguments",
start: this.start, start: this.start,
end: this.end end: this.end,
})); }));
}); });
@@ -387,18 +389,10 @@ AST_Symbol.DEFMETHOD("unmangleable", function(options) {
// labels are always mangleable // labels are always mangleable
AST_Label.DEFMETHOD("unmangleable", return_false); AST_Label.DEFMETHOD("unmangleable", return_false);
AST_Symbol.DEFMETHOD("unreferenced", function() {
return !this.definition().references.length && !this.scope.pinned();
});
AST_Symbol.DEFMETHOD("definition", function() { AST_Symbol.DEFMETHOD("definition", function() {
return this.thedef; return this.thedef;
}); });
AST_Symbol.DEFMETHOD("global", function() {
return this.definition().global;
});
function _default_mangler_options(options) { function _default_mangler_options(options) {
options = defaults(options, { options = defaults(options, {
eval : false, eval : false,
@@ -558,8 +552,8 @@ AST_Sequence.DEFMETHOD("tail_node", function() {
AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options) { AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options) {
options = _default_mangler_options(options); options = _default_mangler_options(options);
base54.reset(); base54.reset();
var fn = AST_Symbol.prototype.add_source_map;
try { try {
var fn = AST_Symbol.prototype.add_source_map;
AST_Symbol.prototype.add_source_map = function() { AST_Symbol.prototype.add_source_map = function() {
if (!this.unmangleable(options)) base54.consider(this.name, -1); if (!this.unmangleable(options)) base54.consider(this.name, -1);
}; };