-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArguments.cs
More file actions
77 lines (72 loc) · 3.34 KB
/
Arguments.cs
File metadata and controls
77 lines (72 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System.Reflection.Emit;
using JetBrains.Annotations;
namespace ILGeneratorExtensions
{
/// <summary>
/// Contains extension methods for dealing with arguments to the method
/// </summary>
[PublicAPI]
public static partial class Arguments
{
/// <summary>
/// Loads the specified argument onto the evaluation stack
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="argNum">The index of the argument to load</param>
[PublicAPI]
public static ILGenerator LoadArgument(this ILGenerator generator, ushort argNum)
{
switch (argNum)
{
case 0:
return generator.FluentEmit(OpCodes.Ldarg_0);
case 1:
return generator.FluentEmit(OpCodes.Ldarg_1);
case 2:
return generator.FluentEmit(OpCodes.Ldarg_2);
case 3:
return generator.FluentEmit(OpCodes.Ldarg_3);
default:
return argNum <= 255
? generator.FluentEmit(OpCodes.Ldarg_S, (byte)argNum)
: generator.FluentEmit(OpCodes.Ldarg, argNum);
}
}
/// <summary>
/// Short-cut to load the first argument - which is the this reference in instance methods
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
[PublicAPI]
public static ILGenerator LoadThis(this ILGenerator generator) => generator.FluentEmit(OpCodes.Ldarg_0);
/// <summary>
/// Loads the address of the specified argument onto the evaluation stack
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="argNum"></param>
[PublicAPI]
public static ILGenerator LoadArgumentAddress(this ILGenerator generator, ushort argNum)
{
return argNum <= 255
? generator.FluentEmit(OpCodes.Ldarga_S, (byte)argNum)
: generator.FluentEmit(OpCodes.Ldarga, argNum);
}
/// <summary>
/// Pop the value on the top of the evaluation stack and stores it in the specified argument
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
/// <param name="argNum">The index of the argument to store the value in</param>
[PublicAPI]
public static ILGenerator StoreInArgument(this ILGenerator generator, ushort argNum)
{
return argNum <= 255
? generator.FluentEmit(OpCodes.Starg_S, (byte)argNum)
: generator.FluentEmit(OpCodes.Starg, argNum);
}
/// <summary>
/// Pushes an unmanaged pointer to the argument list of the current method
/// </summary>
/// <param name="generator">The <see cref="T:System.Reflection.Emit.ILGenerator" /> to emit instructions from</param>
[PublicAPI]
public static ILGenerator LoadArgumentList(this ILGenerator generator) => generator.FluentEmit(OpCodes.Arglist);
}
}