Boot.hx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 = document.getElementById("haxe:trace");
  39. if( d != 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. private static function __string_rec(o,s) {
  58. untyped {
  59. if( o == null )
  60. return "null";
  61. if( s.length >= 5 )
  62. return "<...>"; // too much deep recursion
  63. var t = __js__("typeof(o)");
  64. if( t == "function" && (o.__name__ != null || o.__ename__ != null) )
  65. t = "object";
  66. switch( t ) {
  67. case "object":
  68. if( __js__("o instanceof Array") ) {
  69. if( o.__enum__ != null ) {
  70. if( o.length == 2 )
  71. return o[0];
  72. var str = o[0]+"(";
  73. s += "\t";
  74. for( i in 2...o.length ) {
  75. if( i != 2 )
  76. str += "," + __string_rec(o[i],s);
  77. else
  78. str += __string_rec(o[i],s);
  79. }
  80. return str + ")";
  81. }
  82. var l = o.length;
  83. var i;
  84. var str = "[";
  85. s += "\t";
  86. for( i in 0...l )
  87. str += (if (i > 0) "," else "")+__string_rec(o[i],s);
  88. str += "]";
  89. return str;
  90. }
  91. var tostr;
  92. try {
  93. tostr = untyped o.toString;
  94. } catch( e : Dynamic ) {
  95. // strange error on IE
  96. return "???";
  97. }
  98. if( tostr != null && tostr != __js__("Object.toString") ) {
  99. var s2 = o.toString();
  100. if( s2 != "[object Object]")
  101. return s2;
  102. }
  103. var k : String = null;
  104. var str = "{\n";
  105. s += "\t";
  106. var hasp = (o.hasOwnProperty != null);
  107. __js__("for( var k in o ) { ");
  108. if( hasp && !o.hasOwnProperty(k) )
  109. __js__("continue");
  110. if( k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" || k == "__properties__" )
  111. __js__("continue");
  112. if( str.length != 2 )
  113. str += ", \n";
  114. str += s + k + " : "+__string_rec(o[k],s);
  115. __js__("}");
  116. s = s.substring(1);
  117. str += "\n" + s + "}";
  118. return str;
  119. case "function":
  120. return "<function>";
  121. case "string":
  122. return o;
  123. default:
  124. return String(o);
  125. }
  126. }
  127. }
  128. private static function __interfLoop(cc : Dynamic,cl : Dynamic) {
  129. if( cc == null )
  130. return false;
  131. if( cc == cl )
  132. return true;
  133. var intf : Dynamic = cc.__interfaces__;
  134. if( intf != null )
  135. for( i in 0...intf.length ) {
  136. var i : Dynamic = intf[i];
  137. if( i == cl || __interfLoop(i,cl) )
  138. return true;
  139. }
  140. return __interfLoop(cc.__super__,cl);
  141. }
  142. private static function __instanceof(o : Dynamic,cl) {
  143. untyped {
  144. try {
  145. if( __js__("o instanceof cl") ) {
  146. if( cl == Array )
  147. return (o.__enum__ == null);
  148. return true;
  149. }
  150. if( __interfLoop(o.__class__,cl) )
  151. return true;
  152. } catch( e : Dynamic ) {
  153. if( cl == null )
  154. return false;
  155. }
  156. switch( cl ) {
  157. case Int:
  158. return __js__("Math.ceil(o%2147483648.0) === o");
  159. case Float:
  160. return __js__("typeof(o)") == "number";
  161. case Bool:
  162. return __js__("o === true || o === false");
  163. case String:
  164. return __js__("typeof(o)") == "string";
  165. case Dynamic:
  166. return true;
  167. default:
  168. if( o == null )
  169. return false;
  170. return o.__enum__ == cl || ( cl == Class && o.__name__ != null ) || ( cl == Enum && o.__ename__ != null );
  171. }
  172. }
  173. }
  174. private static function __init() {
  175. untyped {
  176. Lib.isIE = (__js__("typeof document!='undefined'") && document.all != null && __js__("typeof window!='undefined'") && window.opera == null );
  177. Lib.isOpera = (__js__("typeof window!='undefined'") && window.opera != null );
  178. Array.prototype.copy = Array.prototype.slice;
  179. Array.prototype.insert = function(i,x) {
  180. __this__.splice(i,0,x);
  181. };
  182. Array.prototype.remove = if( Array.prototype.indexOf ) function(obj) {
  183. var idx = __this__.indexOf(obj);
  184. if( idx == -1 ) return false;
  185. __this__.splice(idx,1);
  186. return true;
  187. } else function(obj) {
  188. var i = 0;
  189. var l = __this__.length;
  190. while( i < l ) {
  191. if( __this__[i] == obj ) {
  192. __this__.splice(i,1);
  193. return true;
  194. }
  195. i++;
  196. }
  197. return false;
  198. };
  199. Array.prototype.iterator = function() {
  200. return {
  201. cur : 0,
  202. arr : __this__,
  203. hasNext : function() {
  204. return __this__.cur < __this__.arr.length;
  205. },
  206. next : function() {
  207. return __this__.arr[__this__.cur++];
  208. }
  209. }
  210. };
  211. if( String.prototype.cca == null )
  212. String.prototype.cca = String.prototype.charCodeAt;
  213. String.prototype.charCodeAt = function(i) {
  214. var x = __this__.cca(i);
  215. if( x != x ) // fast isNaN
  216. return __js__('undefined'); // isNaN will still return true
  217. return x;
  218. };
  219. var oldsub = String.prototype.substr;
  220. String.prototype.substr = function(pos,len){
  221. if( pos != null && pos != 0 && len != null && len < 0 ) return "";
  222. if( len == null ) len = __this__.length;
  223. if( pos < 0 ){
  224. pos = __this__.length + pos;
  225. if( pos < 0 ) pos = 0;
  226. }else if( len < 0 ){
  227. len = __this__.length + len - pos;
  228. }
  229. return oldsub.apply(__this__,[pos,len]);
  230. };
  231. Function.prototype["$bind"] = function(o){
  232. var f = function(){
  233. return f.method.apply(f.scope, arguments);
  234. }
  235. f.scope = o;
  236. f.method = __this__;
  237. return f;
  238. }
  239. }
  240. }
  241. }