Boot.hx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package python;
  2. import python.lib.Builtin;
  3. import python.internal.EnumImpl;
  4. import python.internal.HxOverrides;
  5. import python.internal.HxException;
  6. @:preCode("
  7. import builtins as _hx_builtin
  8. _hx_classes = dict()
  9. class _hx_AnonObject(object):
  10. def __init__(self, fields):
  11. self.__dict__ = fields
  12. _hx_c = _hx_AnonObject({})
  13. _hx_c._hx_AnonObject = _hx_AnonObject
  14. import functools as _hx_functools
  15. import math as _hx_math
  16. "
  17. )
  18. @:keep class Boot {
  19. static var inspect:Dynamic;
  20. static var builtin:Dynamic;
  21. @:keep static function __init__ () {
  22. Macros.importAs("inspect", "inspect");
  23. Boot.inspect = untyped __python__("inspect");
  24. Boot.builtin = untyped __python__("_hx_builtin");
  25. }
  26. @:keep static inline function isClass(o:Dynamic) : Bool {
  27. return o != null && (o == String || python.lib.Inspect.isclass(o));
  28. //return untyped __define_feature__("python.Boot.isClass", o._hx_class);
  29. }
  30. @:keep static function isAnonObject (o:Dynamic) {
  31. return Builtin.isinstance(o, untyped __python__("_hx_c._hx_AnonObject"));
  32. }
  33. @:keep private static function _add_dynamic(a:Dynamic,b:Dynamic):Dynamic
  34. {
  35. if (builtin.isinstance(a, untyped __python__("str")) || builtin.isinstance(b, untyped __python__("str"))) {
  36. return __string_rec(a,"") + __string_rec(b,"");
  37. }
  38. return untyped __python__("a+b");
  39. }
  40. @:keep private static function __string_rec(o:Dynamic,s:String):String {
  41. if (s == null) s = "";
  42. if( o == null ) return "null";
  43. if( s.length >= 5 ) return "<...>"; // too much deep recursion
  44. if (builtin.isinstance(o, untyped __python__("str"))) return o;
  45. if (builtin.isinstance(o, untyped __python__("bool"))) {
  46. if (untyped o) return "true" else return "false";
  47. }
  48. if (builtin.isinstance(o, untyped __python__("int"))) {
  49. return builtin.str(o);
  50. }
  51. // 1.0 should be printed as 1
  52. if (builtin.isinstance(o, untyped __python__("float"))) {
  53. try {
  54. if (o == Builtin.int(o)) {
  55. return builtin.str(Math.round(o));
  56. } else {
  57. return builtin.str(o);
  58. }
  59. } catch (e:Dynamic) {
  60. return builtin.str(o);
  61. }
  62. }
  63. if (inspect.isfunction(o) || inspect.ismethod(o)) return "<function>";
  64. if (builtin.isinstance(o, Array))
  65. {
  66. var o1:Array<Dynamic> = o;
  67. var l = o1.length;
  68. var st = "[";
  69. s += "\t";
  70. for( i in 0...l ) {
  71. var prefix = "";
  72. if (i > 0) {
  73. prefix = ",";
  74. }
  75. st += prefix + __string_rec(o1[i],s);
  76. }
  77. st += "]";
  78. return st;
  79. }
  80. try {
  81. if (builtin.hasattr(o, "toString")) {
  82. return o.toString();
  83. }
  84. } catch (e:Dynamic) {
  85. }
  86. if (builtin.hasattr(o, "__class__"))
  87. {
  88. if (builtin.isinstance(o, untyped __python__("_hx_c._hx_AnonObject")))
  89. {
  90. var toStr = null;
  91. try
  92. {
  93. var fields = Reflect.fields(o);
  94. var fieldsStr = [for (f in fields) '$f : ${__string_rec(Reflect.field(o,f), s+"\t")}'];
  95. toStr = "{ " + fieldsStr.join(", ") + " }";
  96. }
  97. catch (e:Dynamic) {
  98. trace(e);
  99. }
  100. if (toStr == null)
  101. {
  102. return "{ ... }";
  103. }
  104. else
  105. {
  106. return toStr;
  107. }
  108. }
  109. if (builtin.isinstance(o, untyped __python__("_hx_c.Enum"))) {
  110. var l = builtin.len(o.params);
  111. var hasParams = l > 0;
  112. if (hasParams) {
  113. var paramsStr = "";
  114. for (i in 0...l) {
  115. var prefix = "";
  116. if (i > 0) {
  117. prefix = ",";
  118. }
  119. paramsStr += prefix + __string_rec(o.params[i],s);
  120. }
  121. return o.tag + "(" + paramsStr + ")";
  122. } else {
  123. return o.tag;
  124. }
  125. }
  126. if (builtin.hasattr(o, "_hx_class_name") && o.__class__.__name__ != "type") {
  127. var fields = Type.getInstanceFields(o);
  128. var fieldsStr = [for (f in fields) '$f : ${__string_rec(Reflect.field(o,f), s+"\t")}'];
  129. var toStr = o._hx_class_name + "( " + fieldsStr.join(", ") + " )";
  130. return toStr;
  131. }
  132. if (builtin.hasattr(o, "_hx_class_name") && o.__class__.__name__ == "type") {
  133. var fields = Type.getClassFields(o);
  134. var fieldsStr = [for (f in fields) '$f : ${__string_rec(Reflect.field(o,f), s+"\t")}'];
  135. var toStr = "#" + o._hx_class_name + "( " + fieldsStr.join(", ") + " )";
  136. return toStr;
  137. }
  138. if (o == String) {
  139. return "#String";
  140. }
  141. //if (builtin.hasattr(o, "_hx_name")) {
  142. // return "#" + untyped o._hx_name;
  143. //}
  144. if (o == Array) {
  145. return "#Array";
  146. }
  147. if (builtin.callable(o)) {
  148. return "function";
  149. }
  150. try {
  151. if (builtin.hasattr(o, "__repr__")) {
  152. return untyped o.__repr__();
  153. }
  154. } catch (e:Dynamic) {}
  155. if (builtin.hasattr(o, "__str__")) {
  156. return untyped o.__str__();
  157. }
  158. if (builtin.hasattr(o, "__name__")) {
  159. return untyped o.__name__;
  160. }
  161. return "???";
  162. } else {
  163. try {
  164. inspect.getmembers(o, function (_) return true);
  165. return builtin.str(o);
  166. } catch (e:Dynamic) {
  167. return "???";
  168. }
  169. }
  170. }
  171. }