CLRMethod.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using ILRuntime.Mono.Cecil;
  7. using ILRuntime.Runtime.Intepreter;
  8. using ILRuntime.Runtime.Enviorment;
  9. using ILRuntime.CLR.TypeSystem;
  10. using ILRuntime.Runtime.Stack;
  11. using ILRuntime.CLR.Utils;
  12. namespace ILRuntime.CLR.Method
  13. {
  14. public class CLRMethod : IMethod
  15. {
  16. MethodInfo def;
  17. ConstructorInfo cDef;
  18. List<IType> parameters;
  19. ParameterInfo[] parametersCLR;
  20. ILRuntime.Runtime.Enviorment.AppDomain appdomain;
  21. CLRType declaringType;
  22. ParameterInfo[] param;
  23. bool isConstructor;
  24. CLRRedirectionDelegate redirect;
  25. IType[] genericArguments;
  26. Type[] genericArgumentsCLR;
  27. object[] invocationParam;
  28. bool isDelegateInvoke;
  29. int hashCode = -1;
  30. static int instance_id = 0x20000000;
  31. public IType DeclearingType
  32. {
  33. get
  34. {
  35. return declaringType;
  36. }
  37. }
  38. public string Name
  39. {
  40. get
  41. {
  42. return def.Name;
  43. }
  44. }
  45. public bool HasThis
  46. {
  47. get
  48. {
  49. return isConstructor ? !cDef.IsStatic : !def.IsStatic;
  50. }
  51. }
  52. public int GenericParameterCount
  53. {
  54. get
  55. {
  56. if (def.ContainsGenericParameters && def.IsGenericMethodDefinition)
  57. {
  58. return def.GetGenericArguments().Length;
  59. }
  60. return 0;
  61. }
  62. }
  63. public bool IsGenericInstance
  64. {
  65. get
  66. {
  67. return genericArguments != null;
  68. }
  69. }
  70. public bool IsDelegateInvoke
  71. {
  72. get
  73. {
  74. return isDelegateInvoke;
  75. }
  76. }
  77. public bool IsStatic
  78. {
  79. get
  80. {
  81. if (cDef != null)
  82. return cDef.IsStatic;
  83. else
  84. return def.IsStatic;
  85. }
  86. }
  87. public CLRRedirectionDelegate Redirection { get { return redirect; } }
  88. public MethodInfo MethodInfo { get { return def; } }
  89. public ConstructorInfo ConstructorInfo { get { return cDef; } }
  90. public IType[] GenericArguments { get { return genericArguments; } }
  91. public Type[] GenericArgumentsCLR
  92. {
  93. get
  94. {
  95. if(genericArgumentsCLR == null)
  96. {
  97. if (cDef != null)
  98. genericArgumentsCLR = cDef.GetGenericArguments();
  99. else
  100. genericArgumentsCLR = def.GetGenericArguments();
  101. }
  102. return genericArgumentsCLR;
  103. }
  104. }
  105. internal CLRMethod(MethodInfo def, CLRType type, ILRuntime.Runtime.Enviorment.AppDomain domain)
  106. {
  107. this.def = def;
  108. declaringType = type;
  109. this.appdomain = domain;
  110. param = def.GetParameters();
  111. if (!def.ContainsGenericParameters)
  112. {
  113. ReturnType = domain.GetType(def.ReturnType.FullName);
  114. if (ReturnType == null)
  115. {
  116. ReturnType = domain.GetType(def.ReturnType.AssemblyQualifiedName);
  117. }
  118. }
  119. if (type.IsDelegate && def.Name == "Invoke")
  120. isDelegateInvoke = true;
  121. isConstructor = false;
  122. if (def != null)
  123. {
  124. if (def.IsGenericMethod && !def.IsGenericMethodDefinition)
  125. {
  126. //Redirection of Generic method Definition will be prioritized
  127. if(!appdomain.RedirectMap.TryGetValue(def.GetGenericMethodDefinition(), out redirect))
  128. appdomain.RedirectMap.TryGetValue(def, out redirect);
  129. }
  130. else
  131. appdomain.RedirectMap.TryGetValue(def, out redirect);
  132. }
  133. }
  134. internal CLRMethod(ConstructorInfo def, CLRType type, ILRuntime.Runtime.Enviorment.AppDomain domain)
  135. {
  136. this.cDef = def;
  137. declaringType = type;
  138. this.appdomain = domain;
  139. param = def.GetParameters();
  140. if (!def.ContainsGenericParameters)
  141. {
  142. ReturnType = type;
  143. }
  144. isConstructor = true;
  145. if (def != null)
  146. {
  147. appdomain.RedirectMap.TryGetValue(cDef, out redirect);
  148. }
  149. }
  150. public int ParameterCount
  151. {
  152. get
  153. {
  154. return param != null ? param.Length : 0;
  155. }
  156. }
  157. public List<IType> Parameters
  158. {
  159. get
  160. {
  161. if (parameters == null)
  162. {
  163. InitParameters();
  164. }
  165. return parameters;
  166. }
  167. }
  168. public ParameterInfo[] ParametersCLR
  169. {
  170. get
  171. {
  172. if(parametersCLR == null)
  173. {
  174. if (cDef != null)
  175. parametersCLR = cDef.GetParameters();
  176. else
  177. parametersCLR = def.GetParameters();
  178. }
  179. return parametersCLR;
  180. }
  181. }
  182. public IType ReturnType
  183. {
  184. get;
  185. private set;
  186. }
  187. public bool IsConstructor
  188. {
  189. get
  190. {
  191. return cDef != null;
  192. }
  193. }
  194. void InitParameters()
  195. {
  196. parameters = new List<IType>();
  197. foreach (var i in param)
  198. {
  199. IType type = appdomain.GetType(i.ParameterType.FullName);
  200. if (type == null)
  201. type = appdomain.GetType(i.ParameterType.AssemblyQualifiedName);
  202. if (i.ParameterType.IsGenericTypeDefinition)
  203. {
  204. if (type == null)
  205. type = appdomain.GetType(i.ParameterType.GetGenericTypeDefinition().FullName);
  206. if (type == null)
  207. type = appdomain.GetType(i.ParameterType.GetGenericTypeDefinition().AssemblyQualifiedName);
  208. }
  209. if (i.ParameterType.ContainsGenericParameters)
  210. {
  211. var t = i.ParameterType;
  212. if (t.HasElementType)
  213. t = i.ParameterType.GetElementType();
  214. else if (t.GetGenericArguments().Length > 0)
  215. {
  216. t = t.GetGenericArguments()[0];
  217. }
  218. type = new ILGenericParameterType(t.Name);
  219. }
  220. var pt = i.ParameterType;
  221. if (pt.IsByRef)
  222. {
  223. pt = (i.ParameterType).GetElementType();
  224. appdomain.GetType(pt.FullName);
  225. }
  226. if (type == null)
  227. throw new TypeLoadException();
  228. parameters.Add(type);
  229. }
  230. }
  231. unsafe StackObject* Minus(StackObject* a, int b)
  232. {
  233. return (StackObject*)((long)a - sizeof(StackObject) * b);
  234. }
  235. public unsafe object Invoke(Runtime.Intepreter.ILIntepreter intepreter, StackObject* esp, IList<object> mStack, bool isNewObj = false)
  236. {
  237. if (parameters == null)
  238. {
  239. InitParameters();
  240. }
  241. int paramCount = ParameterCount;
  242. if (invocationParam == null)
  243. invocationParam = new object[paramCount];
  244. object[] param = invocationParam;
  245. for (int i = paramCount; i >= 1; i--)
  246. {
  247. var p = Minus(esp, i);
  248. var pt = this.param[paramCount - i].ParameterType;
  249. var obj = pt.CheckCLRTypes(StackObject.ToObject(p, appdomain, mStack));
  250. obj = ILIntepreter.CheckAndCloneValueType(obj, appdomain);
  251. param[paramCount - i] = obj;
  252. }
  253. if (isConstructor)
  254. {
  255. if (!isNewObj)
  256. {
  257. if (!cDef.IsStatic)
  258. {
  259. object instance = declaringType.TypeForCLR.CheckCLRTypes(StackObject.ToObject((Minus(esp, paramCount + 1)), appdomain, mStack));
  260. if (instance == null)
  261. throw new NullReferenceException();
  262. if (instance is CrossBindingAdaptorType && paramCount == 0)//It makes no sense to call the Adaptor's default constructor
  263. return null;
  264. cDef.Invoke(instance, param);
  265. return null;
  266. }
  267. else
  268. {
  269. throw new NotImplementedException();
  270. }
  271. }
  272. else
  273. {
  274. var res = cDef.Invoke(param);
  275. FixReference(paramCount, esp, param, mStack, null, false);
  276. return res;
  277. }
  278. }
  279. else
  280. {
  281. object instance = null;
  282. if (!def.IsStatic)
  283. {
  284. instance = StackObject.ToObject((Minus(esp, paramCount + 1)), appdomain, mStack);
  285. if (!(instance is Reflection.ILRuntimeWrapperType))
  286. instance = declaringType.TypeForCLR.CheckCLRTypes(instance);
  287. //if (declaringType.IsValueType)
  288. // instance = ILIntepreter.CheckAndCloneValueType(instance, appdomain);
  289. if (instance == null)
  290. throw new NullReferenceException();
  291. }
  292. object res = null;
  293. /*if (redirect != null)
  294. res = redirect(new ILContext(appdomain, intepreter, esp, mStack, this), instance, param, genericArguments);
  295. else*/
  296. {
  297. res = def.Invoke(instance, param);
  298. }
  299. FixReference(paramCount, esp, param, mStack, instance, !def.IsStatic);
  300. return res;
  301. }
  302. }
  303. unsafe void FixReference(int paramCount, StackObject* esp, object[] param, IList<object> mStack,object instance, bool hasThis)
  304. {
  305. var cnt = hasThis ? paramCount + 1 : paramCount;
  306. for (int i = cnt; i >= 1; i--)
  307. {
  308. var p = Minus(esp, i);
  309. var val = i <= paramCount ? param[paramCount - i] : instance;
  310. switch (p->ObjectType)
  311. {
  312. case ObjectTypes.StackObjectReference:
  313. {
  314. var addr = *(long*)&p->Value;
  315. var dst = (StackObject*)addr;
  316. if (dst->ObjectType >= ObjectTypes.Object)
  317. {
  318. var obj = val;
  319. if (obj is CrossBindingAdaptorType)
  320. obj = ((CrossBindingAdaptorType)obj).ILInstance;
  321. mStack[dst->Value] = obj;
  322. }
  323. else
  324. {
  325. ILIntepreter.UnboxObject(dst, val, mStack, appdomain);
  326. }
  327. }
  328. break;
  329. case ObjectTypes.FieldReference:
  330. {
  331. var obj = mStack[p->Value];
  332. if(obj is ILTypeInstance)
  333. {
  334. ((ILTypeInstance)obj)[p->ValueLow] = val;
  335. }
  336. else
  337. {
  338. var t = appdomain.GetType(obj.GetType()) as CLRType;
  339. t.GetField(p->ValueLow).SetValue(obj, val);
  340. }
  341. }
  342. break;
  343. case ObjectTypes.StaticFieldReference:
  344. {
  345. var t = appdomain.GetType(p->Value);
  346. if(t is ILType)
  347. {
  348. ((ILType)t).StaticInstance[p->ValueLow] = val;
  349. }
  350. else
  351. {
  352. ((CLRType)t).SetStaticFieldValue(p->ValueLow, val);
  353. }
  354. }
  355. break;
  356. case ObjectTypes.ArrayReference:
  357. {
  358. var arr = mStack[p->Value] as Array;
  359. arr.SetValue(val, p->ValueLow);
  360. }
  361. break;
  362. }
  363. }
  364. }
  365. public IMethod MakeGenericMethod(IType[] genericArguments)
  366. {
  367. Type[] p = new Type[genericArguments.Length];
  368. for (int i = 0; i < genericArguments.Length; i++)
  369. {
  370. p[i] = genericArguments[i].TypeForCLR;
  371. }
  372. MethodInfo t = null;
  373. #if UNITY_EDITOR || (DEBUG && !DISABLE_ILRUNTIME_DEBUG)
  374. try
  375. {
  376. #endif
  377. t = def.MakeGenericMethod(p);
  378. #if UNITY_EDITOR || (DEBUG && !DISABLE_ILRUNTIME_DEBUG)
  379. }
  380. catch (Exception e)
  381. {
  382. string argString = "";
  383. for (int i = 0; i < genericArguments.Length; i++)
  384. {
  385. argString += genericArguments[i].TypeForCLR.FullName + ", ";
  386. }
  387. argString = argString.Substring(0, argString.Length - 2);
  388. throw new Exception($"MakeGenericMethod failed : {def.DeclaringType.FullName}.{def.Name}<{argString}>");
  389. }
  390. #endif
  391. var res = new CLRMethod(t, declaringType, appdomain);
  392. res.genericArguments = genericArguments;
  393. return res;
  394. }
  395. public override string ToString()
  396. {
  397. if (def != null)
  398. return def.ToString();
  399. else
  400. return cDef.ToString();
  401. }
  402. public override int GetHashCode()
  403. {
  404. if (hashCode == -1)
  405. hashCode = System.Threading.Interlocked.Add(ref instance_id, 1);
  406. return hashCode;
  407. }
  408. }
  409. }