Boot.hx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package js;
  26. class Boot {
  27. private static function __unhtml(s : String) {
  28. return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
  29. }
  30. private static function __trace(v,i : haxe.PosInfos) {
  31. untyped {
  32. var msg = if( i != null ) i.fileName+":"+i.lineNumber+": " else "";
  33. #if jsfl
  34. msg += __string_rec(v,"");
  35. fl.trace(msg);
  36. #else
  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" && console.log != null )
  42. 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. @:defineFeature static inline function isClass(o:Dynamic) : Bool {
  58. return o.__name__;
  59. }
  60. @:defineFeature static inline function isEnum(e:Dynamic) : Bool {
  61. return e.__ename__;
  62. }
  63. @:defineFeature static inline function getClass(o:Dynamic) : Dynamic {
  64. return o.__class__;
  65. }
  66. @:feature("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. @:feature("typed_catch") private static function __instanceof(o : Dynamic,cl) {
  153. untyped {
  154. try {
  155. if( __js__("o instanceof cl") ) {
  156. if( cl == Array )
  157. return (o.__enum__ == null);
  158. return true;
  159. }
  160. if( __interfLoop(js.Boot.getClass(o),cl) )
  161. return true;
  162. } catch( e : Dynamic ) {
  163. if( cl == null )
  164. return false;
  165. }
  166. switch( cl ) {
  167. case Int:
  168. return __js__("Math.ceil(o%2147483648.0) === o");
  169. case Float:
  170. return __js__("typeof(o)") == "number";
  171. case Bool:
  172. return __js__("o === true || o === false");
  173. case String:
  174. return __js__("typeof(o)") == "string";
  175. case Dynamic:
  176. return true;
  177. default:
  178. if( o == null )
  179. return false;
  180. // do not use isClass/isEnum here
  181. __feature__("Class.*",if( cl == Class && o.__name__ != null ) return true);
  182. __feature__("Enum.*",if( cl == Enum && o.__ename__ != null ) return true);
  183. return o.__enum__ == cl;
  184. }
  185. }
  186. }
  187. @:feature("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. }