Boot.hx 7.4 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. #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. lines = lines.concat((pstr +": "+__string_rec(v,"")).split("\n"));
  136. tf.text = lines.join("\n");
  137. var stage = flash.Lib.current.stage;
  138. if( stage == null )
  139. return;
  140. while( lines.length > 1 && tf.height > stage.stageHeight ) {
  141. lines.shift();
  142. tf.text = lines.join("\n");
  143. }
  144. }
  145. public static function __string_rec( v : Dynamic, str : String ) {
  146. var cname = untyped __global__["flash.utils.getQualifiedClassName"](v);
  147. switch( cname ) {
  148. case "Object":
  149. var k : Array<String> = untyped __keys__(v);
  150. var s = "{";
  151. var first = true;
  152. for( i in 0...k.length ) {
  153. var key = k[i];
  154. if( key == "toString" )
  155. try return v.toString() catch( e : Dynamic ) {}
  156. if( first )
  157. first = false;
  158. else
  159. s += ",";
  160. s += " "+key+" : "+__string_rec(v[untyped key],str);
  161. }
  162. if( !first )
  163. s += " ";
  164. s += "}";
  165. return s;
  166. case "Array":
  167. if( v == Array )
  168. return "#Array";
  169. var s = "[";
  170. var i;
  171. var first = true;
  172. var a : Array<Dynamic> = v;
  173. for( i in 0...a.length ) {
  174. if( first )
  175. first = false;
  176. else
  177. s += ",";
  178. s += __string_rec(a[i],str);
  179. }
  180. return s + "]";
  181. default:
  182. switch( untyped __typeof__(v) ) {
  183. case "function": return "<function>";
  184. case "undefined": return "null";
  185. }
  186. }
  187. return new String(v);
  188. }
  189. static function __unprotect__( s : String ) {
  190. return s;
  191. }
  192. static function __init__() untyped {
  193. var aproto = Array.prototype;
  194. aproto.copy = function() {
  195. return __this__.slice();
  196. };
  197. aproto.insert = function(i,x) {
  198. __this__.splice(i,0,x);
  199. };
  200. aproto.remove = function(obj) {
  201. var idx = __this__.indexOf(obj);
  202. if( idx == -1 ) return false;
  203. __this__.splice(idx,1);
  204. return true;
  205. }
  206. aproto.iterator = function() {
  207. var cur = 0;
  208. var arr : Array<Dynamic> = __this__;
  209. return {
  210. hasNext : function() {
  211. return cur < arr.length;
  212. },
  213. next : function() {
  214. return arr[cur++];
  215. }
  216. }
  217. };
  218. aproto.setPropertyIsEnumerable("copy", false);
  219. aproto.setPropertyIsEnumerable("insert", false);
  220. aproto.setPropertyIsEnumerable("remove", false);
  221. aproto.setPropertyIsEnumerable("iterator", false);
  222. #if as3
  223. aproto.filterHX = function(f) {
  224. var ret = [];
  225. var i = 0;
  226. var l = __this__.length;
  227. while ( i < l ) {
  228. if (f(__this__[i]))
  229. ret.push(__this__[i]);
  230. i++;
  231. }
  232. return ret;
  233. };
  234. aproto.mapHX = function(f) {
  235. var ret = [];
  236. var i = 0;
  237. var l = __this__.length;
  238. while( i < l ) {
  239. ret.push(f(__this__[i]));
  240. i++;
  241. }
  242. return ret;
  243. };
  244. aproto.setPropertyIsEnumerable("mapHX", false);
  245. aproto.setPropertyIsEnumerable("filterHX", false);
  246. String.prototype.charCodeAtHX = function(i) : Null<Int> {
  247. #else
  248. aproto.filter = function(f) {
  249. var ret = [];
  250. var i = 0;
  251. var l = __this__.length;
  252. while ( i < l ) {
  253. if (f(__this__[i]))
  254. ret.push(__this__[i]);
  255. i++;
  256. }
  257. return ret;
  258. };
  259. aproto.map = function(f) {
  260. var ret = [];
  261. var i = 0;
  262. var l = __this__.length;
  263. while( i < l ) {
  264. ret.push(f(__this__[i]));
  265. i++;
  266. }
  267. return ret;
  268. };
  269. aproto.setPropertyIsEnumerable("map", false);
  270. aproto.setPropertyIsEnumerable("filter", false);
  271. String.prototype.charCodeAt = function(i) : Null<Int> {
  272. #end
  273. var s : String = __this__;
  274. var x : Float = s.cca(i);
  275. if( __global__["isNaN"](x) )
  276. return null;
  277. return Std.int(x);
  278. };
  279. }
  280. }