Boot.hx 7.4 KB

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