Boot.hx 8.0 KB

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