Boot.hx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. class Boot extends flash.display.MovieClip, implements Dynamic {
  27. static var init : Void -> Void;
  28. static var tf : flash.text.TextField;
  29. static var lines : Array<String>;
  30. static var lastError : flash.Error;
  31. public static var skip_constructor = false;
  32. public function new(?mc:flash.display.MovieClip) {
  33. super();
  34. untyped {
  35. var aproto = Array.prototype;
  36. aproto.copy = function() {
  37. return this.slice();
  38. };
  39. aproto.insert = function(i,x) {
  40. this.splice(i,0,x);
  41. };
  42. aproto.remove = function(obj) {
  43. for( i in 0...this.length )
  44. if( this[i] == obj ) {
  45. this.splice(i,1);
  46. return true;
  47. }
  48. return false;
  49. }
  50. aproto.iterator = function() {
  51. var cur = 0;
  52. var arr : Array<Dynamic> = this;
  53. return {
  54. hasNext : function() {
  55. return cur < arr.length;
  56. },
  57. next : function() {
  58. return arr[cur++];
  59. }
  60. }
  61. };
  62. aproto.setPropertyIsEnumerable("copy", false);
  63. aproto.setPropertyIsEnumerable("insert", false);
  64. aproto.setPropertyIsEnumerable("remove", false);
  65. aproto.setPropertyIsEnumerable("iterator", false);
  66. var cca = String.prototype.charCodeAt;
  67. String.prototype.charCodeAt = function(i) {
  68. var x = cca.call(this,i);
  69. if( __global__["isNaN"](x) )
  70. return null;
  71. return x;
  72. };
  73. }
  74. lines = new Array();
  75. var c = if( mc == null ) this else mc;
  76. flash.Lib.current = c;
  77. try {
  78. untyped if( c.stage != null && c.stage.align == "" )
  79. c.stage.align = "TOP_LEFT";
  80. } catch( e : Dynamic ) {
  81. // security error when loading from different domain
  82. }
  83. if( init != null )
  84. init();
  85. }
  86. public static function enum_to_string( e : { tag : String, params : Array<Dynamic> } ) {
  87. if( e.params == null )
  88. return e.tag;
  89. return e.tag+"("+e.params.join(",")+")";
  90. }
  91. public static function __instanceof( v : Dynamic, t : Dynamic ) {
  92. try {
  93. if( t == Dynamic )
  94. return true;
  95. return untyped __is__(v,t);
  96. } catch( e : Dynamic ) {
  97. }
  98. return false;
  99. }
  100. public static function __clear_trace() {
  101. if( tf == null )
  102. return;
  103. flash.Lib.current.removeChild(tf);
  104. tf = null;
  105. lines = new Array();
  106. }
  107. public static function __set_trace_color(rgb) {
  108. getTrace().textColor = rgb;
  109. }
  110. public static function getTrace() {
  111. var mc = flash.Lib.current;
  112. if( tf == null ) {
  113. tf = new flash.text.TextField();
  114. var format = tf.getTextFormat();
  115. format.font = "_sans";
  116. tf.defaultTextFormat = format;
  117. tf.selectable = false;
  118. tf.width = if( mc.stage == null ) 800 else mc.stage.stageWidth;
  119. tf.autoSize = flash.text.TextFieldAutoSize.LEFT;
  120. tf.mouseEnabled = false;
  121. }
  122. mc.addChild(tf); // on top
  123. return tf;
  124. }
  125. public static function __trace( v : Dynamic, pos : haxe.PosInfos ) {
  126. var tf = getTrace();
  127. var pstr = if( pos == null ) "(null)" else pos.fileName+":"+pos.lineNumber;
  128. lines = lines.concat((pstr +": "+__string_rec(v,"")).split("\n"));
  129. tf.text = lines.join("\n");
  130. var stage = flash.Lib.current.stage;
  131. if( stage == null )
  132. return;
  133. while( tf.height > stage.stageHeight ) {
  134. lines.shift();
  135. tf.text = lines.join("\n");
  136. }
  137. }
  138. public static function __string_rec( v : Dynamic, str : String ) {
  139. var cname = untyped __global__["flash.utils.getQualifiedClassName"](v);
  140. switch( cname ) {
  141. case "Object":
  142. var k : Array<String> = untyped __keys__(v);
  143. var s = "{";
  144. var first = true;
  145. for( i in 0...k.length ) {
  146. var key = k[i];
  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. var s = "[";
  159. var i;
  160. var first = true;
  161. for( i in 0...v.length ) {
  162. if( first )
  163. first = false;
  164. else
  165. s += ",";
  166. s += __string_rec(v[i],str);
  167. }
  168. return s+"]";
  169. default:
  170. switch( untyped __typeof__(v) ) {
  171. case "function": return "<function>";
  172. }
  173. }
  174. return new String(v);
  175. }
  176. static function __unprotect__( s : String ) {
  177. return s;
  178. }
  179. }