Skip to content

Commit

Permalink
#12 Successfully add support for async methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sguldmund committed Jan 27, 2024
1 parent 63cfc59 commit 31e537d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
23 changes: 23 additions & 0 deletions src/Pose/PoseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading.Tasks;
using Pose.IL;

namespace Pose
Expand Down Expand Up @@ -30,5 +31,27 @@ public static void Isolate(Action entryPoint, params Shim[] shims)
Console.WriteLine("----------------------------- Invoking ----------------------------- ");
methodInfo.CreateDelegate(delegateType).DynamicInvoke(entryPoint.Target);
}

public static async Task Isolate(Func<Task> entryPoint, params Shim[] shims)
{
if (shims == null || shims.Length == 0)
{
await entryPoint.Invoke();
return;
}

Shims = shims;
StubCache = new Dictionary<MethodBase, DynamicMethod>();

var delegateType = typeof(Func<Task>);
var rewriter = MethodRewriter.CreateRewriter(entryPoint.Method, false);
Console.WriteLine("----------------------------- Rewriting ----------------------------- ");
var methodInfo = (MethodInfo)(rewriter.Rewrite());

Console.WriteLine("----------------------------- Invoking ----------------------------- ");

// ReSharper disable once PossibleNullReferenceException
await (methodInfo.CreateDelegate(delegateType).DynamicInvoke() as Task);
}
}
}
3 changes: 2 additions & 1 deletion src/Pose/Shim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public static Shim Replace<T>(Expression<Func<T>> expression, bool setter = fals

private static Shim ReplaceImpl<T>(Expression<T> expression, bool setter)
{
// TODO: Figure out if method is an async method. Do that by finding the attribute on the method which designates the state machine
// We could find out whether the method is an async method by checking whether it has the AsyncStateMachineAttribute.
// However, it seems that this is not necessary.
var methodBase = ShimHelper.GetMethodFromExpression(expression.Body, setter, out var instance);
return new Shim(methodBase, instance) { _setter = setter };
}
Expand Down
14 changes: 9 additions & 5 deletions src/Sandbox/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@ public static void Main(string[] args)
}, dateTimeShim);
#elif NETCOREAPP2_0
Console.WriteLine("2.0");
var dateTimeShim = Shim.Replace(() => DateTime.Now).With(() => new DateTime(2004, 1, 1));
var asyncShim = Shim.Replace(() => GetIntAsync()).With(() => Task.FromResult(10));
//var dateTimeShim = Shim.Replace(() => DateTime.Now).With(() => new DateTime(2004, 1, 1));
var asyncShim = Shim.Replace(() => GetIntAsync()).With(() =>
{
Console.WriteLine("This actually works!!!");
return Task.FromResult(15);
});
PoseContext.Isolate(
() =>
async () =>
{
var result = GetIntAsync().GetAwaiter().GetResult();
var result = await GetIntAsync();
Console.WriteLine($"Result: {result}");
//Console.WriteLine(DateTime.Now);
}, dateTimeShim, asyncShim);
}, asyncShim);
#elif NET6_0
Console.WriteLine("6.0");
var dateTimeShim = Shim.Replace(() => DateTime.Now).With(() => new DateTime(2004, 1, 1));
Expand Down

0 comments on commit 31e537d

Please sign in to comment.