Runtime.hx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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 = try
  423. m.Invoke(obj, oargs)
  424. catch(e:TargetInvocationException)
  425. throw e.InnerException;
  426. return cs.internal.Runtime.unbox(retg);
  427. }
  428. var m = methods[0];
  429. if (obj == null && Std.isOfType(m, cs.system.reflection.ConstructorInfo)) {
  430. var ret = try
  431. cast(m, cs.system.reflection.ConstructorInfo).Invoke(oargs)
  432. catch(e:TargetInvocationException)
  433. throw e.InnerException;
  434. return unbox(ret);
  435. }
  436. var ret = try
  437. m.Invoke(obj, oargs)
  438. catch(e:TargetInvocationException)
  439. throw e.InnerException;
  440. return unbox(ret);
  441. }
  442. public static function unbox(dyn:Dynamic):Dynamic {
  443. if (dyn != null && untyped (Lib.getNativeType(dyn) + "").StartsWith("haxe.lang.Null")) {
  444. return dyn.toDynamic();
  445. } else {
  446. return dyn;
  447. }
  448. }
  449. #if !erase_generics
  450. @:functionCode('
  451. if (nullableType.ContainsGenericParameters)
  452. return haxe.lang.Null<object>.ofDynamic<object>(obj);
  453. return nullableType.GetMethod("_ofDynamic").Invoke(null, new object[] { obj });
  454. ')
  455. public static function mkNullable(obj:Dynamic, nullableType:Type):Dynamic {
  456. return null;
  457. }
  458. #else
  459. public static function mkNullable(obj:Dynamic, nullable:Type):Dynamic {
  460. return obj; // do nothing
  461. }
  462. #end
  463. public static function slowCallField(obj:Dynamic, field:String, args:cs.NativeArray<Dynamic>):Dynamic {
  464. if (field == "toString" && (args == null || args.length == 0)) {
  465. return obj.ToString();
  466. }
  467. if (args == null)
  468. args = new cs.NativeArray(0);
  469. var bf:BindingFlags;
  470. var t = Lib.as(obj, cs.system.Type);
  471. if (t == null) {
  472. var s = Lib.as(obj, String);
  473. if (s != null)
  474. return cs.internal.StringExt.StringRefl.handleCallField(untyped s, untyped field, args);
  475. t = untyped obj.GetType();
  476. bf = new Flags(BindingFlags.Instance) | BindingFlags.Public | BindingFlags.FlattenHierarchy;
  477. } else {
  478. if (t == Lib.toNativeType(String) && field == 'fromCharCode')
  479. return cs.internal.StringExt.fromCharCode(toInt(args[0]));
  480. obj = null;
  481. bf = new Flags(BindingFlags.Static) | BindingFlags.Public;
  482. }
  483. var mis:NativeArray<MethodBase> = untyped t.GetMethods(bf);
  484. var last = 0;
  485. for (i in 0...mis.Length) {
  486. var name = mis[i].Name;
  487. if (name == field)
  488. mis[last++] = mis[i];
  489. }
  490. if (last == 0 && (field == "__get" || field == "__set")) {
  491. field = field == "__get" ? "get_Item" : "set_Item";
  492. for (i in 0...mis.Length) {
  493. var name = mis[i].Name;
  494. if (name == field) {
  495. mis[last++] = mis[i];
  496. }
  497. }
  498. }
  499. if (last == 0 && t.IsCOMObject)
  500. return t.InvokeMember(field, BindingFlags.InvokeMethod, null, obj, args);
  501. if (last == 0) {
  502. throw 'Method "$field" not found on type $t';
  503. }
  504. return Runtime.callMethod(obj, mis, last, args);
  505. }
  506. public static function callField(obj:Dynamic, field:String, fieldHash:Int, args:cs.NativeArray<Dynamic>):Dynamic {
  507. var hxObj = Lib.as(obj, HxObject);
  508. if (hxObj != null)
  509. return untyped hxObj.__hx_invokeField(field, (fieldHash == 0) ? FieldLookup.hash(field) : fieldHash, args);
  510. return slowCallField(obj, field, args);
  511. }
  512. public static function getField(obj:Dynamic, field:String, fieldHash:Int, throwErrors:Bool):Dynamic {
  513. var hxObj = Lib.as(obj, HxObject);
  514. if (hxObj != null)
  515. return untyped hxObj.__hx_getField(field, (fieldHash == 0) ? FieldLookup.hash(field) : fieldHash, throwErrors, false, false);
  516. return slowGetField(obj, field, throwErrors);
  517. }
  518. public static function getField_f(obj:Dynamic, field:String, fieldHash:Int, throwErrors:Bool):Float {
  519. var hxObj = Lib.as(obj, HxObject);
  520. if (hxObj != null)
  521. return untyped hxObj.__hx_getField_f(field, (fieldHash == 0) ? FieldLookup.hash(field) : fieldHash, throwErrors, false);
  522. return toDouble(slowGetField(obj, field, throwErrors));
  523. }
  524. public static function setField(obj:Dynamic, field:String, fieldHash:Int, value:Dynamic):Dynamic {
  525. var hxObj = Lib.as(obj, HxObject);
  526. if (hxObj != null)
  527. return untyped hxObj.__hx_setField(field, (fieldHash == 0) ? FieldLookup.hash(field) : fieldHash, value, false);
  528. return slowSetField(obj, field, value);
  529. }
  530. public static function setField_f(obj:Dynamic, field:String, fieldHash:Int, value:Float):Float {
  531. var hxObj = Lib.as(obj, HxObject);
  532. if (hxObj != null)
  533. return untyped hxObj.__hx_setField_f(field, (fieldHash == 0) ? FieldLookup.hash(field) : fieldHash, value, false);
  534. return toDouble(slowSetField(obj, field, value));
  535. }
  536. public static function toString(obj:Dynamic):String {
  537. if (obj == null)
  538. return null;
  539. if (Std.isOfType(obj, Bool))
  540. if (obj)
  541. return "true";
  542. else
  543. return "false";
  544. return untyped obj.ToString();
  545. }
  546. #if erase_generics
  547. inline
  548. #end
  549. public static function typeEq(t1:Type, t2:Type):Bool {
  550. if (t1 == null || t2 == null)
  551. return t1 == t2;
  552. #if !erase_generics
  553. var t1i = t1.IsInterface, t2i = t2.IsInterface;
  554. if (t1i != t2i) {
  555. if (t1i) {
  556. var g = getGenericAttr(t1);
  557. if (g != null)
  558. t1 = g.generic;
  559. } else {
  560. var g = getGenericAttr(t2);
  561. if (g != null)
  562. t2 = g.generic;
  563. }
  564. }
  565. #end
  566. if (t1.GetGenericArguments().Length > 0)
  567. t1 = t1.GetGenericTypeDefinition();
  568. if (t2.GetGenericArguments().Length > 0)
  569. t2 = t2.GetGenericTypeDefinition();
  570. return Object.ReferenceEquals(t1, t2);
  571. }
  572. #if !erase_generics
  573. public static function getGenericAttr(t:cs.system.Type):cs.internal.HxObject.GenericInterface {
  574. for (attr in t.GetCustomAttributes(true))
  575. if (Std.isOfType(attr, cs.internal.HxObject.GenericInterface))
  576. return cast attr;
  577. return null;
  578. }
  579. #end
  580. #if !erase_generics
  581. @:functionCode('
  582. if (obj is To)
  583. return (To) obj;
  584. else if (obj == null)
  585. return default(To);
  586. if (typeof(To) == typeof(double))
  587. return (To)(object) toDouble(obj);
  588. else if (typeof(To) == typeof(int))
  589. return (To)(object) toInt(obj);
  590. else if (typeof(To) == typeof(float))
  591. return (To)(object)(float)toDouble(obj);
  592. else if (typeof(To) == typeof(long))
  593. return (To)(object)(long)toDouble(obj);
  594. else
  595. return (To) obj;
  596. ')
  597. public static function genericCast<To>(obj:Dynamic):To {
  598. return null;
  599. }
  600. #end
  601. @:functionCode('
  602. return (s1 == null ? "null" : s1) + (s2 == null ? "null" : s2);
  603. ')
  604. public static function concat(s1:String, s2:String):String {
  605. return null;
  606. }
  607. public static function toBool(dyn:Dynamic):Bool {
  608. return if (dyn == null) false else untyped __cs__("(bool){0}", dyn);
  609. }
  610. // TODO: change from genericCast to getConverter, so we don't need to handle extra boxing associated with it
  611. /*@:functionCode('
  612. if (typeof(To).TypeHandle == typeof(double).TypeHandle)
  613. return (System.Converter<object,To>) new System.Converter<object,double>(toDouble);
  614. else if (typeof(To).TypeHandle == typeof(double).TypeHandle)
  615. return (System.Converter<object,To>) new System.Converter<object,double>(toDouble);
  616. else
  617. return (System.Converter<object, To>) delegate(object obj) { return (To) obj; };
  618. ')
  619. public static function getConverter<To>():cs.system.Converter<Dynamic,To>
  620. {
  621. return null;
  622. }*/
  623. }
  624. @:nativeGen
  625. @:keep @:native("haxe.lang.EmptyObject") enum EmptyObject {
  626. EMPTY;
  627. }