Std.hx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package;
  2. import python.lib.Builtin;
  3. import python.lib.Inspect;
  4. import python.Boot;
  5. /*
  6. * Copyright (C)2005-2012 Haxe Foundation
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. */
  26. //import flash.Boot;
  27. @:keepInit
  28. @:coreApi /*extern*/ class Std {
  29. public static inline function instance<T:{}, S:T>( value : T, c : Class<S> ) : S {
  30. try {
  31. return Builtin.isinstance(value,c) ? cast value : null;
  32. } catch (e:Dynamic) {
  33. return null;
  34. }
  35. }
  36. public static function is( v : Dynamic, t : Dynamic ) : Bool {
  37. if (v == null && t == null) {
  38. return false;
  39. }
  40. if (t == null) {
  41. return false;
  42. }
  43. if (t == (untyped __python__("Dynamic"))) {
  44. return true;
  45. }
  46. var isBool = Builtin.isinstance(v, (untyped __python__("bool")));
  47. if (t == (untyped __python__("Bool")) && isBool) {
  48. return true;
  49. }
  50. if (!isBool && t != (untyped __python__("Bool")) && t == (untyped __python__("Int")) && Builtin.isinstance(v, (untyped __python__("int")) )) {
  51. return true;
  52. }
  53. var vIsFloat = Builtin.isinstance(v, (untyped __python__("float")));
  54. if (!isBool && vIsFloat && t == (untyped __python__("Int")) && Math.isFinite(v) && v == Std.int(v)) {
  55. return true;
  56. }
  57. if (!isBool && t == (untyped __python__("Float")) && ( Builtin.isinstance(v, (untyped __python__("(float,int)"))))) {
  58. return true;
  59. }
  60. if ( t == (untyped __python__("str"))) {
  61. return Builtin.isinstance(v, String);
  62. }
  63. if (t == Enum && Inspect.isclass(v) && Builtin.hasattr(v, "_hx_constructs")) return true;
  64. if (t == Enum) return false;
  65. if (t == Date && Builtin.isinstance(v, Date)) return true;
  66. if (t == Date) return false;
  67. if (Builtin.isinstance(v, Date)) return false;
  68. if (t == Class && !Builtin.isinstance(v, untyped Enum) && Inspect.isclass(v) && Builtin.hasattr(v, "_hx_class_name") && !Builtin.hasattr(v, "_hx_constructs")) return true;
  69. if (t == Class) return false; // && !Builtin.isinstance(v, untyped Enum) && Builtin.hasattr(v, "__class__") && untyped Builtin.hasattr(v.__class__, "_hx_class_name") && !untyped Builtin.hasattr(v.__class__, "_hx_constructs")) return true;
  70. if (try Builtin.isinstance(v, t) catch (e:Dynamic) false) {
  71. return true;
  72. }
  73. if (Inspect.isclass(t)) {
  74. function loop (intf)
  75. {
  76. var f:Array<Dynamic> = Reflect.field(intf, "_hx_interfaces");
  77. if (f != null) {
  78. for (i in f) {
  79. if ( i == t) {
  80. return true;
  81. } else {
  82. var l = loop(i);
  83. if (l) {
  84. return true;
  85. }
  86. }
  87. }
  88. return false;
  89. } else {
  90. return false;
  91. }
  92. }
  93. return loop(untyped v.__class__);
  94. } else {
  95. return false;
  96. }
  97. //return untyped __is__(v , t); //TODO(av) macro to check t is a Type and not null as dart only perfomrs "is" at compile time despite having a runtime Type
  98. }
  99. // public static inline function instance<T>( v : {}, c : Class<T> ) : T {
  100. // return untyped __as__(v, c);
  101. // }
  102. @:access(python.Boot)
  103. @:keep
  104. public static function string( s : Dynamic ) : String
  105. {
  106. return python.Boot.__string_rec(s, "");
  107. }
  108. public static inline function int( x : Float ) : Int
  109. {
  110. try {
  111. return (untyped __python__("int"))(x);
  112. } catch (e:Dynamic) {
  113. return null;
  114. }
  115. }
  116. public static function parseInt( x : String ) : Null<Int> {
  117. if (x == null) return null;
  118. try {
  119. return (untyped __python__("int"))(x);
  120. } catch (e:Dynamic) {
  121. try {
  122. var prefix = x.substr(0,2).toLowerCase();
  123. if (prefix == "0x") {
  124. return (untyped __python__("int"))(x,16);
  125. }
  126. throw "fail";
  127. } catch (e:Dynamic) {
  128. var r = int(parseFloat(x));
  129. if (r == null) {
  130. var r1 = shortenPossibleNumber(x);
  131. if (r1 != x) {
  132. return parseInt(r1);
  133. } else {
  134. return null;
  135. }
  136. }
  137. return r;
  138. }
  139. }
  140. }
  141. static function shortenPossibleNumber (x:String):String
  142. {
  143. var r = "";
  144. for (i in 0...x.length) {
  145. var c = x.charAt(i);
  146. switch (c.charCodeAt(0)) {
  147. case "0".code
  148. | "1".code
  149. | "2".code
  150. | "3".code
  151. | "4".code
  152. | "5".code
  153. | "6".code
  154. | "7".code
  155. | "8".code
  156. | "9".code
  157. | ".".code : r += c;
  158. case _ : break;
  159. }
  160. }
  161. return r;
  162. }
  163. public static function parseFloat( x : String ) : Float
  164. {
  165. try {
  166. return untyped __python__("float")(x);
  167. } catch (e:Dynamic) {
  168. if (x != null) {
  169. var r1 = shortenPossibleNumber(x);
  170. if (r1 != x) {
  171. return parseFloat(r1);
  172. }
  173. }
  174. return Math.NaN;
  175. }
  176. }
  177. public static inline function random( x : Int ) : Int {
  178. if (x <= 0) {
  179. return 0;
  180. } else {
  181. return int(Math.random()*x);
  182. }
  183. //return dart.Lib.random(x);
  184. // return untyped __cascade__(new dartt.math.Random(), nextInt(x));// //untyped x <= 0 ? 0 : Math.floor(Math.random()*x); import 'dart:marth'; new Random()..nextInt(x);
  185. }
  186. }