Boot.hx 4.4 KB

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