[runtime] Remove all NACL support. It was unmaintained for a long time. (#4955)
[mono.git] / mono / tests / bug-389886-3.cs
1 using System;
2 using System.Threading;
3 using System.Reflection;
4 using System.Reflection.Emit;
5
6 namespace TestApp
7 {
8     class Program
9     {
10                 static AssemblyBuilder assembly;
11                 static ModuleBuilder module;
12                 static Type[] args;
13                 static ConstructorBuilder delegate_ctor;
14                 static MethodBuilder invoke_mb;
15                 static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";
16
17                 static void SetUp ()
18                 {
19                         AssemblyName assemblyName = new AssemblyName ();
20                         assemblyName.Name = ASSEMBLY_NAME;
21
22                         assembly =
23                                 Thread.GetDomain ().DefineDynamicAssembly (
24                                         assemblyName, AssemblyBuilderAccess.RunAndSave, ".");
25
26                         module = assembly.DefineDynamicModule ("module1", "bla.exe");
27                 }
28
29                 static TypeBuilder DefineDelegate () {
30                    TypeBuilder typeBuilder = module.DefineType( "MyDelegate", 
31                                 TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed, 
32                                 typeof (object) );
33                         args = typeBuilder.DefineGenericParameters ("TIn", "TOut");
34
35                         delegate_ctor = typeBuilder.DefineConstructor( 
36                                 MethodAttributes.Public | MethodAttributes.HideBySig | 
37                                 MethodAttributes.RTSpecialName | MethodAttributes.SpecialName, 
38                                 CallingConventions.Standard, 
39                                 new Type[] { typeof(Object), typeof (IntPtr) } ); 
40
41                         delegate_ctor.SetImplementationFlags( MethodImplAttributes.Runtime | MethodImplAttributes.Managed ); 
42
43                         invoke_mb = typeBuilder.DefineMethod( 
44                                 "Invoke", 
45                                 MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, 
46                                 args [1], 
47                                 new Type[] { args [0] } ); 
48
49                         invoke_mb.SetImplementationFlags( MethodImplAttributes.Runtime | MethodImplAttributes.Managed ); 
50
51                         MethodBuilder mb = typeBuilder.DefineMethod( 
52                                 "BeginInvoke", 
53                                 MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, 
54                                 typeof (IAsyncResult), 
55                                 new Type[] { args [0], typeof (AsyncCallback), typeof (object) } ); 
56
57                         mb.SetImplementationFlags( MethodImplAttributes.Runtime | MethodImplAttributes.Managed ); 
58
59                         mb = typeBuilder.DefineMethod( 
60                                 "EndInvoke", 
61                                 MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig, 
62                                 args [1], 
63                                 new Type[] {  typeof (IAsyncResult) } ); 
64
65                         mb.SetImplementationFlags( MethodImplAttributes.Runtime | MethodImplAttributes.Managed ); 
66
67                         return typeBuilder;
68                 }
69
70                 static int Main()
71                 {
72                         SetUp ();
73
74                         TypeBuilder tb = DefineDelegate ();
75                         TypeBuilder main = module.DefineType ("Main", TypeAttributes.Public);
76                         /* >>>move this to after the SetParent call and things will work<<< */
77                         Type inst = tb.MakeGenericType (new Type[] {typeof (double), typeof(int)});
78
79                         tb.SetParent (typeof( System.MulticastDelegate));
80
81                         ConstructorBuilder ctor = main.DefineDefaultConstructor (MethodAttributes.Public);
82
83                         MethodBuilder foo_mb = main.DefineMethod("Foo",  MethodAttributes.Public, typeof (int), new Type[] { typeof (double) } ); 
84                         ILGenerator ig = foo_mb.GetILGenerator ();
85                         ig.Emit (OpCodes.Ldc_I4_0);
86                         ig.Emit (OpCodes.Ret);
87
88
89                         MethodBuilder main_mb = main.DefineMethod ("Main", MethodAttributes.Public | MethodAttributes.Static, typeof (int), Type.EmptyTypes);
90                         ig = main_mb.GetILGenerator ();
91
92                         ig.Emit (OpCodes.Newobj, ctor);
93                         ig.Emit (OpCodes.Ldftn, foo_mb);
94                         ig.Emit (OpCodes.Newobj, TypeBuilder.GetConstructor (inst, delegate_ctor));
95                         ig.Emit (OpCodes.Ldc_R8, 2.2);
96                         ig.Emit (OpCodes.Callvirt, TypeBuilder.GetMethod (inst, invoke_mb));
97
98                         ig.Emit (OpCodes.Ret);
99
100                         tb.CreateType ();
101
102                         Type t = main.CreateType ();
103
104                         MethodInfo method = t.GetMethod ("Main");
105                         method.Invoke (null, null);
106
107                         return 0;
108                 }
109         }
110 }