Std.hx 3.6 KB

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