Runtime.hx 22 KB

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