DelegateAdapter.cs 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ILRuntime.CLR.TypeSystem;
  6. using ILRuntime.CLR.Method;
  7. using ILRuntime.Runtime;
  8. using ILRuntime.Runtime.Stack;
  9. using ILRuntime.Other;
  10. using ILRuntime.Runtime.Enviorment;
  11. namespace ILRuntime.Runtime.Intepreter
  12. {
  13. #region Functions
  14. class FunctionDelegateAdapter<TResult> : DelegateAdapter
  15. {
  16. Func<TResult> action;
  17. static InvocationTypes[] pTypes;
  18. static FunctionDelegateAdapter()
  19. {
  20. pTypes = new InvocationTypes[]
  21. {
  22. InvocationContext.GetInvocationType<TResult>(),
  23. };
  24. }
  25. public FunctionDelegateAdapter()
  26. {
  27. }
  28. private FunctionDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  29. : base(appdomain, instance, method)
  30. {
  31. action = InvokeILMethod;
  32. }
  33. public override Type NativeDelegateType
  34. {
  35. get
  36. {
  37. return typeof(Func<TResult>);
  38. }
  39. }
  40. public override Delegate Delegate
  41. {
  42. get
  43. {
  44. return action;
  45. }
  46. }
  47. unsafe TResult InvokeILMethod()
  48. {
  49. using (var ctx = BeginInvoke())
  50. {
  51. var esp = ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
  52. ctx.SetInvoked(esp);
  53. return ctx.ReadResult<TResult>(pTypes[0]);
  54. }
  55. }
  56. public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  57. {
  58. return new FunctionDelegateAdapter<TResult>(appdomain, instance, method);
  59. }
  60. public override IDelegateAdapter Clone()
  61. {
  62. var res = new FunctionDelegateAdapter<TResult>(appdomain, instance, method);
  63. res.isClone = true;
  64. return res;
  65. }
  66. public override void Combine(Delegate dele)
  67. {
  68. action += (Func<TResult>)dele;
  69. }
  70. public override void Remove(Delegate dele)
  71. {
  72. action -= (Func<TResult>)dele;
  73. }
  74. }
  75. class FunctionDelegateAdapter<T1, TResult> : DelegateAdapter
  76. {
  77. Func<T1, TResult> action;
  78. static InvocationTypes[] pTypes;
  79. static FunctionDelegateAdapter()
  80. {
  81. pTypes = new InvocationTypes[]
  82. {
  83. InvocationContext.GetInvocationType<T1>(),
  84. InvocationContext.GetInvocationType<TResult>(),
  85. };
  86. }
  87. public FunctionDelegateAdapter()
  88. {
  89. }
  90. private FunctionDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  91. : base(appdomain, instance, method)
  92. {
  93. action = InvokeILMethod;
  94. }
  95. public override Type NativeDelegateType
  96. {
  97. get
  98. {
  99. return typeof(Func<T1, TResult>);
  100. }
  101. }
  102. public override Delegate Delegate
  103. {
  104. get
  105. {
  106. return action;
  107. }
  108. }
  109. unsafe TResult InvokeILMethod(T1 p1)
  110. {
  111. using (var ctx = BeginInvoke())
  112. {
  113. ctx.PushParameter(pTypes[0], p1);
  114. var esp = ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
  115. ctx.SetInvoked(esp);
  116. return ctx.ReadResult<TResult>(pTypes[1]);
  117. }
  118. }
  119. public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  120. {
  121. return new FunctionDelegateAdapter<T1, TResult>(appdomain, instance, method);
  122. }
  123. public override IDelegateAdapter Clone()
  124. {
  125. var res = new FunctionDelegateAdapter<T1, TResult>(appdomain, instance, method);
  126. res.isClone = true;
  127. return res;
  128. }
  129. public override void Combine(Delegate dele)
  130. {
  131. action += (Func<T1, TResult>)dele;
  132. }
  133. public override void Remove(Delegate dele)
  134. {
  135. action -= (Func<T1, TResult>)dele;
  136. }
  137. }
  138. class FunctionDelegateAdapter<T1, T2, TResult> : DelegateAdapter
  139. {
  140. Func<T1, T2, TResult> action;
  141. static InvocationTypes[] pTypes;
  142. static FunctionDelegateAdapter()
  143. {
  144. pTypes = new InvocationTypes[]
  145. {
  146. InvocationContext.GetInvocationType<T1>(),
  147. InvocationContext.GetInvocationType<T2>(),
  148. InvocationContext.GetInvocationType<TResult>(),
  149. };
  150. }
  151. public FunctionDelegateAdapter()
  152. {
  153. }
  154. private FunctionDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  155. : base(appdomain, instance, method)
  156. {
  157. action = InvokeILMethod;
  158. }
  159. public override Type NativeDelegateType
  160. {
  161. get
  162. {
  163. return typeof(Func<T1, T2, TResult>);
  164. }
  165. }
  166. public override Delegate Delegate
  167. {
  168. get
  169. {
  170. return action;
  171. }
  172. }
  173. unsafe TResult InvokeILMethod(T1 p1, T2 p2)
  174. {
  175. using (var ctx = BeginInvoke())
  176. {
  177. ctx.PushParameter(pTypes[0], p1);
  178. ctx.PushParameter(pTypes[1], p2);
  179. var esp = ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
  180. ctx.SetInvoked(esp);
  181. return ctx.ReadResult<TResult>(pTypes[2]);
  182. }
  183. }
  184. public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  185. {
  186. return new FunctionDelegateAdapter<T1, T2, TResult>(appdomain, instance, method);
  187. }
  188. public override IDelegateAdapter Clone()
  189. {
  190. var res = new FunctionDelegateAdapter<T1, T2, TResult>(appdomain, instance, method);
  191. res.isClone = true;
  192. return res;
  193. }
  194. public override void Combine(Delegate dele)
  195. {
  196. action += (Func<T1, T2, TResult>)dele;
  197. }
  198. public override void Remove(Delegate dele)
  199. {
  200. action -= (Func<T1, T2, TResult>)dele;
  201. }
  202. }
  203. class FunctionDelegateAdapter<T1, T2, T3, TResult> : DelegateAdapter
  204. {
  205. Func<T1, T2, T3, TResult> action;
  206. static InvocationTypes[] pTypes;
  207. static FunctionDelegateAdapter()
  208. {
  209. pTypes = new InvocationTypes[]
  210. {
  211. InvocationContext.GetInvocationType<T1>(),
  212. InvocationContext.GetInvocationType<T2>(),
  213. InvocationContext.GetInvocationType<T3>(),
  214. InvocationContext.GetInvocationType<TResult>(),
  215. };
  216. }
  217. public FunctionDelegateAdapter()
  218. {
  219. }
  220. private FunctionDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  221. : base(appdomain, instance, method)
  222. {
  223. action = InvokeILMethod;
  224. }
  225. public override Type NativeDelegateType
  226. {
  227. get
  228. {
  229. return typeof(Func<T1, T2, T3, TResult>);
  230. }
  231. }
  232. public override Delegate Delegate
  233. {
  234. get
  235. {
  236. return action;
  237. }
  238. }
  239. unsafe TResult InvokeILMethod(T1 p1, T2 p2, T3 p3)
  240. {
  241. using (var ctx = BeginInvoke())
  242. {
  243. ctx.PushParameter(pTypes[0], p1);
  244. ctx.PushParameter(pTypes[1], p2);
  245. ctx.PushParameter(pTypes[2], p3);
  246. var esp = ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
  247. ctx.SetInvoked(esp);
  248. return ctx.ReadResult<TResult>(pTypes[3]);
  249. }
  250. }
  251. public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  252. {
  253. return new FunctionDelegateAdapter<T1, T2, T3, TResult>(appdomain, instance, method);
  254. }
  255. public override IDelegateAdapter Clone()
  256. {
  257. var res = new FunctionDelegateAdapter<T1, T2, T3, TResult>(appdomain, instance, method);
  258. res.isClone = true;
  259. return res;
  260. }
  261. public override void Combine(Delegate dele)
  262. {
  263. action += (Func<T1, T2, T3, TResult>)dele;
  264. }
  265. public override void Remove(Delegate dele)
  266. {
  267. action -= (Func<T1, T2, T3, TResult>)dele;
  268. }
  269. }
  270. class FunctionDelegateAdapter<T1, T2, T3, T4, TResult> : DelegateAdapter
  271. {
  272. Func<T1, T2, T3, T4, TResult> action;
  273. static InvocationTypes[] pTypes;
  274. static FunctionDelegateAdapter()
  275. {
  276. pTypes = new InvocationTypes[]
  277. {
  278. InvocationContext.GetInvocationType<T1>(),
  279. InvocationContext.GetInvocationType<T2>(),
  280. InvocationContext.GetInvocationType<T3>(),
  281. InvocationContext.GetInvocationType<T4>(),
  282. InvocationContext.GetInvocationType<TResult>(),
  283. };
  284. }
  285. public FunctionDelegateAdapter()
  286. {
  287. }
  288. private FunctionDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  289. : base(appdomain, instance, method)
  290. {
  291. action = InvokeILMethod;
  292. }
  293. public override Type NativeDelegateType
  294. {
  295. get
  296. {
  297. return typeof(Func<T1, T2, T3, T4, TResult>);
  298. }
  299. }
  300. public override Delegate Delegate
  301. {
  302. get
  303. {
  304. return action;
  305. }
  306. }
  307. unsafe TResult InvokeILMethod(T1 p1, T2 p2, T3 p3, T4 p4)
  308. {
  309. using (var ctx = BeginInvoke())
  310. {
  311. ctx.PushParameter(pTypes[0], p1);
  312. ctx.PushParameter(pTypes[1], p2);
  313. ctx.PushParameter(pTypes[2], p3);
  314. ctx.PushParameter(pTypes[3], p4);
  315. var esp = ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
  316. ctx.SetInvoked(esp);
  317. return ctx.ReadResult<TResult>(pTypes[4]);
  318. }
  319. }
  320. public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  321. {
  322. return new FunctionDelegateAdapter<T1, T2, T3, T4, TResult>(appdomain, instance, method);
  323. }
  324. public override IDelegateAdapter Clone()
  325. {
  326. var res = new FunctionDelegateAdapter<T1, T2, T3, T4, TResult>(appdomain, instance, method);
  327. res.isClone = true;
  328. return res;
  329. }
  330. public override void Combine(Delegate dele)
  331. {
  332. action += (Func<T1, T2, T3, T4, TResult>)dele;
  333. }
  334. public override void Remove(Delegate dele)
  335. {
  336. action -= (Func<T1, T2, T3, T4, TResult>)dele;
  337. }
  338. }
  339. #endregion
  340. #region Methods
  341. class MethodDelegateAdapter<T1> : DelegateAdapter
  342. {
  343. Action<T1> action;
  344. static InvocationTypes pType;
  345. static MethodDelegateAdapter()
  346. {
  347. pType = InvocationContext.GetInvocationType<T1>();
  348. }
  349. public MethodDelegateAdapter()
  350. {
  351. }
  352. private MethodDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  353. : base(appdomain, instance, method)
  354. {
  355. action = InvokeILMethod;
  356. }
  357. public override Type NativeDelegateType
  358. {
  359. get
  360. {
  361. return typeof(Action<T1>);
  362. }
  363. }
  364. public override Delegate Delegate
  365. {
  366. get
  367. {
  368. return action;
  369. }
  370. }
  371. unsafe void InvokeILMethod(T1 p1)
  372. {
  373. using (var ctx = BeginInvoke())
  374. {
  375. ctx.PushParameter(pType, p1);
  376. ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
  377. }
  378. }
  379. public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  380. {
  381. return new MethodDelegateAdapter<T1>(appdomain, instance, method);
  382. }
  383. public override IDelegateAdapter Clone()
  384. {
  385. var res = new MethodDelegateAdapter<T1>(appdomain, instance, method);
  386. res.isClone = true;
  387. return res;
  388. }
  389. public override void Combine(Delegate dele)
  390. {
  391. action += (Action<T1>)dele;
  392. }
  393. public override void Remove(Delegate dele)
  394. {
  395. action -= (Action<T1>)dele;
  396. }
  397. }
  398. class MethodDelegateAdapter<T1, T2> : DelegateAdapter
  399. {
  400. Action<T1, T2> action;
  401. static InvocationTypes[] pTypes;
  402. static MethodDelegateAdapter()
  403. {
  404. pTypes = new InvocationTypes[]
  405. {
  406. InvocationContext.GetInvocationType<T1>(),
  407. InvocationContext.GetInvocationType<T2>(),
  408. };
  409. }
  410. public MethodDelegateAdapter()
  411. {
  412. }
  413. private MethodDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  414. : base(appdomain, instance, method)
  415. {
  416. action = InvokeILMethod;
  417. }
  418. public override Type NativeDelegateType
  419. {
  420. get
  421. {
  422. return typeof(Action<T1, T2>);
  423. }
  424. }
  425. public override Delegate Delegate
  426. {
  427. get
  428. {
  429. return action;
  430. }
  431. }
  432. unsafe void InvokeILMethod(T1 p1, T2 p2)
  433. {
  434. using (var ctx = BeginInvoke())
  435. {
  436. ctx.PushParameter(pTypes[0], p1);
  437. ctx.PushParameter(pTypes[1], p2);
  438. ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
  439. }
  440. }
  441. public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  442. {
  443. return new MethodDelegateAdapter<T1, T2>(appdomain, instance, method);
  444. }
  445. public override IDelegateAdapter Clone()
  446. {
  447. var res = new MethodDelegateAdapter<T1, T2>(appdomain, instance, method);
  448. res.isClone = true;
  449. return res;
  450. }
  451. public override void Combine(Delegate dele)
  452. {
  453. action += (Action<T1, T2>)dele;
  454. }
  455. public override void Remove(Delegate dele)
  456. {
  457. action -= (Action<T1, T2>)dele;
  458. }
  459. }
  460. class MethodDelegateAdapter<T1, T2, T3> : DelegateAdapter
  461. {
  462. Action<T1, T2, T3> action;
  463. static InvocationTypes[] pTypes;
  464. static MethodDelegateAdapter()
  465. {
  466. pTypes = new InvocationTypes[]
  467. {
  468. InvocationContext.GetInvocationType<T1>(),
  469. InvocationContext.GetInvocationType<T2>(),
  470. InvocationContext.GetInvocationType<T3>(),
  471. };
  472. }
  473. public MethodDelegateAdapter()
  474. {
  475. }
  476. private MethodDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  477. : base(appdomain, instance, method)
  478. {
  479. action = InvokeILMethod;
  480. }
  481. public override Type NativeDelegateType
  482. {
  483. get
  484. {
  485. return typeof(Action<T1, T2, T3>);
  486. }
  487. }
  488. public override Delegate Delegate
  489. {
  490. get
  491. {
  492. return action;
  493. }
  494. }
  495. unsafe void InvokeILMethod(T1 p1, T2 p2, T3 p3)
  496. {
  497. using (var ctx = BeginInvoke())
  498. {
  499. ctx.PushParameter(pTypes[0], p1);
  500. ctx.PushParameter(pTypes[1], p2);
  501. ctx.PushParameter(pTypes[2], p3);
  502. ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
  503. }
  504. }
  505. public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  506. {
  507. return new MethodDelegateAdapter<T1, T2, T3>(appdomain, instance, method);
  508. }
  509. public override IDelegateAdapter Clone()
  510. {
  511. var res = new MethodDelegateAdapter<T1, T2, T3>(appdomain, instance, method);
  512. res.isClone = true;
  513. return res;
  514. }
  515. public override void Combine(Delegate dele)
  516. {
  517. action += (Action<T1, T2, T3>)dele;
  518. }
  519. public override void Remove(Delegate dele)
  520. {
  521. action -= (Action<T1, T2, T3>)dele;
  522. }
  523. }
  524. class MethodDelegateAdapter<T1, T2, T3, T4> : DelegateAdapter
  525. {
  526. Action<T1, T2, T3, T4> action;
  527. static InvocationTypes[] pTypes;
  528. static MethodDelegateAdapter()
  529. {
  530. pTypes = new InvocationTypes[]
  531. {
  532. InvocationContext.GetInvocationType<T1>(),
  533. InvocationContext.GetInvocationType<T2>(),
  534. InvocationContext.GetInvocationType<T3>(),
  535. InvocationContext.GetInvocationType<T4>(),
  536. };
  537. }
  538. public MethodDelegateAdapter()
  539. {
  540. }
  541. private MethodDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  542. : base(appdomain, instance, method)
  543. {
  544. action = InvokeILMethod;
  545. }
  546. public override Type NativeDelegateType
  547. {
  548. get
  549. {
  550. return typeof(Action<T1, T2, T3, T4>);
  551. }
  552. }
  553. public override Delegate Delegate
  554. {
  555. get
  556. {
  557. return action;
  558. }
  559. }
  560. unsafe void InvokeILMethod(T1 p1, T2 p2, T3 p3, T4 p4)
  561. {
  562. using (var ctx = BeginInvoke())
  563. {
  564. ctx.PushParameter(pTypes[0], p1);
  565. ctx.PushParameter(pTypes[1], p2);
  566. ctx.PushParameter(pTypes[2], p3);
  567. ctx.PushParameter(pTypes[3], p4);
  568. ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
  569. }
  570. }
  571. public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  572. {
  573. return new MethodDelegateAdapter<T1, T2, T3, T4>(appdomain, instance, method);
  574. }
  575. public override IDelegateAdapter Clone()
  576. {
  577. var res = new MethodDelegateAdapter<T1, T2, T3, T4>(appdomain, instance, method);
  578. res.isClone = true;
  579. return res;
  580. }
  581. public override void Combine(Delegate dele)
  582. {
  583. action += (Action<T1, T2, T3, T4>)dele;
  584. }
  585. public override void Remove(Delegate dele)
  586. {
  587. action -= (Action<T1, T2, T3, T4>)dele;
  588. }
  589. }
  590. #if NET_4_6 || NET_STANDARD_2_0
  591. class MethodDelegateAdapter<T1, T2, T3, T4, T5> : DelegateAdapter
  592. {
  593. Action<T1, T2, T3, T4, T5> action;
  594. static InvocationTypes[] pTypes;
  595. static MethodDelegateAdapter()
  596. {
  597. pTypes = new InvocationTypes[]
  598. {
  599. InvocationContext.GetInvocationType<T1>(),
  600. InvocationContext.GetInvocationType<T2>(),
  601. InvocationContext.GetInvocationType<T3>(),
  602. InvocationContext.GetInvocationType<T4>(),
  603. InvocationContext.GetInvocationType<T5>(),
  604. };
  605. }
  606. public MethodDelegateAdapter()
  607. {
  608. }
  609. private MethodDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  610. : base(appdomain, instance, method)
  611. {
  612. action = InvokeILMethod;
  613. }
  614. public override Type NativeDelegateType
  615. {
  616. get
  617. {
  618. return typeof(Action<T1, T2, T3, T4, T5>);
  619. }
  620. }
  621. public override Delegate Delegate
  622. {
  623. get
  624. {
  625. return action;
  626. }
  627. }
  628. unsafe void InvokeILMethod(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
  629. {
  630. using (var ctx = BeginInvoke())
  631. {
  632. ctx.PushParameter(pTypes[0], p1);
  633. ctx.PushParameter(pTypes[1], p2);
  634. ctx.PushParameter(pTypes[2], p3);
  635. ctx.PushParameter(pTypes[3], p4);
  636. ctx.PushParameter(pTypes[4], p5);
  637. ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
  638. }
  639. }
  640. public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  641. {
  642. return new MethodDelegateAdapter<T1, T2, T3, T4, T5>(appdomain, instance, method);
  643. }
  644. public override IDelegateAdapter Clone()
  645. {
  646. var res = new MethodDelegateAdapter<T1, T2, T3, T4, T5>(appdomain, instance, method);
  647. res.isClone = true;
  648. return res;
  649. }
  650. public override void Combine(Delegate dele)
  651. {
  652. action += (Action<T1, T2, T3, T4, T5>)dele;
  653. }
  654. public override void Remove(Delegate dele)
  655. {
  656. action -= (Action<T1, T2, T3, T4, T5>)dele;
  657. }
  658. }
  659. #endif
  660. class MethodDelegateAdapter : DelegateAdapter
  661. {
  662. Action action;
  663. public MethodDelegateAdapter()
  664. {
  665. }
  666. protected MethodDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  667. : base(appdomain, instance, method)
  668. {
  669. action = InvokeILMethod;
  670. }
  671. public override Type NativeDelegateType
  672. {
  673. get
  674. {
  675. return typeof(Action);
  676. }
  677. }
  678. public override Delegate Delegate
  679. {
  680. get
  681. {
  682. return action;
  683. }
  684. }
  685. unsafe void InvokeILMethod()
  686. {
  687. using(var ctx = BeginInvoke())
  688. {
  689. ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
  690. }
  691. }
  692. public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  693. {
  694. return new MethodDelegateAdapter(appdomain, instance, method);
  695. }
  696. public override IDelegateAdapter Clone()
  697. {
  698. var res = new MethodDelegateAdapter(appdomain, instance, method);
  699. res.isClone = true;
  700. return res;
  701. }
  702. public override void Combine(Delegate dele)
  703. {
  704. action += (Action)dele;
  705. }
  706. public override void Remove(Delegate dele)
  707. {
  708. action -= (Action)dele;
  709. }
  710. }
  711. class DummyDelegateAdapter : DelegateAdapter
  712. {
  713. public DummyDelegateAdapter()
  714. {
  715. }
  716. protected DummyDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  717. : base(appdomain, instance, method)
  718. {
  719. }
  720. public override Type NativeDelegateType
  721. {
  722. get
  723. {
  724. throw new NotSupportedException();
  725. }
  726. }
  727. public override Delegate Delegate
  728. {
  729. get
  730. {
  731. ThrowAdapterNotFound(method);
  732. return null;
  733. }
  734. }
  735. void InvokeILMethod()
  736. {
  737. if (method.HasThis)
  738. appdomain.Invoke(method, instance, null);
  739. else
  740. appdomain.Invoke(method, null, null);
  741. }
  742. public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  743. {
  744. return new DummyDelegateAdapter(appdomain, instance, method);
  745. }
  746. public override IDelegateAdapter Clone()
  747. {
  748. var res = new DummyDelegateAdapter(appdomain, instance, method);
  749. res.isClone = true;
  750. return res;
  751. }
  752. public override void Combine(Delegate dele)
  753. {
  754. ThrowAdapterNotFound(method);
  755. }
  756. public override void Remove(Delegate dele)
  757. {
  758. ThrowAdapterNotFound(method);
  759. }
  760. }
  761. #endregion
  762. abstract class DelegateAdapter : ILTypeInstance, IDelegateAdapter
  763. {
  764. protected ILMethod method;
  765. protected ILTypeInstance instance;
  766. protected Enviorment.AppDomain appdomain;
  767. Dictionary<Type, Delegate> converters;
  768. IDelegateAdapter next;
  769. protected bool isClone;
  770. public abstract Delegate Delegate { get; }
  771. public abstract Type NativeDelegateType { get; }
  772. public IDelegateAdapter Next { get { return next; } }
  773. public ILTypeInstance Instance { get { return instance; } }
  774. public ILMethod Method { get { return method; } }
  775. protected DelegateAdapter() { }
  776. protected DelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
  777. {
  778. this.appdomain = appdomain;
  779. this.instance = instance;
  780. this.method = method;
  781. CLRInstance = this;
  782. }
  783. public override bool IsValueType
  784. {
  785. get
  786. {
  787. return false;
  788. }
  789. }
  790. unsafe protected InvocationContext BeginInvoke()
  791. {
  792. var ctx = appdomain.BeginInvoke(method);
  793. *ctx.ESP = default(StackObject);
  794. ctx.ESP++;//required to simulate delegate invocation
  795. return ctx;
  796. }
  797. public unsafe StackObject* ILInvoke(ILIntepreter intp, StackObject* esp, IList<object> mStack)
  798. {
  799. var ebp = esp;
  800. esp = ILInvokeSub(intp, esp, mStack);
  801. return ClearStack(intp, esp, ebp, mStack);
  802. }
  803. unsafe StackObject* ILInvokeSub(ILIntepreter intp, StackObject* esp, IList<object> mStack)
  804. {
  805. var ebp = esp;
  806. bool unhandled;
  807. if (method.HasThis)
  808. esp = ILIntepreter.PushObject(esp, mStack, instance);
  809. int paramCnt = method.ParameterCount;
  810. if (method.IsExtend)
  811. {
  812. esp = ILIntepreter.PushObject(esp, mStack, instance);
  813. paramCnt--;
  814. }
  815. bool useRegister = method.ShouldUseRegisterVM;
  816. for (int i = paramCnt; i > 0; i--)
  817. {
  818. intp.CopyToStack(esp, Minus(ebp, i), mStack);
  819. if (esp->ObjectType < ObjectTypes.Object && useRegister)
  820. mStack.Add(null);
  821. esp++;
  822. }
  823. StackObject* ret;
  824. if (useRegister)
  825. ret = intp.ExecuteR(method, esp, out unhandled);
  826. else
  827. ret = intp.Execute(method, esp, out unhandled);
  828. if (next != null)
  829. {
  830. if (method.ReturnType != appdomain.VoidType)
  831. {
  832. intp.Free(ret - 1);//Return value for multicast delegate doesn't make sense, only return the last one's value
  833. }
  834. DelegateAdapter n = (DelegateAdapter)next;
  835. ret = n.ILInvokeSub(intp, ebp, mStack);
  836. }
  837. return ret;
  838. }
  839. unsafe StackObject* ClearStack(ILIntepreter intp, StackObject* esp, StackObject* ebp, IList<object> mStack)
  840. {
  841. int paramCnt = method.ParameterCount;
  842. if (method.IsExtend)//如果是拓展方法,退一位
  843. {
  844. paramCnt--;
  845. }
  846. object retObj = null;
  847. StackObject retSObj = StackObject.Null;
  848. bool hasReturn = method.ReturnType != appdomain.VoidType;
  849. if (hasReturn)
  850. {
  851. var ret = esp - 1;
  852. retSObj = *ret;
  853. if(ret->ObjectType>= ObjectTypes.Object)
  854. {
  855. retObj = mStack[ret->Value];
  856. if(retObj == null)
  857. {
  858. retSObj.ObjectType = ObjectTypes.Null;
  859. retSObj.Value = -1;
  860. retSObj.ValueLow = 0;
  861. }
  862. intp.Free(ret);
  863. }
  864. }
  865. for (int i = 1; i <= paramCnt; i++)
  866. {
  867. intp.Free(ebp - i);
  868. }
  869. var returnVal = Minus(ebp, paramCnt + 1);
  870. intp.Free(returnVal);//Free delegateInstance
  871. if (hasReturn)
  872. {
  873. *returnVal = retSObj;
  874. if(retObj != null)
  875. {
  876. returnVal->Value = mStack.Count;
  877. mStack.Add(retObj);
  878. }
  879. returnVal++;
  880. }
  881. return returnVal;
  882. }
  883. public abstract IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method);
  884. public new abstract IDelegateAdapter Clone();
  885. public bool IsClone
  886. {
  887. get
  888. {
  889. return isClone;
  890. }
  891. }
  892. public virtual void Combine(IDelegateAdapter adapter)
  893. {
  894. if (next != null)
  895. next.Combine(adapter);
  896. else
  897. next = adapter;
  898. }
  899. public abstract void Combine(Delegate dele);
  900. public virtual void Remove(IDelegateAdapter adapter)
  901. {
  902. if (next != null)
  903. {
  904. if (next.Equals(adapter))
  905. {
  906. next = ((DelegateAdapter)next).next;
  907. }
  908. else
  909. next.Remove(adapter);
  910. }
  911. }
  912. public abstract void Remove(Delegate dele);
  913. public virtual bool Equals(IDelegateAdapter adapter)
  914. {
  915. if (adapter is DelegateAdapter)
  916. {
  917. DelegateAdapter b = (DelegateAdapter)adapter;
  918. return instance == b.instance && method == b.method;
  919. }
  920. else
  921. return false;
  922. }
  923. public override bool Equals(object obj)
  924. {
  925. if (obj is DelegateAdapter)
  926. {
  927. DelegateAdapter b = (DelegateAdapter)obj;
  928. return instance == b.instance && method == b.method;
  929. }
  930. return false;
  931. }
  932. public virtual bool Equals(Delegate dele)
  933. {
  934. return Delegate == dele;
  935. }
  936. public override int GetHashCode()
  937. {
  938. return base.GetHashCode();
  939. }
  940. public override string ToString()
  941. {
  942. return method.ToString();
  943. }
  944. public override bool CanAssignTo(IType type)
  945. {
  946. if (type.IsDelegate)
  947. {
  948. var method_count = method.IsExtend ? method.ParameterCount - 1 : method.ParameterCount;
  949. var im = type.GetMethod("Invoke", method_count);
  950. if (im == null)
  951. {
  952. return false;
  953. }
  954. var ret_type = im.ReturnType;
  955. if (im.ReturnType != appdomain.VoidType && type.IsGenericInstance)
  956. {
  957. ret_type = type.GenericArguments[im.ParameterCount].Value;
  958. }
  959. if (im.IsDelegateInvoke)
  960. {
  961. if (im.ParameterCount == method_count && ret_type == method.ReturnType)
  962. {
  963. for (int i = 0; i < im.ParameterCount; i++)
  964. {
  965. var index = method.IsExtend ? i + 1 : i;
  966. if (type.IsGenericInstance)
  967. {
  968. if (method.Parameters[index] != type.GenericArguments[i].Value)
  969. {
  970. return false;
  971. }
  972. }
  973. else
  974. {
  975. if (im.Parameters[i] != method.Parameters[index])
  976. return false;
  977. }
  978. }
  979. return true;
  980. }
  981. else
  982. return false;
  983. }
  984. else
  985. return false;
  986. }
  987. else
  988. return false;
  989. }
  990. public Delegate GetConvertor(Type type)
  991. {
  992. if (converters == null)
  993. converters = new Dictionary<System.Type, Delegate>(new ByReferenceKeyComparer<Type>());
  994. Delegate res;
  995. if (converters.TryGetValue(type, out res))
  996. return res;
  997. else
  998. {
  999. res = appdomain.DelegateManager.ConvertToDelegate(type, this);
  1000. converters[type] = res;
  1001. return res;
  1002. }
  1003. }
  1004. unsafe StackObject* Minus(StackObject* a, int b)
  1005. {
  1006. return (StackObject*)((long)a - sizeof(StackObject) * b);
  1007. }
  1008. public static void ThrowAdapterNotFound(IMethod method)
  1009. {
  1010. StringBuilder sb = new StringBuilder();
  1011. sb.Append("Cannot find Delegate Adapter for:");
  1012. sb.Append(method.ToString());
  1013. string clsName, rName;
  1014. bool isByRef;
  1015. if (method.ReturnType.Name != "Void" || method.ParameterCount > 0)
  1016. {
  1017. sb.AppendLine(", Please add following code:");
  1018. if (method.ReturnType.Name == "Void")
  1019. {
  1020. sb.Append("appdomain.DelegateManager.RegisterMethodDelegate<");
  1021. bool first = true;
  1022. foreach(var i in method.Parameters)
  1023. {
  1024. if (first)
  1025. {
  1026. first = false;
  1027. }
  1028. else
  1029. {
  1030. sb.Append(", ");
  1031. }
  1032. i.TypeForCLR.GetClassName(out clsName, out rName, out isByRef);
  1033. sb.Append(rName);
  1034. }
  1035. sb.AppendLine(">();");
  1036. }
  1037. else
  1038. {
  1039. sb.Append("appdomain.DelegateManager.RegisterFunctionDelegate<");
  1040. bool first = true;
  1041. foreach (var i in method.Parameters)
  1042. {
  1043. if (first)
  1044. {
  1045. first = false;
  1046. }
  1047. else
  1048. {
  1049. sb.Append(", ");
  1050. }
  1051. i.TypeForCLR.GetClassName(out clsName, out rName, out isByRef);
  1052. sb.Append(rName);
  1053. }
  1054. if (!first)
  1055. sb.Append(", ");
  1056. method.ReturnType.TypeForCLR.GetClassName(out clsName, out rName, out isByRef);
  1057. sb.Append(rName);
  1058. sb.AppendLine(">();");
  1059. }
  1060. }
  1061. throw new KeyNotFoundException(sb.ToString());
  1062. }
  1063. }
  1064. unsafe interface IDelegateAdapter
  1065. { Type NativeDelegateType { get; }
  1066. Delegate Delegate { get; }
  1067. IDelegateAdapter Next { get; }
  1068. ILTypeInstance Instance { get; }
  1069. ILMethod Method { get; }
  1070. StackObject* ILInvoke(ILIntepreter intp, StackObject* esp, IList<object> mStack);
  1071. IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method);
  1072. bool IsClone { get; }
  1073. IDelegateAdapter Clone();
  1074. Delegate GetConvertor(Type type);
  1075. void Combine(IDelegateAdapter adapter);
  1076. void Combine(Delegate dele);
  1077. void Remove(IDelegateAdapter adapter);
  1078. void Remove(Delegate dele);
  1079. bool Equals(IDelegateAdapter adapter);
  1080. bool Equals(Delegate dele);
  1081. }
  1082. }