HaxeFoundation/haxe — pull request #2543 by nadako (2014-01-18 01:40:23+00:00)
Title: [js] optimize Std.is for common types

This is the second part of @deltaluca's JS optimization moved to api_inline as @ncannasse requested in #2255.

I added some unit tests to `Std.unit.hx` to ensure that nothing breaks.

I also added the generation of `__typeof__` next to `__instanceof__` that was already there, as well as `__strict_eq__` and `__strict_neq__` for JS strict equality operators. I believe some code in `Std/Boot/Type` could be ported to use those instead of `__js__` magic.

Main.hx
```haxe
class Main
{
static function main()
{
var x = 10;

trace(Std.is(x, Int));
trace(Std.is(x, Float));
trace(Std.is(x, String));
trace(Std.is(x, Bool));
trace(Std.is(x, Array));

trace(!Std.is(x, Int));
trace(!Std.is(x, Float));
trace(!Std.is(x, String));
trace(!Std.is(x, Bool));
trace(!Std.is(x, Array));
}
}
```

Generated before:
```js
(function () { "use strict";
var Main = function() { };
Main.__name__ = true;
Main.main = function() {
var x = 10;
console.log(js.Boot.__instanceof(x,Int));
console.log(js.Boot.__instanceof(x,Float));
console.log(js.Boot.__instanceof(x,String));
console.log(js.Boot.__instanceof(x,Bool));
console.log(js.Boot.__instanceof(x,Array));
console.log(!js.Boot.__instanceof(x,Int));
console.log(!js.Boot.__instanceof(x,Float));
console.log(!js.Boot.__instanceof(x,String));
console.log(!js.Boot.__instanceof(x,Bool));
console.log(!js.Boot.__instanceof(x,Array));
};
var js = {};
js.Boot = function() { };
js.Boot.__name__ = true;
js.Boot.__interfLoop = function(cc,cl) {
if(cc == null) return false;
if(cc == cl) return true;
var intf = cc.__interfaces__;
if(intf != null) {
var _g1 = 0;
var _g = intf.length;
while(_g1 < _g) {
var i = _g1++;
var i1 = intf[i];
if(i1 == cl || js.Boot.__interfLoop(i1,cl)) return true;
}
}
return js.Boot.__interfLoop(cc.__super__,cl);
};
js.Boot.__instanceof = function(o,cl) {
if(cl == null) return false;
switch(cl) {
case Int:
return (o|0) === o;
case Float:
return typeof(o) == "number";
case Bool:
return typeof(o) == "boolean";
case String:
return typeof(o) == "string";
case Dynamic:
return true;
default:
if(o != null) {
if(typeof(cl) == "function") {
if(o instanceof cl) {
if(cl == Array) return o.__enum__ == null;
return true;
}
if(js.Boot.__interfLoop(o.__class__,cl)) return true;
}
} else return false;
if(cl == Class && o.__name__ != null) return true;
if(cl == Enum && o.__ename__ != null) return true;
return o.__enum__ == cl;
}
};
String.prototype.__class__ = String;
String.__name__ = true;
Array.prototype.__class__ = Array;
Array.__name__ = true;
var Int = { __name__ : ["Int"]};
var Dynamic = { __name__ : ["Dynamic"]};
var Float = Number;
Float.__name__ = ["Float"];
var Bool = Boolean;
Bool.__ename__ = ["Bool"];
var Class = { __name__ : ["Class"]};
var Enum = { };
Main.main();
})();
```

Generated after:
```js
(function () { "use strict";
var Main = function() { };
Main.main = function() {
var x = 10;
console.log(((x | 0) === x));
console.log(typeof(x) == "number");
console.log(typeof(x) == "string");
console.log(typeof(x) == "boolean");
console.log((x instanceof Array) && x.__enum__ == null);
console.log(!((x | 0) === x));
console.log(!(typeof(x) == "number"));
console.log(!(typeof(x) == "string"));
console.log(!(typeof(x) == "boolean"));
console.log(!((x instanceof Array) && x.__enum__ == null));
};
Main.main();
})();
```