Std.hx 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 lua.Boot;
  23. @:keepInit
  24. @:coreApi class Std {
  25. public static inline function is( v : Dynamic, t : Dynamic ) : Bool {
  26. return untyped lua.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. inline public static function string( s : Dynamic ) : String {
  32. return untyped lua.Boot.__string_rec(s,"");
  33. }
  34. public static inline function int( x : Float ) : Int {
  35. return Math.floor(x + .5);
  36. }
  37. public static function parseInt( x : String ) : Null<Int> {
  38. var v = null;
  39. if( v == 0 && (x.charCodeAt(1) == 'x'.code || x.charCodeAt(1) == 'X'.code) )
  40. v = Std.int(untyped tonumber(x,16));
  41. else
  42. v = Std.int(untyped tonumber(x));
  43. if(Math.isNaN(v)) return null;
  44. return cast v;
  45. }
  46. public static inline function parseFloat( x : String ) : Float {
  47. return untyped tonumber(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. var Date = {};
  59. __feature__("js.Boot.getClass", _hxClasses["Date"] = {}, {});
  60. __feature__("js.Boot.isClass", Date.__name__ = ["Date"]);
  61. });
  62. __feature__("Int.*",{
  63. var Int = __feature__("Type.resolveClass", _hxClasses["Int"] = { __name__ : ["Int"] }, { __name__ : ["Int"] });
  64. });
  65. __feature__("Dynamic.*",{
  66. var Dynamic = __feature__("Type.resolveClass", _hxClasses["Dynamic"] = { __name__ : ["Dynamic"] }, { __name__ : ["Dynamic"] });
  67. });
  68. __feature__("Float.*",{
  69. var Float = __feature__("Type.resolveClass", _hxClasses["Float"]={}, {});
  70. Float.__name__ = ["Float"];
  71. });
  72. __feature__("Bool.*",{
  73. var Bool = __feature__("Type.resolveEnum",_hxClasses["Bool"] = {}, {});
  74. Bool.__ename__ = ["Bool"];
  75. });
  76. __feature__("Class.*",{
  77. var Class = __feature__("Type.resolveClass", _hxClasses["Class"] = { __name__ : ["Class"] }, { __name__ : ["Class"] });
  78. });
  79. __feature__("Enum.*",{
  80. var Enum = {};
  81. });
  82. __feature__("Void.*",{
  83. var Void = __feature__("Type.resolveEnum", _hxClasses["Void"] = { __ename__ : ["Void"] }, { __ename__ : ["Void"] });
  84. });
  85. }
  86. }