DelegateAdapter.cs 33 KB

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