Std.hx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright (C)2005-2015 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. import js.Boot;
  23. @:keepInit
  24. @:coreApi class Std {
  25. public static inline function is( v : Dynamic, t : Dynamic ) : Bool {
  26. return untyped js.Boot.__instanceof(v,t);
  27. }
  28. public static inline function instance<T:{},S:T>( value : T, c : Class<S> ) : S {
  29. return untyped __instanceof__(value, c) ? cast value : null;
  30. }
  31. public static function string( s : Dynamic ) : String {
  32. return untyped js.Boot.__string_rec(s,"");
  33. }
  34. public static inline function int( x : Float ) : Int {
  35. return (cast x) | 0;
  36. }
  37. public static function parseInt( x : String ) : Null<Int> {
  38. var v = untyped __js__("parseInt")(x, 10);
  39. // parse again if hexadecimal
  40. if( v == 0 && (x.charCodeAt(1) == 'x'.code || x.charCodeAt(1) == 'X'.code) )
  41. v = untyped __js__("parseInt")(x);
  42. if( untyped __js__("isNaN")(v) )
  43. return null;
  44. return cast v;
  45. }
  46. public static inline function parseFloat( x : String ) : Float {
  47. return untyped __js__("parseFloat")(x);
  48. }
  49. public static function random( x : Int ) : Int {
  50. return untyped x <= 0 ? 0 : Math.floor(Math.random()*x);
  51. }
  52. static function __init__() : Void untyped {
  53. __feature__("js.Boot.getClass",String.prototype.__class__ = __feature__("Type.resolveClass",$hxClasses["String"] = String,String));
  54. __feature__("js.Boot.isClass",String.__name__ = __feature__("Type.getClassName",["String"],true));
  55. __feature__("Type.resolveClass",$hxClasses["Array"] = Array);
  56. __feature__("js.Boot.isClass",Array.__name__ = __feature__("Type.getClassName",["Array"],true));
  57. __feature__("Date.*", {
  58. __feature__("js.Boot.getClass",__js__('Date').prototype.__class__ = __feature__("Type.resolveClass",$hxClasses["Date"] = __js__('Date'),__js__('Date')));
  59. __feature__("js.Boot.isClass",__js__('Date').__name__ = ["Date"]);
  60. });
  61. __feature__("Int.*",{
  62. var Int = __feature__("Type.resolveClass", $hxClasses["Int"] = { __name__ : ["Int"] }, { __name__ : ["Int"] });
  63. });
  64. __feature__("Dynamic.*",{
  65. var Dynamic = __feature__("Type.resolveClass", $hxClasses["Dynamic"] = { __name__ : ["Dynamic"] }, { __name__ : ["Dynamic"] });
  66. });
  67. __feature__("Float.*",{
  68. var Float = __feature__("Type.resolveClass", $hxClasses["Float"] = __js__("Number"), __js__("Number"));
  69. Float.__name__ = ["Float"];
  70. });
  71. __feature__("Bool.*",{
  72. var Bool = __feature__("Type.resolveEnum",$hxClasses["Bool"] = __js__("Boolean"), __js__("Boolean"));
  73. Bool.__ename__ = ["Bool"];
  74. });
  75. __feature__("Class.*",{
  76. var Class = __feature__("Type.resolveClass", $hxClasses["Class"] = { __name__ : ["Class"] }, { __name__ : ["Class"] });
  77. });
  78. __feature__("Enum.*",{
  79. var Enum = {};
  80. });
  81. __feature__("Void.*",{
  82. var Void = __feature__("Type.resolveEnum", $hxClasses["Void"] = { __ename__ : ["Void"] }, { __ename__ : ["Void"] });
  83. });
  84. #if !js_es5
  85. __feature__("Array.map",
  86. if( Array.prototype.map == null )
  87. Array.prototype.map = function(f) {
  88. var a = [];
  89. for( i in 0...__this__.length )
  90. a[i] = f(__this__[i]);
  91. return a;
  92. }
  93. );
  94. __feature__("Array.filter",
  95. if( Array.prototype.filter == null )
  96. Array.prototype.filter = function(f) {
  97. var a = [];
  98. for( i in 0...__this__.length ) {
  99. var e = __this__[i];
  100. if( f(e) ) a.push(e);
  101. }
  102. return a;
  103. }
  104. );
  105. #end
  106. }
  107. }