2
0

Std.hx 5.1 KB

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