From b4da85c26240f45b6e5925f31d02744c62f2075c Mon Sep 17 00:00:00 2001 From: Mike Sidorov Date: Mon, 19 Mar 2012 13:17:54 +0600 Subject: [PATCH 1/7] Shorten Number constants: Infinity and NaN --- lib/squeeze-more.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/squeeze-more.js b/lib/squeeze-more.js index 0908db3e..c9d5be3c 100644 --- a/lib/squeeze-more.js +++ b/lib/squeeze-more.js @@ -44,6 +44,30 @@ function ast_squeeze_more(ast) { } } }, + + // shorten Number constants: + // Number.POSITIVE_INFINITY to 1/0, Number.NEGATIVE_INFINITY to -1/0, Number.NaN to NaN + "dot": function(expr, name) { + if (expr[0] == "name" && expr[1] == "Number" && !scope.has("Number")) { + if (name == "POSITIVE_INFINITY") { + return [ "binary", "/", [ "num", 1 ], [ "num", 0 ] ]; + } + if (name == "NEGATIVE_INFINITY") { + return [ "binary", "/", [ "num", -1 ], [ "num", 0 ] ]; + } + if (name == "NaN" && !scope.has("NaN")) { + return [ "name", "NaN" ]; + } + } + }, + + // shorten Infinity constant to 1/0 + "name": function(name) { + if (name == "Infinity" && !scope.has("Infinity")) { + return [ "binary", "/", [ "num", 1 ], [ "num", 0 ] ]; + } + }, + "call": function(expr, args) { if (expr[0] == "dot" && expr[1][0] == "string" && args.length == 1 && (args[0][1] > 0 && expr[2] == "substring" || expr[2] == "substr")) { From 84f74c417024e15d964eae8850f5a945b2cbfa5a Mon Sep 17 00:00:00 2001 From: Mike Sidorov Date: Mon, 19 Mar 2012 13:31:18 +0600 Subject: [PATCH 2/7] Also correctly shortcut -Infinity --- lib/squeeze-more.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/squeeze-more.js b/lib/squeeze-more.js index c9d5be3c..04011001 100644 --- a/lib/squeeze-more.js +++ b/lib/squeeze-more.js @@ -64,7 +64,14 @@ function ast_squeeze_more(ast) { // shorten Infinity constant to 1/0 "name": function(name) { if (name == "Infinity" && !scope.has("Infinity")) { - return [ "binary", "/", [ "num", 1 ], [ "num", 0 ] ]; + return [ "binary", "/", [ "num", 1 ], [ "num", 0 ] ]; + } + }, + + // shorten -Infinity constant to -1/0 + "unary-prefix": function(op, expr) { + if (op == "-" && expr[0] == "name" && expr[1] == "Infinity" && !scope.has("Infinity")) { + return [ "binary", "/", [ "num", -1 ], [ "num", 0 ] ]; } }, From d902ac5564627a6c39d616373499d20f5e95e9e8 Mon Sep 17 00:00:00 2001 From: Mike Sidorov Date: Mon, 19 Mar 2012 13:46:33 +0600 Subject: [PATCH 3/7] Added unit tests --- test/unit/compress/expected/infinity.js | 1 + test/unit/compress/expected/number1.js | 1 + test/unit/compress/expected/number2.js | 1 + test/unit/compress/test/infinity.js | 8 ++++++++ test/unit/compress/test/number1.js | 3 +++ test/unit/compress/test/number2.js | 4 ++++ 6 files changed, 18 insertions(+) create mode 100644 test/unit/compress/expected/infinity.js create mode 100644 test/unit/compress/expected/number1.js create mode 100644 test/unit/compress/expected/number2.js create mode 100644 test/unit/compress/test/infinity.js create mode 100644 test/unit/compress/test/number1.js create mode 100644 test/unit/compress/test/number2.js diff --git a/test/unit/compress/expected/infinity.js b/test/unit/compress/expected/infinity.js new file mode 100644 index 00000000..e91a1ef3 --- /dev/null +++ b/test/unit/compress/expected/infinity.js @@ -0,0 +1 @@ +function test(){var a={};return a}1/0,-1/0; diff --git a/test/unit/compress/expected/number1.js b/test/unit/compress/expected/number1.js new file mode 100644 index 00000000..e2fb16bd --- /dev/null +++ b/test/unit/compress/expected/number1.js @@ -0,0 +1 @@ +1/0,-1/0,NaN diff --git a/test/unit/compress/expected/number2.js b/test/unit/compress/expected/number2.js new file mode 100644 index 00000000..3cc412a1 --- /dev/null +++ b/test/unit/compress/expected/number2.js @@ -0,0 +1 @@ +(function(){var a={};return a.POSITIVE_INFINITY||a.NEGATIVE_INFINITY||a.NaN})() diff --git a/test/unit/compress/test/infinity.js b/test/unit/compress/test/infinity.js new file mode 100644 index 00000000..2dfae699 --- /dev/null +++ b/test/unit/compress/test/infinity.js @@ -0,0 +1,8 @@ +Infinity; +-Infinity; + +function test() +{ + var Infinity = {}; + return Infinity; +} diff --git a/test/unit/compress/test/number1.js b/test/unit/compress/test/number1.js new file mode 100644 index 00000000..4753466a --- /dev/null +++ b/test/unit/compress/test/number1.js @@ -0,0 +1,3 @@ +Number.POSITIVE_INFINITY; +Number.NEGATIVE_INFINITY; +Number.NaN; diff --git a/test/unit/compress/test/number2.js b/test/unit/compress/test/number2.js new file mode 100644 index 00000000..9355cec4 --- /dev/null +++ b/test/unit/compress/test/number2.js @@ -0,0 +1,4 @@ +(function(){ + var Number = {}; + return Number.POSITIVE_INFINITY || Number.NEGATIVE_INFINITY || Number.NaN; +})(); From ac9cecdceae8e8cb452157329adc60c43a1dffa0 Mon Sep 17 00:00:00 2001 From: Mike Sidorov Date: Mon, 19 Mar 2012 13:48:38 +0600 Subject: [PATCH 4/7] Whitespace fix --- test/unit/compress/test/infinity.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/compress/test/infinity.js b/test/unit/compress/test/infinity.js index 2dfae699..363c616d 100644 --- a/test/unit/compress/test/infinity.js +++ b/test/unit/compress/test/infinity.js @@ -3,6 +3,6 @@ Infinity; function test() { - var Infinity = {}; - return Infinity; + var Infinity = {}; + return Infinity; } From 4f1fa12ceb93dc31a207fb8a425b12b1e422025a Mon Sep 17 00:00:00 2001 From: Mike Sidorov Date: Mon, 19 Mar 2012 16:04:58 +0600 Subject: [PATCH 5/7] Shortcut Number.NaN to 0/0 instead of NaN --- lib/squeeze-more.js | 2 +- test/unit/compress/expected/number1.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/squeeze-more.js b/lib/squeeze-more.js index 04011001..08dd106a 100644 --- a/lib/squeeze-more.js +++ b/lib/squeeze-more.js @@ -56,7 +56,7 @@ function ast_squeeze_more(ast) { return [ "binary", "/", [ "num", -1 ], [ "num", 0 ] ]; } if (name == "NaN" && !scope.has("NaN")) { - return [ "name", "NaN" ]; + return [ "binary", "/", [ "num", 0 ], [ "num", 0 ] ]; } } }, diff --git a/test/unit/compress/expected/number1.js b/test/unit/compress/expected/number1.js index e2fb16bd..9af5c591 100644 --- a/test/unit/compress/expected/number1.js +++ b/test/unit/compress/expected/number1.js @@ -1 +1 @@ -1/0,-1/0,NaN +1/0,-1/0,0/0 From 478640440b7faf7e8f09b78be65eb573ced15c5a Mon Sep 17 00:00:00 2001 From: Mike Sidorov Date: Tue, 20 Mar 2012 11:53:03 +0600 Subject: [PATCH 6/7] Small fixes on NaN --- lib/squeeze-more.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/squeeze-more.js b/lib/squeeze-more.js index 08dd106a..4592d704 100644 --- a/lib/squeeze-more.js +++ b/lib/squeeze-more.js @@ -46,7 +46,7 @@ function ast_squeeze_more(ast) { }, // shorten Number constants: - // Number.POSITIVE_INFINITY to 1/0, Number.NEGATIVE_INFINITY to -1/0, Number.NaN to NaN + // Number.POSITIVE_INFINITY to 1/0, Number.NEGATIVE_INFINITY to -1/0, Number.NaN to 0/0 "dot": function(expr, name) { if (expr[0] == "name" && expr[1] == "Number" && !scope.has("Number")) { if (name == "POSITIVE_INFINITY") { @@ -55,7 +55,7 @@ function ast_squeeze_more(ast) { if (name == "NEGATIVE_INFINITY") { return [ "binary", "/", [ "num", -1 ], [ "num", 0 ] ]; } - if (name == "NaN" && !scope.has("NaN")) { + if (name == "NaN") { return [ "binary", "/", [ "num", 0 ], [ "num", 0 ] ]; } } From 925cb6f647cab531db553d0c2fca994bd79b354d Mon Sep 17 00:00:00 2001 From: Mike Sidorov Date: Fri, 8 Jun 2012 12:12:09 +0600 Subject: [PATCH 7/7] Shorten -Infinity to 1/-0 instead of -1/0 --- lib/squeeze-more.js | 8 ++++---- test/unit/compress/expected/number1.js | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/squeeze-more.js b/lib/squeeze-more.js index 4592d704..cedf74c0 100644 --- a/lib/squeeze-more.js +++ b/lib/squeeze-more.js @@ -46,14 +46,14 @@ function ast_squeeze_more(ast) { }, // shorten Number constants: - // Number.POSITIVE_INFINITY to 1/0, Number.NEGATIVE_INFINITY to -1/0, Number.NaN to 0/0 + // Number.POSITIVE_INFINITY to 1/0, Number.NEGATIVE_INFINITY to 1/-0, Number.NaN to 0/0 "dot": function(expr, name) { if (expr[0] == "name" && expr[1] == "Number" && !scope.has("Number")) { if (name == "POSITIVE_INFINITY") { return [ "binary", "/", [ "num", 1 ], [ "num", 0 ] ]; } if (name == "NEGATIVE_INFINITY") { - return [ "binary", "/", [ "num", -1 ], [ "num", 0 ] ]; + return [ "binary", "/", [ "num", 1 ], [ "num", -0 ] ]; } if (name == "NaN") { return [ "binary", "/", [ "num", 0 ], [ "num", 0 ] ]; @@ -68,10 +68,10 @@ function ast_squeeze_more(ast) { } }, - // shorten -Infinity constant to -1/0 + // shorten -Infinity constant to 1/-0 "unary-prefix": function(op, expr) { if (op == "-" && expr[0] == "name" && expr[1] == "Infinity" && !scope.has("Infinity")) { - return [ "binary", "/", [ "num", -1 ], [ "num", 0 ] ]; + return [ "binary", "/", [ "num", 1 ], [ "num", -0 ] ]; } }, diff --git a/test/unit/compress/expected/number1.js b/test/unit/compress/expected/number1.js index 9af5c591..51a2a73b 100644 --- a/test/unit/compress/expected/number1.js +++ b/test/unit/compress/expected/number1.js @@ -1 +1 @@ -1/0,-1/0,0/0 +1/0,1/-0,0/0