/lib/js/Boot.hx

http://github.com/visup/haxe-titanium-api · Haxe · 234 lines · 201 code · 8 blank · 25 comment · 92 complexity · 4eef15420f566d4363228ddb8cd52a5e MD5 · raw file

  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 js;
  26. class Boot {
  27. private static function __trace(v,i : haxe.PosInfos) {
  28. untyped {
  29. var msg = if( i != null ) i.fileName+":"+i.lineNumber+": " else "";
  30. msg += __string_rec(v,"")+"\n";
  31. untyped Titanium.API.debug(msg);
  32. }
  33. }
  34. private static function __closure(o,f) {
  35. untyped {
  36. var m = o[f];
  37. if( m == null )
  38. return null;
  39. var f = function() { return m.apply(o,arguments); };
  40. f.scope = o;
  41. f.method = m;
  42. return f;
  43. }
  44. }
  45. private static function __string_rec(o,s) {
  46. untyped {
  47. if( o == null )
  48. return "null";
  49. if( s.length >= 5 )
  50. return "<...>"; // too much deep recursion
  51. var t = __js__("typeof(o)");
  52. if( t == "function" && (o.__name__ != null || o.__ename__ != null) )
  53. t = "object";
  54. switch( t ) {
  55. case "object":
  56. if( __js__("o instanceof Array") ) {
  57. if( o.__enum__ != null ) {
  58. if( o.length == 2 )
  59. return o[0];
  60. var str = o[0]+"(";
  61. s += "\t";
  62. for( i in 2...o.length ) {
  63. if( i != 2 )
  64. str += "," + __string_rec(o[i],s);
  65. else
  66. str += __string_rec(o[i],s);
  67. }
  68. return str + ")";
  69. }
  70. var l = o.length;
  71. var i;
  72. var str = "[";
  73. s += "\t";
  74. for( i in 0...l )
  75. str += (if (i > 0) "," else "")+__string_rec(o[i],s);
  76. str += "]";
  77. return str;
  78. }
  79. var tostr;
  80. try {
  81. tostr = untyped o.toString;
  82. } catch( e : Dynamic ) {
  83. // strange error on IE
  84. return "???";
  85. }
  86. if( tostr != null && tostr != __js__("Object.toString") ) {
  87. var s2 = o.toString();
  88. if( s2 != "[object Object]")
  89. return s2;
  90. }
  91. var k : String = null;
  92. var str = "{\n";
  93. s += "\t";
  94. var hasp = (o.hasOwnProperty != null);
  95. __js__("for( var k in o ) { ");
  96. if( hasp && !o.hasOwnProperty(k) )
  97. __js__("continue");
  98. if( k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" )
  99. __js__("continue");
  100. if( str.length != 2 )
  101. str += ", \n";
  102. str += s + k + " : "+__string_rec(o[k],s);
  103. __js__("}");
  104. s = s.substring(1);
  105. str += "\n" + s + "}";
  106. return str;
  107. case "function":
  108. return "<function>";
  109. case "string":
  110. return o;
  111. default:
  112. return String(o);
  113. }
  114. }
  115. }
  116. private static function __interfLoop(cc : Dynamic,cl : Dynamic) {
  117. if( cc == null )
  118. return false;
  119. if( cc == cl )
  120. return true;
  121. var intf : Dynamic = cc.__interfaces__;
  122. if( intf != null )
  123. for( i in 0...intf.length ) {
  124. var i : Dynamic = intf[i];
  125. if( i == cl || __interfLoop(i,cl) )
  126. return true;
  127. }
  128. return __interfLoop(cc.__super__,cl);
  129. }
  130. private static function __instanceof(o : Dynamic,cl) {
  131. untyped {
  132. try {
  133. if( __js__("o instanceof cl") ) {
  134. if( cl == Array )
  135. return (o.__enum__ == null);
  136. return true;
  137. }
  138. if( __interfLoop(o.__class__,cl) )
  139. return true;
  140. } catch( e : Dynamic ) {
  141. if( cl == null )
  142. return false;
  143. }
  144. switch( cl ) {
  145. case Int:
  146. return __js__("Math.ceil(o%2147483648.0) === o");
  147. case Float:
  148. return __js__("typeof(o)") == "number";
  149. case Bool:
  150. return __js__("o === true || o === false");
  151. case String:
  152. return __js__("typeof(o)") == "string";
  153. case Dynamic:
  154. return true;
  155. default:
  156. if( o == null )
  157. return false;
  158. return o.__enum__ == cl || ( cl == Class && o.__name__ != null ) || ( cl == Enum && o.__ename__ != null );
  159. }
  160. }
  161. }
  162. private static function __init() {
  163. untyped {
  164. #if js_namespace
  165. __js__("eval(js.Boot.__ns).Array = Array");
  166. __js__("eval(js.Boot.__ns).String = String");
  167. __js__("eval(js.Boot.__ns).Math = Math");
  168. __js__("eval(js.Boot.__ns).Date = Date");
  169. #end
  170. Array.prototype.copy = Array.prototype.slice;
  171. Array.prototype.insert = function(i,x) {
  172. this.splice(i,0,x);
  173. };
  174. Array.prototype.remove = if( Array.prototype.indexOf ) function(obj) {
  175. var idx = this.indexOf(obj);
  176. if( idx == -1 ) return false;
  177. this.splice(idx,1);
  178. return true;
  179. } else function(obj) {
  180. var i = 0;
  181. var l = this.length;
  182. while( i < l ) {
  183. if( this[i] == obj ) {
  184. this.splice(i,1);
  185. return true;
  186. }
  187. i++;
  188. }
  189. return false;
  190. };
  191. Array.prototype.iterator = function() {
  192. return {
  193. cur : 0,
  194. arr : this,
  195. hasNext : function() {
  196. return this.cur < this.arr.length;
  197. },
  198. next : function() {
  199. return this.arr[this.cur++];
  200. }
  201. }
  202. };
  203. var cca = String.prototype.charCodeAt;
  204. String.prototype.cca = cca;
  205. String.prototype.charCodeAt = function(i) {
  206. var x = cca.call(this,i);
  207. if( isNaN(x) )
  208. return null;
  209. return x;
  210. };
  211. var oldsub = String.prototype.substr;
  212. String.prototype.substr = function(pos,len){
  213. if( pos != null && pos != 0 && len != null && len < 0 ) return "";
  214. if( len == null ) len = this.length;
  215. if( pos < 0 ){
  216. pos = this.length + pos;
  217. if( pos < 0 ) pos = 0;
  218. }else if( len < 0 ){
  219. len = this.length + len - pos;
  220. }
  221. return oldsub.apply(this,[pos,len]);
  222. };
  223. __js__("$closure = js.Boot.__closure");
  224. }
  225. }
  226. }