Runtime.hx 24 KB

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