Boot.hx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 flash;
  26. class Boot {
  27. private static var def_color = 0;
  28. private static var exception = null;
  29. private static function __string_rec(o : Dynamic,s : String) {
  30. untyped {
  31. if( s.length >= 20 )
  32. return "<...>"; // too much deep recursion
  33. var t = __typeof__(o);
  34. if( t == "movieclip" )
  35. t = "object";
  36. else if( t == "function" && (o.__name__ != null || o.__ename__ != null) )
  37. t = "object";
  38. switch( t ) {
  39. case "object":
  40. if( __instanceof__(o,Array) ) {
  41. if( o.__enum__ != null ) {
  42. if( o["length"] == 2 )
  43. return o[0];
  44. var str = o[0]+"(";
  45. s += " ";
  46. for( i in 2...o["length"] ) {
  47. if( i != 2 )
  48. str += "," + __string_rec(o[i],s);
  49. else
  50. str += __string_rec(o[i],s);
  51. }
  52. return str + ")";
  53. }
  54. var l = o["length"];
  55. var i;
  56. var str = "[";
  57. s += " ";
  58. for( i in 0...l )
  59. str += (if (i > 0) "," else "")+__string_rec(o[i],s);
  60. str += "]";
  61. return str;
  62. }
  63. var s2 = o["toString"]();
  64. if( (__typeof__(s2) == "string" || __instanceof__(s2,String)) && s2 != "[object Object]" && s2 != "[type Function]" )
  65. return s2;
  66. var k;
  67. var str = "{\n";
  68. if( typeof(o) == "movieclip" )
  69. str = "MC("+o._name+") "+str;
  70. s += " ";
  71. var keys : Array<String> = __keys__(o);
  72. for( k in keys.iterator() ) {
  73. if( k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" )
  74. continue;
  75. if( str.length != 2 )
  76. str += ",\n";
  77. str += s + k + " : "+__string_rec(o[k],s);
  78. }
  79. s = s.substring(4);
  80. if( str.length != 2 )
  81. str += "\n";
  82. str += s + "}";
  83. return str;
  84. case "function":
  85. return "<function>";
  86. case "string":
  87. return o;
  88. default:
  89. return String(o);
  90. }
  91. }
  92. }
  93. private static function __closure(f,o) {
  94. untyped {
  95. var m = o[f];
  96. if( m == null )
  97. return null;
  98. var f2 = function() {
  99. var me = __arguments__["callee"];
  100. return me["f"]["apply"](me["o"],__arguments__);
  101. };
  102. f2["f"] = m;
  103. f2["o"] = o;
  104. return f2;
  105. }
  106. }
  107. #if flash6
  108. private static function __interfLoop(cc : Dynamic,cl : Dynamic) {
  109. if( cc == null )
  110. return false;
  111. var intf : Array<Dynamic> = cc.__interfaces__;
  112. for( i in 0...intf.length ) {
  113. var i = intf[i];
  114. if( i == cl || __interfLoop(i,cl) )
  115. return true;
  116. }
  117. return __interfLoop(cc.__super__,cl);
  118. }
  119. #end
  120. private static function __instanceof(o : Dynamic,cl) {
  121. untyped {
  122. if( !cl )
  123. return false;
  124. if( __instanceof__(o,cl) ) {
  125. if( cl == Array )
  126. return ( o[__unprotect__("__enum__")] == null );
  127. return true;
  128. }
  129. #if flash6
  130. if( __interfLoop(o[__unprotect__("__class__")],cl) )
  131. return true;
  132. #end
  133. switch( cast cl ) {
  134. case Int:
  135. return __physeq__(Math.ceil(o),o) && isFinite(o) && !(__physeq__(o,true) || __physeq__(o,false));
  136. case Float:
  137. return __typeof__(o) == "number";
  138. case Bool:
  139. return __physeq__(o,true) || __physeq__(o,false);
  140. case String:
  141. return __typeof__(o) == "string";
  142. case Dynamic:
  143. return true;
  144. default:
  145. return o[__unprotect__("__enum__")] == cl ||
  146. (cl == Class && o[__unprotect__("__name__")] != null) ||
  147. (cl == Enum && o[__unprotect__("__ename__")] != null);
  148. }
  149. }
  150. }
  151. private static function getTrace() : flash.TextField untyped {
  152. var root = flash.Lib.current;
  153. var tf : flash.TextField = root.__trace_txt;
  154. if( tf == null ) {
  155. root.createTextField("__trace_txt",1048500,0,0,Stage.width,Stage.height+30);
  156. tf = root.__trace_txt;
  157. var format = tf.getTextFormat();
  158. format.font = "_sans";
  159. tf.setNewTextFormat(format);
  160. tf.selectable = false;
  161. tf.textColor = def_color;
  162. root.__trace_lines = new Array<String>();
  163. }
  164. return tf;
  165. }
  166. private static function __set_trace_color( rgb : Int ) {
  167. getTrace().textColor = rgb;
  168. def_color = rgb;
  169. }
  170. private static function __trace(v,inf : haxe.PosInfos) {
  171. untyped {
  172. var root = flash.Lib.current;
  173. var tf = getTrace();
  174. var s = inf.fileName+(if( inf.lineNumber == null ) "" else ":"+inf.lineNumber)+": "+__string_rec(v,"");
  175. var lines : Array<String> = root.__trace_lines["concat"](s.split("\n"));
  176. tf.text = lines.join("\n");
  177. while( tf.textHeight > Stage.height ) {
  178. lines.shift();
  179. tf.text = lines.join("\n");
  180. }
  181. root.__trace_lines = lines;
  182. }
  183. }
  184. static function __exc(v) {
  185. var s = "";
  186. #if debug
  187. var a : Array<String> = untyped __eval__("$s");
  188. for( i in 0...a.length-1 )
  189. s += "\nCalled from "+a[i];
  190. var old = a.slice(0,a.length-1);
  191. a.splice(0,a.length);
  192. #end
  193. if( untyped Lib.onerror != null )
  194. untyped Lib.onerror(__string_rec(v,""),#if debug old #else [] #end);
  195. else
  196. __trace(__string_rec(v,"")+s,cast { fileName : "(uncaught exception)" });
  197. }
  198. private static function __clear_trace() {
  199. untyped {
  200. var root = flash.Lib.current;
  201. root.__trace_txt["removeTextField"]();
  202. root.__trace_lines = null;
  203. }
  204. }
  205. private static function __init(current : Dynamic) untyped {
  206. // only if not set yet
  207. var g : Dynamic = _global;
  208. if( !g.haxeInitDone ) {
  209. g.haxeInitDone = true;
  210. Array.prototype["copy"] = Array.prototype["slice"];
  211. Array.prototype["insert"] = function(i,x) {
  212. this["splice"](i,0,x);
  213. };
  214. Array.prototype["remove"] = function(obj) {
  215. var i = 0;
  216. var l = this["length"];
  217. while( i < l ) {
  218. if( this[i] == obj ) {
  219. this["splice"](i,1);
  220. return true;
  221. }
  222. i++;
  223. }
  224. return false;
  225. }
  226. Array.prototype["iterator"] = function() {
  227. return {
  228. cur : 0,
  229. arr : this,
  230. hasNext : function() {
  231. return this.cur < this.arr["length"];
  232. },
  233. next : function() {
  234. return this.arr[this.cur++];
  235. }
  236. }
  237. };
  238. _global["ASSetPropFlags"](Array.prototype,null,7);
  239. var cca = String.prototype["charCodeAt"];
  240. String.prototype["cca"] = cca;
  241. String.prototype["charCodeAt"] = function(i) {
  242. var x = cca["call"](this,i);
  243. if( x <= 0 ) // fast NaN
  244. return null;
  245. return x;
  246. };
  247. // create flash package (in for FP7 mark support)
  248. if( _global["flash"] == null )
  249. _global["flash"] = {};
  250. }
  251. // set the Lib variables
  252. current.flash.Lib._global = _global;
  253. current.flash.Lib._root = _root;
  254. current.flash.Lib.current = current;
  255. // prevent closure creation by setting untyped
  256. current[__unprotect__("@instanceof")] = untyped __instanceof;
  257. current[__unprotect__("@closure")] = untyped __closure;
  258. // fix firefox default alignement
  259. if( _global["Stage"]["align"] == "" )
  260. _global["Stage"]["align"] = "LT";
  261. }
  262. }