Boot.hx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. #if !as3
  24. @:keep private class RealBoot extends Boot {
  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)
  37. flash.Lib.current = this;
  38. start();
  39. }
  40. #end
  41. }
  42. #end
  43. @:dox(hide)
  44. @:keep
  45. class Boot extends flash.display.MovieClip {
  46. static var tf:flash.text.TextField;
  47. static var lines:Array<String>;
  48. static var lastError:flash.errors.Error;
  49. public static var skip_constructor = false;
  50. function start() {
  51. #if dontWaitStage
  52. init();
  53. #else
  54. var c = flash.Lib.current;
  55. try {
  56. untyped if (c == this && c.stage != null && c.stage.align == "")
  57. c.stage.align = "TOP_LEFT";
  58. } catch (e:Dynamic) {
  59. // security error when loading from different domain
  60. }
  61. if (c.stage == null)
  62. c.addEventListener(flash.events.Event.ADDED_TO_STAGE, doInitDelay);
  63. else if (c.stage.stageWidth == 0 || c.stage.stageHeight == 0)
  64. untyped __global__["flash.utils.setTimeout"](start, 1);
  65. else
  66. init();
  67. #end
  68. }
  69. function doInitDelay(_) {
  70. flash.Lib.current.removeEventListener(flash.events.Event.ADDED_TO_STAGE, doInitDelay);
  71. start();
  72. }
  73. #if (swc && swf_protected) public #end function init() {
  74. throw "assert";
  75. }
  76. static var IN_E = 0;
  77. public static function enum_to_string(e:{tag:String, params:Array<Dynamic>}) {
  78. if (e.params == null)
  79. return e.tag;
  80. var pstr = [];
  81. if (IN_E > 15) {
  82. pstr.push("...");
  83. } else {
  84. IN_E++;
  85. for (p in e.params)
  86. pstr.push(__string_rec(p, ""));
  87. IN_E--;
  88. }
  89. return e.tag + "(" + pstr.join(",") + ")";
  90. }
  91. public static function __instanceof(v:Dynamic, t:Dynamic) {
  92. try {
  93. if (t == Dynamic)
  94. return v != null;
  95. return untyped __is__(v, t);
  96. } catch (e:Dynamic) {}
  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)
  142. lines = [];
  143. var str = pstr + ": " + __string_rec(v, "");
  144. if (pos != null && pos.customParams != null)
  145. for (v in pos.customParams)
  146. str += "," + __string_rec(v, "");
  147. lines = lines.concat(str.split("\n"));
  148. tf.text = lines.join("\n");
  149. var stage = flash.Lib.current.stage;
  150. if (stage == null)
  151. return;
  152. while (lines.length > 1 && tf.height > stage.stageHeight) {
  153. lines.shift();
  154. tf.text = lines.join("\n");
  155. }
  156. }
  157. public static function __string_rec(v:Dynamic, str:String, maxRecursion:Int = 5) {
  158. if (maxRecursion <= 0) {
  159. return "<...>";
  160. }
  161. var cname = untyped __global__["flash.utils.getQualifiedClassName"](v);
  162. switch (cname) {
  163. case "Object":
  164. var k:Array<String> = untyped __keys__(v);
  165. var s = "{";
  166. var first = true;
  167. for (i in 0...k.length) {
  168. var key = k[i];
  169. if (key == "toString")
  170. try
  171. return v.toString()
  172. catch (e:Dynamic) {}
  173. if (first)
  174. first = false;
  175. else
  176. s += ",";
  177. s += " " + key + " : " + __string_rec(v[untyped key], str, maxRecursion - 1);
  178. }
  179. if (!first)
  180. s += " ";
  181. s += "}";
  182. return s;
  183. case "Array":
  184. if (v == Array)
  185. return "#Array";
  186. var s = "[";
  187. var i;
  188. var first = true;
  189. var a:Array<Dynamic> = v;
  190. for (i in 0...a.length) {
  191. if (first)
  192. first = false;
  193. else
  194. s += ",";
  195. s += __string_rec(a[i], str, maxRecursion - 1);
  196. }
  197. return s + "]";
  198. default:
  199. switch (untyped __typeof__(v)) {
  200. case "function": return "<function>";
  201. case "undefined": return "null";
  202. }
  203. }
  204. return new String(v);
  205. }
  206. static public function fromCodePoint(code:Int) {
  207. var o = new flash.utils.ByteArray();
  208. o.endian = LITTLE_ENDIAN;
  209. o.writeShort((code >> 10) + 0xD7C0);
  210. o.writeShort((code & 0x3FF) + 0xDC00);
  211. o.position = 0;
  212. return o.readMultiByte(4, "unicode");
  213. }
  214. static function __unprotect__(s:String) {
  215. return s;
  216. }
  217. static public function mapDynamic(d:Dynamic, f:Dynamic) {
  218. if (Std.is(d, Array)) {
  219. return untyped d["mapHX"](f);
  220. } else {
  221. return untyped d["map"](f);
  222. }
  223. }
  224. static public function filterDynamic(d:Dynamic, f:Dynamic) {
  225. if (Std.is(d, Array)) {
  226. return untyped d["filterHX"](f);
  227. } else {
  228. return untyped d["filter"](f);
  229. }
  230. }
  231. static function __init__()
  232. untyped {
  233. var d:Dynamic = Date;
  234. d.now = function() {
  235. return __new__(Date);
  236. };
  237. d.fromTime = function(t) {
  238. var d:Date = __new__(Date);
  239. d.setTime(t);
  240. return d;
  241. };
  242. d.fromString = function(s:String) {
  243. switch (s.length) {
  244. case 8: // hh:mm:ss
  245. var k = s.split(":");
  246. var d:Date = __new__(Date);
  247. d.setTime(0);
  248. d.setUTCHours(k[0]);
  249. d.setUTCMinutes(k[1]);
  250. d.setUTCSeconds(k[2]);
  251. return d;
  252. case 10: // YYYY-MM-DD
  253. var k = s.split("-");
  254. return new Date(cast k[0], cast k[1] - 1, cast k[2], 0, 0, 0);
  255. case 19: // YYYY-MM-DD hh:mm:ss
  256. var k = s.split(" ");
  257. var y = k[0].split("-");
  258. var t = k[1].split(":");
  259. return new Date(cast y[0], cast y[1] - 1, cast y[2], cast t[0], cast t[1], cast t[2]);
  260. default:
  261. throw "Invalid date format : " + s;
  262. }
  263. };
  264. d.prototype[#if (as3 || no_flash_override) "toStringHX" #else "toString" #end] = function() {
  265. var date:Date = __this__;
  266. var m = date.getMonth() + 1;
  267. var d = date.getDate();
  268. var h = date.getHours();
  269. var mi = date.getMinutes();
  270. var s = date.getSeconds();
  271. return date.getFullYear() + "-" + (if (m < 10) "0" + m else "" + m) + "-" + (if (d < 10) "0" + d else "" + d) + " "
  272. + (if (h < 10) "0" + h else "" + h) + ":" + (if (mi < 10) "0" + mi else "" + mi) + ":" + (if (s < 10) "0" + s else "" + s);
  273. };
  274. var aproto = Array.prototype;
  275. aproto.copy = function() {
  276. return __this__.slice();
  277. };
  278. aproto.insert = function(i, x) {
  279. __this__.splice(i, 0, x);
  280. };
  281. aproto.remove = function(obj) {
  282. var idx = __this__.indexOf(obj);
  283. if (idx == -1)
  284. return false;
  285. #if flash19
  286. __this__.removeAt(idx);
  287. #else
  288. __this__.splice(idx, 1);
  289. #end
  290. return true;
  291. }
  292. aproto.iterator = function() {
  293. var cur = 0;
  294. var arr:Array<Dynamic> = __this__;
  295. return {
  296. hasNext: function() {
  297. return cur < arr.length;
  298. },
  299. next: function() {
  300. return arr[cur++];
  301. }
  302. }
  303. };
  304. aproto.resize = function(len) {
  305. __this__.length = len;
  306. };
  307. aproto.setPropertyIsEnumerable("copy", false);
  308. aproto.setPropertyIsEnumerable("insert", false);
  309. aproto.setPropertyIsEnumerable("remove", false);
  310. aproto.setPropertyIsEnumerable("iterator", false);
  311. aproto.setPropertyIsEnumerable("resize", false);
  312. #if (as3 || no_flash_override)
  313. aproto.filterHX = function(f) {
  314. var ret = [];
  315. var i = 0;
  316. var l = __this__.length;
  317. while (i < l) {
  318. if (f(__this__[i]))
  319. ret.push(__this__[i]);
  320. i++;
  321. }
  322. return ret;
  323. };
  324. aproto.mapHX = function(f) {
  325. var ret = [];
  326. var i = 0;
  327. var l = __this__.length;
  328. while (i < l) {
  329. ret.push(f(__this__[i]));
  330. i++;
  331. }
  332. return ret;
  333. };
  334. aproto.setPropertyIsEnumerable("mapHX", false);
  335. aproto.setPropertyIsEnumerable("filterHX", false);
  336. String.prototype.charCodeAtHX = function(i):Null<Int> {
  337. #else
  338. aproto["filter"] = function(f) {
  339. var ret = [];
  340. var i = 0;
  341. var l = __this__.length;
  342. while (i < l) {
  343. if (f(__this__[i]))
  344. ret.push(__this__[i]);
  345. i++;
  346. }
  347. return ret;
  348. };
  349. aproto["map"] = function(f) {
  350. var ret = [];
  351. var i = 0;
  352. var l = __this__.length;
  353. while (i < l) {
  354. ret.push(f(__this__[i]));
  355. i++;
  356. }
  357. return ret;
  358. };
  359. aproto.setPropertyIsEnumerable("map", false);
  360. aproto.setPropertyIsEnumerable("filter", false);
  361. String.prototype.charCodeAt = function(i):Null<Int> {
  362. #end
  363. var s:String = __this__;
  364. var x:Float = s.cca(i);
  365. if (__global__["isNaN"](x))
  366. return null;
  367. return Std.int(x);
  368. };
  369. }
  370. }