forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIlCompilerContext.cs
More file actions
97 lines (80 loc) · 2.36 KB
/
Copy pathIlCompilerContext.cs
File metadata and controls
97 lines (80 loc) · 2.36 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// -----------------------------------------------------------------------
// <copyright file="IlCompilerContext.cs" company="Asynkron HB">
// Copyright (C) 2015-2017 Asynkron HB All rights reserved
// </copyright>
// -----------------------------------------------------------------------
#if NET45
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
namespace Wire.Compilation
{
public class IlCompilerContext
{
private int _stackDepth;
public IlCompilerContext(ILGenerator il, Type selfType)
{
Il = new IlEmitter(il);
SelfType = selfType;
}
public IlEmitter Il { get; }
public int StackDepth
{
get => _stackDepth;
set
{
_stackDepth = value;
if (value < 0)
{
throw new NotSupportedException("Stack depth can not be less than 0");
}
}
}
public Type SelfType { get; }
}
public class IlEmitter
{
private readonly ILGenerator _il;
private readonly StringBuilder _sb = new StringBuilder();
public IlEmitter(ILGenerator il)
{
_il = il;
}
public override string ToString()
{
return _sb.ToString();
}
public void Emit(OpCode opcode)
{
_sb.AppendLine($"{opcode}");
_il.Emit(opcode);
}
public void Emit(OpCode opcode, FieldInfo field)
{
_sb.AppendLine($"{opcode} field {field}");
_il.Emit(opcode, field);
}
public void Emit(OpCode opcode, ConstructorInfo ctor)
{
_sb.AppendLine($"{opcode} ctor {ctor}");
_il.Emit(opcode, ctor);
}
public void Emit(OpCode opcode, int value)
{
_sb.AppendLine($"{opcode}_{value}");
_il.Emit(opcode, value);
}
public void Emit(OpCode opcode, Type type)
{
_sb.AppendLine($"{opcode} type {type.Name}");
_il.Emit(opcode, type);
}
public void EmitCall(OpCode opcode, MethodInfo method, Type[] optionalTypes)
{
_sb.AppendLine($"{opcode} {method}");
_il.EmitCall(opcode, method, optionalTypes);
}
}
}
#endif