Skip to content

Commit

Permalink
2010-07-12 Rodrigo Kumpera <rkumpera@novell.com>
Browse files Browse the repository at this point in the history
	* bug-389886-3.cs: New regression test.

svn path=/trunk/mono/; revision=160259
  • Loading branch information
kumpera committed Jul 12, 2010
1 parent d229e91 commit c0da3aa
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
4 changes: 4 additions & 0 deletions mono/tests/ChangeLog
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,7 @@
2010-07-12 Rodrigo Kumpera <rkumpera@novell.com>

* bug-389886-3.cs: New regression test.

2010-07-10 Jonathan Pryor <jpryor@novell.com> 2010-07-10 Jonathan Pryor <jpryor@novell.com>


* Makfile.am (test-process-exit): Expected output is in $srcdir. * Makfile.am (test-process-exit): Expected output is in $srcdir.
Expand Down
3 changes: 2 additions & 1 deletion mono/tests/Makefile.am
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -368,7 +368,8 @@ BASE_TEST_CS_SRC= \
bug-561239.cs \ bug-561239.cs \
bug-562150.cs \ bug-562150.cs \
bug-575941.cs \ bug-575941.cs \
bug-599469.cs bug-599469.cs \
bug-389886-3.cs


TEST_CS_SRC_DIST= \ TEST_CS_SRC_DIST= \
$(BASE_TEST_CS_SRC) \ $(BASE_TEST_CS_SRC) \
Expand Down
110 changes: 110 additions & 0 deletions mono/tests/bug-389886-3.cs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,110 @@
using System;
using System.Threading;
using System.Reflection;
using System.Reflection.Emit;

namespace TestApp
{
class Program
{
static AssemblyBuilder assembly;
static ModuleBuilder module;
static Type[] args;
static ConstructorBuilder delegate_ctor;
static MethodBuilder invoke_mb;
static string ASSEMBLY_NAME = "MonoTests.System.Reflection.Emit.TypeBuilderTest";

static void SetUp ()
{
AssemblyName assemblyName = new AssemblyName ();
assemblyName.Name = ASSEMBLY_NAME;

assembly =
Thread.GetDomain ().DefineDynamicAssembly (
assemblyName, AssemblyBuilderAccess.RunAndSave, ".");

module = assembly.DefineDynamicModule ("module1", "bla.exe");
}

static TypeBuilder DefineDelegate () {
TypeBuilder typeBuilder = module.DefineType( "MyDelegate",
TypeAttributes.Public | TypeAttributes.Class | TypeAttributes.Sealed,
typeof (object) );
args = typeBuilder.DefineGenericParameters ("TIn", "TOut");

delegate_ctor = typeBuilder.DefineConstructor(
MethodAttributes.Public | MethodAttributes.HideBySig |
MethodAttributes.RTSpecialName | MethodAttributes.SpecialName,
CallingConventions.Standard,
new Type[] { typeof(Object), typeof (IntPtr) } );

delegate_ctor.SetImplementationFlags( MethodImplAttributes.Runtime | MethodImplAttributes.Managed );

invoke_mb = typeBuilder.DefineMethod(
"Invoke",
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig,
args [1],
new Type[] { args [0] } );

invoke_mb.SetImplementationFlags( MethodImplAttributes.Runtime | MethodImplAttributes.Managed );

MethodBuilder mb = typeBuilder.DefineMethod(
"BeginInvoke",
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig,
typeof (IAsyncResult),
new Type[] { args [0], typeof (AsyncCallback), typeof (object) } );

mb.SetImplementationFlags( MethodImplAttributes.Runtime | MethodImplAttributes.Managed );

mb = typeBuilder.DefineMethod(
"EndInvoke",
MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.HideBySig,
args [1],
new Type[] { typeof (IAsyncResult) } );

mb.SetImplementationFlags( MethodImplAttributes.Runtime | MethodImplAttributes.Managed );

return typeBuilder;
}

static int Main()
{
SetUp ();

TypeBuilder tb = DefineDelegate ();
TypeBuilder main = module.DefineType ("Main", TypeAttributes.Public);
/* >>>move this to after the SetParent call and things will work<<< */
Type inst = tb.MakeGenericType (new Type[] {typeof (double), typeof(int)});

tb.SetParent (typeof( System.MulticastDelegate));

ConstructorBuilder ctor = main.DefineDefaultConstructor (MethodAttributes.Public);

MethodBuilder foo_mb = main.DefineMethod("Foo", MethodAttributes.Public, typeof (int), new Type[] { typeof (double) } );
ILGenerator ig = foo_mb.GetILGenerator ();
ig.Emit (OpCodes.Ldc_I4_0);
ig.Emit (OpCodes.Ret);


MethodBuilder main_mb = main.DefineMethod ("Main", MethodAttributes.Public | MethodAttributes.Static, typeof (int), Type.EmptyTypes);
ig = main_mb.GetILGenerator ();

ig.Emit (OpCodes.Newobj, ctor);
ig.Emit (OpCodes.Ldftn, foo_mb);
ig.Emit (OpCodes.Newobj, TypeBuilder.GetConstructor (inst, delegate_ctor));
ig.Emit (OpCodes.Ldc_R8, 2.2);
ig.Emit (OpCodes.Callvirt, TypeBuilder.GetMethod (inst, invoke_mb));

ig.Emit (OpCodes.Ret);

tb.CreateType ();

Type t = main.CreateType ();

MethodInfo method = t.GetMethod ("Main");
method.Invoke (null, null);

return 0;
}
}
}

0 comments on commit c0da3aa

Please sign in to comment.