Boot.hx 8.0 KB

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