Boot.hx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (C)2005-2017 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. private class HaxeError extends js.Error {
  24. var val:Dynamic;
  25. public function new(val:Dynamic) untyped {
  26. super();
  27. this.val = __define_feature__("js.Boot.HaxeError", val);
  28. this.message = String(val);
  29. if (js.Error.captureStackTrace) js.Error.captureStackTrace(this, HaxeError);
  30. }
  31. public static function wrap(val:Dynamic):Dynamic untyped {
  32. return if (__instanceof__(val, js.Error)) val else new HaxeError(val);
  33. }
  34. }
  35. @:dox(hide)
  36. class Boot {
  37. static inline function isClass(o:Dynamic) : Bool {
  38. return untyped __define_feature__("js.Boot.isClass", o.__name__);
  39. }
  40. static inline function isEnum(e:Dynamic) : Bool {
  41. return untyped __define_feature__("js.Boot.isEnum", e.__ename__);
  42. }
  43. static function getClass(o:Dynamic) : Dynamic {
  44. if (Std.is(o, Array))
  45. return Array;
  46. else {
  47. var cl = untyped __define_feature__("js.Boot.getClass", o.__class__);
  48. if (cl != null)
  49. return cl;
  50. var name = __nativeClassName(o);
  51. if (name != null)
  52. return __resolveNativeClass(name);
  53. return null;
  54. }
  55. }
  56. @:ifFeature("has_enum")
  57. private static function __string_rec(o,s:String) {
  58. untyped {
  59. if( o == null )
  60. return "null";
  61. if( s.length >= 5 )
  62. return "<...>"; // too much deep recursion
  63. var t = js.Lib.typeof(o);
  64. if( t == "function" && (isClass(o) || isEnum(o)) )
  65. t = "object";
  66. switch( t ) {
  67. case "object":
  68. if( __js__("o instanceof Array") ) {
  69. if( o.__enum__ ) {
  70. if( o.length == 2 )
  71. return o[0];
  72. var str = o[0]+"(";
  73. s += "\t";
  74. for( i in 2...o.length ) {
  75. if( i != 2 )
  76. str += "," + __string_rec(o[i],s);
  77. else
  78. str += __string_rec(o[i],s);
  79. }
  80. return str + ")";
  81. }
  82. var l = o.length;
  83. var i;
  84. var str = "[";
  85. s += "\t";
  86. for( i in 0...l )
  87. str += (if (i > 0) "," else "")+__string_rec(o[i],s);
  88. str += "]";
  89. return str;
  90. }
  91. var tostr;
  92. try {
  93. tostr = untyped o.toString;
  94. } catch( e : Dynamic ) {
  95. // strange error on IE
  96. return "???";
  97. }
  98. if( tostr != null && tostr != __js__("Object.toString") && js.Lib.typeof(tostr) == "function" ) {
  99. var s2 = o.toString();
  100. if( s2 != "[object Object]")
  101. return s2;
  102. }
  103. var k : String = null;
  104. var str = "{\n";
  105. s += "\t";
  106. var hasp = (o.hasOwnProperty != null);
  107. __js__("for( var k in o ) {");
  108. if( hasp && !o.hasOwnProperty(k) )
  109. __js__("continue");
  110. if( k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" || k == "__properties__" )
  111. __js__("continue");
  112. if( str.length != 2 )
  113. str += ", \n";
  114. str += s + k + " : "+__string_rec(o[k],s);
  115. __js__("}");
  116. s = s.substring(1);
  117. str += "\n" + s + "}";
  118. return str;
  119. case "function":
  120. return "<function>";
  121. case "string":
  122. return o;
  123. default:
  124. return String(o);
  125. }
  126. }
  127. }
  128. private static function __interfLoop(cc : Dynamic,cl : Dynamic) {
  129. if( cc == null )
  130. return false;
  131. if( cc == cl )
  132. return true;
  133. var intf : Dynamic = cc.__interfaces__;
  134. if( intf != null )
  135. for( i in 0...intf.length ) {
  136. var i : Dynamic = intf[i];
  137. if( i == cl || __interfLoop(i,cl) )
  138. return true;
  139. }
  140. return __interfLoop(cc.__super__,cl);
  141. }
  142. @:ifFeature("typed_catch") private static function __instanceof(o : Dynamic,cl : Dynamic) {
  143. if( cl == null )
  144. return false;
  145. switch( cl ) {
  146. case Int:
  147. return js.Lib.typeof(o) == "number" && untyped __js__("(o|0) === o");
  148. case Float:
  149. return js.Lib.typeof(o) == "number";
  150. case Bool:
  151. return js.Lib.typeof(o) == "boolean";
  152. case String:
  153. return js.Lib.typeof(o) == "string";
  154. case Array:
  155. return (untyped __js__("(o instanceof Array)")) && o.__enum__ == null;
  156. case Dynamic:
  157. return true;
  158. default:
  159. if( o != null ) {
  160. // Check if o is an instance of a Haxe class or a native JS object
  161. if( js.Lib.typeof(cl) == "function" ) {
  162. if( untyped __js__("o instanceof cl") )
  163. return true;
  164. if( __interfLoop(getClass(o),cl) )
  165. return true;
  166. }
  167. else if ( js.Lib.typeof(cl) == "object" && __isNativeObj(cl) ) {
  168. if( untyped __js__("o instanceof cl") )
  169. return true;
  170. }
  171. } else {
  172. return false;
  173. }
  174. // do not use isClass/isEnum here
  175. untyped __feature__("Class.*",if( cl == Class && o.__name__ != null ) return true);
  176. untyped __feature__("Enum.*",if( cl == Enum && o.__ename__ != null ) return true);
  177. return o.__enum__ == cl;
  178. }
  179. }
  180. @:ifFeature("typed_cast") private static function __cast(o : Dynamic, t : Dynamic) {
  181. if (__instanceof(o, t)) return o;
  182. else throw "Cannot cast " +Std.string(o) + " to " +Std.string(t);
  183. }
  184. static var __toStr = untyped ({}).toString;
  185. // get native JS [[Class]]
  186. static function __nativeClassName(o:Dynamic):String {
  187. var name = untyped __toStr.call(o).slice(8, -1);
  188. // exclude general Object and Function
  189. // also exclude Math and JSON, because instanceof cannot be called on them
  190. if (name == "Object" || name == "Function" || name == "Math" || name == "JSON")
  191. return null;
  192. return name;
  193. }
  194. // check for usable native JS object
  195. static function __isNativeObj(o:Dynamic):Bool {
  196. return __nativeClassName(o) != null;
  197. }
  198. // resolve native JS class in the global scope:
  199. static function __resolveNativeClass(name:String) {
  200. return untyped js.Lib.global[name];
  201. }
  202. }