Boot.hx 7.0 KB

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