Std.hx 4.9 KB

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