Boot.hx 7.8 KB

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