Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storten Number and Infinity constants #340

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions lib/squeeze-more.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,37 @@ function ast_squeeze_more(ast) {
}
}
},

// shorten Number constants:
// Number.POSITIVE_INFINITY to 1/0, Number.NEGATIVE_INFINITY to -1/0, Number.NaN to NaN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ] ];
}
if (name == "NaN" && !scope.has("NaN")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to check whether NaN is bound in a Lexical Environment.

return [ "binary", "/", [ "num", 0 ], [ "num", 0 ] ];
}
}
},

// shorten Infinity constant to 1/0
"name": function(name) {
if (name == "Infinity" && !scope.has("Infinity")) {
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 ] ];
}
},

"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")) {
Expand Down
1 change: 1 addition & 0 deletions test/unit/compress/expected/infinity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
function test(){var a={};return a}1/0,-1/0;
1 change: 1 addition & 0 deletions test/unit/compress/expected/number1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1/0,-1/0,0/0
1 change: 1 addition & 0 deletions test/unit/compress/expected/number2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(function(){var a={};return a.POSITIVE_INFINITY||a.NEGATIVE_INFINITY||a.NaN})()
8 changes: 8 additions & 0 deletions test/unit/compress/test/infinity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Infinity;
-Infinity;

function test()
{
var Infinity = {};
return Infinity;
}
3 changes: 3 additions & 0 deletions test/unit/compress/test/number1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Number.POSITIVE_INFINITY;
Number.NEGATIVE_INFINITY;
Number.NaN;
4 changes: 4 additions & 0 deletions test/unit/compress/test/number2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(function(){
var Number = {};
return Number.POSITIVE_INFINITY || Number.NEGATIVE_INFINITY || Number.NaN;
})();