Boot.hx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (c) 2005-2008, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package flash;
  26. #if !as3
  27. @:keep private class RealBoot extends Boot, implements Dynamic {
  28. #if swc
  29. public function new() {
  30. super();
  31. }
  32. public static function init(mc) {
  33. flash.Lib.current = mc;
  34. new RealBoot().init();
  35. }
  36. #else
  37. function new() {
  38. super();
  39. if( flash.Lib.current == null ) flash.Lib.current = this;
  40. start();
  41. }
  42. #end
  43. }
  44. #end
  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 (mt && !doc_gen) mt.flash.Init.check(); #end
  53. #if dontWaitStage
  54. init();
  55. #else
  56. var c = flash.Lib.current;
  57. try {
  58. untyped if( c == this && c.stage != null && c.stage.align == "" )
  59. c.stage.align = "TOP_LEFT";
  60. } catch( e : Dynamic ) {
  61. // security error when loading from different domain
  62. }
  63. if( c.stage == null )
  64. c.addEventListener(flash.events.Event.ADDED_TO_STAGE, doInitDelay);
  65. else if( c.stage.stageWidth == 0 )
  66. untyped __global__["flash.utils.setTimeout"](start,1);
  67. else
  68. init();
  69. #end
  70. }
  71. function doInitDelay(_) {
  72. flash.Lib.current.removeEventListener(flash.events.Event.ADDED_TO_STAGE, doInitDelay);
  73. start();
  74. }
  75. #if (swc && swf_protected) public #end function init() {
  76. throw "assert";
  77. }
  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. for( p in e.params )
  83. pstr.push(__string_rec(p,""));
  84. return e.tag+"("+pstr.join(",")+")";
  85. }
  86. public static function __instanceof( v : Dynamic, t : Dynamic ) {
  87. try {
  88. if( t == Dynamic )
  89. return true;
  90. return untyped __is__(v,t);
  91. } catch( e : Dynamic ) {
  92. }
  93. return false;
  94. }
  95. public static function __clear_trace() {
  96. if( tf == null )
  97. return;
  98. tf.parent.removeChild(tf);
  99. tf = null;
  100. lines = null;
  101. }
  102. public static function __set_trace_color(rgb) {
  103. var tf = getTrace();
  104. tf.textColor = rgb;
  105. tf.filters = [];
  106. }
  107. public static function getTrace() {
  108. var mc = flash.Lib.current;
  109. if( tf == null ) {
  110. tf = new flash.text.TextField();
  111. #if flash10_2
  112. var color = 0xFFFFFF, glow = 0;
  113. if( mc.stage != null ) {
  114. glow = mc.stage.color;
  115. color = 0xFFFFFF - glow;
  116. }
  117. tf.textColor = color;
  118. tf.filters = [new flash.filters.GlowFilter(glow, 1, 2, 2, 20)];
  119. #end
  120. var format = tf.getTextFormat();
  121. format.font = "_sans";
  122. tf.defaultTextFormat = format;
  123. tf.selectable = false;
  124. tf.width = if( mc.stage == null ) 800 else mc.stage.stageWidth;
  125. tf.autoSize = flash.text.TextFieldAutoSize.LEFT;
  126. tf.mouseEnabled = false;
  127. }
  128. if( mc.stage == null )
  129. mc.addChild(tf);
  130. else
  131. mc.stage.addChild(tf); // on top
  132. return tf;
  133. }
  134. public static function __trace( v : Dynamic, pos : haxe.PosInfos ) {
  135. var tf = getTrace();
  136. var pstr = if( pos == null ) "(null)" else pos.fileName+":"+pos.lineNumber;
  137. if( lines == null ) lines = [];
  138. lines = lines.concat((pstr +": "+__string_rec(v,"")).split("\n"));
  139. tf.text = lines.join("\n");
  140. var stage = flash.Lib.current.stage;
  141. if( stage == null )
  142. return;
  143. while( lines.length > 1 && tf.height > stage.stageHeight ) {
  144. lines.shift();
  145. tf.text = lines.join("\n");
  146. }
  147. }
  148. public static function __string_rec( v : Dynamic, str : String ) {
  149. var cname = untyped __global__["flash.utils.getQualifiedClassName"](v);
  150. switch( cname ) {
  151. case "Object":
  152. var k : Array<String> = untyped __keys__(v);
  153. var s = "{";
  154. var first = true;
  155. for( i in 0...k.length ) {
  156. var key = k[i];
  157. if( key == "toString" )
  158. try return v.toString() catch( e : Dynamic ) {}
  159. if( first )
  160. first = false;
  161. else
  162. s += ",";
  163. s += " "+key+" : "+__string_rec(v[untyped key],str);
  164. }
  165. if( !first )
  166. s += " ";
  167. s += "}";
  168. return s;
  169. case "Array":
  170. if( v == Array )
  171. return "#Array";
  172. var s = "[";
  173. var i;
  174. var first = true;
  175. var a : Array<Dynamic> = v;
  176. for( i in 0...a.length ) {
  177. if( first )
  178. first = false;
  179. else
  180. s += ",";
  181. s += __string_rec(a[i],str);
  182. }
  183. return s+"]";
  184. default:
  185. switch( untyped __typeof__(v) ) {
  186. case "function": return "<function>";
  187. }
  188. }
  189. return new String(v);
  190. }
  191. static function __unprotect__( s : String ) {
  192. return s;
  193. }
  194. static function __init__() untyped {
  195. var aproto = Array.prototype;
  196. aproto.copy = function() {
  197. return __this__.slice();
  198. };
  199. aproto.insert = function(i,x) {
  200. __this__.splice(i,0,x);
  201. };
  202. aproto.remove = function(obj) {
  203. var idx = __this__.indexOf(obj);
  204. if( idx == -1 ) return false;
  205. __this__.splice(idx,1);
  206. return true;
  207. }
  208. aproto.iterator = function() {
  209. var cur = 0;
  210. var arr : Array<Dynamic> = __this__;
  211. return {
  212. hasNext : function() {
  213. return cur < arr.length;
  214. },
  215. next : function() {
  216. return arr[cur++];
  217. }
  218. }
  219. };
  220. aproto.setPropertyIsEnumerable("copy", false);
  221. aproto.setPropertyIsEnumerable("insert", false);
  222. aproto.setPropertyIsEnumerable("remove", false);
  223. aproto.setPropertyIsEnumerable("iterator", false);
  224. #if as3
  225. String.prototype.charCodeAtHX = function(i) : Null<Int> {
  226. #else
  227. String.prototype.charCodeAt = function(i) : Null<Int> {
  228. #end
  229. var s : String = __this__;
  230. var x : Float = s.cca(i);
  231. if( __global__["isNaN"](x) )
  232. return null;
  233. return Std.int(x);
  234. };
  235. }
  236. }