-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCILSupport.cs
More file actions
298 lines (240 loc) · 9.88 KB
/
Copy pathCILSupport.cs
File metadata and controls
298 lines (240 loc) · 9.88 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using JSIL.Translator;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Cecil.Mdb;
using Mono.Cecil.Pdb;
namespace JSIL.Internal {
public class AssemblyDataResolver : IDisposable
{
private readonly CachingMetadataResolver _CachingMetadataResolver;
private readonly AssemblyResolver _AssemblyResolver;
private readonly AssemblyCache _AssemblyCache;
private readonly bool OwnsCache;
public AssemblyDataResolver(IEnumerable<string> dirs, Configuration configuration, AssemblyCache cache = null)
{
if (cache == null) {
cache = new AssemblyCache();
OwnsCache = true;
}
_AssemblyCache = cache;
_AssemblyResolver = new AssemblyResolver(dirs, configuration, cache);
_CachingMetadataResolver = new CachingMetadataResolver(_AssemblyResolver);
}
public AssemblyDataResolver(Configuration configuration, AssemblyCache cache = null) : this(Enumerable.Empty<string>(), configuration, cache)
{
}
public CachingMetadataResolver CachingMetadataResolver
{
get { return _CachingMetadataResolver; }
}
public AssemblyResolver AssemblyResolver
{
get { return _AssemblyResolver; }
}
public AssemblyCache AssemblyCache
{
get { return _AssemblyCache; }
}
public void Dispose()
{
if (OwnsCache) {
_AssemblyCache.Dispose();
};
_AssemblyResolver.Dispose();
}
}
public class AssemblyCache : ConcurrentCache<string, AssemblyDefinition> {
public bool RegisterAssembly (AssemblyDefinition assembly) {
if (assembly == null)
throw new ArgumentNullException("assembly");
var name = assembly.Name.FullName;
return TryCreate(name, (fullName) => assembly);
}
}
public class AssemblyResolver : BaseAssemblyResolver, IDisposable {
private static readonly byte[] PCLPublicKeyToken = {
124, 236, 133, 215,
190, 167, 121, 142
};
private static readonly byte[] BCLPublicKeyToken = {
183, 122, 92, 86,
25, 52, 224, 137
};
protected readonly Configuration Configuration;
protected readonly AssemblyCache Cache;
protected readonly bool OwnsCache;
public AssemblyResolver(IEnumerable<string> dirs, Configuration configuration, AssemblyCache cache = null) {
Configuration = configuration;
OwnsCache = (cache == null);
Cache = cache ?? new AssemblyCache();
foreach (var dir in dirs)
AddSearchDirectory(dir);
}
public void Dispose () {
if (OwnsCache)
Cache.Dispose();
}
public AssemblyNameReference FilterPortableClassLibraryReferences (AssemblyNameReference name) {
// Portable class libraries are pretty shoddily constructed. Who came up with this nonsense?
if (!name.PublicKeyToken.SequenceEqual(PCLPublicKeyToken))
return name;
var bclName = new AssemblyNameReference(
name.Name,
// FIXME: Is this right? It was 4.0 before, but that's definitely wrong.
// This should pick the highest appropriate version from the GAC.
null
) {
Culture = name.Culture,
PublicKeyToken = BCLPublicKeyToken
};
return bclName;
}
public AssemblyNameReference FilterRedirectedReferences (AssemblyNameReference name, out string redirectedFrom) {
redirectedFrom = null;
foreach (var kvp in Configuration.Assemblies.Redirects) {
if (Regex.IsMatch(name.FullName, kvp.Key, RegexOptions.IgnoreCase)) {
redirectedFrom = name.FullName;
return AssemblyNameReference.Parse(kvp.Value);
}
}
return name;
}
public override AssemblyDefinition Resolve (AssemblyNameReference name, ReaderParameters parameters) {
if (name == null)
throw new ArgumentNullException("name");
var actualName = FilterPortableClassLibraryReferences(name);
string redirectedFrom;
actualName = FilterRedirectedReferences(name, out redirectedFrom);
var result = Cache.GetOrCreate(actualName.FullName, (fullName) => {
var assembly = base.Resolve(actualName, parameters);
if (redirectedFrom != null)
Console.Error.WriteLine("// Redirected '{0}' to '{1}'", redirectedFrom, actualName.FullName);
return assembly;
});
return result;
}
}
public class NoSymbolsException : FileNotFoundException {
public NoSymbolsException (ModuleDefinition module, string fileName)
: base(
String.Format("Module '{0}' has no symbols.", Path.GetFileName(module.FullyQualifiedName)), fileName
) {
}
}
public class SymbolProvider : ISymbolReaderProvider {
public ISymbolReader GetSymbolReader (ModuleDefinition module, string fileName) {
if (String.IsNullOrWhiteSpace(fileName))
throw new ArgumentException("Invalid path", "fileName");
var pdbPath = Path.Combine(
Path.GetDirectoryName(fileName),
Path.GetFileNameWithoutExtension(fileName) + ".pdb"
);
var mdbPath = Path.Combine(
Path.GetDirectoryName(fileName),
Path.GetFileName(fileName) + ".mdb"
);
if (File.Exists(pdbPath)) {
return (new PdbReaderProvider()).GetSymbolReader(module, File.OpenRead(pdbPath));
} else if (File.Exists(mdbPath)) {
return (new MdbReaderProvider()).GetSymbolReader(module, fileName);
} else {
throw new NoSymbolsException(module, fileName);
}
}
public ISymbolReader GetSymbolReader (ModuleDefinition module, Stream symbolStream) {
// Internal constructor for no reason! Thanks!
return (new PdbReaderProvider()).GetSymbolReader(module, symbolStream);
}
}
public class CachingMetadataResolver : MetadataResolver {
public struct Key {
public readonly int HashCode;
public readonly string Namespace;
public readonly string Module;
public readonly string Name;
public readonly string DeclaringTypeName;
public Key (TypeReference tr) {
Namespace = tr.Namespace;
Module = tr.Module.Name;
Name = tr.Name;
HashCode = Namespace.GetHashCode() ^ Name.GetHashCode() ^ Module.GetHashCode();
if (tr.DeclaringType != null) {
DeclaringTypeName = tr.DeclaringType.FullName;
HashCode = HashCode ^ DeclaringTypeName.GetHashCode();
} else {
DeclaringTypeName = null;
}
}
public bool Equals (Key rhs) {
return (Namespace == rhs.Namespace) && (Module == rhs.Module) &&
(DeclaringTypeName == rhs.DeclaringTypeName) &&
(Name == rhs.Name);
}
public override bool Equals (object obj) {
if (obj is Key) {
return Equals((Key)obj);
}
return base.Equals(obj);
}
public override int GetHashCode() {
return HashCode;
}
}
public class KeyComparer : IEqualityComparer<Key> {
public bool Equals (Key x, Key y) {
return x.Equals(y);
}
public int GetHashCode (Key obj) {
return obj.HashCode;
}
}
public readonly ConcurrentCache<Key, TypeDefinition> Cache;
protected readonly ConcurrentCache<Key, TypeDefinition>.CreatorFunction<TypeReference> DoResolve;
public CachingMetadataResolver (IAssemblyResolver assemblyResolver) :
base(assemblyResolver) {
Cache = new ConcurrentCache<Key, TypeDefinition>(
Environment.ProcessorCount, 4096, new KeyComparer()
);
DoResolve = (key, type) => {
var result = base.Resolve(type);
return result;
};
}
public override TypeDefinition Resolve (TypeReference type) {
var key = new Key(type);
var result = Cache.GetOrCreate(
key, type, DoResolve
);
return result;
}
}
public class FullNameAssemblyComparer : IEqualityComparer<AssemblyDefinition> {
public bool Equals (AssemblyDefinition x, AssemblyDefinition y) {
return x.FullName.Equals(y.FullName);
}
public int GetHashCode (AssemblyDefinition obj) {
return obj.FullName.GetHashCode();
}
}
public static class CecilUtil {
// WHY IS CECIL SO DUMB?
public static MethodReference RebindMethod (MethodReference method, TypeReference newDeclaringType, TypeReference newReturnType = null) {
var result = new MethodReference(
method.Name, newReturnType ?? method.ReturnType, newDeclaringType
) {
HasThis = method.HasThis,
ExplicitThis = method.ExplicitThis
};
// TODO: Copy more attributes?
return result;
}
}
}