From 1b5183dd5ee7d2b147f5862c9abf8f45f50fc82f Mon Sep 17 00:00:00 2001 From: Mihai Bazon Date: Tue, 4 Sep 2012 15:36:14 +0300 Subject: checkpoint --- lib/compress.js | 36 ++++++++++++++++++++++++++++++------ lib/scope.js | 12 ++++++++++++ lib/utils.js | 5 ++--- tmp/test-node.js | 2 +- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 8b826bdd..8db05333 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -64,6 +64,7 @@ function Compressor(options, false_by_default) { evaluate : !false_by_default, booleans : !false_by_default, dwloops : !false_by_default, + hoist : !false_by_default, warnings : true }); @@ -140,7 +141,7 @@ function Compressor(options, false_by_default) { statements = eliminate_dead_code(statements, compressor); } if (compressor.option("sequences")) { - statements = sequencesize(statements); + statements = sequencesize(statements, compressor); } return statements; }; @@ -157,7 +158,7 @@ function Compressor(options, false_by_default) { } return a; }, []); - } + }; function eliminate_dead_code(statements, compressor) { var has_quit = false; @@ -194,7 +195,7 @@ function Compressor(options, false_by_default) { } return a; }, []); - } + }; // XXX: this is destructive -- it modifies tree nodes. function sequencesize(statements) { @@ -227,7 +228,7 @@ function Compressor(options, false_by_default) { return a; }, []); return statements; - } + }; /* -----[ boolean/negation helpers ]----- */ @@ -479,6 +480,18 @@ function Compressor(options, false_by_default) { return self; }); + SQUEEZE(AST_Scope, function(self, compressor){ + self = self.clone(); + self.hoist_declarations(compressor); + self.body = tighten_body(self.body, compressor); + return self; + }); + + AST_Scope.DEFMETHOD("hoist_declarations", function(compressor){ + if (compressor.option("hoist")) { + } + }); + SQUEEZE(AST_EmptyStatement, function(self, compressor){ return self; }); @@ -559,6 +572,7 @@ function Compressor(options, false_by_default) { // “has no side effects”; also it doesn't work for cases like // `x && true`, though it probably should. var cond = self.condition.evaluate(compressor); + self.condition = cond[0]; if (cond.length == 2) { if (cond[1]) { AST_Node.warn("Condition always true [{line},{col}]", self.condition.start); @@ -568,6 +582,13 @@ function Compressor(options, false_by_default) { return self.alternative || make_node(AST_EmptyStatement, self); } } + if (self.condition instanceof AST_UnaryPrefix + && self.condition.operator == "!") { + self.condition = self.condition.expression; + var tmp = self.body; + self.body = self.alternative || make_node(AST_EmptyStatement, self); + self.alternative = tmp; + } if (self.body instanceof AST_SimpleStatement && self.alternative instanceof AST_SimpleStatement) { return make_node(AST_SimpleStatement, self, { @@ -578,7 +599,9 @@ function Compressor(options, false_by_default) { }).optimize(compressor) }); } - if ((!self.alternative || self.alternative instanceof AST_EmptyStatement) && self.body instanceof AST_SimpleStatement) { + if ((!self.alternative + || self.alternative instanceof AST_EmptyStatement) + && self.body instanceof AST_SimpleStatement) { return make_node(AST_SimpleStatement, self, { body: make_node(AST_Binary, self, { operator : "&&", @@ -589,7 +612,7 @@ function Compressor(options, false_by_default) { } if (self.body instanceof AST_EmptyStatement && self.alternative - && !(self.alternative instanceof AST_EmptyStatement)) { + && self.alternative instanceof AST_SimpleStatement) { return make_node(AST_SimpleStatement, self, { body: make_node(AST_Binary, self, { operator : "||", @@ -656,6 +679,7 @@ function Compressor(options, false_by_default) { SQUEEZE(AST_Lambda, function(self, compressor){ self = self.clone(); + self.hoist_declarations(compressor); if (self.name) self.name = self.name.squeeze(compressor); self.argnames = do_list(self.argnames, compressor); self.body = self.body.squeeze(compressor); diff --git a/lib/scope.js b/lib/scope.js index 9c2f8530..0f9969d2 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -165,6 +165,7 @@ AST_Toplevel.DEFMETHOD("scope_warnings", function(options){ unreferenced : true, assign_to_global : true, func_arguments : true, + nested_defuns : true, eval : true }); var tw = new TreeWalker(function(node){ @@ -225,6 +226,17 @@ AST_Toplevel.DEFMETHOD("scope_warnings", function(options){ col: node.start.col }); } + if (options.nested_defuns + && node instanceof AST_Defun + && !(tw.parent() instanceof AST_Scope + || (tw.parent() instanceof AST_BlockStatement + && tw.parent(1) instanceof AST_Scope))) { + AST_Node.warn("Function {name} declared in nested statement [{line},{col}]", { + name: node.name.name, + line: node.start.line, + col: node.start.col + }); + } }); this.walk(tw); }); diff --git a/lib/utils.js b/lib/utils.js index dbd0079f..519e6962 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -145,9 +145,8 @@ var MAP = (function(){ // feeling it works well in general for many scripts (well, better // than alphabetical order). It would be nice if we could adapt it to // the currently running script. -// var BASE54_DIGITS = "etnrisouaflchpdvmgybwESxTNCkLAOM_DPHBjFIqRUzWXV$JKQGYZ0516372984"; - -var BASE54_DIGITS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789"; +var BASE54_DIGITS = "etnrisouaflchpdvmgybwESxTNCkLAOM_DPHBjFIqRUzWXV$JKQGYZ0516372984"; +//var BASE54_DIGITS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_0123456789"; function base54(num) { var ret = "", base = 54; diff --git a/tmp/test-node.js b/tmp/test-node.js index ce03b591..d737afde 100755 --- a/tmp/test-node.js +++ b/tmp/test-node.js @@ -22,7 +22,7 @@ time_it("scope", function(){ ast.figure_out_scope(); }); -// ast.scope_warnings(); +ast.scope_warnings(); time_it("mangle", function(){ ast.mangle_names(); -- cgit v1.2.3