Std.hx 3.9 KB

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