Boot.hx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package flash;
  23. @:keep
  24. class Boot {
  25. private static var def_color = 0;
  26. private static var exception = null;
  27. private static function __string_rec(o : Dynamic,s : String) {
  28. untyped {
  29. if( s.length >= 20 )
  30. return "<...>"; // too much deep recursion
  31. var t = __typeof__(o);
  32. if( t == "movieclip" )
  33. t = "object";
  34. else if( t == "function" && (o.__name__ != null || o.__ename__ != null) )
  35. t = "object";
  36. switch( t ) {
  37. case "object":
  38. if( __instanceof__(o,Array) ) {
  39. if( o.__enum__ != null ) {
  40. if( o["length"] == 2 )
  41. return o[0];
  42. var str = o[0]+"(";
  43. s += " ";
  44. for( i in 2...o["length"] ) {
  45. if( i != 2 )
  46. str += "," + __string_rec(o[i],s);
  47. else
  48. str += __string_rec(o[i],s);
  49. }
  50. return str + ")";
  51. }
  52. var l = o["length"];
  53. var i;
  54. var str = "[";
  55. s += " ";
  56. for( i in 0...l )
  57. str += (if (i > 0) "," else "")+__string_rec(o[i],s);
  58. str += "]";
  59. return str;
  60. }
  61. var s2 = o["toString"]();
  62. if( (__typeof__(s2) == "string" || __instanceof__(s2,String)) && s2 != "[object Object]" && s2 != "[type Function]" )
  63. return s2;
  64. var k;
  65. var str = "{\n";
  66. if( typeof(o) == "movieclip" )
  67. str = "MC("+o._name+") "+str;
  68. s += " ";
  69. var keys : Array<String> = __keys__(o);
  70. for( k in keys.iterator() ) {
  71. if( k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" )
  72. continue;
  73. if( str.length != 2 )
  74. str += ",\n";
  75. str += s + k + " : "+__string_rec(o[k],s);
  76. }
  77. s = s.substring(4);
  78. if( str.length != 2 )
  79. str += "\n";
  80. str += s + "}";
  81. return str;
  82. case "function":
  83. return "<function>";
  84. case "string":
  85. return o;
  86. default:
  87. return String(o);
  88. }
  89. }
  90. }
  91. private static function __closure(f,o) {
  92. untyped {
  93. var m = o[f];
  94. if( m == null )
  95. return null;
  96. var f2 = function() {
  97. var me = __arguments__["callee"];
  98. return me["f"]["apply"](me["o"],__arguments__);
  99. };
  100. f2["f"] = m;
  101. f2["o"] = o;
  102. return f2;
  103. }
  104. }
  105. #if flash6
  106. private static function __interfLoop(cc : Dynamic,cl : Dynamic) {
  107. if( cc == null )
  108. return false;
  109. var intf : Array<Dynamic> = cc.__interfaces__;
  110. for( i in 0...intf.length ) {
  111. var i = intf[i];
  112. if( i == cl || __interfLoop(i,cl) )
  113. return true;
  114. }
  115. return __interfLoop(cc.__super__,cl);
  116. }
  117. #end
  118. private static function __instanceof(o : Dynamic,cl) {
  119. untyped {
  120. if( !cl )
  121. return false;
  122. if( __instanceof__(o,cl) ) {
  123. if( cl == Array )
  124. return ( o[__unprotect__("__enum__")] == null );
  125. return true;
  126. }
  127. #if flash6
  128. if( __interfLoop(o[__unprotect__("__class__")],cl) )
  129. return true;
  130. #end
  131. switch( cast cl ) {
  132. case Int:
  133. return __typeof__(o) == "number" && __physeq__(Math.ceil(o),o%2147483648.0) && !(__physeq__(o,true) || __physeq__(o,false));
  134. case Float:
  135. return __typeof__(o) == "number";
  136. case Bool:
  137. return __physeq__(o,true) || __physeq__(o,false);
  138. case String:
  139. return __typeof__(o) == "string";
  140. case Dynamic:
  141. return true;
  142. default:
  143. return o[__unprotect__("__enum__")] == cl ||
  144. (cl == Class && o[__unprotect__("__name__")] != null) ||
  145. (cl == Enum && o[__unprotect__("__ename__")] != null);
  146. }
  147. }
  148. }
  149. private static function getTrace() : flash.TextField untyped {
  150. var root = flash.Lib.current;
  151. var tf : flash.TextField = root.__trace_txt;
  152. if( tf == null ) {
  153. var w = Stage.width, h = Stage.height;
  154. if( w == 0 ) w = 800;
  155. if( h == 0 ) h = 600;
  156. root.createTextField("__trace_txt",1048500,0,0,w,h+30);
  157. tf = root.__trace_txt;
  158. var format = tf.getTextFormat();
  159. format.font = "_sans";
  160. tf.setNewTextFormat(format);
  161. tf.selectable = false;
  162. tf.textColor = def_color;
  163. root.__trace_lines = new Array<String>();
  164. }
  165. return tf;
  166. }
  167. private static function __set_trace_color( rgb : Int ) {
  168. getTrace().textColor = rgb;
  169. def_color = rgb;
  170. }
  171. private static function __trace(v,inf : haxe.PosInfos) {
  172. untyped {
  173. var root = flash.Lib.current;
  174. var tf = getTrace();
  175. var s = inf.fileName+(if( inf.lineNumber == null ) "" else ":"+inf.lineNumber)+": "+__string_rec(v,"");
  176. var lines : Array<String> = root.__trace_lines["concat"](s.split("\n"));
  177. tf.text = lines.join("\n");
  178. while( lines.length > 1 && tf.textHeight > Stage.height ) {
  179. lines.shift();
  180. tf.text = lines.join("\n");
  181. }
  182. root.__trace_lines = lines;
  183. }
  184. }
  185. static function __exc(v) {
  186. var s = "";
  187. #if debug
  188. var a : Array<String> = untyped __eval__("$s");
  189. for( i in 0...a.length-1 )
  190. s += "\nCalled from "+a[i];
  191. var old = a.slice(0,a.length-1);
  192. a.splice(0,a.length);
  193. #end
  194. if( untyped Lib.onerror != null )
  195. untyped Lib.onerror(__string_rec(v,""),#if debug old #else [] #end);
  196. else
  197. __trace(__string_rec(v,"")+s,cast { fileName : "(uncaught exception)" });
  198. }
  199. private static function __clear_trace() {
  200. untyped {
  201. var root = flash.Lib.current;
  202. root.__trace_txt["removeTextField"]();
  203. root.__trace_lines = null;
  204. }
  205. }
  206. private static function __init(current : Dynamic) untyped {
  207. // only if not set yet
  208. var g : Dynamic = _global;
  209. if( !g.haxeInitDone ) {
  210. g.haxeInitDone = true;
  211. Array.prototype["copy"] = Array.prototype["slice"];
  212. Array.prototype["insert"] = function(i,x) {
  213. __this__["splice"](i,0,x);
  214. };
  215. Array.prototype["remove"] = function(obj) {
  216. var i = 0;
  217. var l = __this__["length"];
  218. while( i < l ) {
  219. if( __this__[i] == obj ) {
  220. __this__["splice"](i,1);
  221. return true;
  222. }
  223. i++;
  224. }
  225. return false;
  226. }
  227. Array.prototype["iterator"] = function() {
  228. return {
  229. cur : 0,
  230. arr : __this__,
  231. hasNext : function() {
  232. return __this__.cur < __this__.arr["length"];
  233. },
  234. next : function() {
  235. return __this__.arr[__this__.cur++];
  236. }
  237. }
  238. };
  239. Array.prototype["map"] = function(f) {
  240. var ret = [];
  241. var i = 0;
  242. var l = __this__["length"];
  243. while( i < l ) {
  244. ret.push(f(__this__[i]));
  245. i++;
  246. }
  247. return ret;
  248. };
  249. Array.prototype["filter"] = function(f) {
  250. var ret = [];
  251. var i = 0;
  252. var l = __this__["length"];
  253. while ( i < l ) {
  254. if (f(__this__[i]))
  255. ret.push(__this__[i]);
  256. i++;
  257. }
  258. return ret;
  259. };
  260. _global["ASSetPropFlags"](Array.prototype,null,7);
  261. var cca = String.prototype["charCodeAt"];
  262. String.prototype["cca"] = cca;
  263. String.prototype["charCodeAt"] = function(i) {
  264. var x = __this__["cca"](i);
  265. if( x <= 0 ) // fast NaN
  266. return null;
  267. return x;
  268. };
  269. // create flash package (in for FP7 mark support)
  270. if( _global["flash"] == null )
  271. _global["flash"] = {};
  272. }
  273. // set the Lib variables
  274. current.flash.Lib._global = _global;
  275. current.flash.Lib._root = _root;
  276. current.flash.Lib.current = current;
  277. // prevent closure creation by setting untyped
  278. current[__unprotect__("@instanceof")] = flash.Boot[__unprotect__("__instanceof")];
  279. current[__unprotect__("@closure")] = flash.Boot[__unprotect__("__closure")];
  280. // fix firefox default alignement
  281. if( _global["Stage"]["align"] == "" )
  282. _global["Stage"]["align"] = "LT";
  283. #if mt mt.flash.Init.check(); #end
  284. }
  285. }