PageRenderTime 45ms CodeModel.GetById 8ms app.highlight 30ms RepoModel.GetById 2ms app.codeStats 0ms

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