Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
<Compile Include="$(BclSourcesRoot)\System\Reflection\MethodBase.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\MethodBaseInvoker.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\MethodInvoker.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\MethodInvokerCommon.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\ModifiedType.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\RtFieldInfo.cs" />
<Compile Include="$(BclSourcesRoot)\System\Reflection\RuntimeAssembly.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Runtime.CompilerServices;
using static System.Reflection.InvokerEmitUtil;
using static System.Reflection.MethodBase;
using static System.RuntimeType;

namespace System.Reflection
{
public partial class ConstructorInvoker
{
private readonly Signature? _signature;
private readonly CreateUninitializedCache? _allocator;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private object CreateUninitializedObject() => _allocator!.CreateUninitializedObject(_declaringType);

internal unsafe ConstructorInvoker(RuntimeConstructorInfo constructor) : this(constructor, constructor.Signature.Arguments)
private bool ShouldAllocate
{
_signature = constructor.Signature;
_invokeFunc_RefArgs = InterpretedInvoke;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _allocator is not null;
}
private unsafe Delegate CreateInvokeDelegateForInterpreted()
{
Debug.Assert(MethodInvokerCommon.UseInterpretedPath);
Debug.Assert(_strategy == InvokerStrategy.Ref4 || _strategy == InvokerStrategy.RefMany);

return (InvokeFunc_RefArgs)InterpretedInvoke;
}

private unsafe object? InterpretedInvoke(object? obj, IntPtr* args)
private unsafe object? InterpretedInvoke(IntPtr _, object? obj, IntPtr* args)
{
return RuntimeMethodHandle.InvokeMethod(obj, (void**)args, _signature!, isConstructor: obj is null);
return RuntimeMethodHandle.InvokeMethod(obj, (void**)args, _method.Signature, isConstructor: obj is null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private MethodBaseInvoker Invoker
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
return _invoker ??= new MethodBaseInvoker(this, Signature);
return _invoker ??= new MethodBaseInvoker(this, Signature.Arguments, ReturnType);
}
}

Expand Down Expand Up @@ -132,13 +132,17 @@ Signature LazyCreateSignature()
int argCount = (parameters != null) ? parameters.Length : 0;
if (Signature.Arguments.Length != argCount)
throw new TargetParameterCountException(SR.Arg_ParmCnt);
object? retValue = argCount switch

object? retValue = Invoker.Strategy switch
{
0 => Invoker.InvokeWithNoArgs(obj, invokeAttr),
1 => Invoker.InvokeWithOneArg(obj, invokeAttr, binder, parameters!, culture),
2 or 3 or 4 => Invoker.InvokeWithFewArgs(obj, invokeAttr, binder, parameters!, culture),
_ => Invoker.InvokeWithManyArgs(obj, invokeAttr, binder, parameters!, culture),
MethodBase.InvokerStrategy.Obj0 => Invoker.InvokeWithNoArgs(obj, invokeAttr),
MethodBase.InvokerStrategy.Obj1 => Invoker.InvokeWith1Arg(obj, invokeAttr, binder, parameters!, culture),
MethodBase.InvokerStrategy.Obj4 => Invoker.InvokeWith4Args(obj, invokeAttr, binder, parameters!, culture),
MethodBase.InvokerStrategy.ObjSpan => Invoker.InvokeWithSpanArgs(obj, invokeAttr, binder, parameters!, culture),
MethodBase.InvokerStrategy.Ref4 => Invoker.InvokeWith4RefArgs(obj, invokeAttr, binder, parameters, culture),
_ => Invoker.InvokeWithManyRefArgs(obj, invokeAttr, binder, parameters!, culture)
};

GC.KeepAlive(this);
return retValue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace System.Reflection
Expand All @@ -15,152 +14,188 @@ internal static unsafe class InstanceCalliHelper
// Zero parameter methods such as property getters:

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static bool Call(delegate*<object, bool> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static byte Call(delegate*<object, byte> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static char Call(delegate*<object, char> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static DateTime Call(delegate*<object, DateTime> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static DateTimeOffset Call(delegate*<object, DateTimeOffset> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static decimal Call(delegate*<object, decimal> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static double Call(delegate*<object, double> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static float Call(delegate*<object, float> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static Guid Call(delegate*<object, Guid> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static short Call(delegate*<object, short> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static int Call(delegate*<object, int> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static long Call(delegate*<object, long> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static nint Call(delegate*<object, nint> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static nuint Call(delegate*<object, nuint> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static object? Call(delegate*<object, object?> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static sbyte Call(delegate*<object, sbyte> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static ushort Call(delegate*<object, ushort> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static uint Call(delegate*<object, uint> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static ulong Call(delegate*<object, ulong> fn, object o) => fn(o);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, void> fn, object o) => fn(o);

// One parameter methods with no return such as property setters:

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, bool, void> fn, object o, bool arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, byte, void> fn, object o, byte arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, char, void> fn, object o, char arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, DateTime, void> fn, object o, DateTime arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, DateTimeOffset, void> fn, object o, DateTimeOffset arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, decimal, void> fn, object o, decimal arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, double, void> fn, object o, double arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, float, void> fn, object o, float arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, Guid, void> fn, object o, Guid arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, short, void> fn, object o, short arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, int, void> fn, object o, int arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, long, void> fn, object o, long arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, nint, void> fn, object o, nint arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, nuint, void> fn, object o, nuint arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, object?, void> fn, object o, object? arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, sbyte, void> fn, object o, sbyte arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, ushort, void> fn, object o, ushort arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, uint, void> fn, object o, uint arg1) => fn(o, arg1);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, ulong, void> fn, object o, ulong arg1) => fn(o, arg1);

// Other methods:

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, object?, object?, void> fn, object o, object? arg1, object? arg2)
=> fn(o, arg1, arg2);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, object?, object?, object?, void> fn, object o, object? arg1, object? arg2, object? arg3)
=> fn(o, arg1, arg2, arg3);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, object?, object?, object?, object?, void> fn, object o, object? arg1, object? arg2, object? arg3, object? arg4)
=> fn(o, arg1, arg2, arg3, arg4);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, object?, object?, object?, object?, object?, void> fn, object o, object? arg1, object? arg2, object? arg3, object? arg4, object? arg5)
=> fn(o, arg1, arg2, arg3, arg4, arg5);

[Intrinsic]
[MethodImpl(MethodImplOptions.NoInlining)]
internal static void Call(delegate*<object, object?, object?, object?, object?, object?, object?, void> fn, object o, object? arg1, object? arg2, object? arg3, object? arg4, object? arg5, object? arg6)
=> fn(o, arg1, arg2, arg3, arg4, arg5, arg6);

[Intrinsic]
internal static void Call(delegate*<object, IEnumerable<object>?, void> fn, object o, IEnumerable<object>? arg1)
=> fn(o, arg1);

[Intrinsic]
internal static void Call(delegate*<object, IEnumerable<object>?, IEnumerable<object>?, void> fn, object o, IEnumerable<object>? arg1, IEnumerable<object>? arg2)
=> fn(o, arg1, arg2);
}
}
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using static System.Reflection.InvokerEmitUtil;
using static System.Reflection.MethodBase;
using static System.RuntimeType;

namespace System.Reflection
{
internal partial class MethodBaseInvoker
{
private readonly Signature? _signature;
private readonly CreateUninitializedCache? _allocator;

internal unsafe MethodBaseInvoker(RuntimeMethodInfo method) : this(method, method.Signature.Arguments)
{
_signature = method.Signature;
_invocationFlags = method.ComputeAndUpdateInvocationFlags();
_invokeFunc_RefArgs = InterpretedInvoke_Method;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private object CreateUninitializedObject() => _allocator!.CreateUninitializedObject(_declaringType!);

internal unsafe MethodBaseInvoker(RuntimeConstructorInfo constructor) : this(constructor, constructor.Signature.Arguments)
private bool ShouldAllocate
{
_signature = constructor.Signature;
_invocationFlags = constructor.ComputeAndUpdateInvocationFlags();
_invokeFunc_RefArgs = InterpretedInvoke_Constructor;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _allocator is not null;
}

internal unsafe MethodBaseInvoker(DynamicMethod method, Signature signature) : this(method, signature.Arguments)
private unsafe Delegate CreateInvokeDelegateForInterpreted()
{
_signature = signature;
_invokeFunc_RefArgs = InterpretedInvoke_Method;
Debug.Assert(MethodInvokerCommon.UseInterpretedPath);
Debug.Assert(_strategy == InvokerStrategy.Ref4 || _strategy == InvokerStrategy.RefMany);

if (_method is RuntimeMethodInfo)
{
return (InvokeFunc_RefArgs)InterpretedInvoke_Method;
}

if (_method is RuntimeConstructorInfo)
{
return (InvokeFunc_RefArgs)InterpretedInvoke_Constructor;
}

Debug.Assert(_method is DynamicMethod);
return (InvokeFunc_RefArgs)InterpretedInvoke_DynamicMethod;
}

private unsafe object? InterpretedInvoke_Constructor(object? obj, IntPtr* args) =>
RuntimeMethodHandle.InvokeMethod(obj, (void**)args, _signature!, isConstructor: obj is null);
private unsafe object? InterpretedInvoke_Method(IntPtr _, object? obj, IntPtr* args) =>
RuntimeMethodHandle.InvokeMethod(obj, (void**)args, ((RuntimeMethodInfo)_method).Signature, isConstructor: false);

private unsafe object? InterpretedInvoke_Constructor(IntPtr _, object? obj, IntPtr* args) =>
RuntimeMethodHandle.InvokeMethod(obj, (void**)args, ((RuntimeConstructorInfo)_method).Signature, isConstructor: obj is null);

private unsafe object? InterpretedInvoke_Method(object? obj, IntPtr* args) =>
RuntimeMethodHandle.InvokeMethod(obj, (void**)args, _signature!, isConstructor: false);
private unsafe object? InterpretedInvoke_DynamicMethod(IntPtr _, object? obj, IntPtr* args) =>
RuntimeMethodHandle.InvokeMethod(obj, (void**)args, ((DynamicMethod)_method).Signature, isConstructor: false);
}
}
Loading
Loading