Boot.hx 5.9 KB

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