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