CLRMethod.cs 15 KB

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