Boot.hx 4.4 KB

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