Skip to content

Commit

Permalink
Merge pull request #20 from Miista/15-cache-resolved-methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Miista committed Jan 17, 2024
2 parents 022962e + cd6a203 commit 9c227df
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 68 deletions.
16 changes: 8 additions & 8 deletions src/Pose/Extensions/DictionaryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using System.Collections.Generic;

namespace Pose.Extensions
// Putting the extension method under this namespace makes it synonymous
// with its official counterpart when using the library on platforms
// which do not have the official version.
#if !(NETCOREAPP2_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER)
// ReSharper disable once CheckNamespace
namespace System.Collections.Generic
{
internal static class DictionaryExtensions
{
public static bool TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
#if NETSTANDARD2_0 || NET48
try
{
dictionary.Add(key, value);
Expand All @@ -16,9 +18,7 @@ internal static class DictionaryExtensions
{
return false;
}
#else
return dictionary.TryAdd(key, value);
#endif
}
}
}
}
#endif
8 changes: 4 additions & 4 deletions src/Pose/Extensions/ILGeneratorExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Reflection;
using System.Reflection.Emit;

namespace Pose.Extensions
{
using System;
using System.Reflection;
using System.Reflection.Emit;

internal static class ILGeneratorExtensions
{
public static byte[] GetILBytes(this ILGenerator ilGenerator)
Expand Down
9 changes: 5 additions & 4 deletions src/Pose/Helpers/StubHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ namespace Pose.Helpers
{
internal static class StubHelper
{
private static readonly MethodInfo GetMethodDescriptor =
typeof(DynamicMethod).GetMethod("GetMethodDescriptor", BindingFlags.Instance | BindingFlags.NonPublic)
?? throw new Exception($"Cannot get method GetMethodDescriptor from type {nameof(DynamicMethod)}");

public static IntPtr GetMethodPointer(MethodBase method)
{
if (method is DynamicMethod dynamicMethod)
{
var methodDescriptor = typeof(DynamicMethod).GetMethod("GetMethodDescriptor", BindingFlags.Instance | BindingFlags.NonPublic)
?? throw new Exception($"Cannot get method GetMethodDescriptor from type {nameof(DynamicMethod)}");

return ((RuntimeMethodHandle)methodDescriptor.Invoke(dynamicMethod, null)).GetFunctionPointer();
return ((RuntimeMethodHandle)GetMethodDescriptor.Invoke(dynamicMethod, null)).GetFunctionPointer();
}

return method.MethodHandle.GetFunctionPointer();
Expand Down
12 changes: 7 additions & 5 deletions src/Pose/IL/MethodRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ namespace Pose.IL
internal class MethodRewriter
{
private static readonly List<OpCode> IgnoredOpCodes = new List<OpCode> { OpCodes.Endfilter, OpCodes.Endfinally };


private static readonly MethodInfo Unbox =
typeof(Unsafe).GetMethod(nameof(Unsafe.Unbox))
?? throw new Exception($"Cannot get method {nameof(Unsafe.Unbox)} from type {nameof(Unsafe)}");

private readonly MethodBase _method;
private readonly Type _owningType;
private readonly bool _isInterfaceDispatch;

private int _exceptionBlockLevel;
private TypeInfo _constrainedType;

Expand Down Expand Up @@ -247,9 +251,7 @@ private void EmitILForExceptionHandlers(ILGenerator ilGenerator, Instruction ins

private void EmitThisPointerAccessForBoxedValueType(ILGenerator ilGenerator)
{
var unboxMethod = typeof(Unsafe).GetMethod(nameof(Unsafe.Unbox)) ?? throw new Exception($"Cannot get method {nameof(Unsafe.Unbox)} from type {nameof(Unsafe)}");

ilGenerator.Emit(OpCodes.Call, unboxMethod.MakeGenericMethod(_method.DeclaringType));
ilGenerator.Emit(OpCodes.Call, Unbox.MakeGenericMethod(_method.DeclaringType));
}

private void EmitILForInlineNone(ILGenerator ilGenerator, Instruction instruction)
Expand Down
125 changes: 83 additions & 42 deletions src/Pose/IL/Stubs.cs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Pose/Pose.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Title>Pose</Title>
<Description>Replace any .NET method (including static and non-virtual) with a delegate</Description>
<PackageVersion>1.2.1</PackageVersion>
<TargetFrameworks>netstandard2.0;netcoreapp3.0;net48;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netcoreapp2.0;netcoreapp3.0;net48;net7.0;net8.0</TargetFrameworks>
<DebugType>portable</DebugType>
<AssemblyName>Pose</AssemblyName>
<PackageId>Pose</PackageId>
Expand Down
3 changes: 0 additions & 3 deletions test/Pose.Tests/Extensions/DictionaryExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System.Collections.Generic;
using FluentAssertions;
// ReSharper disable once RedundantUsingDirective
// The using statement is not redundant on platforms targeting net48
using Pose.Extensions;
using Xunit;

namespace Pose.Tests
Expand Down
2 changes: 1 addition & 1 deletion test/Pose.Tests/Pose.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.0;netcoreapp3.0;net47;net48;net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>netcoreapp2.0;netcoreapp3.0;netcoreapp3.1;net47;net48;net6.0;net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>

Expand Down

0 comments on commit 9c227df

Please sign in to comment.