Code tweaks, added basic test
このコミットが含まれているのは:
1 親
99d4a04c54
コミット
2b310aba25
2ファイル変更
+37
-5
検索条件に一致するファイルはありません
@@ -11,15 +11,20 @@ namespace ComputeSharp.Shaders.Extensions
|
||||
/// A <see langword="class"/> that can be used to easily (lol) create dynamic methods
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of <see cref="Delegate"/> that will be used to wrap the new methods</typeparam>
|
||||
public static class DynamicMethod<T> where T : Delegate
|
||||
internal static class DynamicMethod<T> where T : Delegate
|
||||
{
|
||||
/// <summary>
|
||||
/// The owner type for new dynamic methods
|
||||
/// </summary>
|
||||
private static readonly Type OwnerType = typeof(DynamicMethod<T>);
|
||||
|
||||
/// <summary>
|
||||
/// The return type of the <typeparamref name="T"/> <see langword="delegate"/>
|
||||
/// </summary>
|
||||
private static readonly Type ReturnType;
|
||||
|
||||
/// <summary>
|
||||
/// The array of types of the arguments of the <typeparamref name="T"/> <see langword="delegate"/>
|
||||
/// The types of the arguments of the <typeparamref name="T"/> <see langword="delegate"/>
|
||||
/// </summary>
|
||||
private static readonly Type[] ParameterTypes;
|
||||
|
||||
@@ -41,15 +46,14 @@ namespace ComputeSharp.Shaders.Extensions
|
||||
/// <summary>
|
||||
/// Creates a new <typeparamref name="T"/> <see langword="delegate"/> for the target owner
|
||||
/// </summary>
|
||||
/// <param name="owner">The <see cref="Type"/> instance that will own the new method</param>
|
||||
/// <param name="builder">An <see cref="Action"/> that builds the IL bytecode for the new method</param>
|
||||
/// <returns>A new dynamic method wrapped as a <typeparamref name="T"/> <see langword="delegate"/></returns>
|
||||
[Pure]
|
||||
public static T New(Type owner, Action<ILGenerator> builder)
|
||||
public static T New(Action<ILGenerator> builder)
|
||||
{
|
||||
// Create a new dynamic method with a unique id
|
||||
string id = $"__IL__{typeof(T).Name}_{Interlocked.Increment(ref _Count)}";
|
||||
DynamicMethod method = new DynamicMethod(id, ReturnType, ParameterTypes, owner);
|
||||
DynamicMethod method = new DynamicMethod(id, ReturnType, ParameterTypes, OwnerType);
|
||||
|
||||
// Build the IL bytecode
|
||||
ILGenerator il = method.GetILGenerator();
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
using System.Reflection.Emit;
|
||||
using ComputeSharp.Shaders.Extensions;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
|
||||
namespace ComputeSharp.Tests.Internals
|
||||
{
|
||||
[TestClass]
|
||||
[TestCategory("DynamicMethod")]
|
||||
public class DynamicMethodTests
|
||||
{
|
||||
public delegate int Square(int x);
|
||||
|
||||
public void TestSquare()
|
||||
{
|
||||
Square square = DynamicMethod<Square>.New(il =>
|
||||
{
|
||||
il.Emit(OpCodes.Ldarg_0);
|
||||
il.Emit(OpCodes.Dup);
|
||||
il.Emit(OpCodes.Mul);
|
||||
il.Emit(OpCodes.Ret);
|
||||
});
|
||||
|
||||
int result = square(2); // result = 2 * 2
|
||||
|
||||
Assert.IsTrue(result == 4);
|
||||
}
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする