Boot.hx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (C)2005-2018 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. if ((cast js.Error).captureStackTrace) (cast js.Error).captureStackTrace(this, HaxeError);
  31. }
  32. public static function wrap(val:Dynamic):js.Error {
  33. return if (js.Syntax.instanceof(val, js.Error)) val else new HaxeError(val);
  34. }
  35. static function __init__() {
  36. js.Object.defineProperty((cast HaxeError).prototype, "message", {get: () -> (cast String)(js.Lib.nativeThis.val)});
  37. }
  38. }
  39. @:dox(hide)
  40. class Boot {
  41. static inline function isClass(o:Dynamic) : Bool {
  42. return untyped __define_feature__("js.Boot.isClass", o.__name__);
  43. }
  44. static inline function isEnum(e:Dynamic) : Bool {
  45. return untyped __define_feature__("js.Boot.isEnum", e.__ename__);
  46. }
  47. static function getClass(o:Dynamic) : Dynamic {
  48. if (Std.is(o, Array))
  49. return Array;
  50. else {
  51. var cl = untyped __define_feature__("js.Boot.getClass", o.__class__);
  52. if (cl != null)
  53. return cl;
  54. var name = __nativeClassName(o);
  55. if (name != null)
  56. return __resolveNativeClass(name);
  57. return null;
  58. }
  59. }
  60. @:ifFeature("has_enum")
  61. private static function __string_rec(o,s:String) {
  62. untyped {
  63. if( o == null )
  64. return "null";
  65. if( s.length >= 5 )
  66. return "<...>"; // too much deep recursion
  67. var t = js.Syntax.typeof(o);
  68. if( t == "function" && (isClass(o) || isEnum(o)) )
  69. t = "object";
  70. switch( t ) {
  71. case "object":
  72. #if !js_enums_as_arrays
  73. if (o.__enum__) {
  74. var e = $hxEnums[o.__enum__];
  75. var n = e.__constructs__[o._hx_index];
  76. var con = e[n];
  77. if (con.__params__) {
  78. s += "\t";
  79. return n + "(" +
  80. [for (p in (con.__params__:Array<String>)) __string_rec(o[p],s)].join(",") + ")";
  81. } else {
  82. return n;
  83. }
  84. }
  85. #end
  86. if( js.Syntax.instanceof(o, Array) ) {
  87. #if js_enums_as_arrays
  88. if( o.__enum__ ) {
  89. if( o.length == 2 )
  90. return o[0];
  91. var str = o[0]+"(";
  92. s += "\t";
  93. for( i in 2...o.length ) {
  94. if( i != 2 )
  95. str += "," + __string_rec(o[i],s);
  96. else
  97. str += __string_rec(o[i],s);
  98. }
  99. return str + ")";
  100. }
  101. #end
  102. var l = o.length;
  103. var i;
  104. var str = "[";
  105. s += "\t";
  106. for( i in 0...l )
  107. str += (if (i > 0) "," else "")+__string_rec(o[i],s);
  108. str += "]";
  109. return str;
  110. }
  111. var tostr;
  112. try {
  113. tostr = untyped o.toString;
  114. } catch( e : Dynamic ) {
  115. // strange error on IE
  116. return "???";
  117. }
  118. if( tostr != null && tostr != __js__("Object.toString") && js.Syntax.typeof(tostr) == "function" ) {
  119. var s2 = o.toString();
  120. if( s2 != "[object Object]")
  121. return s2;
  122. }
  123. var k : String = null;
  124. var str = "{\n";
  125. s += "\t";
  126. var hasp = (o.hasOwnProperty != null);
  127. __js__("for( var k in o ) {");
  128. if( hasp && !o.hasOwnProperty(k) )
  129. __js__("continue");
  130. if( k == "prototype" || k == "__class__" || k == "__super__" || k == "__interfaces__" || k == "__properties__" )
  131. __js__("continue");
  132. if( str.length != 2 )
  133. str += ", \n";
  134. str += s + k + " : "+__string_rec(o[k],s);
  135. __js__("}");
  136. s = s.substring(1);
  137. str += "\n" + s + "}";
  138. return str;
  139. case "function":
  140. return "<function>";
  141. case "string":
  142. return o;
  143. default:
  144. return String(o);
  145. }
  146. }
  147. }
  148. private static function __interfLoop(cc : Dynamic,cl : Dynamic) {
  149. if( cc == null )
  150. return false;
  151. if( cc == cl )
  152. return true;
  153. var intf : Dynamic = cc.__interfaces__;
  154. if( intf != null )
  155. for( i in 0...intf.length ) {
  156. var i : Dynamic = intf[i];
  157. if( i == cl || __interfLoop(i,cl) )
  158. return true;
  159. }
  160. return __interfLoop(cc.__super__,cl);
  161. }
  162. @:ifFeature("typed_catch") @:pure private static function __instanceof(o : Dynamic,cl : Dynamic) {
  163. if( cl == null )
  164. return false;
  165. switch( cl ) {
  166. case Int:
  167. return js.Syntax.typeof(o) == "number" && js.Syntax.strictEq(o | 0, o);
  168. case Float:
  169. return js.Syntax.typeof(o) == "number";
  170. case Bool:
  171. return js.Syntax.typeof(o) == "boolean";
  172. case String:
  173. return js.Syntax.typeof(o) == "string";
  174. case Array:
  175. return js.Syntax.instanceof(o, Array) && o.__enum__ == null;
  176. case Dynamic:
  177. return true;
  178. default:
  179. if( o != null ) {
  180. // Check if o is an instance of a Haxe class or a native JS object
  181. if( js.Syntax.typeof(cl) == "function" ) {
  182. if( js.Syntax.instanceof(o, cl) )
  183. return true;
  184. if( __interfLoop(getClass(o),cl) )
  185. return true;
  186. }
  187. else if ( js.Syntax.typeof(cl) == "object" && __isNativeObj(cl) ) {
  188. if( js.Syntax.instanceof(o, cl) )
  189. return true;
  190. }
  191. } else {
  192. return false;
  193. }
  194. // do not use isClass/isEnum here
  195. untyped __feature__("Class.*",if( cl == Class && o.__name__ != null ) return true);
  196. untyped __feature__("Enum.*",if( cl == Enum && o.__ename__ != null ) return true);
  197. #if js_enums_as_arrays
  198. return o.__enum__ == cl;
  199. #else
  200. return (untyped $hxEnums[o.__enum__]) == cl;
  201. #end
  202. }
  203. }
  204. @:ifFeature("typed_cast") private static function __cast(o : Dynamic, t : Dynamic) {
  205. if (__instanceof(o, t)) return o;
  206. else throw "Cannot cast " +Std.string(o) + " to " +Std.string(t);
  207. }
  208. static var __toStr = untyped ({}).toString;
  209. // get native JS [[Class]]
  210. static function __nativeClassName(o:Dynamic):String {
  211. var name = untyped __toStr.call(o).slice(8, -1);
  212. // exclude general Object and Function
  213. // also exclude Math and JSON, because instanceof cannot be called on them
  214. if (name == "Object" || name == "Function" || name == "Math" || name == "JSON")
  215. return null;
  216. return name;
  217. }
  218. // check for usable native JS object
  219. static function __isNativeObj(o:Dynamic):Bool {
  220. return __nativeClassName(o) != null;
  221. }
  222. // resolve native JS class in the global scope:
  223. static function __resolveNativeClass(name:String) {
  224. return js.Lib.global[cast name];
  225. }
  226. }