Std.hx 4.0 KB

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