From 423c4f6a45f2cc29aceb558d2a2f1761c6402bc5 Mon Sep 17 00:00:00 2001 From: liiir1985 Date: Sat, 29 Aug 2020 13:24:59 +0800 Subject: [PATCH] fixed #366 --- .../Enviorment/CrossBindingMethodInfo.cs | 2 + .../Adapters/IAsyncStateMachineAdaptor.cs | 131 ++++++++---------- ILRuntimeTest/TestMainForm.cs | 3 +- TestCases/AsyncAwaitTest.cs | 46 ++++++ 4 files changed, 106 insertions(+), 76 deletions(-) diff --git a/ILRuntime/Runtime/Enviorment/CrossBindingMethodInfo.cs b/ILRuntime/Runtime/Enviorment/CrossBindingMethodInfo.cs index f93bd7fc..0814bd1e 100644 --- a/ILRuntime/Runtime/Enviorment/CrossBindingMethodInfo.cs +++ b/ILRuntime/Runtime/Enviorment/CrossBindingMethodInfo.cs @@ -730,6 +730,8 @@ protected void EnsureMethod(ILTypeInstance ins) rt = domain.GetType(ReturnType); if (ilType.FirstCLRBaseType != null) baseMethod = ilType.FirstCLRBaseType.GetMethod(Name, param, null, rt); + if (ilType.FirstCLRInterface != null) + baseMethod = ilType.FirstCLRInterface.GetMethod(Name, param, null, rt); if (baseMethod == null) method = ilType.GetMethod(Name, param, null, rt); } diff --git a/ILRuntimeTest/Adapters/IAsyncStateMachineAdaptor.cs b/ILRuntimeTest/Adapters/IAsyncStateMachineAdaptor.cs index 7c22f9c3..0bf52431 100644 --- a/ILRuntimeTest/Adapters/IAsyncStateMachineAdaptor.cs +++ b/ILRuntimeTest/Adapters/IAsyncStateMachineAdaptor.cs @@ -1,93 +1,74 @@ using System; -using System.Runtime.CompilerServices; using ILRuntime.CLR.Method; using ILRuntime.Runtime.Enviorment; using ILRuntime.Runtime.Intepreter; namespace ILRuntimeTest.TestFramework { - /// - /// 用于async await适配 - /// - public class IAsyncStateMachineClassInheritanceAdaptor : CrossBindingAdaptor - { - public override Type BaseCLRType - { - get - { - return typeof (IAsyncStateMachine); - } - } + public class IAsyncStateMachineClassInheritanceAdaptor : CrossBindingAdaptor + { + static CrossBindingMethodInfo mMoveNext_0 = new CrossBindingMethodInfo("MoveNext"); + static CrossBindingMethodInfo mSetStateMachine_1 = new CrossBindingMethodInfo("SetStateMachine"); + public override Type BaseCLRType + { + get + { + return typeof(System.Runtime.CompilerServices.IAsyncStateMachine); + } + } - public override Type AdaptorType - { - get - { - return typeof (IAsyncStateMachineAdaptor); - } - } + public override Type AdaptorType + { + get + { + return typeof(IAsyncStateMachineAdaptor); + } + } - public override object CreateCLRInstance(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance) - { - return new IAsyncStateMachineAdaptor(appdomain, instance); - } - - public class IAsyncStateMachineAdaptor: IAsyncStateMachine, CrossBindingAdaptorType - { - private ILTypeInstance instance; - private ILRuntime.Runtime.Enviorment.AppDomain appDomain; + public override object CreateCLRInstance(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance) + { + return new IAsyncStateMachineAdaptor(appdomain, instance); + } - private IMethod mMoveNext; - private IMethod mSetStateMachine; - private readonly object[] param1 = new object[1]; + public class IAsyncStateMachineAdaptor : System.Runtime.CompilerServices.IAsyncStateMachine, CrossBindingAdaptorType + { + ILTypeInstance instance; + ILRuntime.Runtime.Enviorment.AppDomain appdomain; - public IAsyncStateMachineAdaptor() - { - } + public IAsyncStateMachineAdaptor() + { - public IAsyncStateMachineAdaptor(ILRuntime.Runtime.Enviorment.AppDomain appDomain, ILTypeInstance instance) - { - this.appDomain = appDomain; - this.instance = instance; - } + } - public ILTypeInstance ILInstance - { - get - { - return instance; - } - } + public IAsyncStateMachineAdaptor(ILRuntime.Runtime.Enviorment.AppDomain appdomain, ILTypeInstance instance) + { + this.appdomain = appdomain; + this.instance = instance; + } - public void MoveNext() - { - if (this.mMoveNext == null) - { - mMoveNext = instance.Type.GetMethod("MoveNext", 0); - } - this.appDomain.Invoke(mMoveNext, instance, null); - } + public ILTypeInstance ILInstance { get { return instance; } } - public void SetStateMachine(IAsyncStateMachine stateMachine) - { - if (this.mSetStateMachine == null) - { - mSetStateMachine = instance.Type.GetMethod("SetStateMachine"); - } - this.appDomain.Invoke(mSetStateMachine, instance, stateMachine); - } + public void MoveNext() + { + mMoveNext_0.Invoke(this.instance); + } - public override string ToString() - { - IMethod m = this.appDomain.ObjectType.GetMethod("ToString", 0); - m = instance.Type.GetVirtualMethod(m); - if (m == null || m is ILMethod) - { - return instance.ToString(); - } + public void SetStateMachine(System.Runtime.CompilerServices.IAsyncStateMachine stateMachine) + { + mSetStateMachine_1.Invoke(this.instance, stateMachine); + } - return instance.Type.FullName; - } - } - } + public override string ToString() + { + IMethod m = appdomain.ObjectType.GetMethod("ToString", 0); + m = instance.Type.GetVirtualMethod(m); + if (m == null || m is ILMethod) + { + return instance.ToString(); + } + else + return instance.Type.FullName; + } + } + } } diff --git a/ILRuntimeTest/TestMainForm.cs b/ILRuntimeTest/TestMainForm.cs index d3ed6268..5ea91eb7 100644 --- a/ILRuntimeTest/TestMainForm.cs +++ b/ILRuntimeTest/TestMainForm.cs @@ -6,6 +6,7 @@ using System.IO; using System.Linq; using System.Reflection; +using System.Runtime.CompilerServices; using System.Text; using System.Windows.Forms; using ILRuntime.CLR.TypeSystem; @@ -249,7 +250,7 @@ private void button1_Click(object sender, EventArgs e) { var msg = ILRuntime.Runtime.Enviorment.CrossBindingCodeGenerator.GenerateCrossBindingAdapterCode(typeof(TestClass2), "ILRuntimeTest"); MessageBox.Show(msg); - msg = ILRuntime.Runtime.Enviorment.CrossBindingCodeGenerator.GenerateCrossBindingAdapterCode(typeof(IDisposable), "ILRuntimeTest"); + msg = ILRuntime.Runtime.Enviorment.CrossBindingCodeGenerator.GenerateCrossBindingAdapterCode(typeof(IAsyncStateMachine), "ILRuntimeTest"); MessageBox.Show(msg); } diff --git a/TestCases/AsyncAwaitTest.cs b/TestCases/AsyncAwaitTest.cs index de183819..4609f3dd 100644 --- a/TestCases/AsyncAwaitTest.cs +++ b/TestCases/AsyncAwaitTest.cs @@ -137,9 +137,55 @@ private static async Task> readText(string path) return lines; }*/ + public async static void TestRun5() + { + Console.WriteLine("TestConditionAwait Start "); + await DoConditionAwait(); + Console.WriteLine("TestConditionAwait End"); + } + + static int v = 0; + static async Task DoConditionAwait() + { + if (v > 0) + { + await Task.Delay(1000); + } + Console.WriteLine("DoConditionAwait End"); + } + class AsyncAwaitTest1 { public string a; } + + public static class TestClass + { + public static async Task Show1() + { + await Task.CompletedTask; + } + + public static void Show2() + { + Show3(); + } + + public static async void Show3() + { + Console.WriteLine("Show3"); + await Task.CompletedTask; + } + } + + public static void TestRun6() + { + InitAsync(); + } + private static async void InitAsync() + { + Console.WriteLine("a1"); + TestClass.Show2(); + } } }