Runtime.hx 24 KB

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