Boot.hx 6.9 KB

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