Boot.hx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. @:keep private class RealBoot extends Boot {
  24. #if swc
  25. public function new() {
  26. super();
  27. }
  28. public static function initSwc(mc) {
  29. flash.Lib.current = mc;
  30. new RealBoot().init();
  31. }
  32. #else
  33. function new() {
  34. super();
  35. if (flash.Lib.current == null)
  36. flash.Lib.current = this;
  37. start();
  38. }
  39. #end
  40. }
  41. @:dox(hide)
  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. static var IN_E = 0;
  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. if (IN_E > 15) {
  80. pstr.push("...");
  81. } else {
  82. IN_E++;
  83. for (p in e.params)
  84. pstr.push(__string_rec(p, ""));
  85. IN_E--;
  86. }
  87. return e.tag + "(" + pstr.join(",") + ")";
  88. }
  89. public static function __instanceof(v:Dynamic, t:Dynamic) {
  90. try {
  91. if (t == Dynamic)
  92. return v != null;
  93. return untyped __is__(v, t);
  94. } catch (e:Dynamic) {}
  95. return false;
  96. }
  97. public static function __clear_trace() {
  98. if (tf == null)
  99. return;
  100. tf.parent.removeChild(tf);
  101. tf = null;
  102. lines = null;
  103. }
  104. public static function __set_trace_color(rgb) {
  105. var tf = getTrace();
  106. tf.textColor = rgb;
  107. tf.filters = [];
  108. }
  109. public static function getTrace() {
  110. var mc = flash.Lib.current;
  111. if (tf == null) {
  112. tf = new flash.text.TextField();
  113. #if flash10_2
  114. var color = 0xFFFFFF, glow = 0;
  115. if (mc.stage != null) {
  116. glow = mc.stage.color;
  117. color = 0xFFFFFF - glow;
  118. }
  119. tf.textColor = color;
  120. tf.filters = [new flash.filters.GlowFilter(glow, 1, 2, 2, 20)];
  121. #end
  122. var format = tf.getTextFormat();
  123. format.font = "_sans";
  124. tf.defaultTextFormat = format;
  125. tf.selectable = false;
  126. tf.width = if (mc.stage == null) 800 else mc.stage.stageWidth;
  127. tf.autoSize = flash.text.TextFieldAutoSize.LEFT;
  128. tf.mouseEnabled = false;
  129. }
  130. if (mc.stage == null)
  131. mc.addChild(tf);
  132. else
  133. mc.stage.addChild(tf); // on top
  134. return tf;
  135. }
  136. public static function __trace(v:Dynamic, pos:haxe.PosInfos) {
  137. var tf = getTrace();
  138. var pstr = if (pos == null) "(null)" else pos.fileName + ":" + pos.lineNumber;
  139. if (lines == null)
  140. lines = [];
  141. var str = pstr + ": " + __string_rec(v, "");
  142. if (pos != null && pos.customParams != null)
  143. for (v in pos.customParams)
  144. str += "," + __string_rec(v, "");
  145. lines = lines.concat(str.split("\n"));
  146. tf.text = lines.join("\n");
  147. var stage = flash.Lib.current.stage;
  148. if (stage == null)
  149. return;
  150. while (lines.length > 1 && tf.height > stage.stageHeight) {
  151. lines.shift();
  152. tf.text = lines.join("\n");
  153. }
  154. }
  155. public static function __string_rec(v:Dynamic, str:String, maxRecursion:Int = 5) {
  156. if (maxRecursion <= 0) {
  157. return "<...>";
  158. }
  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
  169. return v.toString()
  170. catch (e:Dynamic) {}
  171. if (first)
  172. first = false;
  173. else
  174. s += ",";
  175. s += " " + key + " : " + __string_rec(v[untyped key], str, maxRecursion - 1);
  176. }
  177. if (!first)
  178. s += " ";
  179. s += "}";
  180. return s;
  181. case "Array":
  182. if (v == Array)
  183. return "#Array";
  184. var s = "[";
  185. var i;
  186. var first = true;
  187. var a:Array<Dynamic> = v;
  188. for (i in 0...a.length) {
  189. if (first)
  190. first = false;
  191. else
  192. s += ",";
  193. s += __string_rec(a[i], str, maxRecursion - 1);
  194. }
  195. return s + "]";
  196. default:
  197. switch (untyped __typeof__(v)) {
  198. case "function": return "<function>";
  199. case "undefined": return "null";
  200. }
  201. }
  202. return new String(v);
  203. }
  204. static public function fromCodePoint(code:Int) {
  205. var o = new flash.utils.ByteArray();
  206. o.endian = LITTLE_ENDIAN;
  207. o.writeShort((code >> 10) + 0xD7C0);
  208. o.writeShort((code & 0x3FF) + 0xDC00);
  209. o.position = 0;
  210. return o.readMultiByte(4, "unicode");
  211. }
  212. static function __unprotect__(s:String) {
  213. return s;
  214. }
  215. static public function mapDynamic(d:Dynamic, f:Dynamic) {
  216. if (Std.isOfType(d, Array)) {
  217. return untyped d["mapHX"](f);
  218. } else {
  219. return untyped d["map"](f);
  220. }
  221. }
  222. static public function filterDynamic(d:Dynamic, f:Dynamic) {
  223. if (Std.isOfType(d, Array)) {
  224. return untyped d["filterHX"](f);
  225. } else {
  226. return untyped d["filter"](f);
  227. }
  228. }
  229. static function __init__()
  230. untyped {
  231. var d:Dynamic = Date;
  232. d.now = function() {
  233. return __new__(Date);
  234. };
  235. d.fromTime = function(t) {
  236. var d:Date = __new__(Date);
  237. d.setTime(t);
  238. return d;
  239. };
  240. d.fromString = function(s:String) {
  241. switch (s.length) {
  242. case 8: // hh:mm:ss
  243. var k = s.split(":");
  244. var d:Date = __new__(Date);
  245. d.setTime(0);
  246. d.setUTCHours(k[0]);
  247. d.setUTCMinutes(k[1]);
  248. d.setUTCSeconds(k[2]);
  249. return d;
  250. case 10: // YYYY-MM-DD
  251. var k = s.split("-");
  252. return new Date(cast k[0], cast k[1] - 1, cast k[2], 0, 0, 0);
  253. case 19: // YYYY-MM-DD hh:mm:ss
  254. var k = s.split(" ");
  255. var y = k[0].split("-");
  256. var t = k[1].split(":");
  257. return new Date(cast y[0], cast y[1] - 1, cast y[2], cast t[0], cast t[1], cast t[2]);
  258. default:
  259. throw "Invalid date format : " + s;
  260. }
  261. };
  262. d.prototype[#if no_flash_override "toStringHX" #else "toString" #end] = function() {
  263. var date:Date = __this__;
  264. var m = date.getMonth() + 1;
  265. var d = date.getDate();
  266. var h = date.getHours();
  267. var mi = date.getMinutes();
  268. var s = date.getSeconds();
  269. return date.getFullYear() + "-" + (if (m < 10) "0" + m else "" + m) + "-" + (if (d < 10) "0" + d else "" + d) + " "
  270. + (if (h < 10) "0" + h else "" + h) + ":" + (if (mi < 10) "0" + mi else "" + mi) + ":" + (if (s < 10) "0" + s else "" + s);
  271. };
  272. var aproto = Array.prototype;
  273. aproto.copy = function() {
  274. return __this__.slice();
  275. };
  276. aproto.insert = function(i, x) {
  277. __this__.splice(i, 0, x);
  278. };
  279. aproto.contains = function(obj) {
  280. return __this__.indexOf(obj) != -1;
  281. }
  282. aproto.remove = function(obj) {
  283. var idx = __this__.indexOf(obj);
  284. if (idx == -1)
  285. return false;
  286. #if flash19
  287. // removeAt is only available through as3 namespace and genswf9 will only generate it for a known Array,
  288. // so we have to type it properly (thus the typecheck). See https://github.com/HaxeFoundation/haxe/issues/8612
  289. (__this__:Array<Dynamic>).removeAt(idx);
  290. #else
  291. __this__.splice(idx, 1);
  292. #end
  293. return true;
  294. }
  295. aproto.iterator = function() {
  296. return new haxe.iterators.ArrayIterator(cast __this__);
  297. };
  298. aproto.keyValueIterator = function() {
  299. return new haxe.iterators.ArrayKeyValueIterator(untyped __this__);
  300. };
  301. aproto.resize = function(len) {
  302. __this__.length = len;
  303. };
  304. aproto.setPropertyIsEnumerable("copy", false);
  305. aproto.setPropertyIsEnumerable("insert", false);
  306. aproto.setPropertyIsEnumerable("contains", false);
  307. aproto.setPropertyIsEnumerable("remove", false);
  308. aproto.setPropertyIsEnumerable("iterator", false);
  309. aproto.setPropertyIsEnumerable("keyValueIterator", false);
  310. aproto.setPropertyIsEnumerable("resize", false);
  311. #if no_flash_override
  312. aproto.filterHX = function(f) {
  313. var ret = [];
  314. var i = 0;
  315. var l = __this__.length;
  316. while (i < l) {
  317. if (f(__this__[i]))
  318. ret.push(__this__[i]);
  319. i++;
  320. }
  321. return ret;
  322. };
  323. aproto.mapHX = function(f) {
  324. var ret = [];
  325. var i = 0;
  326. var l = __this__.length;
  327. while (i < l) {
  328. ret.push(f(__this__[i]));
  329. i++;
  330. }
  331. return ret;
  332. };
  333. aproto.setPropertyIsEnumerable("mapHX", false);
  334. aproto.setPropertyIsEnumerable("filterHX", false);
  335. String.prototype.charCodeAtHX = function(i):Null<Int> {
  336. #else
  337. aproto["filter"] = function(f) {
  338. var ret = [];
  339. var i = 0;
  340. var l = __this__.length;
  341. while (i < l) {
  342. if (f(__this__[i]))
  343. ret.push(__this__[i]);
  344. i++;
  345. }
  346. return ret;
  347. };
  348. aproto["map"] = function(f) {
  349. var ret = [];
  350. var i = 0;
  351. var l = __this__.length;
  352. while (i < l) {
  353. ret.push(f(__this__[i]));
  354. i++;
  355. }
  356. return ret;
  357. };
  358. aproto.setPropertyIsEnumerable("map", false);
  359. aproto.setPropertyIsEnumerable("filter", false);
  360. String.prototype.charCodeAt = function(i):Null<Int> {
  361. #end
  362. var s:String = __this__;
  363. var x:Float = s.cca(i);
  364. if (__global__["isNaN"](x))
  365. return null;
  366. return Std.int(x);
  367. };
  368. }
  369. }