Skip to content

Commit

Permalink
#12 Another attempt
Browse files Browse the repository at this point in the history
  • Loading branch information
sguldmund committed Jan 15, 2024
1 parent 5f5a01f commit 456721a
Showing 1 changed file with 63 additions and 11 deletions.
74 changes: 63 additions & 11 deletions src/Sandbox/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,58 @@

namespace Pose.Sandbox
{
namespace System.Runtime.CompilerServices
{
// AsyncVoidMethodBuilder.cs in your project
public class AsyncTaskMethodBuilder
{
public void AwaitOnCompleted<TAwaiter, TStateMachine>(
ref TAwaiter awaiter,
ref TStateMachine stateMachine
)
where TAwaiter : INotifyCompletion
where TStateMachine : IAsyncStateMachine
{

}

public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(
ref TAwaiter awaiter, ref TStateMachine stateMachine)
where TAwaiter : ICriticalNotifyCompletion
where TStateMachine : IAsyncStateMachine
{

}

public void SetStateMachine(IAsyncStateMachine stateMachine) {}

public void SetException(Exception exception) {}

public Task Task => null;

public AsyncTaskMethodBuilder()
=> Console.WriteLine(".ctor");

public static AsyncTaskMethodBuilder Create()
=> new AsyncTaskMethodBuilder();

public void SetResult() => Console.WriteLine("SetResult");

public void Start<TStateMachine>(ref TStateMachine stateMachine)
where TStateMachine : IAsyncStateMachine
{
Console.WriteLine("Start");
var methodInfos = stateMachine.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);
var methodRewriter = MethodRewriter.CreateRewriter(methodInfos[0], false);
var methodBase = methodRewriter.Rewrite();
stateMachine.MoveNext();
}

// AwaitOnCompleted, AwaitUnsafeOnCompleted, SetException
// and SetStateMachine are empty
}
}

public class Program
{
public static async Task<int> GetAsyncInt() => await Task.FromResult(1);
Expand All @@ -20,18 +72,18 @@ public static async Task Lol()

public static void Main(string[] args)
{
//Lol().GetAwaiter().GetResult();

var shim = Shim
.Replace(() => Program.GetAsyncInt())
.With(() => Task.FromResult(2));
Lol().GetAwaiter().GetResult();

PoseContext.IsolateAsync(
async () =>
{
var @int = await GetAsyncInt();
Console.WriteLine(@int);
}, shim).GetAwaiter().GetResult();
// var shim = Shim
// .Replace(() => Program.GetAsyncInt())
// .With(() => Task.FromResult(2));
//
// PoseContext.IsolateAsync(
// async () =>
// {
// var @int = await GetAsyncInt();
// Console.WriteLine(@int);
// }, shim).GetAwaiter().GetResult();
/*
#if NET48
Console.WriteLine("4.8");
Expand Down

0 comments on commit 456721a

Please sign in to comment.