Merge pull request #214 from QuickJack/cd2c570c5543963d987f51080218715407c5d4b9
[mono.git] / mcs / class / corlib / System.Reflection.Emit / DynamicMethod.cs
index b37f707d641ead2369cb4f9074dadc32f0544d52..b038897d1db4f765f0e304104703a4a060ecbad8 100644 (file)
@@ -1,5 +1,5 @@
 //
-// System.Reflection.Emit/DynamicMethod.cs
+// System.Reflection.Emit.DynamicMethod.cs
 //
 // Author:
 //   Paolo Molaro (lupus@ximian.com)
@@ -31,7 +31,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0 || BOOTSTRAP_NET_2_0
 
 using System;
 using System.Reflection;
@@ -42,10 +41,11 @@ using System.Runtime.InteropServices;
 
 namespace System.Reflection.Emit {
 
-#if NET_2_0
        [ComVisible (true)]
-#endif
+       [StructLayout (LayoutKind.Sequential)]
        public sealed class DynamicMethod : MethodInfo {
+
+#pragma warning disable 169, 414, 649
                #region Sync with reflection.h
                private RuntimeMethodHandle mhandle;
                private string name;
@@ -62,10 +62,13 @@ namespace System.Reflection.Emit {
                private IntPtr referenced_by;
                private Type owner;
                #endregion
+#pragma warning restore 169, 414, 649
+               
                private Delegate deleg;
                private MonoMethod method;
                private ParameterBuilder[] pinfo;
                internal bool creating;
+               private DynamicILInfo il_info;
 
                public DynamicMethod (string name, Type returnType, Type[] parameterTypes, Module m) : this (name, returnType, parameterTypes, m, false) {
                }
@@ -88,7 +91,10 @@ namespace System.Reflection.Emit {
                public DynamicMethod (string name, Type returnType, Type[] parameterTypes) : this (name, returnType, parameterTypes, false) {
                }
 
-               public DynamicMethod (string name, Type returnType, Type[] parameterTypes, bool skipVisibility) : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, null, null, false, true) {
+               [MonoTODO ("Visibility is not restricted")]
+               public DynamicMethod (string name, Type returnType, Type[] parameterTypes, bool restrictedSkipVisibility)
+                       : this (name, MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, returnType, parameterTypes, null, null, restrictedSkipVisibility, true)
+               {
                }
 
                DynamicMethod (string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type [] parameterTypes, Type owner, Module m, bool skipVisibility, bool anonHosted)
@@ -108,7 +114,7 @@ namespace System.Reflection.Emit {
                        }
 
                        if (m == null)
-                               m = AnonHostModuleHolder.anon_host_module;
+                               m = AnonHostModuleHolder.AnonHostModule;
 
                        this.name = name;
                        this.attributes = attributes | MethodAttributes.Static;
@@ -123,12 +129,9 @@ namespace System.Reflection.Emit {
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
                private extern void create_dynamic_method (DynamicMethod m);
 
-               [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               private extern void destroy_dynamic_method (DynamicMethod m);
-
                private void CreateDynMethod () {
                        if (mhandle.Value == IntPtr.Zero) {
-                               if (ilgen == null || (ILGenerator.Mono_GetCurrentOffset (ilgen) == 0))
+                               if (ilgen == null || ilgen.ILOffset == 0)
                                        throw new InvalidOperationException ("Method '" + name + "' does not have a method body.");
 
                                ilgen.label_fixup ();
@@ -154,11 +157,6 @@ namespace System.Reflection.Emit {
                        }
                }
 
-               ~DynamicMethod ()
-               {
-                       destroy_dynamic_method (this);
-               }
-
                [ComVisible (true)]
                public Delegate CreateDelegate (Type delegateType)
                {
@@ -185,7 +183,7 @@ namespace System.Reflection.Emit {
                        return Delegate.CreateDelegate (delegateType, target, this);
                }
                
-               public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string strParamName)
+               public ParameterBuilder DefineParameter (int position, ParameterAttributes attributes, string parameterName)
                {
                        //
                        // Extension: Mono allows position == 0 for the return attribute
@@ -195,7 +193,7 @@ namespace System.Reflection.Emit {
 
                        RejectIfCreated ();
 
-                       ParameterBuilder pb = new ParameterBuilder (this, position, attributes, strParamName);
+                       ParameterBuilder pb = new ParameterBuilder (this, position, attributes, parameterName);
                        if (pinfo == null)
                                pinfo = new ParameterBuilder [parameters.Length + 1];
                        pinfo [position] = pb;
@@ -217,16 +215,17 @@ namespace System.Reflection.Emit {
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO("Not implemented")]
                public DynamicILInfo GetDynamicILInfo () {
-                       throw new NotImplementedException ();
+                       if (il_info == null)
+                               il_info = new DynamicILInfo (this);
+                       return il_info;
                }
 
                public ILGenerator GetILGenerator () {
                        return GetILGenerator (64);
                }
 
-               public ILGenerator GetILGenerator (int size) {
+               public ILGenerator GetILGenerator (int streamSize) {
                        if (((GetMethodImplementationFlags () & MethodImplAttributes.CodeTypeMask) != 
                                 MethodImplAttributes.IL) ||
                                ((GetMethodImplementationFlags () & MethodImplAttributes.ManagedMask) != 
@@ -234,7 +233,7 @@ namespace System.Reflection.Emit {
                                throw new InvalidOperationException ("Method body should not exist.");
                        if (ilgen != null)
                                return ilgen;
-                       ilgen = new ILGenerator (Module, new DynamicMethodTokenGenerator (this), size);
+                       ilgen = new ILGenerator (Module, new DynamicMethodTokenGenerator (this), streamSize);
                        return ilgen;
                }               
 
@@ -252,6 +251,15 @@ namespace System.Reflection.Emit {
                        }
                        return retval;
                }
+               
+               internal override int GetParameterCount ()
+               {
+                       return parameters == null ? 0 : parameters.Length;
+               }               
+
+               internal override Type GetParameterType (int pos) {
+                       return parameters [pos];
+               }
 
                /*
                public override object Invoke (object obj, object[] parameters) {
@@ -264,11 +272,18 @@ namespace System.Reflection.Emit {
 
                public override object Invoke (object obj, BindingFlags invokeAttr,
                                                                           Binder binder, object[] parameters,
-                                                                          CultureInfo culture) {
-                       CreateDynMethod ();
-                       if (method == null)
-                               method = new MonoMethod (mhandle);
-                       return method.Invoke (obj, parameters);
+                                                                          CultureInfo culture)
+               {
+                       try {
+                               CreateDynMethod ();
+                               if (method == null)
+                                       method = new MonoMethod (mhandle);
+
+                               return method.Invoke (obj, parameters);
+                       }
+                       catch (MethodAccessException mae) {
+                               throw new TargetInvocationException ("Method cannot be invoked.", mae);
+                       }
                }
 
                [MonoTODO("Not implemented")]
@@ -371,10 +386,6 @@ namespace System.Reflection.Emit {
                                throw new InvalidOperationException ("Type definition of the method is complete.");
                }
 
-               private Exception NotSupported () {
-                       return new NotSupportedException ("The invoked member is not supported on a dynamic method.");
-               }
-
                internal int AddRef (object reference) {
                        if (refs == null)
                                refs = new object [4];
@@ -401,6 +412,12 @@ namespace System.Reflection.Emit {
 
                                anon_host_module = ab.GetManifestModule ();
                        }
+
+                       public static Module AnonHostModule {
+                               get {
+                                       return anon_host_module;
+                               }
+                       }
                }
        }
 
@@ -420,7 +437,7 @@ namespace System.Reflection.Emit {
                        throw new InvalidOperationException ();
                }
 
-               public int GetToken (MemberInfo member) {
+               public int GetToken (MemberInfo member, bool create_open_instance) {
                        return m.AddRef (member);
                }
 
@@ -430,4 +447,3 @@ namespace System.Reflection.Emit {
        }
 }
 
-#endif