Boot.hx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (C)2005-2015 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. private static function __unhtml(s : String) {
  38. return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  39. }
  40. private static function __trace(v,i : haxe.PosInfos) {
  41. untyped {
  42. var msg = if( i != null ) i.fileName+":"+i.lineNumber+": " else "";
  43. #if jsfl
  44. msg += __string_rec(v,"");
  45. fl.trace(msg);
  46. #else
  47. msg += __string_rec(v, "");
  48. if( i != null && i.customParams != null )
  49. for( v in i.customParams )
  50. msg += "," + __string_rec(v, "");
  51. var d;
  52. if( __js__("typeof")(document) != "undefined" && (d = document.getElementById("haxe:trace")) != null )
  53. d.innerHTML += __unhtml(msg)+"<br/>";
  54. else if( __js__("typeof console") != "undefined" && __js__("console").log != null )
  55. __js__("console").log(msg);
  56. #end
  57. }
  58. }
  59. private static function __clear_trace() {
  60. untyped {
  61. #if jsfl
  62. fl.outputPanel.clear();
  63. #else
  64. var d = document.getElementById("haxe:trace");
  65. if( d != null )
  66. d.innerHTML = "";
  67. #end
  68. }
  69. }
  70. static inline function isClass(o:Dynamic) : Bool {
  71. return untyped __define_feature__("js.Boot.isClass", o.__name__);
  72. }
  73. static inline function isEnum(e:Dynamic) : Bool {
  74. return untyped __define_feature__("js.Boot.isEnum", e.__ename__);
  75. }
  76. static function getClass(o:Dynamic) : Dynamic {
  77. if (Std.is(o, Array))
  78. return Array;
  79. else {
  80. var cl = untyped __define_feature__("js.Boot.getClass", o.__class__);
  81. if (cl != null)
  82. return cl;
  83. var name = __nativeClassName(o);
  84. if (name != null)
  85. return __resolveNativeClass(name);
  86. return null;
  87. }
  88. }
  89. @:ifFeature("has_enum")
  90. private static function __string_rec(o,s:String) {
  91. untyped {
  92. if( o == null )
  93. return "null";
  94. if( s.length >= 5 )
  95. return "<...>"; // too much deep recursion
  96. var t = __js__("typeof(o)");
  97. if( t == "function" && (isClass(o) || isEnum(o)) )
  98. t = "object";
  99. switch( t ) {
  100. case "object":
  101. if( __js__("o instanceof Array") ) {
  102. if( o.__enum__ ) {
  103. if( o.length == 2 )
  104. return o[0];
  105. var str = o[0]+"(";
  106. s += "\t";
  107. for( i in 2...o.length ) {
  108. if( i != 2 )
  109. str += "," + __string_rec(o[i],s);
  110. else
  111. str += __string_rec(o[i],s);
  112. }
  113. return str + ")";
  114. }
  115. var l = o.length;
  116. var i;
  117. var str = "[";
  118. s += "\t";
  119. for( i in 0...l )
  120. str += (if (i > 0) "," else "")+__string_rec(o[i],s);
  121. str += "]";
  122. return str;
  123. }
  124. var tostr;
  125. try {
  126. tostr = untyped o.toString;
  127. } catch( e : Dynamic ) {
  128. // strange error on IE
  129. return "???";
  130. }
  131. if( tostr != null && tostr != __js__("Object.toString") && __typeof__(tostr) == "function" ) {
  132. var s2 = o.toString();
  133. if( s2 != "[object Object]")
  134. return s2;
  135. }
  136. var k : String = null;
  137. var str = "{\n";
  138. s += "\t";
  139. var hasp = (o.hasOwnProperty != null);
  140. __js__("for( var k in o ) {");
  141. if( hasp && !o.hasOwnProperty(k) )
  142. __js__("continue");
  143. if( k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" || k == "__properties__" )
  144. __js__("continue");
  145. if( str.length != 2 )
  146. str += ", \n";
  147. str += s + k + " : "+__string_rec(o[k],s);
  148. __js__("}");
  149. s = s.substring(1);
  150. str += "\n" + s + "}";
  151. return str;
  152. case "function":
  153. return "<function>";
  154. case "string":
  155. return o;
  156. default:
  157. return String(o);
  158. }
  159. }
  160. }
  161. private static function __interfLoop(cc : Dynamic,cl : Dynamic) {
  162. if( cc == null )
  163. return false;
  164. if( cc == cl )
  165. return true;
  166. var intf : Dynamic = cc.__interfaces__;
  167. if( intf != null )
  168. for( i in 0...intf.length ) {
  169. var i : Dynamic = intf[i];
  170. if( i == cl || __interfLoop(i,cl) )
  171. return true;
  172. }
  173. return __interfLoop(cc.__super__,cl);
  174. }
  175. @:ifFeature("typed_catch") private static function __instanceof(o : Dynamic,cl : Dynamic) {
  176. if( cl == null )
  177. return false;
  178. switch( cl ) {
  179. case Int:
  180. return (untyped __js__("(o|0) === o"));
  181. case Float:
  182. return (untyped __js__("typeof"))(o) == "number";
  183. case Bool:
  184. return (untyped __js__("typeof"))(o) == "boolean";
  185. case String:
  186. return (untyped __js__("typeof"))(o) == "string";
  187. case Array:
  188. return (untyped __js__("(o instanceof Array)")) && o.__enum__ == null;
  189. case Dynamic:
  190. return true;
  191. default:
  192. if( o != null ) {
  193. // Check if o is an instance of a Haxe class or a native JS object
  194. if( (untyped __js__("typeof"))(cl) == "function" ) {
  195. if( untyped __js__("o instanceof cl") )
  196. return true;
  197. if( __interfLoop(getClass(o),cl) )
  198. return true;
  199. }
  200. else if ( (untyped __js__("typeof"))(cl) == "object" && __isNativeObj(cl) ) {
  201. if( untyped __js__("o instanceof cl") )
  202. return true;
  203. }
  204. } else {
  205. return false;
  206. }
  207. // do not use isClass/isEnum here
  208. untyped __feature__("Class.*",if( cl == Class && o.__name__ != null ) return true);
  209. untyped __feature__("Enum.*",if( cl == Enum && o.__ename__ != null ) return true);
  210. return o.__enum__ == cl;
  211. }
  212. }
  213. @:ifFeature("typed_cast") private static function __cast(o : Dynamic, t : Dynamic) {
  214. if (__instanceof(o, t)) return o;
  215. else throw "Cannot cast " +Std.string(o) + " to " +Std.string(t);
  216. }
  217. static var __toStr = untyped __js__("{}.toString");
  218. // get native JS [[Class]]
  219. static function __nativeClassName(o:Dynamic):String {
  220. var name = untyped __toStr.call(o).slice(8, -1);
  221. // exclude general Object and Function
  222. // also exclude Math and JSON, because instanceof cannot be called on them
  223. if (name == "Object" || name == "Function" || name == "Math" || name == "JSON")
  224. return null;
  225. return name;
  226. }
  227. // check for usable native JS object
  228. static function __isNativeObj(o:Dynamic):Bool {
  229. return __nativeClassName(o) != null;
  230. }
  231. // resolve native JS class in the global scope:
  232. static function __resolveNativeClass(name:String) {
  233. return untyped js.Lib.global[name];
  234. }
  235. }