Debian Codegraph @

external/cecil-legacy/Test/Mono.Cecil.Tests/ImportCecilTests.cs

raw symbols SCIP
1 using System;
2 using System.Collections.Generic;
3 using System.Diagnostics;
4 using System.IO;
5 using System.Linq;
6 using SR = System.Reflection;
7 using System.Runtime.CompilerServices;
8
9 using Mono.Cecil.Cil;
10
11 using NUnit.Framework;
12
13 namespace Mono.Cecil.Tests {
14
15 [TestFixture]
16 public class ImportCecilTests : BaseTestFixture {
17
18 [Test]
19 public void ImportStringByRef ()
20 {
21 var get_string = Compile<Func<string, string>> ((module, body) => {
22 var type = module.Types [1];
23
24 var method_by_ref = new MethodDefinition {
25 Name = "ModifyString",
26 IsPrivate = true,
27 IsStatic = true,
28 };
29
30 type.Methods.Add (method_by_ref);
31
32 method_by_ref.MethodReturnType.ReturnType = module.Import (typeof (void).ToDefinition ());
33
34 method_by_ref.Parameters.Add (new ParameterDefinition (module.Import (typeof (string).ToDefinition ())));
35 method_by_ref.Parameters.Add (new ParameterDefinition (module.Import (new ByReferenceType (typeof (string).ToDefinition ()))));
36
37 var m_il = method_by_ref.Body.GetILProcessor ();
38 m_il.Emit (OpCodes.Ldarg_1);
39 m_il.Emit (OpCodes.Ldarg_0);
40 m_il.Emit (OpCodes.Stind_Ref);
41 m_il.Emit (OpCodes.Ret);
42
43 var v_0 = new VariableDefinition (module.Import (typeof (string).ToDefinition ()));
44 body.Variables.Add (v_0);
45
46 var il = body.GetILProcessor ();
47 il.Emit (OpCodes.Ldnull);
48 il.Emit (OpCodes.Stloc, v_0);
49 il.Emit (OpCodes.Ldarg_0);
50 il.Emit (OpCodes.Ldloca, v_0);
51 il.Emit (OpCodes.Call, method_by_ref);
52 il.Emit (OpCodes.Ldloc_0);
53 il.Emit (OpCodes.Ret);
54 });
55
56 Assert.AreEqual ("foo", get_string ("foo"));
57 }
58
59 [Test]
60 public void ImportStringArray ()
61 {
62 var identity = Compile<Func<string [,], string [,]>> ((module, body) => {
63 var il = body.GetILProcessor ();
64 il.Emit (OpCodes.Ldarg_0);
65 il.Emit (OpCodes.Ret);
66 });
67
68 var array = new string [2, 2];
69
70 Assert.AreEqual (array, identity (array));
71 }
72
73 [Test]
74 public void ImportFieldStringEmpty ()
75 {
76 var get_empty = Compile<Func<string>> ((module, body) => {
77 var il = body.GetILProcessor ();
78 il.Emit (OpCodes.Ldsfld, module.Import (typeof (string).GetField ("Empty").ToDefinition ()));
79 il.Emit (OpCodes.Ret);
80 });
81
82 Assert.AreEqual ("", get_empty ());
83 }
84
85 [Test]
86 public void ImportStringConcat ()
87 {
88 var concat = Compile<Func<string, string, string>> ((module, body) => {
89 var il = body.GetILProcessor ();
90 il.Emit (OpCodes.Ldarg_0);
91 il.Emit (OpCodes.Ldarg_1);
92 il.Emit (OpCodes.Call, module.Import (typeof (string).GetMethod ("Concat", new [] { typeof (string), typeof (string) }).ToDefinition ()));
93 il.Emit (OpCodes.Ret);
94 });
95
96 Assert.AreEqual ("FooBar", concat ("Foo", "Bar"));
97 }
98
99 public class Generic<T> {
100 public T Field;
101
102 public T Method (T t)
103 {
104 return t;
105 }
106
107 public TS GenericMethod<TS> (T t, TS s)
108 {
109 return s;
110 }
111
112 public Generic<TS> ComplexGenericMethod<TS> (T t, TS s)
113 {
114 return new Generic<TS> { Field = s };
115 }
116 }
117
118 [Test]
119 public void ImportGenericField ()
120 {
121 var get_field = Compile<Func<Generic<string>, string>> ((module, body) => {
122 var generic_def = module.Import (typeof (Generic<>)).Resolve ();
123 var field_def = generic_def.Fields.Where (f => f.Name == "Field").First ();
124
125 var field_string = field_def.MakeGeneric (module.Import (typeof (string)));
126
127 var field_ref = module.Import (field_string);
128
129 var il = body.GetILProcessor ();
130 il.Emit (OpCodes.Ldarg_0);
131 il.Emit (OpCodes.Ldfld, field_ref);
132 il.Emit (OpCodes.Ret);
133 });
134
135 var generic = new Generic<string> {
136 Field = "foo",
137 };
138
139 Assert.AreEqual ("foo", get_field (generic));
140 }
141
142 [Test]
143 public void ImportGenericMethod ()
144 {
145 var generic_identity = Compile<Func<Generic<int>, int, int>> ((module, body) => {
146 var generic_def = module.Import (typeof (Generic<>)).Resolve ();
147 var method_def = generic_def.Methods.Where (m => m.Name == "Method").First ();
148
149 var method_int = method_def.MakeGeneric (module.Import (typeof (int)));
150 var method_ref = module.Import (method_int);
151
152 var il = body.GetILProcessor ();
153 il.Emit (OpCodes.Ldarg_0);
154 il.Emit (OpCodes.Ldarg_1);
155 il.Emit (OpCodes.Callvirt, method_ref);
156 il.Emit (OpCodes.Ret);
157 });
158
159 Assert.AreEqual (42, generic_identity (new Generic<int> (), 42));
160 }
161
162 [Test]
163 public void ImportGenericMethodSpec ()
164 {
165 var gen_spec_id = Compile<Func<Generic<string>, int, int>> ((module, body) => {
166 var generic_def = module.Import (typeof (Generic<>)).Resolve ();
167 var method_def = generic_def.Methods.Where (m => m.Name == "GenericMethod").First ();
168
169 var method_string = method_def.MakeGeneric (module.Import (typeof (string)));
170
171 var method_instance = method_string.MakeGenericMethod (module.Import (typeof (int)));
172
173 var method_ref = module.Import (method_instance);
174
175 var il = body.GetILProcessor ();
176 il.Emit (OpCodes.Ldarg_0);
177 il.Emit (OpCodes.Ldnull);
178 il.Emit (OpCodes.Ldarg_1);
179 il.Emit (OpCodes.Callvirt, method_ref);
180 il.Emit (OpCodes.Ret);
181 });
182
183 Assert.AreEqual (42, gen_spec_id (new Generic<string> (), 42));
184 }
185
186 [Test]
187 public void ImportComplexGenericMethodSpec ()
188 {
189 var gen_spec_id = Compile<Func<Generic<string>, int, int>> ((module, body) => {
190 var generic_def = module.Import (typeof (Generic<>)).Resolve ();
191 var method_def = generic_def.Methods.Where (m => m.Name == "ComplexGenericMethod").First ();
192
193 var method_string = method_def.MakeGeneric (module.Import (typeof (string)));
194 var method_instance = method_string.MakeGenericMethod (module.Import (typeof (int)));
195 var method_ref = module.Import (method_instance);
196
197 var field_def = generic_def.Fields.Where (f => f.Name == "Field").First ();
198 var field_int = field_def.MakeGeneric (module.Import (typeof (int)));
199 var field_ref = module.Import (field_int);
200
201 var il = body.GetILProcessor ();
202 il.Emit (OpCodes.Ldarg_0);
203 il.Emit (OpCodes.Ldnull);
204 il.Emit (OpCodes.Ldarg_1);
205 il.Emit (OpCodes.Callvirt, method_ref);
206 il.Emit (OpCodes.Ldfld, field_ref);
207 il.Emit (OpCodes.Ret);
208 });
209
210 Assert.AreEqual (42, gen_spec_id (new Generic<string> (), 42));
211 }
212
213 [Test]
214 public void ImportMethodOnOpenGeneric ()
215 {
216 var generic = typeof (Generic<>).ToDefinition ();
217 var module = ModuleDefinition.CreateModule ("foo", ModuleKind.Dll);
218
219 var method = module.Import (generic.GetMethod ("Method"));
220
221 Assert.AreEqual ("T Mono.Cecil.Tests.ImportCecilTests/Generic`1::Method(T)", method.FullName);
222 }
223
224 public class ContextGeneric1Method2<G1>
225 {
226 public G1 GenericMethod<R1, S1> (R1 r, S1 s)
227 {
228 return default (G1);
229 }
230 }
231
232 public class ContextGeneric2Method1<G2, H2>
233 {
234 public R2 GenericMethod<R2> (G2 g, H2 h)
235 {
236 return default (R2);
237 }
238 }
239
240 public class NestedGenericsA<A>
241 {
242 public class NestedGenericsB<B>
243 {
244 public class NestedGenericsC<C>
245 {
246 public A GenericMethod (B b, C c)
247 {
248 return default (A);
249 }
250 }
251 }
252 }
253
254 [Test]
255 public void ContextGenericTest ()
256 {
257 var module = ModuleDefinition.ReadModule (typeof (ContextGeneric1Method2<>).Module.FullyQualifiedName);
258 // by mixing open generics with 2 & 1 parameters, we make sure the right context is used (because otherwise, an exception will be thrown)
259 var type = typeof (ContextGeneric1Method2<>).MakeGenericType (typeof (ContextGeneric2Method1<,>));
260 var meth = type.GetMethod ("GenericMethod");
261 var imported_type = module.Import (type);
262 var method = module.Import (meth, imported_type);
263 Assert.AreEqual ("G1 Mono.Cecil.Tests.ImportCecilTests/ContextGeneric1Method2`1<Mono.Cecil.Tests.ImportCecilTests/ContextGeneric2Method1`2<G2,H2>>::GenericMethod<R1,S1>(R1,S1)", method.FullName);
264
265 // and the other way around
266 type = typeof (ContextGeneric2Method1<,>).MakeGenericType (typeof (ContextGeneric1Method2<>), typeof (IList<>));
267 meth = type.GetMethod ("GenericMethod");
268 imported_type = module.Import (type);
269 method = module.Import (meth, imported_type);
270 Assert.AreEqual ("R2 Mono.Cecil.Tests.ImportCecilTests/ContextGeneric2Method1`2<Mono.Cecil.Tests.ImportCecilTests/ContextGeneric1Method2`1<G1>,System.Collections.Generic.IList`1<T>>::GenericMethod<R2>(G2,H2)", method.FullName);
271
272 // not sure about this one
273 type = typeof (NestedGenericsA<string>.NestedGenericsB<int>.NestedGenericsC<float>);
274 meth = type.GetMethod ("GenericMethod");
275 imported_type = module.Import (type);
276 method = module.Import (meth, imported_type);
277 Assert.AreEqual ("A Mono.Cecil.Tests.ImportCecilTests/NestedGenericsA`1/NestedGenericsB`1/NestedGenericsC`1<System.String,System.Int32,System.Single>::GenericMethod(B,C)", method.FullName);
278
279 // We need both the method & type !
280 type = typeof (Generic<>).MakeGenericType (typeof (string));
281 meth = type.GetMethod ("ComplexGenericMethod");
282 imported_type = module.Import (type);
283 method = module.Import (meth, imported_type);
284 Assert.AreEqual ("Mono.Cecil.Tests.ImportCecilTests/Generic`1<TS> Mono.Cecil.Tests.ImportCecilTests/Generic`1<System.String>::ComplexGenericMethod<TS>(T,TS)", method.FullName);
285 }
286
287 delegate void Emitter (ModuleDefinition module, MethodBody body);
288
289 [MethodImpl (MethodImplOptions.NoInlining)]
290 static TDelegate Compile<TDelegate> (Emitter emitter)
291 where TDelegate : class
292 {
293 var name = GetTestCaseName ();
294
295 var module = CreateTestModule<TDelegate> (name, emitter);
296 var assembly = LoadTestModule (module);
297
298 return CreateRunDelegate<TDelegate> (GetTestCase (name, assembly));
299 }
300
301 static TDelegate CreateRunDelegate<TDelegate> (Type type)
302 where TDelegate : class
303 {
304 return (TDelegate) (object) Delegate.CreateDelegate (typeof (TDelegate), type.GetMethod ("Run"));
305 }
306
307 static Type GetTestCase (string name, SR.Assembly assembly)
308 {
309 return assembly.GetType (name);
310 }
311
312 static SR.Assembly LoadTestModule (ModuleDefinition module)
313 {
314 using (var stream = new MemoryStream ()) {
315 module.Write (stream);
316 File.WriteAllBytes (Path.Combine (Path.Combine (Path.GetTempPath (), "cecil"), module.Name + ".dll"), stream.ToArray ());
317 return SR.Assembly.Load (stream.ToArray ());
318 }
319 }
320
321 static ModuleDefinition CreateTestModule<TDelegate> (string name, Emitter emitter)
322 {
323 var module = CreateModule (name);
324
325 var type = new TypeDefinition (
326 "",
327 name,
328 TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Abstract,
329 module.Import (typeof (object)));
330
331 module.Types.Add (type);
332
333 var method = CreateMethod (type, typeof (TDelegate).GetMethod ("Invoke"));
334
335 emitter (module, method.Body);
336
337 return module;
338 }
339
340 static MethodDefinition CreateMethod (TypeDefinition type, SR.MethodInfo pattern)
341 {
342 var module = type.Module;
343
344 var method = new MethodDefinition {
345 Name = "Run",
346 IsPublic = true,
347 IsStatic = true,
348 };
349
350 type.Methods.Add (method);
351
352 method.MethodReturnType.ReturnType = module.Import (pattern.ReturnType);
353
354 foreach (var parameter_pattern in pattern.GetParameters ())
355 method.Parameters.Add (new ParameterDefinition (module.Import (parameter_pattern.ParameterType)));
356
357 return method;
358 }
359
360 static ModuleDefinition CreateModule (string name)
361 {
362 return ModuleDefinition.CreateModule (name, ModuleKind.Dll);
363 }
364
365 [MethodImpl (MethodImplOptions.NoInlining)]
366 static string GetTestCaseName ()
367 {
368 var stack_trace = new StackTrace ();
369 var stack_frame = stack_trace.GetFrame (2);
370
371 return "ImportCecil_" + stack_frame.GetMethod ().Name;
372 }
373 }
374 }