| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using ILRuntime.CLR.TypeSystem;
- using ILRuntime.CLR.Method;
- using ILRuntime.Runtime;
- using ILRuntime.Runtime.Stack;
- using ILRuntime.Other;
- using ILRuntime.Runtime.Enviorment;
- namespace ILRuntime.Runtime.Intepreter
- {
- #region Functions
- class FunctionDelegateAdapter<TResult> : DelegateAdapter
- {
- Func<TResult> action;
- static InvocationTypes[] pTypes;
- static FunctionDelegateAdapter()
- {
- pTypes = new InvocationTypes[]
- {
- InvocationContext.GetInvocationType<TResult>(),
- };
- }
- public FunctionDelegateAdapter()
- {
- }
- private FunctionDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- : base(appdomain, instance, method)
- {
- action = InvokeILMethod;
- }
- public override Delegate Delegate
- {
- get
- {
- return action;
- }
- }
- unsafe TResult InvokeILMethod()
- {
- using (var ctx = BeginInvoke())
- {
- var esp = ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
- ctx.SetInvoked(esp);
- return ctx.ReadResult<TResult>(pTypes[0]);
- }
- }
- public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- {
- return new FunctionDelegateAdapter<TResult>(appdomain, instance, method);
- }
- public override IDelegateAdapter Clone()
- {
- var res = new FunctionDelegateAdapter<TResult>(appdomain, instance, method);
- res.isClone = true;
- return res;
- }
- public override void Combine(Delegate dele)
- {
- action += (Func<TResult>)dele;
- }
- public override void Remove(Delegate dele)
- {
- action -= (Func<TResult>)dele;
- }
- }
- class FunctionDelegateAdapter<T1, TResult> : DelegateAdapter
- {
- Func<T1, TResult> action;
- static InvocationTypes[] pTypes;
- static FunctionDelegateAdapter()
- {
- pTypes = new InvocationTypes[]
- {
- InvocationContext.GetInvocationType<T1>(),
- InvocationContext.GetInvocationType<TResult>(),
- };
- }
- public FunctionDelegateAdapter()
- {
- }
- private FunctionDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- : base(appdomain, instance, method)
- {
- action = InvokeILMethod;
- }
- public override Delegate Delegate
- {
- get
- {
- return action;
- }
- }
- unsafe TResult InvokeILMethod(T1 p1)
- {
- using (var ctx = BeginInvoke())
- {
- ctx.PushParameter(pTypes[0], p1);
- var esp = ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
- ctx.SetInvoked(esp);
- return ctx.ReadResult<TResult>(pTypes[1]);
- }
- }
- public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- {
- return new FunctionDelegateAdapter<T1, TResult>(appdomain, instance, method);
- }
- public override IDelegateAdapter Clone()
- {
- var res = new FunctionDelegateAdapter<T1, TResult>(appdomain, instance, method);
- res.isClone = true;
- return res;
- }
- public override void Combine(Delegate dele)
- {
- action += (Func<T1, TResult>)dele;
- }
- public override void Remove(Delegate dele)
- {
- action -= (Func<T1, TResult>)dele;
- }
- }
- class FunctionDelegateAdapter<T1, T2, TResult> : DelegateAdapter
- {
- Func<T1, T2, TResult> action;
- static InvocationTypes[] pTypes;
- static FunctionDelegateAdapter()
- {
- pTypes = new InvocationTypes[]
- {
- InvocationContext.GetInvocationType<T1>(),
- InvocationContext.GetInvocationType<T2>(),
- InvocationContext.GetInvocationType<TResult>(),
- };
- }
- public FunctionDelegateAdapter()
- {
- }
- private FunctionDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- : base(appdomain, instance, method)
- {
- action = InvokeILMethod;
- }
- public override Delegate Delegate
- {
- get
- {
- return action;
- }
- }
- unsafe TResult InvokeILMethod(T1 p1, T2 p2)
- {
- using (var ctx = BeginInvoke())
- {
- ctx.PushParameter(pTypes[0], p1);
- ctx.PushParameter(pTypes[1], p2);
- var esp = ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
- ctx.SetInvoked(esp);
- return ctx.ReadResult<TResult>(pTypes[2]);
- }
- }
- public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- {
- return new FunctionDelegateAdapter<T1, T2, TResult>(appdomain, instance, method);
- }
- public override IDelegateAdapter Clone()
- {
- var res = new FunctionDelegateAdapter<T1, T2, TResult>(appdomain, instance, method);
- res.isClone = true;
- return res;
- }
- public override void Combine(Delegate dele)
- {
- action += (Func<T1, T2, TResult>)dele;
- }
- public override void Remove(Delegate dele)
- {
- action -= (Func<T1, T2, TResult>)dele;
- }
- }
- class FunctionDelegateAdapter<T1, T2, T3, TResult> : DelegateAdapter
- {
- Func<T1, T2, T3, TResult> action;
- static InvocationTypes[] pTypes;
- static FunctionDelegateAdapter()
- {
- pTypes = new InvocationTypes[]
- {
- InvocationContext.GetInvocationType<T1>(),
- InvocationContext.GetInvocationType<T2>(),
- InvocationContext.GetInvocationType<T3>(),
- InvocationContext.GetInvocationType<TResult>(),
- };
- }
- public FunctionDelegateAdapter()
- {
- }
- private FunctionDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- : base(appdomain, instance, method)
- {
- action = InvokeILMethod;
- }
- public override Delegate Delegate
- {
- get
- {
- return action;
- }
- }
- unsafe TResult InvokeILMethod(T1 p1, T2 p2, T3 p3)
- {
- using (var ctx = BeginInvoke())
- {
- ctx.PushParameter(pTypes[0], p1);
- ctx.PushParameter(pTypes[1], p2);
- ctx.PushParameter(pTypes[2], p3);
- var esp = ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
- ctx.SetInvoked(esp);
- return ctx.ReadResult<TResult>(pTypes[3]);
- }
- }
- public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- {
- return new FunctionDelegateAdapter<T1, T2, T3, TResult>(appdomain, instance, method);
- }
- public override IDelegateAdapter Clone()
- {
- var res = new FunctionDelegateAdapter<T1, T2, T3, TResult>(appdomain, instance, method);
- res.isClone = true;
- return res;
- }
- public override void Combine(Delegate dele)
- {
- action += (Func<T1, T2, T3, TResult>)dele;
- }
- public override void Remove(Delegate dele)
- {
- action -= (Func<T1, T2, T3, TResult>)dele;
- }
- }
- class FunctionDelegateAdapter<T1, T2, T3, T4, TResult> : DelegateAdapter
- {
- Func<T1, T2, T3, T4, TResult> action;
- static InvocationTypes[] pTypes;
- static FunctionDelegateAdapter()
- {
- pTypes = new InvocationTypes[]
- {
- InvocationContext.GetInvocationType<T1>(),
- InvocationContext.GetInvocationType<T2>(),
- InvocationContext.GetInvocationType<T3>(),
- InvocationContext.GetInvocationType<T4>(),
- InvocationContext.GetInvocationType<TResult>(),
- };
- }
- public FunctionDelegateAdapter()
- {
- }
- private FunctionDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- : base(appdomain, instance, method)
- {
- action = InvokeILMethod;
- }
- public override Delegate Delegate
- {
- get
- {
- return action;
- }
- }
- unsafe TResult InvokeILMethod(T1 p1, T2 p2, T3 p3, T4 p4)
- {
- using (var ctx = BeginInvoke())
- {
- ctx.PushParameter(pTypes[0], p1);
- ctx.PushParameter(pTypes[1], p2);
- ctx.PushParameter(pTypes[2], p3);
- ctx.PushParameter(pTypes[3], p4);
- var esp = ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
- ctx.SetInvoked(esp);
- return ctx.ReadResult<TResult>(pTypes[4]);
- }
- }
- public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- {
- return new FunctionDelegateAdapter<T1, T2, T3, T4, TResult>(appdomain, instance, method);
- }
- public override IDelegateAdapter Clone()
- {
- var res = new FunctionDelegateAdapter<T1, T2, T3, T4, TResult>(appdomain, instance, method);
- res.isClone = true;
- return res;
- }
- public override void Combine(Delegate dele)
- {
- action += (Func<T1, T2, T3, T4, TResult>)dele;
- }
- public override void Remove(Delegate dele)
- {
- action -= (Func<T1, T2, T3, T4, TResult>)dele;
- }
- }
- #endregion
- #region Methods
- class MethodDelegateAdapter<T1> : DelegateAdapter
- {
- Action<T1> action;
- static InvocationTypes pType;
- static MethodDelegateAdapter()
- {
- pType = InvocationContext.GetInvocationType<T1>();
- }
- public MethodDelegateAdapter()
- {
-
- }
- private MethodDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- : base(appdomain, instance, method)
- {
- action = InvokeILMethod;
- }
- public override Delegate Delegate
- {
- get
- {
- return action;
- }
- }
- unsafe void InvokeILMethod(T1 p1)
- {
- using (var ctx = BeginInvoke())
- {
- ctx.PushParameter(pType, p1);
- ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
- }
- }
- public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- {
- return new MethodDelegateAdapter<T1>(appdomain, instance, method);
- }
- public override IDelegateAdapter Clone()
- {
- var res = new MethodDelegateAdapter<T1>(appdomain, instance, method);
- res.isClone = true;
- return res;
- }
- public override void Combine(Delegate dele)
- {
- action += (Action<T1>)dele;
- }
- public override void Remove(Delegate dele)
- {
- action -= (Action<T1>)dele;
- }
- }
- class MethodDelegateAdapter<T1, T2> : DelegateAdapter
- {
- Action<T1, T2> action;
- static InvocationTypes[] pTypes;
- static MethodDelegateAdapter()
- {
- pTypes = new InvocationTypes[]
- {
- InvocationContext.GetInvocationType<T1>(),
- InvocationContext.GetInvocationType<T2>(),
- };
- }
- public MethodDelegateAdapter()
- {
- }
- private MethodDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- : base(appdomain, instance, method)
- {
- action = InvokeILMethod;
- }
- public override Delegate Delegate
- {
- get
- {
- return action;
- }
- }
- unsafe void InvokeILMethod(T1 p1, T2 p2)
- {
- using (var ctx = BeginInvoke())
- {
- ctx.PushParameter(pTypes[0], p1);
- ctx.PushParameter(pTypes[1], p2);
- ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
- }
- }
- public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- {
- return new MethodDelegateAdapter<T1, T2>(appdomain, instance, method);
- }
- public override IDelegateAdapter Clone()
- {
- var res = new MethodDelegateAdapter<T1, T2>(appdomain, instance, method);
- res.isClone = true;
- return res;
- }
- public override void Combine(Delegate dele)
- {
- action += (Action<T1, T2>)dele;
- }
- public override void Remove(Delegate dele)
- {
- action -= (Action<T1, T2>)dele;
- }
- }
- class MethodDelegateAdapter<T1, T2, T3> : DelegateAdapter
- {
- Action<T1, T2, T3> action;
- static InvocationTypes[] pTypes;
- static MethodDelegateAdapter()
- {
- pTypes = new InvocationTypes[]
- {
- InvocationContext.GetInvocationType<T1>(),
- InvocationContext.GetInvocationType<T2>(),
- InvocationContext.GetInvocationType<T3>(),
- };
- }
- public MethodDelegateAdapter()
- {
- }
- private MethodDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- : base(appdomain, instance, method)
- {
- action = InvokeILMethod;
- }
- public override Delegate Delegate
- {
- get
- {
- return action;
- }
- }
- unsafe void InvokeILMethod(T1 p1, T2 p2, T3 p3)
- {
- using (var ctx = BeginInvoke())
- {
- ctx.PushParameter(pTypes[0], p1);
- ctx.PushParameter(pTypes[1], p2);
- ctx.PushParameter(pTypes[2], p3);
- ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
- }
- }
- public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- {
- return new MethodDelegateAdapter<T1, T2, T3>(appdomain, instance, method);
- }
- public override IDelegateAdapter Clone()
- {
- var res = new MethodDelegateAdapter<T1, T2, T3>(appdomain, instance, method);
- res.isClone = true;
- return res;
- }
- public override void Combine(Delegate dele)
- {
- action += (Action<T1, T2, T3>)dele;
- }
- public override void Remove(Delegate dele)
- {
- action -= (Action<T1, T2, T3>)dele;
- }
- }
- class MethodDelegateAdapter<T1, T2, T3, T4> : DelegateAdapter
- {
- Action<T1, T2, T3, T4> action;
- static InvocationTypes[] pTypes;
- static MethodDelegateAdapter()
- {
- pTypes = new InvocationTypes[]
- {
- InvocationContext.GetInvocationType<T1>(),
- InvocationContext.GetInvocationType<T2>(),
- InvocationContext.GetInvocationType<T3>(),
- InvocationContext.GetInvocationType<T4>(),
- };
- }
- public MethodDelegateAdapter()
- {
- }
- private MethodDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- : base(appdomain, instance, method)
- {
- action = InvokeILMethod;
- }
- public override Delegate Delegate
- {
- get
- {
- return action;
- }
- }
- unsafe void InvokeILMethod(T1 p1, T2 p2, T3 p3, T4 p4)
- {
- using (var ctx = BeginInvoke())
- {
- ctx.PushParameter(pTypes[0], p1);
- ctx.PushParameter(pTypes[1], p2);
- ctx.PushParameter(pTypes[2], p3);
- ctx.PushParameter(pTypes[3], p4);
- ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
- }
- }
- public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- {
- return new MethodDelegateAdapter<T1, T2, T3, T4>(appdomain, instance, method);
- }
- public override IDelegateAdapter Clone()
- {
- var res = new MethodDelegateAdapter<T1, T2, T3, T4>(appdomain, instance, method);
- res.isClone = true;
- return res;
- }
- public override void Combine(Delegate dele)
- {
- action += (Action<T1, T2, T3, T4>)dele;
- }
- public override void Remove(Delegate dele)
- {
- action -= (Action<T1, T2, T3, T4>)dele;
- }
- }
- #if NET_4_6 || NET_STANDARD_2_0
- class MethodDelegateAdapter<T1, T2, T3, T4, T5> : DelegateAdapter
- {
- Action<T1, T2, T3, T4, T5> action;
- static InvocationTypes[] pTypes;
- static MethodDelegateAdapter()
- {
- pTypes = new InvocationTypes[]
- {
- InvocationContext.GetInvocationType<T1>(),
- InvocationContext.GetInvocationType<T2>(),
- InvocationContext.GetInvocationType<T3>(),
- InvocationContext.GetInvocationType<T4>(),
- InvocationContext.GetInvocationType<T5>(),
- };
- }
- public MethodDelegateAdapter()
- {
- }
- private MethodDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- : base(appdomain, instance, method)
- {
- action = InvokeILMethod;
- }
- public override Delegate Delegate
- {
- get
- {
- return action;
- }
- }
- unsafe void InvokeILMethod(T1 p1, T2 p2, T3 p3, T4 p4, T5 p5)
- {
- using (var ctx = BeginInvoke())
- {
- ctx.PushParameter(pTypes[0], p1);
- ctx.PushParameter(pTypes[1], p2);
- ctx.PushParameter(pTypes[2], p3);
- ctx.PushParameter(pTypes[3], p4);
- ctx.PushParameter(pTypes[4], p5);
- ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
- }
- }
- public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- {
- return new MethodDelegateAdapter<T1, T2, T3, T4, T5>(appdomain, instance, method);
- }
- public override IDelegateAdapter Clone()
- {
- var res = new MethodDelegateAdapter<T1, T2, T3, T4, T5>(appdomain, instance, method);
- res.isClone = true;
- return res;
- }
- public override void Combine(Delegate dele)
- {
- action += (Action<T1, T2, T3, T4, T5>)dele;
- }
- public override void Remove(Delegate dele)
- {
- action -= (Action<T1, T2, T3, T4, T5>)dele;
- }
- }
- #endif
- class MethodDelegateAdapter : DelegateAdapter
- {
- Action action;
-
- public MethodDelegateAdapter()
- {
- }
- protected MethodDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- : base(appdomain, instance, method)
- {
- action = InvokeILMethod;
- }
- public override Delegate Delegate
- {
- get
- {
- return action;
- }
- }
- unsafe void InvokeILMethod()
- {
- using(var ctx = BeginInvoke())
- {
- ILInvoke(ctx.Intepreter, ctx.ESP, ctx.ManagedStack);
- }
- }
- public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- {
- return new MethodDelegateAdapter(appdomain, instance, method);
- }
- public override IDelegateAdapter Clone()
- {
- var res = new MethodDelegateAdapter(appdomain, instance, method);
- res.isClone = true;
- return res;
- }
- public override void Combine(Delegate dele)
- {
- action += (Action)dele;
- }
- public override void Remove(Delegate dele)
- {
- action -= (Action)dele;
- }
- }
- class DummyDelegateAdapter : DelegateAdapter
- {
- public DummyDelegateAdapter()
- {
- }
- protected DummyDelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- : base(appdomain, instance, method)
- {
-
- }
- public override Delegate Delegate
- {
- get
- {
- ThrowAdapterNotFound(method);
- return null;
- }
- }
- void InvokeILMethod()
- {
- if (method.HasThis)
- appdomain.Invoke(method, instance, null);
- else
- appdomain.Invoke(method, null, null);
- }
- public override IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- {
- return new DummyDelegateAdapter(appdomain, instance, method);
- }
- public override IDelegateAdapter Clone()
- {
- var res = new DummyDelegateAdapter(appdomain, instance, method);
- res.isClone = true;
- return res;
- }
- public override void Combine(Delegate dele)
- {
- ThrowAdapterNotFound(method);
- }
- public override void Remove(Delegate dele)
- {
- ThrowAdapterNotFound(method);
- }
- }
- #endregion
- abstract class DelegateAdapter : ILTypeInstance, IDelegateAdapter
- {
- protected ILMethod method;
- protected ILTypeInstance instance;
- protected Enviorment.AppDomain appdomain;
- Dictionary<Type, Delegate> converters;
- IDelegateAdapter next;
- protected bool isClone;
- public abstract Delegate Delegate { get; }
- public IDelegateAdapter Next { get { return next; } }
- public ILTypeInstance Instance { get { return instance; } }
- public ILMethod Method { get { return method; } }
- protected DelegateAdapter() { }
- protected DelegateAdapter(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method)
- {
- this.appdomain = appdomain;
- this.instance = instance;
- this.method = method;
- CLRInstance = this;
- }
- public override bool IsValueType
- {
- get
- {
- return false;
- }
- }
- unsafe protected InvocationContext BeginInvoke()
- {
- var ctx = appdomain.BeginInvoke(method);
- *ctx.ESP = default(StackObject);
- ctx.ESP++;//required to simulate delegate invocation
- return ctx;
- }
- public unsafe StackObject* ILInvoke(ILIntepreter intp, StackObject* esp, IList<object> mStack)
- {
- var ebp = esp;
- esp = ILInvokeSub(intp, esp, mStack);
- return ClearStack(intp, esp, ebp, mStack);
- }
- unsafe StackObject* ILInvokeSub(ILIntepreter intp, StackObject* esp, IList<object> mStack)
- {
- var ebp = esp;
- bool unhandled;
- if (method.HasThis)
- esp = ILIntepreter.PushObject(esp, mStack, instance);
- int paramCnt = method.ParameterCount;
- bool useRegister = method.ShouldUseRegisterVM;
- for (int i = paramCnt; i > 0; i--)
- {
- intp.CopyToStack(esp, Minus(ebp, i), mStack);
- if (esp->ObjectType < ObjectTypes.Object && useRegister)
- mStack.Add(null);
- esp++;
- }
- StackObject* ret;
- if (useRegister)
- ret = intp.ExecuteR(method, esp, out unhandled);
- else
- ret = intp.Execute(method, esp, out unhandled);
- if (next != null)
- {
- if (method.ReturnType != appdomain.VoidType)
- {
- intp.Free(ret - 1);//Return value for multicast delegate doesn't make sense, only return the last one's value
- }
- DelegateAdapter n = (DelegateAdapter)next;
- ret = n.ILInvokeSub(intp, ebp, mStack);
- }
- return ret;
- }
- unsafe StackObject* ClearStack(ILIntepreter intp, StackObject* esp, StackObject* ebp, IList<object> mStack)
- {
- int paramCnt = method.ParameterCount;
- object retObj = null;
- StackObject retSObj = StackObject.Null;
- bool hasReturn = method.ReturnType != appdomain.VoidType;
- if (hasReturn)
- {
- var ret = esp - 1;
- retSObj = *ret;
- if(ret->ObjectType>= ObjectTypes.Object)
- {
- retObj = mStack[ret->Value];
- if(retObj == null)
- {
- retSObj.ObjectType = ObjectTypes.Null;
- retSObj.Value = -1;
- retSObj.ValueLow = 0;
- }
- intp.Free(ret);
- }
- }
- for (int i = 1; i <= paramCnt; i++)
- {
- intp.Free(ebp - i);
- }
- var returnVal = Minus(ebp, paramCnt + 1);
- intp.Free(returnVal);//Free delegateInstance
- if (hasReturn)
- {
- *returnVal = retSObj;
- if(retObj != null)
- {
- returnVal->Value = mStack.Count;
- mStack.Add(retObj);
- }
- returnVal++;
- }
- return returnVal;
- }
- public abstract IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method);
- public new abstract IDelegateAdapter Clone();
- public bool IsClone
- {
- get
- {
- return isClone;
- }
- }
- public virtual void Combine(IDelegateAdapter adapter)
- {
- if (next != null)
- next.Combine(adapter);
- else
- next = adapter;
- }
- public abstract void Combine(Delegate dele);
- public virtual void Remove(IDelegateAdapter adapter)
- {
- if (next != null)
- {
- if (next.Equals(adapter))
- {
- next = ((DelegateAdapter)next).next;
- }
- else
- next.Remove(adapter);
- }
- }
- public abstract void Remove(Delegate dele);
- public virtual bool Equals(IDelegateAdapter adapter)
- {
- if (adapter is DelegateAdapter)
- {
- DelegateAdapter b = (DelegateAdapter)adapter;
- return instance == b.instance && method == b.method;
- }
- else
- return false;
- }
- public override bool Equals(object obj)
- {
- if (obj is DelegateAdapter)
- {
- DelegateAdapter b = (DelegateAdapter)obj;
- return instance == b.instance && method == b.method;
- }
- return false;
- }
- public virtual bool Equals(Delegate dele)
- {
- return Delegate == dele;
- }
- public override int GetHashCode()
- {
- return base.GetHashCode();
- }
- public override string ToString()
- {
- return method.ToString();
- }
- public override bool CanAssignTo(IType type)
- {
- if (type.IsDelegate)
- {
- var im = type.GetMethod("Invoke", method.ParameterCount);
- if (im.IsDelegateInvoke)
- {
- if (im.ParameterCount == method.ParameterCount && im.ReturnType == method.ReturnType)
- {
- for (int i = 0; i < im.ParameterCount; i++)
- {
- if (im.Parameters[i] != method.Parameters[i])
- return false;
- }
- return true;
- }
- else
- return false;
- }
- else
- return false;
- }
- else
- return false;
- }
- public Delegate GetConvertor(Type type)
- {
- if (converters == null)
- converters = new Dictionary<System.Type, Delegate>(new ByReferenceKeyComparer<Type>());
- Delegate res;
- if (converters.TryGetValue(type, out res))
- return res;
- else
- {
- res = appdomain.DelegateManager.ConvertToDelegate(type, this);
- converters[type] = res;
- return res;
- }
- }
- unsafe StackObject* Minus(StackObject* a, int b)
- {
- return (StackObject*)((long)a - sizeof(StackObject) * b);
- }
- public static void ThrowAdapterNotFound(IMethod method)
- {
- StringBuilder sb = new StringBuilder();
- sb.Append("Cannot find Delegate Adapter for:");
- sb.Append(method.ToString());
- string clsName, rName;
- bool isByRef;
- if (method.ReturnType.Name != "Void" || method.ParameterCount > 0)
- {
- sb.AppendLine(", Please add following code:");
- if (method.ReturnType.Name == "Void")
- {
- sb.Append("appdomain.DelegateManager.RegisterMethodDelegate<");
- bool first = true;
- foreach(var i in method.Parameters)
- {
- if (first)
- {
- first = false;
- }
- else
- {
- sb.Append(", ");
- }
- i.TypeForCLR.GetClassName(out clsName, out rName, out isByRef);
- sb.Append(rName);
- }
- sb.AppendLine(">();");
- }
- else
- {
- sb.Append("appdomain.DelegateManager.RegisterFunctionDelegate<");
- bool first = true;
- foreach (var i in method.Parameters)
- {
- if (first)
- {
- first = false;
- }
- else
- {
- sb.Append(", ");
- }
- i.TypeForCLR.GetClassName(out clsName, out rName, out isByRef);
- sb.Append(rName);
- }
- if (!first)
- sb.Append(", ");
- method.ReturnType.TypeForCLR.GetClassName(out clsName, out rName, out isByRef);
- sb.Append(rName);
- sb.AppendLine(">();");
- }
- }
- throw new KeyNotFoundException(sb.ToString());
- }
- }
- unsafe interface IDelegateAdapter
- {
- Delegate Delegate { get; }
- IDelegateAdapter Next { get; }
- ILTypeInstance Instance { get; }
- ILMethod Method { get; }
- StackObject* ILInvoke(ILIntepreter intp, StackObject* esp, IList<object> mStack);
- IDelegateAdapter Instantiate(Enviorment.AppDomain appdomain, ILTypeInstance instance, ILMethod method);
- bool IsClone { get; }
- IDelegateAdapter Clone();
- Delegate GetConvertor(Type type);
- void Combine(IDelegateAdapter adapter);
- void Combine(Delegate dele);
- void Remove(IDelegateAdapter adapter);
- void Remove(Delegate dele);
- bool Equals(IDelegateAdapter adapter);
- bool Equals(Delegate dele);
- }
- }
|