diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-01-19 11:53:52 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-01-19 11:53:52 -0800 |
commit | b961c328f28daff5d1824ad4d04e076ab0ac7f34 (patch) | |
tree | ce8001773dcac0d4cb58108e9751beef4f32c367 /tools/js-optimizer.js | |
parent | 35cbbe82edf2173092592244aa342285fbcb5e44 (diff) |
emit 5.0 instead of +5 in asm
Diffstat (limited to 'tools/js-optimizer.js')
-rw-r--r-- | tools/js-optimizer.js | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index 69abe23a..8cd357cd 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -2149,6 +2149,19 @@ function eliminateMemSafe(ast) { eliminate(ast, true); } +// Change +5 to DOT$ZERO(5). We then textually change 5 to 5.0 (uglify's ast cannot differentiate between 5 and 5.0 directly) +function prepDotZero(ast) { + traverse(ast, function(node, type) { + if (type == 'unary-prefix' && node[1] == '+') { + if (node[2][0] == 'num') { + return ['call', ['name', 'DOT$ZERO'], [['num', node[2][1]]]]; + } else if (node[2][0] == 'unary-prefix' && node[2][1] == '-' && node[2][2][0] == 'num') { + return ['call', ['name', 'DOT$ZERO'], [['num', -node[2][2][1]]]]; + } + } + }); +} + // Passes table var compress = false, printMetadata = true, asm = false, last = false; @@ -2186,15 +2199,23 @@ if (metadata) setGeneratedFunctions(metadata); arguments_.slice(1).forEach(function(arg) { passes[arg](ast); }); +if (asm && last) { + prepDotZero(ast); +} var js = astToSrc(ast, compress), old; +if (asm && last) { + js = js.replace(/DOT\$ZERO\(([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)\)/g, function(m, num) { + if (num.indexOf('.') >= 0) return num; + var e = num.indexOf('e'); + if (e < 0) return num + '.0'; + return num.substr(0, e) + '.0' + num.substr(e); + }); +} // remove unneeded newlines+spaces, and print do { old = js; js = js.replace(/\n *\n/g, '\n'); - if (asm && last) { - js = js.replace(/ = \+0([,;])/g, function(m, end) { return ' = 0.0' + end }); // asm requires 0.0 in var definitions, not +0 - } } while (js != old); print(js); print('\n'); |