1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*
* Copyright (C)2005-2012 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package js;
private class HaxeError extends js.Error {
var val:Dynamic;
public function new(val:Dynamic) untyped {
super();
this.val = __define_feature__("js.Boot.HaxeError", val);
this.message = String(val);
if (js.Error.captureStackTrace) js.Error.captureStackTrace(this, HaxeError);
}
}
@:dox(hide)
class Boot {
private static function __unhtml(s : String) {
return s.split("&").join("&").split("<").join("<").split(">").join(">");
}
private static function __trace(v,i : haxe.PosInfos) {
untyped {
var msg = if( i != null ) i.fileName+":"+i.lineNumber+": " else "";
#if jsfl
msg += __string_rec(v,"");
fl.trace(msg);
#else
msg += __string_rec(v, "");
if( i != null && i.customParams != null )
for( v in i.customParams )
msg += "," + __string_rec(v, "");
var d;
if( __js__("typeof")(document) != "undefined" && (d = document.getElementById("haxe:trace")) != null )
d.innerHTML += __unhtml(msg)+"<br/>";
else if( __js__("typeof console") != "undefined" && __js__("console").log != null )
__js__("console").log(msg);
#end
}
}
private static function __clear_trace() {
untyped {
#if jsfl
fl.outputPanel.clear();
#else
var d = document.getElementById("haxe:trace");
if( d != null )
d.innerHTML = "";
#end
}
}
static inline function isClass(o:Dynamic) : Bool {
return untyped __define_feature__("js.Boot.isClass", o.__name__);
}
static inline function isEnum(e:Dynamic) : Bool {
return untyped __define_feature__("js.Boot.isEnum", e.__ename__);
}
static function getClass(o:Dynamic) : Dynamic {
if (Std.is(o, Array))
return Array;
else {
var cl = untyped __define_feature__("js.Boot.getClass", o.__class__);
if (cl != null)
return cl;
var name = __nativeClassName(o);
if (name != null)
return __resolveNativeClass(name);
return null;
}
}
@:ifFeature("has_enum")
private static function __string_rec(o,s:String) {
untyped {
if( o == null )
return "null";
if( s.length >= 5 )
return "<...>"; // too much deep recursion
var t = __js__("typeof(o)");
if( t == "function" && (isClass(o) || isEnum(o)) )
t = "object";
switch( t ) {
case "object":
if( __js__("o instanceof Array") ) {
if( o.__enum__ ) {
if( o.length == 2 )
return o[0];
var str = o[0]+"(";
s += "\t";
for( i in 2...o.length ) {
if( i != 2 )
str += "," + __string_rec(o[i],s);
else
str += __string_rec(o[i],s);
}
return str + ")";
}
var l = o.length;
var i;
var str = "[";
s += "\t";
for( i in 0...l )
str += (if (i > 0) "," else "")+__string_rec(o[i],s);
str += "]";
return str;
}
var tostr;
try {
tostr = untyped o.toString;
} catch( e : Dynamic ) {
// strange error on IE
return "???";
}
if( tostr != null && tostr != __js__("Object.toString") && __typeof__(tostr) == "function" ) {
var s2 = o.toString();
if( s2 != "[object Object]")
return s2;
}
var k : String = null;
var str = "{\n";
s += "\t";
var hasp = (o.hasOwnProperty != null);
__js__("for( var k in o ) {");
if( hasp && !o.hasOwnProperty(k) )
__js__("continue");
if( k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" || k == "__properties__" )
__js__("continue");
if( str.length != 2 )
str += ", \n";
str += s + k + " : "+__string_rec(o[k],s);
__js__("}");
s = s.substring(1);
str += "\n" + s + "}";
return str;
case "function":
return "<function>";
case "string":
return o;
default:
return String(o);
}
}
}
private static function __interfLoop(cc : Dynamic,cl : Dynamic) {
if( cc == null )
return false;
if( cc == cl )
return true;
var intf : Dynamic = cc.__interfaces__;
if( intf != null )
for( i in 0...intf.length ) {
var i : Dynamic = intf[i];
if( i == cl || __interfLoop(i,cl) )
return true;
}
return __interfLoop(cc.__super__,cl);
}
@:ifFeature("typed_catch") private static function __instanceof(o : Dynamic,cl : Dynamic) {
if( cl == null )
return false;
switch( cl ) {
case Int:
return (untyped __js__("(o|0) === o"));
case Float:
return (untyped __js__("typeof"))(o) == "number";
case Bool:
return (untyped __js__("typeof"))(o) == "boolean";
case String:
return (untyped __js__("typeof"))(o) == "string";
case Array:
return (untyped __js__("(o instanceof Array)")) && o.__enum__ == null;
case Dynamic:
return true;
default:
if( o != null ) {
// Check if o is an instance of a Haxe class or a native JS object
if( (untyped __js__("typeof"))(cl) == "function" ) {
if( untyped __js__("o instanceof cl") )
return true;
if( __interfLoop(getClass(o),cl) )
return true;
}
else if ( (untyped __js__("typeof"))(cl) == "object" && __isNativeObj(cl) ) {
if( untyped __js__("o instanceof cl") )
return true;
}
} else {
return false;
}
// do not use isClass/isEnum here
untyped __feature__("Class.*",if( cl == Class && o.__name__ != null ) return true);
untyped __feature__("Enum.*",if( cl == Enum && o.__ename__ != null ) return true);
return o.__enum__ == cl;
}
}
@:ifFeature("typed_cast") private static function __cast(o : Dynamic, t : Dynamic) {
if (__instanceof(o, t)) return o;
else throw "Cannot cast " +Std.string(o) + " to " +Std.string(t);
}
static var __toStr = untyped __js__("{}.toString");
// get native JS [[Class]]
static function __nativeClassName(o:Dynamic):String {
var name = untyped __toStr.call(o).slice(8, -1);
// exclude general Object and Function
// also exclude Math and JSON, because instanceof cannot be called on them
if (name == "Object" || name == "Function" || name == "Math" || name == "JSON")
return null;
return name;
}
// check for usable native JS object
static function __isNativeObj(o:Dynamic):Bool {
return __nativeClassName(o) != null;
}
// resolve native JS class in the global scope:
static function __resolveNativeClass(name:String) {
return untyped js.Lib.global[name];
}
}
|