Runtime.hx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. /*
  2. * Copyright (C)2005-2019 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 cs.internal;
  23. import cs.Lib;
  24. import cs.Lib.*;
  25. import cs.NativeArray;
  26. import cs.StdTypes;
  27. import cs.system.Activator;
  28. import cs.system.IConvertible;
  29. import cs.system.IComparable;
  30. import cs.system.reflection.MethodBase;
  31. import cs.system.reflection.MethodInfo;
  32. import cs.system.reflection.*;
  33. import cs.system.Type;
  34. import cs.system.Object;
  35. /**
  36. This class is meant for internal compiler use only. It provides the Haxe runtime
  37. compatibility to the host language.
  38. **/
  39. @:nativeGen
  40. @:native('haxe.lang.Runtime')
  41. @:access(String)
  42. @:classCode('
  43. public static object getField(haxe.lang.HxObject obj, string field, int fieldHash, bool throwErrors)
  44. {
  45. if (obj == null && !throwErrors) return null;
  46. return obj.__hx_getField(field, (fieldHash == 0) ? haxe.lang.FieldLookup.hash(field) : fieldHash, throwErrors, false, false);
  47. }
  48. public static double getField_f(haxe.lang.HxObject obj, string field, int fieldHash, bool throwErrors)
  49. {
  50. if (obj == null && !throwErrors) return 0.0;
  51. return obj.__hx_getField_f(field, (fieldHash == 0) ? haxe.lang.FieldLookup.hash(field) : fieldHash, throwErrors, false);
  52. }
  53. public static object setField(haxe.lang.HxObject obj, string field, int fieldHash, object value)
  54. {
  55. return obj.__hx_setField(field, (fieldHash == 0) ? haxe.lang.FieldLookup.hash(field) : fieldHash, value, false);
  56. }
  57. public static double setField_f(haxe.lang.HxObject obj, string field, int fieldHash, double value)
  58. {
  59. return obj.__hx_setField_f(field, (fieldHash == 0) ? haxe.lang.FieldLookup.hash(field) : fieldHash, value, false);
  60. }
  61. public static object callField(haxe.lang.HxObject obj, string field, int fieldHash, object[] args)
  62. {
  63. return obj.__hx_invokeField(field, (fieldHash == 0) ? haxe.lang.FieldLookup.hash(field) : fieldHash, args);
  64. }
  65. ')
  66. @:keep class Runtime {
  67. @:readOnly public static var undefined(default, never):Dynamic = new cs.system.Object();
  68. public static function closure(obj:Dynamic, hash:Int, field:String):Dynamic {
  69. return new cs.internal.Function.Closure(obj, field, hash);
  70. }
  71. public static function eq(v1:Dynamic, v2:Dynamic):Bool {
  72. if (Object.ReferenceEquals(v1, v2))
  73. return true;
  74. if (Object.ReferenceEquals(v1, null) || Object.ReferenceEquals(v2, null))
  75. return false;
  76. var v1c = Lib.as(v1, IConvertible);
  77. if (v1c != null) {
  78. var v2c = Lib.as(v2, IConvertible);
  79. if (v2c == null) {
  80. return false;
  81. }
  82. var t1 = v1c.GetTypeCode(), t2 = v2c.GetTypeCode();
  83. if (t1 == t2)
  84. return Object._Equals(v1c, v2c);
  85. if (t1 == cs.system.TypeCode.String || t2 == cs.system.TypeCode.String)
  86. return false;
  87. switch [t1, t2] {
  88. case [Decimal, _] | [_, Decimal]:
  89. return v1c.ToDecimal(null) == v2c.ToDecimal(null);
  90. case [Int64, _] | [_, Int64]:
  91. return v1c.ToInt64(null) == v2c.ToInt64(null);
  92. case [UInt64 | DateTime, _] | [_, UInt64 | DateTime]:
  93. return v1c.ToUInt64(null) == v2c.ToUInt64(null);
  94. case [Double | Single, _] | [_, Double | Single]:
  95. return v1c.ToDouble(null) == v2c.ToDouble(null);
  96. case _:
  97. return v1c.ToInt32(null) == v2c.ToInt32(null);
  98. }
  99. }
  100. var v1v = Lib.as(v1, cs.system.ValueType);
  101. if (v1v != null) {
  102. return v1.Equals(v2);
  103. #if !erase_generics
  104. }
  105. else {
  106. var v1t = Lib.as(v1, Type);
  107. if (v1t != null) {
  108. var v2t = Lib.as(v2, Type);
  109. if (v2t != null)
  110. return typeEq(v1t, v2t);
  111. return false;
  112. }
  113. #end
  114. }
  115. return false;
  116. }
  117. public static function refEq(v1:{}, v2:{}):Bool {
  118. #if !erase_generics
  119. if (Std.isOfType(v1, Type))
  120. return typeEq(Lib.as(v1, Type), Lib.as(v2, Type));
  121. #end
  122. return Object.ReferenceEquals(v1, v2);
  123. }
  124. public static function toDouble(obj:Dynamic):Float {
  125. return (obj == null) ? .0 : Std.isOfType(obj, Float) ? cast obj : Lib.as(obj, IConvertible).ToDouble(null);
  126. }
  127. public static function toInt(obj:Dynamic):Int {
  128. return (obj == null) ? 0 : Std.isOfType(obj, Int) ? cast obj : Lib.as(obj, IConvertible).ToInt32(null);
  129. }
  130. #if erase_generics
  131. public static function toLong(obj:Dynamic):Int64 {
  132. return (obj == null) ? 0 : Std.isOfType(obj, Int64) ? cast obj : Lib.as(obj, IConvertible).ToInt64(null);
  133. }
  134. #end
  135. public static function isInt(obj:Dynamic):Bool {
  136. var cv1 = Lib.as(obj, IConvertible);
  137. if (cv1 != null) {
  138. switch (cv1.GetTypeCode()) {
  139. case Double:
  140. var d:Float = cast obj;
  141. return d >= cs.system.Int32.MinValue && d <= cs.system.Int32.MaxValue && d == (cast(d, Int));
  142. case UInt32, Int32:
  143. return true;
  144. default:
  145. return false;
  146. }
  147. }
  148. return false;
  149. }
  150. public static function isUInt(obj:Dynamic):Bool {
  151. var cv1 = Lib.as(obj, IConvertible);
  152. if (cv1 != null) {
  153. switch (cv1.GetTypeCode()) {
  154. case Double:
  155. var d:Float = cast obj;
  156. return d >= cs.system.UInt32.MinValue && d <= cs.system.UInt32.MaxValue && d == (cast(d, UInt));
  157. case UInt32:
  158. return true;
  159. default:
  160. return false;
  161. }
  162. }
  163. return false;
  164. }
  165. public static function compare(v1:Dynamic, v2:Dynamic):Int {
  166. if (Object.ReferenceEquals(v1, v2))
  167. return 0;
  168. if (Object.ReferenceEquals(v1, null))
  169. return -1;
  170. if (Object.ReferenceEquals(v2, null))
  171. return 1;
  172. var cv1 = Lib.as(v1, IConvertible);
  173. if (cv1 != null) {
  174. var cv2 = Lib.as(v2, IConvertible);
  175. if (cv2 == null) {
  176. throw new cs.system.ArgumentException("Cannot compare " + getNativeType(v1).ToString() + " and " + getNativeType(v2).ToString());
  177. }
  178. switch (cv1.GetTypeCode()) {
  179. case cs.system.TypeCode.String:
  180. if (cv2.GetTypeCode() != cs.system.TypeCode.String)
  181. throw new cs.system.ArgumentException("Cannot compare " + getNativeType(v1).ToString() + " and " + getNativeType(v2).ToString());
  182. var s1 = Lib.as(v1, String);
  183. var s2 = Lib.as(v2, String);
  184. return String.Compare(s1, s2, cs.system.StringComparison.Ordinal);
  185. case cs.system.TypeCode.Double:
  186. var d1:Float = cast v1, d2:Float = cv2.ToDouble(null);
  187. return (d1 < d2) ? -1 : (d1 > d2) ? 1 : 0;
  188. default:
  189. var d1d = cv1.ToDouble(null);
  190. var d2d = cv2.ToDouble(null);
  191. return (d1d < d2d) ? -1 : (d1d > d2d) ? 1 : 0;
  192. }
  193. }
  194. var c1 = Lib.as(v1, IComparable);
  195. var c2 = Lib.as(v2, IComparable);
  196. if (c1 == null || c2 == null) {
  197. throw new cs.system.ArgumentException("Cannot compare " + getNativeType(v1).ToString() + " and " + getNativeType(v2).ToString());
  198. }
  199. return c1.CompareTo(c2);
  200. }
  201. public static function plus(v1:Dynamic, v2:Dynamic):Dynamic {
  202. if (Std.isOfType(v1, String) || Std.isOfType(v2, String))
  203. return Std.string(v1) + Std.string(v2);
  204. if (v1 == null) {
  205. if (v2 == null)
  206. return null;
  207. v1 = 0;
  208. } else if (v2 == null)
  209. v2 = 0;
  210. var cv1 = Lib.as(v1, IConvertible);
  211. if (cv1 != null) {
  212. var cv2 = Lib.as(v2, IConvertible);
  213. if (cv2 == null) {
  214. throw new cs.system.ArgumentException("Cannot dynamically add " + cs.Lib.getNativeType(v1).ToString() + " and "
  215. + cs.Lib.getNativeType(v2).ToString());
  216. }
  217. return cv1.ToDouble(null) + cv2.ToDouble(null);
  218. }
  219. throw new cs.system.ArgumentException("Cannot dynamically add " + v1 + " and " + v2);
  220. }
  221. public static function slowGetField(obj:Dynamic, field:String, throwErrors:Bool):Dynamic {
  222. if (obj == null)
  223. if (throwErrors)
  224. throw new cs.system.NullReferenceException("Cannot access field \'" + field + "\' of null.");
  225. else
  226. return null;
  227. var t = Lib.as(obj, cs.system.Type);
  228. var bf = if (t == null) {
  229. var s = Lib.as(obj, String);
  230. if (s != null)
  231. return cs.internal.StringExt.StringRefl.handleGetField(s, field, throwErrors);
  232. t = obj.GetType();
  233. new cs.Flags(BindingFlags.Instance) | BindingFlags.Public | BindingFlags.FlattenHierarchy;
  234. } else {
  235. if (t == Lib.toNativeType(String) && field == "fromCharCode")
  236. return new cs.internal.Function.Closure(StringExt, field, 0);
  237. obj = null;
  238. new cs.Flags(BindingFlags.Static) | BindingFlags.Public;
  239. }
  240. var f = t.GetField(field, bf);
  241. if (f != null) {
  242. return unbox(f.GetValue(obj));
  243. } else {
  244. var prop = t.GetProperty(field, bf);
  245. if (prop == null) {
  246. var m = t.GetMember(field, bf);
  247. if (m.length == 0 && (field == "__get" || field == "__set"))
  248. m = t.GetMember(field == "__get" ? "get_Item" : "set_Item", bf);
  249. if (m.Length > 0) {
  250. return new cs.internal.Function.Closure(obj != null ? obj : t, field, 0);
  251. } else {
  252. // COM object handling
  253. if (t.IsCOMObject) {
  254. try {
  255. return t.InvokeMember(field, BindingFlags.GetProperty, null, obj, new cs.NativeArray(0));
  256. } catch (e:cs.system.Exception) {
  257. // Closures of COM objects not supported currently
  258. }
  259. }
  260. if (throwErrors)
  261. throw "Cannot access field \'" + field + "\'.";
  262. else
  263. return null;
  264. }
  265. }
  266. return unbox(prop.GetValue(obj, null));
  267. }
  268. }
  269. public static function slowHasField(obj:Dynamic, field:String):Bool {
  270. if (obj == null)
  271. return false;
  272. var t = Lib.as(obj, cs.system.Type);
  273. var bf = if (t == null) {
  274. var s = Lib.as(obj, String);
  275. if (s != null)
  276. return cs.internal.StringExt.StringRefl.handleGetField(s, field, false) != null;
  277. t = obj.GetType();
  278. new cs.Flags(BindingFlags.Instance) | BindingFlags.Public | BindingFlags.FlattenHierarchy;
  279. } else {
  280. if (t == Lib.toNativeType(String))
  281. return field == "fromCharCode";
  282. obj = null;
  283. new cs.Flags(BindingFlags.Static) | BindingFlags.Public;
  284. }
  285. var mi = t.GetMember(field, bf);
  286. return mi != null && mi.length > 0;
  287. }
  288. public static function slowSetField(obj:Dynamic, field:String, value:Dynamic):Dynamic {
  289. if (obj == null)
  290. throw new cs.system.NullReferenceException("Cannot access field \'" + field + "\' of null.");
  291. var t = Lib.as(obj, cs.system.Type);
  292. var bf = if (t == null) {
  293. t = obj.GetType();
  294. new cs.Flags(BindingFlags.Instance) | BindingFlags.Public | BindingFlags.FlattenHierarchy;
  295. } else {
  296. obj = null;
  297. new cs.Flags(BindingFlags.Static) | BindingFlags.Public;
  298. }
  299. var f = t.GetField(field, bf);
  300. if (f != null) {
  301. if (f.FieldType.ToString().StartsWith("haxe.lang.Null")) {
  302. value = mkNullable(value, f.FieldType);
  303. }
  304. if (value != null
  305. && Object.ReferenceEquals(Lib.toNativeType(cs.system.Double), Lib.getNativeType(value))
  306. && !Object.ReferenceEquals(t, f.FieldType)) {
  307. var ic = Lib.as(value, IConvertible);
  308. value = ic.ToType(f.FieldType, null);
  309. }
  310. f.SetValue(obj, value);
  311. return value;
  312. } else {
  313. var prop = t.GetProperty(field, bf);
  314. if (prop == null) {
  315. // COM object handling
  316. if (t.IsCOMObject) {
  317. try {
  318. return t.InvokeMember(field, BindingFlags.SetProperty, null, obj, cs.NativeArray.make(value));
  319. } catch (e:cs.system.Exception) {
  320. // Closures of COM objects not supported currently
  321. }
  322. }
  323. throw "Field \'" + field + "\' not found for writing from Class " + t;
  324. }
  325. if (prop.PropertyType.ToString().StartsWith("haxe.lang.Null")) {
  326. value = mkNullable(value, prop.PropertyType);
  327. }
  328. if (Object.ReferenceEquals(Lib.toNativeType(cs.system.Double), Lib.getNativeType(value))
  329. && !Object.ReferenceEquals(t, prop.PropertyType)) {
  330. var ic = Lib.as(value, IConvertible);
  331. value = ic.ToType(prop.PropertyType, null);
  332. }
  333. prop.SetValue(obj, value, null);
  334. return value;
  335. }
  336. }
  337. public static function callMethod(obj:Dynamic, methods:NativeArray<MethodBase>, methodLength:Int, args:cs.NativeArray<Dynamic>):Dynamic {
  338. if (methodLength == 0)
  339. throw "No available methods";
  340. var length = args.length;
  341. var oargs:NativeArray<Dynamic> = new NativeArray(length);
  342. var ts:NativeArray<Type> = new NativeArray(length);
  343. var rates:NativeArray<Int> = new NativeArray(methods.Length);
  344. for (i in 0...length) {
  345. oargs[i] = args[i];
  346. if (args[i] != null)
  347. ts[i] = Lib.getNativeType(args[i]);
  348. }
  349. var last = 0;
  350. // first filter by number of parameters and if it is assignable
  351. if (methodLength > 1) {
  352. for (i in 0...methodLength) {
  353. var params = methods[i].GetParameters();
  354. if (params.Length != length) {
  355. continue;
  356. } else {
  357. var fits = true, crate = 0;
  358. for (i in 0...params.Length) {
  359. var param = params[i].ParameterType;
  360. var strParam = param + "";
  361. if (param.IsAssignableFrom(ts[i]) || (ts[i] == null && !param.IsValueType)) {
  362. // if it is directly assignable, we'll give it top rate
  363. continue;
  364. } else if (untyped strParam.StartsWith("haxe.lang.Null")
  365. || ((oargs[i] == null || Std.isOfType(oargs[i], IConvertible))
  366. && cast(untyped __typeof__(IConvertible), Type).IsAssignableFrom(param))) {
  367. // if it needs conversion, give a penalty. TODO rate penalty
  368. crate++;
  369. continue;
  370. } else if (!param.ContainsGenericParameters) { // generics don't appear as assignable, but may be in the end. no rate there.
  371. fits = false;
  372. break;
  373. }
  374. }
  375. if (fits) {
  376. rates[last] = crate;
  377. methods[last++] = methods[i];
  378. }
  379. }
  380. }
  381. methodLength = last;
  382. } else if (methodLength == 1 && methods[0].GetParameters().Length != length) {
  383. methodLength = 0;
  384. }
  385. // At this time, we should be left with only one method.
  386. // Of course, realistically, we can be left with plenty of methods, if there are lots of variants with IConvertible
  387. // But at this time we still aren't rating the best methods
  388. // FIXME rate best methods
  389. if (methodLength == 0)
  390. throw "Invalid calling parameters for method " + methods[0].Name;
  391. var best = cs.system.Double.PositiveInfinity;
  392. var bestMethod = 0;
  393. for (i in 0...methodLength) {
  394. if (rates[i] < best) {
  395. bestMethod = i;
  396. best = rates[i];
  397. }
  398. }
  399. methods[0] = methods[bestMethod];
  400. var params = methods[0].GetParameters();
  401. for (i in 0...params.Length) {
  402. var param = params[i].ParameterType;
  403. var strParam = param + "", arg = oargs[i];
  404. if (StringTools.startsWith(strParam, "haxe.lang.Null")) {
  405. oargs[i] = mkNullable(arg, param);
  406. } else if (cast(untyped __typeof__(IConvertible), Type).IsAssignableFrom(param)) {
  407. if (arg == null) {
  408. if (param.IsValueType)
  409. oargs[i] = Activator.CreateInstance(param);
  410. } else if (!cs.Lib.getNativeType(arg).IsAssignableFrom(param)) {
  411. oargs[i] = cast(arg, IConvertible).ToType(param, null);
  412. }
  413. }
  414. }
  415. if (methods[0].ContainsGenericParameters && Std.isOfType(methods[0], cs.system.reflection.MethodInfo)) {
  416. var m:MethodInfo = cast methods[0];
  417. var tgs = m.GetGenericArguments();
  418. for (i in 0...tgs.Length) {
  419. tgs[i] = untyped __typeof__(Dynamic);
  420. }
  421. m = m.MakeGenericMethod(tgs);
  422. var retg = m.Invoke(obj, oargs);
  423. return cs.internal.Runtime.unbox(retg);
  424. }
  425. var m = methods[0];
  426. if (obj == null && Std.isOfType(m, cs.system.reflection.ConstructorInfo)) {
  427. var ret = cast(m, cs.system.reflection.ConstructorInfo).Invoke(oargs);
  428. return unbox(ret);
  429. }
  430. var ret = m.Invoke(obj, oargs);
  431. return unbox(ret);
  432. }
  433. public static function unbox(dyn:Dynamic):Dynamic {
  434. if (dyn != null && untyped (Lib.getNativeType(dyn) + "").StartsWith("haxe.lang.Null")) {
  435. return dyn.toDynamic();
  436. } else {
  437. return dyn;
  438. }
  439. }
  440. #if !erase_generics
  441. @:functionCode('
  442. if (nullableType.ContainsGenericParameters)
  443. return haxe.lang.Null<object>.ofDynamic<object>(obj);
  444. return nullableType.GetMethod("_ofDynamic").Invoke(null, new object[] { obj });
  445. ')
  446. public static function mkNullable(obj:Dynamic, nullableType:Type):Dynamic {
  447. return null;
  448. }
  449. #else
  450. public static function mkNullable(obj:Dynamic, nullable:Type):Dynamic {
  451. return obj; // do nothing
  452. }
  453. #end
  454. public static function slowCallField(obj:Dynamic, field:String, args:cs.NativeArray<Dynamic>):Dynamic {
  455. if (field == "toString" && (args == null || args.length == 0)) {
  456. return obj.ToString();
  457. }
  458. if (args == null)
  459. args = new cs.NativeArray(0);
  460. var bf:BindingFlags;
  461. var t = Lib.as(obj, cs.system.Type);
  462. if (t == null) {
  463. var s = Lib.as(obj, String);
  464. if (s != null)
  465. return cs.internal.StringExt.StringRefl.handleCallField(untyped s, untyped field, args);
  466. t = untyped obj.GetType();
  467. bf = new Flags(BindingFlags.Instance) | BindingFlags.Public | BindingFlags.FlattenHierarchy;
  468. } else {
  469. if (t == Lib.toNativeType(String) && field == 'fromCharCode')
  470. return cs.internal.StringExt.fromCharCode(toInt(args[0]));
  471. obj = null;
  472. bf = new Flags(BindingFlags.Static) | BindingFlags.Public;
  473. }
  474. var mis:NativeArray<MethodBase> = untyped t.GetMethods(bf);
  475. var last = 0;
  476. for (i in 0...mis.Length) {
  477. var name = mis[i].Name;
  478. if (name == field)
  479. mis[last++] = mis[i];
  480. }
  481. if (last == 0 && (field == "__get" || field == "__set")) {
  482. field = field == "__get" ? "get_Item" : "set_Item";
  483. for (i in 0...mis.Length) {
  484. var name = mis[i].Name;
  485. if (name == field) {
  486. mis[last++] = mis[i];
  487. }
  488. }
  489. }
  490. if (last == 0 && t.IsCOMObject)
  491. return t.InvokeMember(field, BindingFlags.InvokeMethod, null, obj, args);
  492. if (last == 0) {
  493. throw 'Method "$field" not found on type $t';
  494. }
  495. return Runtime.callMethod(obj, mis, last, args);
  496. }
  497. public static function callField(obj:Dynamic, field:String, fieldHash:Int, args:cs.NativeArray<Dynamic>):Dynamic {
  498. var hxObj = Lib.as(obj, HxObject);
  499. if (hxObj != null)
  500. return untyped hxObj.__hx_invokeField(field, (fieldHash == 0) ? FieldLookup.hash(field) : fieldHash, args);
  501. return slowCallField(obj, field, args);
  502. }
  503. public static function getField(obj:Dynamic, field:String, fieldHash:Int, throwErrors:Bool):Dynamic {
  504. var hxObj = Lib.as(obj, HxObject);
  505. if (hxObj != null)
  506. return untyped hxObj.__hx_getField(field, (fieldHash == 0) ? FieldLookup.hash(field) : fieldHash, throwErrors, false, false);
  507. return slowGetField(obj, field, throwErrors);
  508. }
  509. public static function getField_f(obj:Dynamic, field:String, fieldHash:Int, throwErrors:Bool):Float {
  510. var hxObj = Lib.as(obj, HxObject);
  511. if (hxObj != null)
  512. return untyped hxObj.__hx_getField_f(field, (fieldHash == 0) ? FieldLookup.hash(field) : fieldHash, throwErrors, false);
  513. return toDouble(slowGetField(obj, field, throwErrors));
  514. }
  515. public static function setField(obj:Dynamic, field:String, fieldHash:Int, value:Dynamic):Dynamic {
  516. var hxObj = Lib.as(obj, HxObject);
  517. if (hxObj != null)
  518. return untyped hxObj.__hx_setField(field, (fieldHash == 0) ? FieldLookup.hash(field) : fieldHash, value, false);
  519. return slowSetField(obj, field, value);
  520. }
  521. public static function setField_f(obj:Dynamic, field:String, fieldHash:Int, value:Float):Float {
  522. var hxObj = Lib.as(obj, HxObject);
  523. if (hxObj != null)
  524. return untyped hxObj.__hx_setField_f(field, (fieldHash == 0) ? FieldLookup.hash(field) : fieldHash, value, false);
  525. return toDouble(slowSetField(obj, field, value));
  526. }
  527. public static function toString(obj:Dynamic):String {
  528. if (obj == null)
  529. return null;
  530. if (Std.isOfType(obj, Bool))
  531. if (obj)
  532. return "true";
  533. else
  534. return "false";
  535. return untyped obj.ToString();
  536. }
  537. #if erase_generics
  538. inline
  539. #end
  540. public static function typeEq(t1:Type, t2:Type):Bool {
  541. if (t1 == null || t2 == null)
  542. return t1 == t2;
  543. #if !erase_generics
  544. var t1i = t1.IsInterface, t2i = t2.IsInterface;
  545. if (t1i != t2i) {
  546. if (t1i) {
  547. var g = getGenericAttr(t1);
  548. if (g != null)
  549. t1 = g.generic;
  550. } else {
  551. var g = getGenericAttr(t2);
  552. if (g != null)
  553. t2 = g.generic;
  554. }
  555. }
  556. #end
  557. if (t1.GetGenericArguments().Length > 0)
  558. t1 = t1.GetGenericTypeDefinition();
  559. if (t2.GetGenericArguments().Length > 0)
  560. t2 = t2.GetGenericTypeDefinition();
  561. return Object.ReferenceEquals(t1, t2);
  562. }
  563. #if !erase_generics
  564. public static function getGenericAttr(t:cs.system.Type):cs.internal.HxObject.GenericInterface {
  565. for (attr in t.GetCustomAttributes(true))
  566. if (Std.isOfType(attr, cs.internal.HxObject.GenericInterface))
  567. return cast attr;
  568. return null;
  569. }
  570. #end
  571. #if !erase_generics
  572. @:functionCode('
  573. if (obj is To)
  574. return (To) obj;
  575. else if (obj == null)
  576. return default(To);
  577. if (typeof(To) == typeof(double))
  578. return (To)(object) toDouble(obj);
  579. else if (typeof(To) == typeof(int))
  580. return (To)(object) toInt(obj);
  581. else if (typeof(To) == typeof(float))
  582. return (To)(object)(float)toDouble(obj);
  583. else if (typeof(To) == typeof(long))
  584. return (To)(object)(long)toDouble(obj);
  585. else
  586. return (To) obj;
  587. ')
  588. public static function genericCast<To>(obj:Dynamic):To {
  589. return null;
  590. }
  591. #end
  592. @:functionCode('
  593. return (s1 == null ? "null" : s1) + (s2 == null ? "null" : s2);
  594. ')
  595. public static function concat(s1:String, s2:String):String {
  596. return null;
  597. }
  598. public static function toBool(dyn:Dynamic):Bool {
  599. return if (dyn == null) false else untyped __cs__("(bool){0}", dyn);
  600. }
  601. // TODO: change from genericCast to getConverter, so we don't need to handle extra boxing associated with it
  602. /*@:functionCode('
  603. if (typeof(To).TypeHandle == typeof(double).TypeHandle)
  604. return (System.Converter<object,To>) new System.Converter<object,double>(toDouble);
  605. else if (typeof(To).TypeHandle == typeof(double).TypeHandle)
  606. return (System.Converter<object,To>) new System.Converter<object,double>(toDouble);
  607. else
  608. return (System.Converter<object, To>) delegate(object obj) { return (To) obj; };
  609. ')
  610. public static function getConverter<To>():cs.system.Converter<Dynamic,To>
  611. {
  612. return null;
  613. }*/
  614. }
  615. @:nativeGen
  616. @:keep @:native("haxe.lang.EmptyObject") enum EmptyObject {
  617. EMPTY;
  618. }