HaxeFoundation/haxe — pull request #2476 by nadako (2013-12-19 00:35:45+00:00)
Title: [js] Optimize Std.string for basic types

This is a part of @deltaluca's JS optimization for Std.string, moved to api_inline as @ncannasse requested in #2255.

Code:
```haxe
class Main {
static function main() {
var x = 10;
trace(Std.string(x));
var x:UInt = 10;
trace(Std.string(x));
var x = 10.0;
trace(Std.string(x));
var x = "10";
trace(Std.string(x));
var x = true;
trace(Std.string(x));
}
}
```

JS Before:
```js
(function () { "use strict";
var Main = function() { };
Main.__name__ = true;
Main.main = function() {
var x = 10;
console.log(Std.string(x));
var x1 = 10;
console.log(Std.string(x1));
var x2 = 10.0;
console.log(Std.string(x2));
var x3 = "10";
console.log(Std.string(x3));
var x4 = true;
console.log(Std.string(x4));
};
var Std = function() { };
Std.__name__ = true;
Std.string = function(s) {
return js.Boot.__string_rec(s,"");
};
var js = {};
js.Boot = function() { };
js.Boot.__name__ = true;
js.Boot.__string_rec = function(o,s) {
if(o == null) return "null";
if(s.length >= 5) return "<...>";
var t = typeof(o);
if(t == "function" && (o.__name__ || o.__ename__)) t = "object";
switch(t) {
case "object":
if(o instanceof Array) {
if(o.__enum__) {
if(o.length == 2) return o[0];
var str = o[0] + "(";
s += "\t";
var _g1 = 2;
var _g = o.length;
while(_g1 < _g) {
var i = _g1++;
if(i != 2) str += "," + js.Boot.__string_rec(o[i],s); else str += js.Boot.__string_rec(o[i],s);
}
return str + ")";
}
var l = o.length;
var i;
var str = "[";
s += "\t";
var _g = 0;
while(_g < l) {
var i1 = _g++;
str += (i1 > 0?",":"") + js.Boot.__string_rec(o[i1],s);
}
str += "]";
return str;
}
var tostr;
try {
tostr = o.toString;
} catch( e ) {
return "???";
}
if(tostr != null && tostr != Object.toString) {
var s2 = o.toString();
if(s2 != "[object Object]") return s2;
}
var k = null;
var str = "{\n";
s += "\t";
var hasp = o.hasOwnProperty != null;
for( var k in o ) {
if(hasp && !o.hasOwnProperty(k)) {
continue;
}
if(k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" || k == "__properties__") {
continue;
}
if(str.length != 2) str += ", \n";
str += s + k + " : " + js.Boot.__string_rec(o[k],s);
}
s = s.substring(1);
str += "\n" + s + "}";
return str;
case "function":
return "";
case "string":
return o;
default:
return String(o);
}
};
String.__name__ = true;
Array.__name__ = true;
Main.main();
})();
```

JS After:
```js
(function () { "use strict";
var Main = function() { };
Main.main = function() {
var x = 10;
console.log("" + x);
var x1 = 10;
console.log("" + x1);
var x2 = 10.0;
console.log("" + x2);
var x3 = "10";
console.log(x3);
var x4 = true;
console.log("" + x4);
};
Main.main();
})();
```