Math in JS generates uneused code

66 views
Skip to first unread message

Porfírio José Pereira Ribeiro

unread,
May 5, 2013, 11:24:49 AM5/5/13
to haxe...@googlegroups.com
Hi.

When you use some functions of the Math class it generates a bunch of unnecessary code

Example:

class Main {    
   
static function main()  {
        trace
(Math.round(10.5));
   
}
}

Will generate:
Main.main = function() {
    console
.log(Math.round(10.5));
}
Math.__name__ = ["Math"];
Math.NaN = Number.NaN;
Math.NEGATIVE_INFINITY = Number.NEGATIVE_INFINITY;
Math.POSITIVE_INFINITY = Number.POSITIVE_INFINITY;
Math.isFinite = function(i) {
   
return isFinite(i);
};
Math.isNaN = function(i) {
   
return isNaN(i);
};

Those functions are not used. and even if used they should be inlined.

Bruno Garcia

unread,
May 6, 2013, 2:56:29 PM5/6/13
to haxe...@googlegroups.com
On 05/05/2013 08:24 AM, Porf�rio Jos� Pereira Ribeiro wrote:
> When you use some functions of the Math class it generates a bunch of
> unnecessary code

Fix in the works, keep an eye on
https://code.google.com/p/haxe/issues/detail?id=1666.

It might be too late for 3.0 at this point, but certainly for 3.2.

Bruno

Porfírio José Pereira Ribeiro

unread,
May 6, 2013, 4:24:45 PM5/6/13
to haxe...@googlegroups.com, b...@aduros.com
Great, hope it gets included soon!

There are other variables that could also change like the js.Browser
For example js.Browser.document includes code to check if window.document is undefined and makes it null if so.
I dont see in JS anyone checking if document exists or not.
If i am targeting JS and using a class called Browser its because my code is to run in a browser and that variable will exist.

Again its generating unneded code and makes it more hard to read.

I usualy have my own Browser class with this:
class Browser {
   
public static var window(get,null) :js.html.DOMWindow;
   
static inline function get_window():js.html.DOMWindow return untyped __js__("window");
   
public static var document(get,null) :js.html.Document;
   
static inline function get_document():js.html.Document return untyped __js__("document");
   
public static var location(get,null) :js.html.Location;
   
static inline function get_location():js.html.Location return untyped __js__("location");        
   
public static var navigator(get,null) :js.html.Navigator;
   
static inline function get_navigator():js.html.Navigator return untyped __js__("navigator");    
   
public static var console(get, null):js.html.Console;
   
static inline function get_console():js.html.Console return untyped __js__("console");
   
public static var performance(get, null):js.html.Performance;
   
static inline function get_performance():js.html.Performance return untyped __js__("performance");    
}

With this code using Browser.document simply generates document. It makes the code smaller and looks more like plain JS!

I am aware that (Browser.document==null) will always be false. But i dont really see people checking existance of that variable anyway.
Might be wrong...



Segunda-feira, 6 de Maio de 2013 19:56:29 UTC+1, Bruno Garcia escreveu:

Bruno Garcia

unread,
May 6, 2013, 4:55:51 PM5/6/13
to haxe...@googlegroups.com
On 05/06/2013 01:24 PM, Porf�rio Jos� Pereira Ribeiro wrote:
> With this code using Browser.document simply generates document. It
> makes the code smaller and looks more like plain JS!

Haxe's JS output also works in NodeJS and other environments where
window doesn't exist. The typeof check is required to prevent a
ReferenceError on those environments. Even if the class is called
Browser, we don't want apps to blow up just because you imported it.

We also can't have inline getters like your sample, because it does the
wrong thing if you have a local variable with the same name:

function foo () {
var document = 666;

// TypeError: 666.getElementById is not a function
Browser.document.getElementById("kaboom");
}

Bruno

Porfírio Ribeiro

unread,
May 6, 2013, 5:28:53 PM5/6/13
to haxe...@googlegroups.com
Ohh, i realy haven't tought about that. You are right.

2013/5/6 Bruno Garcia <b...@aduros.com>
    }

Bruno

--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/groups/opt_out.





--
Porfirio Ribeiro
Reply all
Reply to author
Forward
0 new messages