Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
BoundConstants.cs
Go to the documentation of this file.
6
8
9internal sealed class BoundConstants
10{
11 private readonly struct TypedConstant : IEquatable<TypedConstant>
12 {
13 internal readonly object Value;
14
15 internal readonly Type Type;
16
17 internal TypedConstant(object value, Type type)
18 {
19 Value = value;
20 Type = type;
21 }
22
23 public override int GetHashCode()
24 {
26 }
27
29 {
30 if (Value == other.Value)
31 {
32 return Type.Equals(other.Type);
33 }
34 return false;
35 }
36
37 public override bool Equals([NotNullWhen(true)] object obj)
38 {
40 {
41 return Equals(other);
42 }
43 return false;
44 }
45 }
46
47 private readonly List<object> _values = new List<object>();
48
50
52
54
55 internal int Count => _values.Count;
56
57 internal object[] ToArray()
58 {
59 return _values.ToArray();
60 }
61
62 internal void AddReference(object value, Type type)
63 {
65 {
67 }
68 Helpers.IncrementCount(new TypedConstant(value, type), _references);
69 }
70
71 internal void EmitConstant(LambdaCompiler lc, object value, Type type)
72 {
74 {
75 lc.IL.Emit(OpCodes.Ldloc, value2);
76 return;
77 }
80 }
81
83 {
84 int num = 0;
86 {
87 if (ShouldCache(reference.Value))
88 {
89 num++;
90 }
91 }
92 if (num == 0)
93 {
94 return;
95 }
97 _cache.Clear();
99 {
100 if (ShouldCache(reference2.Value))
101 {
102 if (--num > 0)
103 {
104 lc.IL.Emit(OpCodes.Dup);
105 }
106 LocalBuilder localBuilder = lc.IL.DeclareLocal(reference2.Key.Type);
107 EmitConstantFromArray(lc, reference2.Key.Value, localBuilder.LocalType);
108 lc.IL.Emit(OpCodes.Stloc, localBuilder);
110 }
111 }
112 }
113
114 private static bool ShouldCache(int refCount)
115 {
116 return refCount > 2;
117 }
118
120 {
121 lc.EmitClosureArgument();
123 }
124
126 {
128 {
131 }
132 lc.IL.EmitPrimitive(value2);
133 lc.IL.Emit(OpCodes.Ldelem_Ref);
134 if (type.IsValueType)
135 {
136 lc.IL.Emit(OpCodes.Unbox_Any, type);
137 }
138 else if (type != typeof(object))
139 {
140 lc.IL.Emit(OpCodes.Castclass, type);
141 }
142 }
143}
bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value)
bool TryAdd(TKey key, TValue value)
void Add(TKey key, TValue value)
void EmitConstantFromArray(LambdaCompiler lc, object value, Type type)
void EmitConstant(LambdaCompiler lc, object value, Type type)
static void EmitConstantsArray(LambdaCompiler lc)
readonly Dictionary< object, int > _indexes
readonly Dictionary< TypedConstant, LocalBuilder > _cache
readonly Dictionary< TypedConstant, int > _references
static readonly OpCode Castclass
Definition OpCodes.cs:235
static readonly OpCode Unbox_Any
Definition OpCodes.cs:329
static readonly OpCode Stloc
Definition OpCodes.cs:429
static readonly OpCode Ldfld
Definition OpCodes.cs:245
static readonly OpCode Ldelem_Ref
Definition OpCodes.cs:307
static readonly OpCode Ldloc
Definition OpCodes.cs:425
static readonly OpCode Dup
Definition OpCodes.cs:77
override bool Equals(object? o)
Definition Type.cs:1113
override int GetHashCode()
Definition Type.cs:1122
override bool Equals([NotNullWhen(true)] object obj)