Boot.hx 7.8 KB

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