Std.hx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Copyright (C)2005-2019 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 hl.Boot;
  23. private typedef Rand = hl.Abstract<"hl_random">;
  24. @:coreApi
  25. class Std {
  26. static var rnd : Rand;
  27. static var toStringDepth : Int = 0;
  28. static function __init__() : Void {
  29. rnd = rnd_sys();
  30. }
  31. @:hlNative("std","rnd_init_system") static function rnd_sys() : Rand { return null; }
  32. @:hlNative("std","rnd_int") static function rnd_int( r : Rand ) : Int { return 0; }
  33. @:hlNative("std","rnd_float") static function rnd_float( r : Rand ) : Float { return 0.; }
  34. public static function random( x : Int ) : Int {
  35. return x <= 0 ? 0 : (rnd_int(rnd) & 0x3FFFFFFF) % x;
  36. }
  37. public static function is( v : Dynamic, t : Dynamic ) : Bool {
  38. var t : hl.BaseType = t;
  39. if( t == null ) return false;
  40. switch( t.__type__.kind ) {
  41. case HDyn:
  42. return v != null;
  43. case HF64:
  44. switch( hl.Type.getDynamic(v).kind ) {
  45. case HUI8, HUI16, HI32:
  46. return true;
  47. default:
  48. }
  49. case HI32:
  50. switch( hl.Type.getDynamic(v).kind ) {
  51. case HF32, HF64:
  52. var v : Float = v;
  53. return Std.int(v) == v;
  54. default:
  55. }
  56. default:
  57. }
  58. return t.check(v);
  59. }
  60. extern public static function downcast<T:{},S:T>( value : T, c : Class<S> ) : S;
  61. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  62. public static inline function instance<T:{},S:T>( value : T, c : Class<S> ) : S {
  63. return downcast(value, c);
  64. }
  65. extern public static inline function int( x : Float ) : Int {
  66. return untyped $int(x);
  67. }
  68. @:keep public static function string( s : Dynamic ) : String {
  69. var len = 0;
  70. var bytes = hl.Bytes.fromValue(s,new hl.Ref(len));
  71. return @:privateAccess String.__alloc__(bytes,len);
  72. }
  73. public static function parseInt( x : String ) : Null<Int> {
  74. if( x == null ) return null;
  75. return @:privateAccess x.bytes.parseInt(0, x.length<<1);
  76. }
  77. public static function parseFloat( x : String ) : Float {
  78. if( x == null ) return Math.NaN;
  79. return @:privateAccess x.bytes.parseFloat(0, x.length<<1);
  80. }
  81. @:keep static function __add__( a : Dynamic, b : Dynamic ) : Dynamic {
  82. var ta = hl.Type.getDynamic(a);
  83. var tb = hl.Type.getDynamic(b);
  84. if( ta == hl.Type.get("") )
  85. return (a : String) + b;
  86. if( tb == hl.Type.get("") )
  87. return a + (b : String);
  88. switch(ta.kind) {
  89. case HUI8, HUI16, HI32:
  90. var a : Int = a;
  91. switch( tb.kind ) {
  92. case HUI8, HUI16, HI32: return a + (b:Int);
  93. case HF32, HF64: return a + (b:Float);
  94. case HVoid: return a;
  95. default:
  96. }
  97. case HF32, HF64:
  98. var a : Float = a;
  99. switch( tb.kind ) {
  100. case HUI8, HUI16, HI32: return a + (b:Int);
  101. case HF32, HF64: return a + (b:Float);
  102. case HVoid: return a;
  103. default:
  104. }
  105. case HVoid:
  106. switch( tb.kind ) {
  107. case HUI8, HUI16, HI32, HF32, HF64: return b;
  108. case HVoid: return 0.;
  109. default:
  110. }
  111. default:
  112. }
  113. throw "Can't add "+a+"("+ta+") and "+b+"("+tb+")";
  114. return null;
  115. }
  116. }