Boot.hx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 js;
  23. import js.Syntax; // import it here so it's always available in the compiler
  24. @:dox(hide)
  25. class Boot {
  26. static inline function isClass(o:Dynamic):Bool {
  27. return untyped __define_feature__("js.Boot.isClass", o.__name__);
  28. }
  29. static inline function isInterface(o:Class<Dynamic>):Bool {
  30. return untyped __define_feature__("js.Boot.isInterface", o.__isInterface__);
  31. }
  32. static inline function isEnum(e:Dynamic):Bool {
  33. return untyped __define_feature__("js.Boot.isEnum", e.__ename__);
  34. }
  35. @:pure static function getClass(o:Null<Dynamic>):Null<Dynamic> {
  36. if (o == null) {
  37. return null;
  38. } else if (Std.isOfType(o, Array)) {
  39. return Array;
  40. } else {
  41. var cl = untyped __define_feature__("js.Boot.getClass", o.__class__);
  42. if (cl != null)
  43. return cl;
  44. var name = __nativeClassName(o);
  45. if (name != null)
  46. return __resolveNativeClass(name);
  47. return null;
  48. }
  49. }
  50. @:ifFeature("has_enum")
  51. private static function __string_rec(o, s:String) {
  52. untyped {
  53. if (o == null)
  54. return "null";
  55. if (s.length >= 5)
  56. return "<...>"; // too much deep recursion
  57. var t = js.Syntax.typeof(o);
  58. if (t == "function" && (isClass(o) || isEnum(o)))
  59. t = "object";
  60. switch (t) {
  61. case "object":
  62. #if !js_enums_as_arrays
  63. __feature__("has_enum", if (o.__enum__) {
  64. var e = $hxEnums[o.__enum__];
  65. var con = e.__constructs__[o._hx_index];
  66. var n = con._hx_name;
  67. if (con.__params__) {
  68. s += "\t";
  69. return n + "(" + [for (p in (con.__params__ : Array<String>)) __string_rec(o[p], s)].join(",") + ")";
  70. } else {
  71. return n;
  72. }
  73. });
  74. #end
  75. if (js.Syntax.instanceof(o, Array)) {
  76. #if js_enums_as_arrays
  77. __feature__("has_enum", if (o.__enum__) {
  78. if (o.length == 2)
  79. return o[0];
  80. var str = o[0] + "(";
  81. s += "\t";
  82. for (i in 2...o.length) {
  83. if (i != 2)
  84. str += "," + __string_rec(o[i], s);
  85. else
  86. str += __string_rec(o[i], s);
  87. }
  88. return str + ")";
  89. });
  90. #end
  91. var str = "[";
  92. s += "\t";
  93. for (i in 0...o.length)
  94. str += (if (i > 0) "," else "") + __string_rec(o[i], s);
  95. str += "]";
  96. return str;
  97. }
  98. var tostr;
  99. try {
  100. tostr = untyped o.toString;
  101. } catch (e:Dynamic) {
  102. // strange error on IE
  103. return "???";
  104. }
  105. if (tostr != null && tostr != js.Syntax.code("Object.toString") && js.Syntax.typeof(tostr) == "function") {
  106. var s2 = o.toString();
  107. if (s2 != "[object Object]")
  108. return s2;
  109. }
  110. var str = "{\n";
  111. s += "\t";
  112. var hasp = (o.hasOwnProperty != null);
  113. var k:String = null;
  114. js.Syntax.code("for( {0} in {1} ) {", k, o);
  115. if (hasp && !o.hasOwnProperty(k))
  116. js.Syntax.code("continue");
  117. if (k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" || k == "__properties__")
  118. js.Syntax.code("continue");
  119. if (str.length != 2)
  120. str += ", \n";
  121. str += s + k + " : " + __string_rec(o[k], s);
  122. js.Syntax.code("}");
  123. s = s.substring(1);
  124. str += "\n" + s + "}";
  125. return str;
  126. case "function":
  127. return "<function>";
  128. case "string":
  129. return o;
  130. default:
  131. return String(o);
  132. }
  133. }
  134. }
  135. @:pure private static function __interfLoop(cc:Dynamic, cl:Dynamic) {
  136. if (cc == null)
  137. return false;
  138. if (cc == cl)
  139. return true;
  140. var intf:Dynamic = cc.__interfaces__;
  141. if (intf != null
  142. // ES6 classes inherit statics, so we want to avoid accessing inherited `__interfaces__`
  143. #if (js_es >= 6) && (cc.__super__ == null || cc.__super__.__interfaces__ != intf) #end
  144. ) {
  145. for (i in 0...intf.length) {
  146. var i:Dynamic = intf[i];
  147. if (i == cl || __interfLoop(i, cl))
  148. return true;
  149. }
  150. }
  151. return __interfLoop(cc.__super__, cl);
  152. }
  153. @:pure private static function __instanceof(o:Dynamic, cl:Dynamic) {
  154. if (cl == null)
  155. return false;
  156. switch (cl) {
  157. case Int:
  158. return js.Syntax.typeof(o) == "number" && js.Syntax.strictEq(o | 0, o);
  159. case Float:
  160. return js.Syntax.typeof(o) == "number";
  161. case Bool:
  162. return js.Syntax.typeof(o) == "boolean";
  163. case String:
  164. return js.Syntax.typeof(o) == "string";
  165. case Array:
  166. return js.Syntax.instanceof(o, Array) #if js_enums_as_arrays && o.__enum__ == null #end;
  167. case Dynamic:
  168. return o != null;
  169. default:
  170. if (o != null) {
  171. // Check if o is an instance of a Haxe class or a native JS object
  172. if (js.Syntax.typeof(cl) == "function") {
  173. if (__downcastCheck(o, cl))
  174. return true;
  175. } else if (js.Syntax.typeof(cl) == "object" && __isNativeObj(cl)) {
  176. if (js.Syntax.instanceof(o, cl))
  177. return true;
  178. }
  179. } else {
  180. return false;
  181. }
  182. // do not use isClass/isEnum here
  183. untyped __feature__("Class.*", if (cl == Class && o.__name__ != null) return true);
  184. untyped __feature__("Enum.*", if (cl == Enum && o.__ename__ != null) return true);
  185. #if js_enums_as_arrays
  186. return o.__enum__ == cl;
  187. #else
  188. return untyped __feature__(
  189. "has_enum",
  190. if (o.__enum__ != null) ($hxEnums[o.__enum__]) == cl else false,
  191. false
  192. );
  193. #end
  194. }
  195. }
  196. static function __downcastCheck(o:Dynamic, cl:Class<Dynamic>):Bool {
  197. return js.Syntax.instanceof(o, cl) || (isInterface(cl) && inline __implements(o, cl));
  198. }
  199. static function __implements(o:Dynamic, iface:Class<Dynamic>):Bool {
  200. return __interfLoop(getClass(o), iface);
  201. }
  202. @:ifFeature("typed_cast") private static function __cast(o:Dynamic, t:Dynamic) {
  203. if (o == null || __instanceof(o, t))
  204. return o;
  205. else
  206. throw "Cannot cast " + Std.string(o) + " to " + Std.string(t);
  207. }
  208. static var __toStr:js.lib.Function;
  209. static function __init__() {
  210. Boot.__toStr = (cast {}).toString;
  211. }
  212. // get native JS [[Class]]
  213. static function __nativeClassName(o:Dynamic):String {
  214. var name:String = __toStr.call(o).slice(8, -1);
  215. // exclude general Object and Function
  216. // also exclude Math and JSON, because instanceof cannot be called on them
  217. if (name == "Object" || name == "Function" || name == "Math" || name == "JSON")
  218. return null;
  219. return name;
  220. }
  221. // check for usable native JS object
  222. static function __isNativeObj(o:Dynamic):Bool {
  223. return __nativeClassName(o) != null;
  224. }
  225. // resolve native JS class in the global scope:
  226. static function __resolveNativeClass(name:String) {
  227. return js.Lib.global[cast name];
  228. }
  229. }