Imported Upstream version 5.16.0.100

Former-commit-id: 38faa55fb9669e35e7d8448b15c25dc447f25767
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2018-08-07 15:19:03 +00:00
parent 0a9828183b
commit 7d7f676260
4419 changed files with 170950 additions and 90273 deletions

View File

@@ -48,6 +48,7 @@ namespace System.Reflection.Emit
public virtual void Emit(System.Reflection.Emit.OpCode opcode, System.Type cls) { }
public virtual void EmitCall(System.Reflection.Emit.OpCode opcode, System.Reflection.MethodInfo methodInfo, System.Type[] optionalParameterTypes) { }
public virtual void EmitCalli(System.Reflection.Emit.OpCode opcode, System.Reflection.CallingConventions callingConvention, System.Type returnType, System.Type[] parameterTypes, System.Type[] optionalParameterTypes) { }
public virtual void EmitCalli(System.Reflection.Emit.OpCode opcode, System.Runtime.InteropServices.CallingConvention unmanagedCallConv, Type returnType, Type[] parameterTypes) { }
public virtual void EmitWriteLine(System.Reflection.Emit.LocalBuilder localBuilder) { }
public virtual void EmitWriteLine(System.Reflection.FieldInfo fld) { }
public virtual void EmitWriteLine(string value) { }

View File

@@ -13,6 +13,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\System.Runtime\ref\System.Runtime.csproj" />
<ProjectReference Include="..\..\System.Runtime.InteropServices\ref\System.Runtime.InteropServices.csproj" />
<ProjectReference Include="..\..\System.Reflection\ref\System.Reflection.csproj" />
<ProjectReference Include="..\..\System.Reflection.Primitives\ref\System.Reflection.Primitives.csproj" />
</ItemGroup>

View File

@@ -0,0 +1,147 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Linq;
using System.Runtime.InteropServices;
using Xunit;
namespace System.Reflection.Emit.Tests
{
public class ILGeneratorEmit4
{
[Fact]
public void TestEmitCalliBlittable()
{
int a = 1, b = 1, result = 2;
ModuleBuilder moduleBuilder = Helpers.DynamicModule();
TypeBuilder typeBuilder = moduleBuilder.DefineType("T", TypeAttributes.Public);
Type returnType = typeof(int);
MethodBuilder methodBuilder = typeBuilder.DefineMethod("F",
MethodAttributes.Public | MethodAttributes.Static, returnType, new Type[] { typeof(IntPtr), typeof(int), typeof(int) });
methodBuilder.SetImplementationFlags(MethodImplAttributes.NoInlining);
ILGenerator il = methodBuilder.GetILGenerator();
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Ldarg_0);
il.EmitCalli(OpCodes.Calli, CallingConvention.StdCall, returnType, new Type[] { typeof(int), typeof(int) });
il.Emit(OpCodes.Ret);
Type dynamicType = typeBuilder.CreateType();
var del = new Int32SumStdCall(Int32Sum);
IntPtr funcPtr = Marshal.GetFunctionPointerForDelegate(del);
object resultValue = dynamicType
.GetMethod("F", BindingFlags.Public | BindingFlags.Static)
.Invoke(null, new object[] { funcPtr, a, b });
GC.KeepAlive(del);
Assert.IsType(returnType, resultValue);
Assert.Equal(result, resultValue);
}
[Fact]
public void TestDynamicMethodEmitCalliBlittable()
{
int a = 1, b = 1, result = 2;
Type returnType = typeof(int);
var dynamicMethod = new DynamicMethod("F", returnType, new Type[] { typeof(IntPtr), typeof(int), typeof(int) });
ILGenerator il = dynamicMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_2);
il.Emit(OpCodes.Ldarg_0);
il.EmitCalli(OpCodes.Calli, CallingConvention.StdCall, returnType, new Type[] { typeof(int), typeof(int) });
il.Emit(OpCodes.Ret);
var del = new Int32SumStdCall(Int32Sum);
IntPtr funcPtr = Marshal.GetFunctionPointerForDelegate(del);
object resultValue = dynamicMethod
.Invoke(null, new object[] { funcPtr, a, b });
GC.KeepAlive(del);
Assert.IsType(returnType, resultValue);
Assert.Equal(result, resultValue);
}
[Fact]
public void TestEmitCalliNonBlittable()
{
string input = "Test string!", result = "!gnirts tseT";
ModuleBuilder moduleBuilder = Helpers.DynamicModule();
TypeBuilder typeBuilder = moduleBuilder.DefineType("T", TypeAttributes.Public);
Type returnType = typeof(string);
MethodBuilder methodBuilder = typeBuilder.DefineMethod("F",
MethodAttributes.Public | MethodAttributes.Static, returnType, new Type[] { typeof(IntPtr), typeof(string) });
methodBuilder.SetImplementationFlags(MethodImplAttributes.NoInlining);
ILGenerator il = methodBuilder.GetILGenerator();
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_0);
il.EmitCalli(OpCodes.Calli, CallingConvention.Cdecl, returnType, new Type[] { typeof(string) });
il.Emit(OpCodes.Ret);
Type dynamicType = typeBuilder.CreateType();
var del = new StringReverseCdecl(StringReverse);
IntPtr funcPtr = Marshal.GetFunctionPointerForDelegate(del);
object resultValue = dynamicType
.GetMethod("F", BindingFlags.Public | BindingFlags.Static)
.Invoke(null, new object[] { funcPtr, input });
GC.KeepAlive(del);
Assert.IsType(returnType, resultValue);
Assert.Equal(result, resultValue);
}
[Fact]
public void TestDynamicMethodEmitCalliNonBlittable()
{
string input = "Test string!", result = "!gnirts tseT";
Type returnType = typeof(string);
var dynamicMethod = new DynamicMethod("F", returnType, new Type[] { typeof(IntPtr), typeof(string) });
ILGenerator il = dynamicMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg_1);
il.Emit(OpCodes.Ldarg_0);
il.EmitCalli(OpCodes.Calli, CallingConvention.Cdecl, returnType, new Type[] { typeof(string) });
il.Emit(OpCodes.Ret);
var del = new StringReverseCdecl(StringReverse);
IntPtr funcPtr = Marshal.GetFunctionPointerForDelegate(del);
object resultValue = dynamicMethod
.Invoke(null, new object[] { funcPtr, input });
GC.KeepAlive(del);
Assert.IsType(returnType, resultValue);
Assert.Equal(result, resultValue);
}
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
private delegate int Int32SumStdCall(int a, int b);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate string StringReverseCdecl(string a);
private static int Int32Sum(int a, int b) => a + b;
private static string StringReverse(string a) => string.Join("", a.Reverse());
}
}

View File

@@ -12,6 +12,7 @@
<Compile Include="ILGenerator\Emit1Tests.cs" />
<Compile Include="ILGenerator\Emit2Tests.cs" />
<Compile Include="ILGenerator\Emit3Tests.cs" />
<Compile Include="ILGenerator\Emit4Tests.cs" />
<Compile Include="ILGenerator\EmitWriteLineTests.cs" />
<Compile Include="ILGenerator\ExceptionEmitTests.cs" />
<Compile Include="ILGenerator\ILOffsetTests.cs" />