Boot.hx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (c) 2005, 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 {
  27. private static var exc : Array<Dynamic>;
  28. private static function __string_rec(o,s) {
  29. untyped {
  30. if( s.length >= 20 )
  31. return "<...>"; // too much deep recursion
  32. var t = __typeof__(o);
  33. if( t == "movieclip" )
  34. t = "object";
  35. else if( t == "function" && o.__interfaces__ != null )
  36. t = "object";
  37. switch( t ) {
  38. case "object":
  39. if( __instanceof__(o,Array) ) {
  40. var l = o.length;
  41. var i;
  42. var str = "[";
  43. s += " ";
  44. for( i in 0...l )
  45. str += (if (i > 0) "," else "")+__string_rec(o[i],s);
  46. s = s.substring(4);
  47. str += "]";
  48. return str;
  49. }
  50. var s2 = o.toString();
  51. if( (__typeof__(s2) == "string" || __instanceof__(s2,String)) && s2 != "[object Object]" && s2 != "[type Function]" )
  52. return s2;
  53. var k;
  54. var str = "{\n";
  55. if( typeof(o) == "movieclip" )
  56. str = "MC("+o._name+") "+str;
  57. s += " ";
  58. for( k in (__keys__(o)).iterator() ) {
  59. if( str.length != 2 )
  60. str += ",\n";
  61. if( k == "__construct__" && __typeof__(o[k]) == "function" )
  62. str += s + k + " : <function>";
  63. else
  64. str += s + k + " : "+__string_rec(o[k],s);
  65. }
  66. s = s.substring(4);
  67. if( str.length != 2 )
  68. str += "\n";
  69. str += s + "}";
  70. return str;
  71. case "function":
  72. return "<function>";
  73. case "string":
  74. return o;
  75. default:
  76. return String(o);
  77. }
  78. }
  79. }
  80. private static function __closure(f,o) {
  81. untyped {
  82. var f2 = function() {
  83. var me = __arguments__.callee;
  84. return me.f.apply(me.o,__arguments__);
  85. };
  86. f2.f = o[f];
  87. f2.o = o;
  88. return f2;
  89. }
  90. }
  91. private static function __instanceof(o,cl) {
  92. untyped {
  93. if( __instanceof__(o,cl) )
  94. return true;
  95. switch( cl ) {
  96. case Int:
  97. return (Math.ceil(o) == o) && isFinite(o);
  98. case Float:
  99. return __typeof__(o) == "number";
  100. case Bool:
  101. return (o == true || o == false);
  102. case String:
  103. return __typeof__(o) == "string";
  104. default:
  105. return false;
  106. }
  107. }
  108. }
  109. private static function __trace(v,inf) {
  110. untyped {
  111. var root = flash.Lib.current;
  112. var tf = root.__trace_txt;
  113. if( tf == null ) {
  114. root.createTextField("__trace_txt",1048500,0,0,Stage.width,Stage.height);
  115. tf = root.__trace_txt;
  116. tf.selectable = false;
  117. root.__trace_lines = new Array<String>();
  118. }
  119. var s = inf.fileName+":"+inf.lineNumber+": "+__string_rec(v,"");
  120. var lines = root.__trace_lines.concat(s.split("\n"));
  121. root.__trace_lines = lines;
  122. var nlines = Stage.height / 16;
  123. if( lines.length > nlines )
  124. lines.splice(0,lines.length-nlines);
  125. tf.text = lines.join("\n");
  126. }
  127. }
  128. private static function __clear_trace() {
  129. untyped {
  130. _root.__trace_txt.removeTextField();
  131. _root.__trace_lines = null;
  132. }
  133. }
  134. private static function __init(current) {
  135. untyped {
  136. var obj = _global["Object"];
  137. var flash = current["flash"];
  138. if( flash.text == null )
  139. flash.text = __new__(obj);
  140. flash.text.StyleSheet = TextField["StyleSheet"];
  141. flash.system = __new__(obj);
  142. flash.system.Capabilities = System.capabilities;
  143. flash.system.Security = System.security;
  144. Math.pi = Math["PI"];
  145. #if use_ime
  146. flash.system.IME = System["IME"];
  147. flash.system.IME._ALPHANUMERIC_FULL = System["IME"]["ALPHANUMERIC_FULL"];
  148. flash.system.IME._ALPHANUMERIC_HALF = System["IME"]["ALPHANUMERIC_HALF"];
  149. flash.system.IME._CHINESE = System["IME"]["CHINESE"];
  150. flash.system.IME._JAPANESE_HIRAGANA = System["IME"]["JAPANESE_HIRAGANA"];
  151. flash.system.IME._JAPANESE_KATAKANA_FULL = System["IME"]["JAPANESE_KATAKANA_FULL"];
  152. flash.system.IME._JAPANESE_KATAKANA_HALF = System["IME"]["JAPANESE_KATAKANA_HALF"];
  153. flash.system.IME._KOREAN = System["IME"]["KOREAN"];
  154. flash.system.IME._UNKNOWN = System["IME"]["UNKNOWN"];
  155. #end
  156. Node = _global["XMLNode"];
  157. Node.element_node = 1;
  158. Node.text_node = 3;
  159. Node.prototype.removeChild = Node.prototype.removeNode;
  160. Node.prototype.replaceChild = function(cnew,cold) {
  161. this.insertBefore(cnew,cold);
  162. this.removeChild(cold);
  163. };
  164. Node.prototype.nodes = function() {
  165. return untyped {
  166. p : 0,
  167. a : childNodes,
  168. next : function() {
  169. while( true ) {
  170. var x = this.a[this.p];
  171. if( x == null )
  172. return null;
  173. this.p++;
  174. if( x.nodeType == 1 )
  175. return x;
  176. }
  177. return null;
  178. },
  179. hasNext : function() {
  180. var x = this.next();
  181. if( x != null ) {
  182. this.p--;
  183. return true;
  184. }
  185. return false;
  186. }
  187. };
  188. };
  189. Array.prototype.copy = Array.prototype.slice;
  190. Array.prototype.insert = function(i,x) {
  191. this.splice(i,0,x);
  192. };
  193. Array.prototype.remove = function(obj) {
  194. var i = 0;
  195. var l = this.length;
  196. while( i < l ) {
  197. if( this[i] == obj ) {
  198. this.splice(i,1);
  199. return true;
  200. }
  201. i++;
  202. }
  203. return false;
  204. }
  205. Array.prototype.iterator = function() {
  206. return {
  207. cur : 0,
  208. max : this.length,
  209. arr : this,
  210. hasNext : function() {
  211. return this.cur < this.max;
  212. },
  213. next : function() {
  214. return this.arr[this.cur++];
  215. }
  216. }
  217. };
  218. Array.prototype.indexes = function() {
  219. return {
  220. cur : 0,
  221. max : this.length,
  222. hasNext : function() {
  223. return this.cur < this.max;
  224. },
  225. next : function() {
  226. return this.cur++;
  227. }
  228. }
  229. };
  230. Date.now = function() {
  231. return __new__(Date);
  232. };
  233. Date.prototype.toString = function() {
  234. var m = this.getMonth() + 1;
  235. var d = this.getDate();
  236. var h = this.getHours();
  237. var mi = this.getMinutes();
  238. var s = this.getSeconds();
  239. if( d < 10 )
  240. d = "0" + d;
  241. if( m < 10 )
  242. m = "0" + m;
  243. if( h < 10 )
  244. h = "0" + h;
  245. if( mi < 10 )
  246. mi = "0" + mi;
  247. if( s < 10 )
  248. s = "0" + s;
  249. return this.getFullYear()+"-"+m+"-"+d+" "+h+":"+mi+":"+s;
  250. };
  251. // copy base classes from root to flash package
  252. // we can't make a loop since we need to assign short-type-ids
  253. flash.Accessibility = _global["Accessibility"];
  254. flash.Camera = _global["Camera"];
  255. flash.Color = _global["Color"];
  256. flash.Key = _global["Key"];
  257. flash.LoadVars = _global["LoadVars"];
  258. flash.LocalConnection = _global["LocalConnection"];
  259. flash.Microphone = _global["Microphone"];
  260. flash.Mouse = _global["Mouse"];
  261. flash.MovieClip = _global["MovieClip"];
  262. flash.MovieClipLoader = _global["MovieClipLoader"];
  263. flash.PrintJob = _global["PrintJob"];
  264. flash.Selection = _global["Selection"];
  265. flash.SharedObject = _global["SharedObject"];
  266. flash.Sound = _global["Sound"];
  267. flash.Stage = _global["Stage"];
  268. flash.System = _global["System"];
  269. flash.TextField = _global["TextField"];
  270. flash.TextFormat = _global["TextFormat"];
  271. flash.TextSnapshot = _global["TextSnapshot"];
  272. flash.Video = _global["Video"];
  273. flash.XMLSocket = _global["XMLSocket"];
  274. flash.NetConnection = _global["NetConnection"];
  275. flash.NetStream = _global["NetStream"];
  276. Lib._global = _global;
  277. Lib._root = _root;
  278. Lib.current = current;
  279. Int = __new__(obj);
  280. Bool = __new__(obj);
  281. Bool["true"] = true;
  282. Bool["false"] = false;
  283. Float = _global["Number"];
  284. // prevent closure creation by setting untyped
  285. current["@instanceof"] = untyped __instanceof;
  286. current["@closure"] = untyped __closure;
  287. exc = new Array();
  288. current["@exc"] = exc;
  289. }
  290. }
  291. }