diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-06-26 10:53:17 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-06-26 10:53:17 -0700 |
commit | 501022cbaa9e63cab9cc218ee0239256945a3061 (patch) | |
tree | fbad1d122eff2e22707552bff81b35fe2df4a621 /tools/eliminator | |
parent | 493d0dcf303d66726be5bb08c187f8183ee96a65 (diff) | |
parent | 5383aa8bf93e5c9fed3f67853b2675ab2be10493 (diff) |
Merge branch 'source-maps' of github.com:int3/emscripten into int3-source-maps
Conflicts:
tools/js-optimizer.js
Diffstat (limited to 'tools/eliminator')
-rw-r--r-- | tools/eliminator/node_modules/uglify-js/lib/parse-js.js | 32 | ||||
-rw-r--r-- | tools/eliminator/node_modules/uglify-js/lib/process.js | 72 |
2 files changed, 88 insertions, 16 deletions
diff --git a/tools/eliminator/node_modules/uglify-js/lib/parse-js.js b/tools/eliminator/node_modules/uglify-js/lib/parse-js.js index a89163c6..c7c2025f 100644 --- a/tools/eliminator/node_modules/uglify-js/lib/parse-js.js +++ b/tools/eliminator/node_modules/uglify-js/lib/parse-js.js @@ -679,7 +679,28 @@ function NodeWithToken(str, start, end) { this.end = end; }; -NodeWithToken.prototype.toString = function() { return this.name; }; +NodeWithToken.prototype = { + get length() { + return this.name.length; + }, + set length(v) { + return this.name.length = v; + }, + replace: function() { return this.name.replace.apply(this.name, arguments); }, + concat: function() { return this.name.concat.apply(this.name, arguments); }, + indexOf: function() { return this.name.indexOf.apply(this.name, arguments); }, + lastIndexOf: function() { return this.name.lastIndexOf.apply(this.name, arguments); }, + lastIndexOf: function() { return this.name.lastIndexOf.apply(this.name, arguments); }, + match: function() { return this.name.match.apply(this.name, arguments); }, + search: function() { return this.name.search.apply(this.name, arguments); }, + slice: function() { return this.name.slice.apply(this.name, arguments); }, + split: function() { return this.name.split.apply(this.name, arguments); }, + substr: function() { return this.name.substr.apply(this.name, arguments); }, + substring: function() { return this.name.substring.apply(this.name, arguments); }, + toString: function() { return this.name; }, + toJSON: function() { return this.name; }, + valueOf: function() { return this.name; }, +}; function parse($TEXT, exigent_mode, embed_tokens) { @@ -765,15 +786,12 @@ function parse($TEXT, exigent_mode, embed_tokens) { return ex; }; - function add_tokens(str, start, end) { - return str instanceof NodeWithToken ? str : new NodeWithToken(str, start, end); - }; - function maybe_embed_tokens(parser) { if (embed_tokens) return function() { var start = S.token; var ast = parser.apply(this, arguments); - ast[0] = add_tokens(ast[0], start, prev()); + ast.start = start; + ast.end = prev; return ast; }; else return parser; @@ -1314,7 +1332,7 @@ function characters(str) { function member(name, array) { for (var i = array.length; --i >= 0;) - if (array[i] === name) + if (array[i] == name) return true; return false; }; diff --git a/tools/eliminator/node_modules/uglify-js/lib/process.js b/tools/eliminator/node_modules/uglify-js/lib/process.js index c3abb6f8..e1d692b2 100644 --- a/tools/eliminator/node_modules/uglify-js/lib/process.js +++ b/tools/eliminator/node_modules/uglify-js/lib/process.js @@ -64,6 +64,19 @@ var jsp = require("./parse-js"), PRECEDENCE = jsp.PRECEDENCE, OPERATORS = jsp.OPERATORS; +function NodeWithLine(str, line) { + this.str = str; + this.line = line; +} + +NodeWithLine.prototype = new String(); +NodeWithLine.prototype.toString = function() { return this.str; } +NodeWithLine.prototype.valueOf = function() { return this.str; } +NodeWithLine.prototype.lineComment = function() { return " //@line " + this.line; } + +// XXX ugly hack +String.prototype.lineComment = function() { return ""; } + /* -----[ helper for AST traversal ]----- */ function ast_walker() { @@ -1363,6 +1376,7 @@ var SPLICE_NEEDS_BRACKETS = jsp.array_to_hash([ "if", "while", "do", "for", "for function gen_code(ast, options) { options = defaults(options, { + debug: false, indent_start : 0, indent_level : 4, quote_keys : false, @@ -1422,7 +1436,19 @@ function gen_code(ast, options) { }; function add_commas(a) { - return a.join("," + space); + var str = a.join("," + space); + if (options.debug) { + // if a line contains more than one comma-separated segment, assign it the + // original line number of the first NodeWithLine segment + for (var i = 0, l = a.length; i < l; i ++) { + var v = a[i]; + if (v instanceof NodeWithLine) { + v.str = str; + return v + } + } + } + return str; }; function parenthesize(expr) { @@ -1484,7 +1510,10 @@ function gen_code(ast, options) { a.push(m[2] + "e-" + (m[1].length + m[2].length), str.substr(str.indexOf("."))); } - return best_of(a); + var best = best_of(a); + if (options.debug && this.start) + return new NodeWithLine(best, this.start.line); + return best; }; var w = ast_walker(); @@ -1512,7 +1541,15 @@ function gen_code(ast, options) { }, "block": make_block, "var": function(defs) { - return "var " + add_commas(MAP(defs, make_1vardef)) + ";"; + var s = "var " + add_commas(MAP(defs, make_1vardef)) + ";"; + if (options.debug) { + // hack: we don't support mapping one optimized line to more than one + // generated line, so in case of multiple comma-separated var definitions, + // just take the first + if (defs[0][1] && defs[0][1].start) + return s + (new NodeWithLine(s, defs[0][1].start.line)).lineComment(); + } + return s; }, "const": function(defs) { return "const " + add_commas(MAP(defs, make_1vardef)) + ";"; @@ -1567,7 +1604,10 @@ function gen_code(ast, options) { "assign": function(op, lvalue, rvalue) { if (op && op !== true) op += "="; else op = "="; - return add_spaces([ make(lvalue), op, parenthesize(rvalue, "seq") ]); + var s = add_spaces([ make(lvalue), op, parenthesize(rvalue, "seq") ]); + if (options.debug && this.start) + return new NodeWithLine(s, this.start.line); + return s; }, "dot": function(expr) { var out = make(expr), i = 1; @@ -1584,9 +1624,12 @@ function gen_code(ast, options) { var f = make(func); if (needs_parens(func)) f = "(" + f + ")"; - return f + "(" + add_commas(MAP(args, function(expr){ + var str = f + "(" + add_commas(MAP(args, function(expr){ return parenthesize(expr, "seq"); })) + ")"; + if (options.debug && this.start) + return new NodeWithLine(str, this.start.line) + return str; }, "function": make_function, "defun": make_function, @@ -1621,8 +1664,9 @@ function gen_code(ast, options) { }, "return": function(expr) { var out = [ "return" ]; - if (expr != null) out.push(make(expr)); - return add_spaces(out) + ";"; + var str = make(expr); + if (expr != null) out.push(str); + return add_spaces(out) + ";" + (str ? str.lineComment() : ''); }, "binary": function(operator, lvalue, rvalue) { var left = make(lvalue), right = make(rvalue); @@ -1642,7 +1686,16 @@ function gen_code(ast, options) { && rvalue[0] == "regexp" && /^script/i.test(rvalue[1])) { right = " " + right; } - return add_spaces([ left, operator, right ]); + var str = add_spaces([ left, operator, right ]); + if (options.debug) { + if (this.start) + return new NodeWithLine(str, this.start.line); + else if (lvalue.start) + return new NodeWithLine(str, lvalue.start.line); + else if (rvalue.start) + return new NodeWithLine(str, rvalue.start.line); + } + return str; }, "unary-prefix": function(operator, expr) { var val = make(expr); @@ -1698,7 +1751,8 @@ function gen_code(ast, options) { })), "]" ]); }, "stat": function(stmt) { - return make(stmt).replace(/;*\s*$/, ";"); + var str = make(stmt); + return str.replace(/;*\s*$/, ";") + str.lineComment(); }, "seq": function() { return add_commas(MAP(slice(arguments), make)); |