Boot.hx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. 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. 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 mt mt.flash.Init.check(); #end
  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 )
  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. 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. for( p in e.params )
  82. pstr.push(__string_rec(p,""));
  83. return e.tag+"("+pstr.join(",")+")";
  84. }
  85. public static function __instanceof( v : Dynamic, t : Dynamic ) {
  86. try {
  87. if( t == Dynamic )
  88. return true;
  89. return untyped __is__(v,t);
  90. } catch( e : Dynamic ) {
  91. }
  92. return false;
  93. }
  94. public static function __clear_trace() {
  95. if( tf == null )
  96. return;
  97. tf.parent.removeChild(tf);
  98. tf = null;
  99. lines = null;
  100. }
  101. public static function __set_trace_color(rgb) {
  102. getTrace().textColor = rgb;
  103. }
  104. public static function getTrace() {
  105. var mc = flash.Lib.current;
  106. if( tf == null ) {
  107. tf = new flash.text.TextField();
  108. var format = tf.getTextFormat();
  109. format.font = "_sans";
  110. tf.defaultTextFormat = format;
  111. tf.selectable = false;
  112. tf.width = if( mc.stage == null ) 800 else mc.stage.stageWidth;
  113. tf.autoSize = flash.text.TextFieldAutoSize.LEFT;
  114. tf.mouseEnabled = false;
  115. }
  116. if( mc.stage == null )
  117. mc.addChild(tf);
  118. else
  119. mc.stage.addChild(tf); // on top
  120. return tf;
  121. }
  122. public static function __trace( v : Dynamic, pos : haxe.PosInfos ) {
  123. var tf = getTrace();
  124. var pstr = if( pos == null ) "(null)" else pos.fileName+":"+pos.lineNumber;
  125. if( lines == null ) lines = [];
  126. lines = lines.concat((pstr +": "+__string_rec(v,"")).split("\n"));
  127. tf.text = lines.join("\n");
  128. var stage = flash.Lib.current.stage;
  129. if( stage == null )
  130. return;
  131. while( lines.length > 1 && tf.height > stage.stageHeight ) {
  132. lines.shift();
  133. tf.text = lines.join("\n");
  134. }
  135. }
  136. public static function __string_rec( v : Dynamic, str : String ) {
  137. var cname = untyped __global__["flash.utils.getQualifiedClassName"](v);
  138. switch( cname ) {
  139. case "Object":
  140. var k : Array<String> = untyped __keys__(v);
  141. var s = "{";
  142. var first = true;
  143. for( i in 0...k.length ) {
  144. var key = k[i];
  145. if( key == "toString" )
  146. try return v.toString() catch( e : Dynamic ) {}
  147. if( first )
  148. first = false;
  149. else
  150. s += ",";
  151. s += " "+key+" : "+__string_rec(v[untyped key],str);
  152. }
  153. if( !first )
  154. s += " ";
  155. s += "}";
  156. return s;
  157. case "Array":
  158. if( v == Array )
  159. return "#Array";
  160. var s = "[";
  161. var i;
  162. var first = true;
  163. var a : Array<Dynamic> = v;
  164. for( i in 0...a.length ) {
  165. if( first )
  166. first = false;
  167. else
  168. s += ",";
  169. s += __string_rec(a[i],str);
  170. }
  171. return s+"]";
  172. default:
  173. switch( untyped __typeof__(v) ) {
  174. case "function": return "<function>";
  175. }
  176. }
  177. return new String(v);
  178. }
  179. static function __unprotect__( s : String ) {
  180. return s;
  181. }
  182. static function __init__() untyped {
  183. var aproto = Array.prototype;
  184. aproto.copy = function() {
  185. return __this__.slice();
  186. };
  187. aproto.insert = function(i,x) {
  188. __this__.splice(i,0,x);
  189. };
  190. aproto.remove = function(obj) {
  191. var idx = __this__.indexOf(obj);
  192. if( idx == -1 ) return false;
  193. __this__.splice(idx,1);
  194. return true;
  195. }
  196. aproto.iterator = function() {
  197. var cur = 0;
  198. var arr : Array<Dynamic> = __this__;
  199. return {
  200. hasNext : function() {
  201. return cur < arr.length;
  202. },
  203. next : function() {
  204. return arr[cur++];
  205. }
  206. }
  207. };
  208. aproto.setPropertyIsEnumerable("copy", false);
  209. aproto.setPropertyIsEnumerable("insert", false);
  210. aproto.setPropertyIsEnumerable("remove", false);
  211. aproto.setPropertyIsEnumerable("iterator", false);
  212. String.prototype.charCodeAt = function(i) : Null<Int> {
  213. var s : String = __this__;
  214. var x : Float = s.cca(i);
  215. if( __global__["isNaN"](x) )
  216. return null;
  217. return Std.int(x);
  218. };
  219. }
  220. }