Boot.hx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. return untyped __define_feature__("js.Boot.getClass", o.__class__);
  65. }
  66. @:ifFeature("has_enum")
  67. private static function __string_rec(o,s:String) {
  68. untyped {
  69. if( o == null )
  70. return "null";
  71. if( s.length >= 5 )
  72. return "<...>"; // too much deep recursion
  73. var t = __js__("typeof(o)");
  74. if( t == "function" && (isClass(o) || isEnum(o)) )
  75. t = "object";
  76. switch( t ) {
  77. case "object":
  78. if( __js__("o instanceof Array") ) {
  79. if( o.__enum__ ) {
  80. if( o.length == 2 )
  81. return o[0];
  82. var str = o[0]+"(";
  83. s += "\t";
  84. for( i in 2...o.length ) {
  85. if( i != 2 )
  86. str += "," + __string_rec(o[i],s);
  87. else
  88. str += __string_rec(o[i],s);
  89. }
  90. return str + ")";
  91. }
  92. var l = o.length;
  93. var i;
  94. var str = "[";
  95. s += "\t";
  96. for( i in 0...l )
  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__("Object.toString") ) {
  109. var s2 = o.toString();
  110. if( s2 != "[object Object]")
  111. return s2;
  112. }
  113. var k : String = null;
  114. var str = "{\n";
  115. s += "\t";
  116. var hasp = (o.hasOwnProperty != null);
  117. __js__("for( var k in o ) {");
  118. if( hasp && !o.hasOwnProperty(k) )
  119. __js__("continue");
  120. if( k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" || k == "__properties__" )
  121. __js__("continue");
  122. if( str.length != 2 )
  123. str += ", \n";
  124. str += s + k + " : "+__string_rec(o[k],s);
  125. __js__("}");
  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. 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. for( i in 0...intf.length ) {
  146. var i : Dynamic = intf[i];
  147. if( i == cl || __interfLoop(i,cl) )
  148. return true;
  149. }
  150. return __interfLoop(cc.__super__,cl);
  151. }
  152. @:ifFeature("typed_catch") private static function __instanceof(o : Dynamic,cl : Dynamic) {
  153. if( cl == null )
  154. return false;
  155. switch( cl ) {
  156. case Int:
  157. return (untyped __js__("(o|0) === o"));
  158. case Float:
  159. return (untyped __js__("typeof"))(o) == "number";
  160. case Bool:
  161. return (untyped __js__("typeof"))(o) == "boolean";
  162. case String:
  163. return (untyped __js__("typeof"))(o) == "string";
  164. case Dynamic:
  165. return true;
  166. default:
  167. if( o != null ) {
  168. // Check if o is an instance of a Haxe class
  169. if( (untyped __js__("typeof"))(cl) == "function" ) {
  170. if( untyped __js__("o instanceof cl") ) {
  171. if( cl == Array )
  172. return (o.__enum__ == null);
  173. return true;
  174. }
  175. if( __interfLoop(getClass(o),cl) )
  176. return true;
  177. }
  178. } else {
  179. return false;
  180. }
  181. // do not use isClass/isEnum here
  182. untyped __feature__("Class.*",if( cl == Class && o.__name__ != null ) return true);
  183. untyped __feature__("Enum.*",if( cl == Enum && o.__ename__ != null ) return true);
  184. return o.__enum__ == cl;
  185. }
  186. }
  187. @:ifFeature("typed_cast") private static function __cast(o : Dynamic, t : Dynamic) {
  188. if (__instanceof(o, t)) return o;
  189. else throw "Cannot cast " +Std.string(o) + " to " +Std.string(t);
  190. }
  191. }