Boot.hx 8.7 KB

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