Skip to content

Commit

Permalink
fixed #366
Browse files Browse the repository at this point in the history
  • Loading branch information
liiir1985 committed Aug 29, 2020
1 parent dcad617 commit 423c4f6
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 76 deletions.
2 changes: 2 additions & 0 deletions ILRuntime/Runtime/Enviorment/CrossBindingMethodInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
131 changes: 56 additions & 75 deletions ILRuntimeTest/Adapters/IAsyncStateMachineAdaptor.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// 用于async await适配
/// </summary>
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<System.Runtime.CompilerServices.IAsyncStateMachine> mSetStateMachine_1 = new CrossBindingMethodInfo<System.Runtime.CompilerServices.IAsyncStateMachine>("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;
}
}
}
}
3 changes: 2 additions & 1 deletion ILRuntimeTest/TestMainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
46 changes: 46 additions & 0 deletions TestCases/AsyncAwaitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,55 @@ private static async Task<List<string>> 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();
}
}
}

0 comments on commit 423c4f6

Please sign in to comment.