Skip to content

Commit

Permalink
test for calli
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton92nd committed Feb 26, 2019
1 parent ea38837 commit 7611ff8
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions GrEmit.Tests/Test.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.Runtime.InteropServices;

using GrEmit.Utils;

Expand Down Expand Up @@ -373,6 +374,37 @@ public void TestByRefGeneric()
}
}

#if !NETCOREAPP2_0
[Test]
public void TestCalliWithNativeCallingConvention()
{
var assembly = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(Guid.NewGuid().ToString()), AssemblyBuilderAccess.Run);
var module = assembly.DefineDynamicModule(Guid.NewGuid().ToString());
var typeBuilder = module.DefineType("TestType", TypeAttributes.Class | TypeAttributes.Public);
const string methodName = "TestMethod";
var defineMethod = typeBuilder.DefineMethod(methodName, MethodAttributes.Public | MethodAttributes.Static, typeof(int), new Type[] {typeof(IntPtr)});
var methodBuilder = defineMethod;
using (var il = new GroboIL(methodBuilder))
{
il.Ldc_I4(10);
il.Ldarg(0);
il.Calli(CallingConvention.StdCall, typeof(int), new []{typeof(int)});
il.Ret();
}
var type = typeBuilder.CreateType();
var method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static);
var invocationResult = method.Invoke(null, new object[] {Marshal.GetFunctionPointerForDelegate(new FooDelegate(Foo))});
Assert.That(invocationResult, Is.EqualTo(15));
}

private delegate int FooDelegate(int x);

private static int Foo(int x)
{
return x + 5;
}
#endif

#if NET45
[Test, Ignore("Is used for debugging")]
public void TestEnumerable()
Expand Down

0 comments on commit 7611ff8

Please sign in to comment.