Boot.hx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. #if !as3
  24. @:keep private class RealBoot extends Boot implements Dynamic {
  25. #if swc
  26. public function new() {
  27. super();
  28. }
  29. public static function initSwc(mc) {
  30. flash.Lib.current = mc;
  31. new RealBoot().init();
  32. }
  33. #else
  34. function new() {
  35. super();
  36. if( flash.Lib.current == null ) flash.Lib.current = this;
  37. start();
  38. }
  39. #end
  40. }
  41. #end
  42. @:keep
  43. class Boot extends flash.display.MovieClip {
  44. static var tf : flash.text.TextField;
  45. static var lines : Array<String>;
  46. static var lastError : flash.errors.Error;
  47. public static var skip_constructor = false;
  48. function start() {
  49. #if dontWaitStage
  50. init();
  51. #else
  52. var c = flash.Lib.current;
  53. try {
  54. untyped if( c == this && c.stage != null && c.stage.align == "" )
  55. c.stage.align = "TOP_LEFT";
  56. } catch( e : Dynamic ) {
  57. // security error when loading from different domain
  58. }
  59. if( c.stage == null )
  60. c.addEventListener(flash.events.Event.ADDED_TO_STAGE, doInitDelay);
  61. else if( c.stage.stageWidth == 0 || c.stage.stageHeight == 0 )
  62. untyped __global__["flash.utils.setTimeout"](start,1);
  63. else
  64. init();
  65. #end
  66. }
  67. function doInitDelay(_) {
  68. flash.Lib.current.removeEventListener(flash.events.Event.ADDED_TO_STAGE, doInitDelay);
  69. start();
  70. }
  71. #if (swc && swf_protected) public #end function init() {
  72. throw "assert";
  73. }
  74. static var IN_E = 0;
  75. public static function enum_to_string( e : { tag : String, params : Array<Dynamic> } ) {
  76. if( e.params == null )
  77. return e.tag;
  78. var pstr = [];
  79. if( IN_E > 15 ) {
  80. pstr.push("...");
  81. } else {
  82. IN_E++;
  83. for( p in e.params )
  84. pstr.push(__string_rec(p, ""));
  85. IN_E--;
  86. }
  87. return e.tag+"("+pstr.join(",")+")";
  88. }
  89. public static function __instanceof( v : Dynamic, t : Dynamic ) {
  90. try {
  91. if( t == Dynamic )
  92. return true;
  93. return untyped __is__(v,t);
  94. } catch( e : Dynamic ) {
  95. }
  96. return false;
  97. }
  98. public static function __clear_trace() {
  99. if( tf == null )
  100. return;
  101. tf.parent.removeChild(tf);
  102. tf = null;
  103. lines = null;
  104. }
  105. public static function __set_trace_color(rgb) {
  106. var tf = getTrace();
  107. tf.textColor = rgb;
  108. tf.filters = [];
  109. }
  110. public static function getTrace() {
  111. var mc = flash.Lib.current;
  112. if( tf == null ) {
  113. tf = new flash.text.TextField();
  114. #if flash10_2
  115. var color = 0xFFFFFF, glow = 0;
  116. if( mc.stage != null ) {
  117. glow = mc.stage.color;
  118. color = 0xFFFFFF - glow;
  119. }
  120. tf.textColor = color;
  121. tf.filters = [new flash.filters.GlowFilter(glow, 1, 2, 2, 20)];
  122. #end
  123. var format = tf.getTextFormat();
  124. format.font = "_sans";
  125. tf.defaultTextFormat = format;
  126. tf.selectable = false;
  127. tf.width = if( mc.stage == null ) 800 else mc.stage.stageWidth;
  128. tf.autoSize = flash.text.TextFieldAutoSize.LEFT;
  129. tf.mouseEnabled = false;
  130. }
  131. if( mc.stage == null )
  132. mc.addChild(tf);
  133. else
  134. mc.stage.addChild(tf); // on top
  135. return tf;
  136. }
  137. public static function __trace( v : Dynamic, pos : haxe.PosInfos ) {
  138. var tf = getTrace();
  139. var pstr = if( pos == null ) "(null)" else pos.fileName+":"+pos.lineNumber;
  140. if( lines == null ) lines = [];
  141. var str = pstr +": "+__string_rec(v, "");
  142. if( pos != null && pos.customParams != null )
  143. for( v in pos.customParams )
  144. str += ","+__string_rec(v, "");
  145. lines = lines.concat(str.split("\n"));
  146. tf.text = lines.join("\n");
  147. var stage = flash.Lib.current.stage;
  148. if( stage == null )
  149. return;
  150. while( lines.length > 1 && tf.height > stage.stageHeight ) {
  151. lines.shift();
  152. tf.text = lines.join("\n");
  153. }
  154. }
  155. public static function __string_rec( v : Dynamic, str : String ) {
  156. var cname = untyped __global__["flash.utils.getQualifiedClassName"](v);
  157. switch( cname ) {
  158. case "Object":
  159. var k : Array<String> = untyped __keys__(v);
  160. var s = "{";
  161. var first = true;
  162. for( i in 0...k.length ) {
  163. var key = k[i];
  164. if( key == "toString" )
  165. try return v.toString() catch( e : Dynamic ) {}
  166. if( first )
  167. first = false;
  168. else
  169. s += ",";
  170. s += " "+key+" : "+__string_rec(v[untyped key],str);
  171. }
  172. if( !first )
  173. s += " ";
  174. s += "}";
  175. return s;
  176. case "Array":
  177. if( v == Array )
  178. return "#Array";
  179. var s = "[";
  180. var i;
  181. var first = true;
  182. var a : Array<Dynamic> = v;
  183. for( i in 0...a.length ) {
  184. if( first )
  185. first = false;
  186. else
  187. s += ",";
  188. s += __string_rec(a[i],str);
  189. }
  190. return s + "]";
  191. default:
  192. switch( untyped __typeof__(v) ) {
  193. case "function": return "<function>";
  194. case "undefined": return "null";
  195. }
  196. }
  197. return new String(v);
  198. }
  199. static function __unprotect__( s : String ) {
  200. return s;
  201. }
  202. static public function mapDynamic(d:Dynamic, f:Dynamic) {
  203. if (Std.is(d, Array)) {
  204. return untyped d["mapHX"](f);
  205. } else {
  206. return untyped d["map"](f);
  207. }
  208. }
  209. static public function filterDynamic(d:Dynamic, f:Dynamic) {
  210. if (Std.is(d, Array)) {
  211. return untyped d["filterHX"](f);
  212. } else {
  213. return untyped d["filter"](f);
  214. }
  215. }
  216. static function __init__() untyped {
  217. var aproto = Array.prototype;
  218. aproto.copy = function() {
  219. return __this__.slice();
  220. };
  221. aproto.insert = function(i,x) {
  222. __this__.splice(i,0,x);
  223. };
  224. aproto.remove = function(obj) {
  225. var idx = __this__.indexOf(obj);
  226. if( idx == -1 ) return false;
  227. __this__.splice(idx,1);
  228. return true;
  229. }
  230. aproto.iterator = function() {
  231. var cur = 0;
  232. var arr : Array<Dynamic> = __this__;
  233. return {
  234. hasNext : function() {
  235. return cur < arr.length;
  236. },
  237. next : function() {
  238. return arr[cur++];
  239. }
  240. }
  241. };
  242. aproto.setPropertyIsEnumerable("copy", false);
  243. aproto.setPropertyIsEnumerable("insert", false);
  244. aproto.setPropertyIsEnumerable("remove", false);
  245. aproto.setPropertyIsEnumerable("iterator", false);
  246. #if (as3 || no_flash_override)
  247. aproto.filterHX = function(f) {
  248. var ret = [];
  249. var i = 0;
  250. var l = __this__.length;
  251. while ( i < l ) {
  252. if (f(__this__[i]))
  253. ret.push(__this__[i]);
  254. i++;
  255. }
  256. return ret;
  257. };
  258. aproto.mapHX = function(f) {
  259. var ret = [];
  260. var i = 0;
  261. var l = __this__.length;
  262. while( i < l ) {
  263. ret.push(f(__this__[i]));
  264. i++;
  265. }
  266. return ret;
  267. };
  268. aproto.sliceHX = function(?s = 0, ?e = 16777215) {
  269. if (e == null) e = 16777215;
  270. return __this__.slice(s,e);
  271. };
  272. aproto.setPropertyIsEnumerable("mapHX", false);
  273. aproto.setPropertyIsEnumerable("filterHX", false);
  274. aproto.setPropertyIsEnumerable("sliceHX", false);
  275. String.prototype.charCodeAtHX = function(i) : Null<Int> {
  276. #else
  277. aproto["filter"] = function(f) {
  278. var ret = [];
  279. var i = 0;
  280. var l = __this__.length;
  281. while ( i < l ) {
  282. if (f(__this__[i]))
  283. ret.push(__this__[i]);
  284. i++;
  285. }
  286. return ret;
  287. };
  288. aproto["map"] = function(f) {
  289. var ret = [];
  290. var i = 0;
  291. var l = __this__.length;
  292. while( i < l ) {
  293. ret.push(f(__this__[i]));
  294. i++;
  295. }
  296. return ret;
  297. };
  298. aproto["slice_o"] = aproto["slice"];
  299. aproto["slice"] = function(?s = 0, ?e = 16777215) {
  300. if (e == null) e = 16777215;
  301. return __this__.slice_o(s,e);
  302. };
  303. aproto.setPropertyIsEnumerable("map", false);
  304. aproto.setPropertyIsEnumerable("filter", false);
  305. aproto.setPropertyIsEnumerable("slice", false);
  306. aproto.setPropertyIsEnumerable("slice_o", false);
  307. String.prototype.charCodeAt = function(i) : Null<Int> {
  308. #end
  309. var s : String = __this__;
  310. var x : Float = s.cca(i);
  311. if( __global__["isNaN"](x) )
  312. return null;
  313. return Std.int(x);
  314. };
  315. }
  316. }