diff options
author | Alon Zakai <alonzakai@gmail.com> | 2013-10-09 13:22:24 -0400 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2013-10-09 13:22:24 -0400 |
commit | 028a3210fc61df40fae2854be6ad8b65e49ba633 (patch) | |
tree | 7ca87e0f4137cc520ffb6b7c7359adfec8380780 | |
parent | e10ee6c5cb0aa89bbae465a1e667a03c0e13aa39 (diff) |
avoid X-(-Y) in js optimizer; fixes #1685
-rw-r--r-- | tests/test_other.py | 2 | ||||
-rw-r--r-- | tools/js-optimizer.js | 26 | ||||
-rw-r--r-- | tools/test-js-optimizer-asm-minlast-output.js | 2 | ||||
-rw-r--r-- | tools/test-js-optimizer-asm-minlast.js | 8 |
4 files changed, 33 insertions, 5 deletions
diff --git a/tests/test_other.py b/tests/test_other.py index 81221229..d4a5bb3a 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -1645,6 +1645,8 @@ f.close() ['asm', 'outline']), (path_from_root('tools', 'test-js-optimizer-asm-outline3.js'), open(path_from_root('tools', 'test-js-optimizer-asm-outline3-output.js')).read(), ['asm', 'outline']), + (path_from_root('tools', 'test-js-optimizer-asm-minlast.js'), open(path_from_root('tools', 'test-js-optimizer-asm-minlast-output.js')).read(), + ['asm', 'minifyWhitespace', 'last']), ]: print input output = Popen(listify(NODE_JS) + [path_from_root('tools', 'js-optimizer.js'), input] + passes, stdin=PIPE, stdout=PIPE).communicate()[0] diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js index e5e0d287..022bdf47 100644 --- a/tools/js-optimizer.js +++ b/tools/js-optimizer.js @@ -3788,11 +3788,27 @@ function asmLastOpts(ast) { node[1] = simplifyNotCompsDirect(['unary-prefix', '!', conditionToBreak]); return node; } - } else if (type == 'binary' && node[1] == '&' && node[3][0] == 'unary-prefix' && node[3][1] == '-' && node[3][2][0] == 'num' && node[3][2][1] == 1) { - // Change &-1 into |0, at this point the hint is no longer needed - node[1] = '|'; - node[3] = node[3][2]; - node[3][1] = 0; + } else if (type == 'binary') { + if (node[1] === '&') { + if (node[3][0] === 'unary-prefix' && node[3][1] === '-' && node[3][2][0] === 'num' && node[3][2][1] === 1) { + // Change &-1 into |0, at this point the hint is no longer needed + node[1] = '|'; + node[3] = node[3][2]; + node[3][1] = 0; + } + } else if (node[1] === '-' && node[3][0] === 'unary-prefix') { + // avoid X - (-Y) because some minifiers buggily emit X--Y which is invalid as -- can be a unary. Transform to + // X + Y + if (node[3][1] === '-') { // integer + node[1] = '+'; + node[3] = node[3][2]; + } else if (node[3][1] === '+') { // float + if (node[3][2][0] === 'unary-prefix' && node[3][2][1] === '-') { + node[1] = '+'; + node[3][2] = node[3][2][2]; + } + } + } } }); }); diff --git a/tools/test-js-optimizer-asm-minlast-output.js b/tools/test-js-optimizer-asm-minlast-output.js new file mode 100644 index 00000000..d25c6d9b --- /dev/null +++ b/tools/test-js-optimizer-asm-minlast-output.js @@ -0,0 +1,2 @@ +function test($34){var $35=0;$35=$34+130.0;$35=$34+130;return $35|0} + diff --git a/tools/test-js-optimizer-asm-minlast.js b/tools/test-js-optimizer-asm-minlast.js new file mode 100644 index 00000000..6d172899 --- /dev/null +++ b/tools/test-js-optimizer-asm-minlast.js @@ -0,0 +1,8 @@ +function test($34) { + var $35 = 0; + $35=($34)-((+-130)); + $35=($34)-(-130); + return $35 | 0; +} +// EMSCRIPTEN_GENERATED_FUNCTIONS: ["test"] + |