Runtime.hx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * Copyright (C)2005-2017 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, Array 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:Array<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. // @:functionCode('
  521. // if (field == "toString")
  522. // {
  523. // if (args == null)
  524. // return obj.ToString();
  525. // field = "ToString";
  526. // }
  527. // if (args == null) args = new Array<object>();
  528. // System.Reflection.BindingFlags bf;
  529. // System.Type t = obj as System.Type;
  530. // if (t == null)
  531. // {
  532. // string s = obj as string;
  533. // if (s != null)
  534. // return haxe.lang.StringRefl.handleCallField(s, field, args);
  535. // t = obj.GetType();
  536. // bf = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.FlattenHierarchy;
  537. // } else {
  538. // if (t == typeof(string) && field.Equals("fromCharCode"))
  539. // return haxe.lang.StringExt.fromCharCode(toInt(args[0]));
  540. // obj = null;
  541. // bf = System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public;
  542. // }
  543. // System.Reflection.MethodInfo[] mis = t.GetMethods(bf);
  544. // int last = 0;
  545. // for (int i = 0; i < mis.Length; i++)
  546. // {
  547. // string name = mis[i].Name;
  548. // if (name.Equals(field))
  549. // {
  550. // mis[last++] = mis[i];
  551. // }
  552. // }
  553. // if (last == 0 && (field == "__get" || field == "__set"))
  554. // {
  555. // field = field == "__get" ? "get_Item" : "set_Item";
  556. // for (int i = 0; i < mis.Length; i++)
  557. // {
  558. // string name = mis[i].Name;
  559. // if (name.Equals(field))
  560. // {
  561. // mis[last++] = mis[i];
  562. // }
  563. // }
  564. // }
  565. // if (last == 0 && t.IsCOMObject)
  566. // {
  567. // object[] oargs = new object[arrLen(args)];
  568. // for (int i = 0; i < oargs.Length; i++)
  569. // {
  570. // oargs[i] = args[i];
  571. // }
  572. // return t.InvokeMember(field, System.Reflection.BindingFlags.InvokeMethod, null, obj, oargs);
  573. // }
  574. // if (last == 0)
  575. // {
  576. // throw haxe.lang.HaxeException.wrap("Method \'" + field + "\' not found on type " + t);
  577. // }
  578. // return haxe.lang.Runtime.callMethod(obj, mis, last, args);
  579. // ')
  580. public static function slowCallField(obj:Dynamic, field:String, args:Array<Dynamic>):Dynamic
  581. {
  582. if (field == "toString" && (args == null || args.length == 0))
  583. {
  584. return obj.ToString();
  585. }
  586. if (args == null) args = [];
  587. var bf:BindingFlags;
  588. var t = Lib.as(obj,cs.system.Type);
  589. if (t == null)
  590. {
  591. var s = Lib.as(obj,String);
  592. if (s != null)
  593. return cs.internal.StringExt.StringRefl.handleCallField(untyped s, untyped field, args);
  594. t = untyped obj.GetType();
  595. bf = new Flags(BindingFlags.Instance) | BindingFlags.Public | BindingFlags.FlattenHierarchy;
  596. } else {
  597. if (t == Lib.toNativeType(String) && field == 'fromCharCode')
  598. return cs.internal.StringExt.fromCharCode(toInt(args[0]));
  599. obj = null;
  600. bf = new Flags(BindingFlags.Static) | BindingFlags.Public;
  601. }
  602. var mis:NativeArray<MethodBase> = untyped t.GetMethods(bf);
  603. var last = 0;
  604. for (i in 0...mis.Length)
  605. {
  606. var name = mis[i].Name;
  607. if (name == field)
  608. mis[last++] = mis[i];
  609. }
  610. if (last == 0 && (field == "__get" || field == "__set"))
  611. {
  612. field = field == "__get" ? "get_Item" : "set_Item";
  613. for (i in 0...mis.Length)
  614. {
  615. var name = mis[i].Name;
  616. if (name == field)
  617. {
  618. mis[last++] = mis[i];
  619. }
  620. }
  621. }
  622. if (last == 0 && t.IsCOMObject)
  623. {
  624. var oargs = new NativeArray(args.length);
  625. for (i in 0...oargs.Length)
  626. {
  627. oargs[i] = args[i];
  628. }
  629. return t.InvokeMember(field, BindingFlags.InvokeMethod, null, obj, oargs);
  630. }
  631. if (last == 0)
  632. {
  633. throw 'Method "$field" not found on type $t';
  634. }
  635. return Runtime.callMethod(obj, mis, last, args);
  636. }
  637. public static function callField(obj:Dynamic, field:String, fieldHash:Int, args:Array<Dynamic>):Dynamic
  638. {
  639. var hxObj = Lib.as(obj, HxObject);
  640. if (hxObj != null)
  641. return untyped hxObj.__hx_invokeField(field, (fieldHash == 0) ? FieldLookup.hash(field) : fieldHash, args);
  642. return slowCallField(obj, field, args);
  643. }
  644. public static function getField(obj:Dynamic, field:String, fieldHash:Int, throwErrors:Bool):Dynamic
  645. {
  646. var hxObj = Lib.as(obj, HxObject);
  647. if (hxObj != null)
  648. return untyped hxObj.__hx_getField(field, (fieldHash == 0) ? FieldLookup.hash(field) : fieldHash, throwErrors, false, false);
  649. return slowGetField(obj, field, throwErrors);
  650. }
  651. public static function getField_f(obj:Dynamic, field:String, fieldHash:Int, throwErrors:Bool):Float
  652. {
  653. var hxObj = Lib.as(obj, HxObject);
  654. if (hxObj != null)
  655. return untyped hxObj.__hx_getField_f(field, (fieldHash == 0) ? FieldLookup.hash(field) : fieldHash, throwErrors, false);
  656. return toDouble(slowGetField(obj, field, throwErrors));
  657. }
  658. public static function setField(obj:Dynamic, field:String, fieldHash:Int, value:Dynamic):Dynamic
  659. {
  660. var hxObj = Lib.as(obj, HxObject);
  661. if (hxObj != null)
  662. return untyped hxObj.__hx_setField(field, (fieldHash == 0) ? FieldLookup.hash(field) : fieldHash, value, false);
  663. return slowSetField(obj, field, value);
  664. }
  665. public static function setField_f(obj:Dynamic, field:String, fieldHash:Int, value:Float):Float
  666. {
  667. var hxObj = Lib.as(obj, HxObject);
  668. if (hxObj != null)
  669. return untyped hxObj.__hx_setField_f(field, (fieldHash == 0) ? FieldLookup.hash(field) : fieldHash, value, false);
  670. return toDouble(slowSetField(obj, field, value));
  671. }
  672. public static function toString(obj:Dynamic):String
  673. {
  674. if (obj == null)
  675. return null;
  676. if (Std.is(obj, Bool))
  677. if(obj)
  678. return "true";
  679. else
  680. return "false";
  681. return untyped obj.ToString();
  682. }
  683. #if erase_generics
  684. inline
  685. #end
  686. public static function typeEq(t1:Type, t2:Type):Bool
  687. {
  688. if (t1 == null || t2 == null)
  689. return t1 == t2;
  690. #if !erase_generics
  691. var t1i = t1.IsInterface,
  692. t2i = t2.IsInterface;
  693. if (t1i != t2i)
  694. {
  695. if (t1i)
  696. {
  697. var g = getGenericAttr(t1);
  698. if (g != null)
  699. t1 = g.generic;
  700. } else {
  701. var g = getGenericAttr(t2);
  702. if (g != null)
  703. t2 = g.generic;
  704. }
  705. }
  706. #end
  707. if (t1.GetGenericArguments().Length > 0) t1 = t1.GetGenericTypeDefinition();
  708. if (t2.GetGenericArguments().Length > 0) t2 = t2.GetGenericTypeDefinition();
  709. return Object.ReferenceEquals(t1,t2);
  710. }
  711. #if !erase_generics
  712. public static function getGenericAttr(t:cs.system.Type):cs.internal.HxObject.GenericInterface
  713. {
  714. for (attr in t.GetCustomAttributes(true))
  715. if (Std.is(attr,cs.internal.HxObject.GenericInterface))
  716. return cast attr;
  717. return null;
  718. }
  719. #end
  720. #if !erase_generics
  721. @:functionCode('
  722. if (obj is To)
  723. return (To) obj;
  724. else if (obj == null)
  725. return default(To);
  726. if (typeof(To) == typeof(double))
  727. return (To)(object) toDouble(obj);
  728. else if (typeof(To) == typeof(int))
  729. return (To)(object) toInt(obj);
  730. else if (typeof(To) == typeof(float))
  731. return (To)(object)(float)toDouble(obj);
  732. else if (typeof(To) == typeof(long))
  733. return (To)(object)(long)toDouble(obj);
  734. else
  735. return (To) obj;
  736. ')
  737. public static function genericCast<To>(obj:Dynamic):To
  738. {
  739. return null;
  740. }
  741. #end
  742. @:functionCode('
  743. return (s1 == null ? "null" : s1) + (s2 == null ? "null" : s2);
  744. ')
  745. public static function concat(s1:String, s2:String):String
  746. {
  747. return null;
  748. }
  749. public static function toBool(dyn:Dynamic):Bool
  750. {
  751. return if (dyn == null) false else untyped __cs__("(bool){0}", dyn);
  752. }
  753. //TODO: change from genericCast to getConverter, so we don't need to handle extra boxing associated with it
  754. /*@:functionCode('
  755. if (typeof(To).TypeHandle == typeof(double).TypeHandle)
  756. return (System.Converter<object,To>) new System.Converter<object,double>(toDouble);
  757. else if (typeof(To).TypeHandle == typeof(double).TypeHandle)
  758. return (System.Converter<object,To>) new System.Converter<object,double>(toDouble);
  759. else
  760. return (System.Converter<object, To>) delegate(object obj) { return (To) obj; };
  761. ')
  762. public static function getConverter<To>():cs.system.Converter<Dynamic,To>
  763. {
  764. return null;
  765. }*/
  766. }
  767. @:nativeGen
  768. @:keep @:native("haxe.lang.EmptyObject") enum EmptyObject
  769. {
  770. EMPTY;
  771. }