Std.hx 3.2 KB

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