Std.hx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (C)2005-2012 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. package;
  23. import python.lib.Builtin;
  24. import python.lib.Inspect;
  25. import python.Boot;
  26. @:keepInit
  27. @:coreApi /*extern*/ class Std {
  28. public static inline function instance<T:{}, S:T>( value : T, c : Class<S> ) : S {
  29. try {
  30. return Builtin.isinstance(value,c) ? cast value : null;
  31. } catch (e:Dynamic) {
  32. return null;
  33. }
  34. }
  35. public static function is( v : Dynamic, t : Dynamic ) : Bool {
  36. if (v == null && t == null) {
  37. return false;
  38. }
  39. if (t == null) {
  40. return false;
  41. }
  42. if (t == (python.Syntax.pythonCode("Dynamic"))) {
  43. return true;
  44. }
  45. var isBool = Builtin.isinstance(v, (python.Syntax.pythonCode("bool")));
  46. if (t == (python.Syntax.pythonCode("Bool")) && isBool) {
  47. return true;
  48. }
  49. if (!isBool && t != (python.Syntax.pythonCode("Bool")) && t == (python.Syntax.pythonCode("Int")) && Builtin.isinstance(v, (python.Syntax.pythonCode("int")) )) {
  50. return true;
  51. }
  52. var vIsFloat = Builtin.isinstance(v, (python.Syntax.pythonCode("float")));
  53. if (!isBool && vIsFloat && t == (python.Syntax.pythonCode("Int")) && Math.isFinite(v) && v == Std.int(v)) {
  54. return true;
  55. }
  56. if (!isBool && t == (python.Syntax.pythonCode("Float")) && ( Builtin.isinstance(v, (python.Syntax.pythonCode("(float,int)"))))) {
  57. return true;
  58. }
  59. if ( t == (python.Syntax.pythonCode("str"))) {
  60. return Builtin.isinstance(v, String);
  61. }
  62. if (t == Enum && Inspect.isclass(v) && Builtin.hasattr(v, "_hx_constructs")) return true;
  63. if (t == Enum) return false;
  64. if (t == Date && Builtin.isinstance(v, Date)) return true;
  65. if (t == Date) return false;
  66. if (Builtin.isinstance(v, Date)) return false;
  67. if (t == Class && !Builtin.isinstance(v, untyped Enum) && Inspect.isclass(v) && Builtin.hasattr(v, "_hx_class_name") && !Builtin.hasattr(v, "_hx_constructs")) return true;
  68. 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;
  69. if (try Builtin.isinstance(v, t) catch (e:Dynamic) false) {
  70. return true;
  71. }
  72. if (Inspect.isclass(t)) {
  73. function loop (intf)
  74. {
  75. var f:Array<Dynamic> = if (Builtin.hasattr(intf,"_hx_interfaces")) Builtin.getattr(intf, "_hx_interfaces") else [];
  76. if (f != null) {
  77. for (i in f) {
  78. if ( i == t) {
  79. return true;
  80. } else {
  81. var l = loop(i);
  82. if (l) {
  83. return true;
  84. }
  85. }
  86. }
  87. return false;
  88. } else {
  89. return false;
  90. }
  91. }
  92. return loop(untyped v.__class__);
  93. } else {
  94. return false;
  95. }
  96. }
  97. @:access(python.Boot)
  98. @:keep
  99. public static function string( s : Dynamic ) : String
  100. {
  101. return python.Boot.toString(s);
  102. }
  103. public static inline function int( x : Float ) : Int
  104. {
  105. try {
  106. return (python.Syntax.pythonCode("int"))(x);
  107. } catch (e:Dynamic) {
  108. return null;
  109. }
  110. }
  111. public static function parseInt( x : String ) : Null<Int> {
  112. if (x == null) return null;
  113. try {
  114. return (python.Syntax.pythonCode("int"))(x);
  115. } catch (e:Dynamic) {
  116. try {
  117. var prefix = x.substr(0,2).toLowerCase();
  118. if (prefix == "0x") {
  119. return (python.Syntax.pythonCode("int"))(x,16);
  120. }
  121. throw "fail";
  122. } catch (e:Dynamic) {
  123. var r = int(parseFloat(x));
  124. if (r == null) {
  125. var r1 = shortenPossibleNumber(x);
  126. if (r1 != x) {
  127. return parseInt(r1);
  128. } else {
  129. return null;
  130. }
  131. }
  132. return r;
  133. }
  134. }
  135. }
  136. static function shortenPossibleNumber (x:String):String
  137. {
  138. var r = "";
  139. for (i in 0...x.length) {
  140. var c = x.charAt(i);
  141. switch (c.charCodeAt(0)) {
  142. case "0".code
  143. | "1".code
  144. | "2".code
  145. | "3".code
  146. | "4".code
  147. | "5".code
  148. | "6".code
  149. | "7".code
  150. | "8".code
  151. | "9".code
  152. | ".".code : r += c;
  153. case _ : break;
  154. }
  155. }
  156. return r;
  157. }
  158. public static function parseFloat( x : String ) : Float
  159. {
  160. try {
  161. return python.Syntax.pythonCode("float")(x);
  162. } catch (e:Dynamic) {
  163. if (x != null) {
  164. var r1 = shortenPossibleNumber(x);
  165. if (r1 != x) {
  166. return parseFloat(r1);
  167. }
  168. }
  169. return Math.NaN;
  170. }
  171. }
  172. public static inline function random( x : Int ) : Int {
  173. if (x <= 0) {
  174. return 0;
  175. } else {
  176. return int(Math.random()*x);
  177. }
  178. //return dart.Lib.random(x);
  179. // 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);
  180. }
  181. }