Author: jbevain
Date: 2008-01-30 07:51:26 -0500 (Wed, 30 Jan 2008)
New Revision: 94369

Added:
   trunk/mcs/class/System.Core/System.Linq.Expressions/EmitContext.cs
Modified:
   trunk/mcs/class/System.Core/System.Core.dll.sources
   trunk/mcs/class/System.Core/System.Linq.Expressions/ChangeLog
   trunk/mcs/class/System.Core/System.Linq.Expressions/LambdaExpression.cs
Log:
2008-01-30  Jb Evain  <[EMAIL PROTECTED]>

        * LambdaExpression.cs, EmitContext.cs: small refactoring.
        Extract the different EmitContexts to their own file.


Modified: trunk/mcs/class/System.Core/System.Core.dll.sources
===================================================================
--- trunk/mcs/class/System.Core/System.Core.dll.sources 2008-01-30 12:43:36 UTC 
(rev 94368)
+++ trunk/mcs/class/System.Core/System.Core.dll.sources 2008-01-30 12:51:26 UTC 
(rev 94369)
@@ -32,6 +32,7 @@
 System.Linq.Expressions/ConditionalExpression.cs
 System.Linq.Expressions/ConstantExpression.cs
 System.Linq.Expressions/ElementInit.cs
+System.Linq.Expressions/EmitContext.cs
 System.Linq.Expressions/Expression.cs
 System.Linq.Expressions/Expression_T.cs
 System.Linq.Expressions/ExpressionPrinter.cs

Modified: trunk/mcs/class/System.Core/System.Linq.Expressions/ChangeLog
===================================================================
--- trunk/mcs/class/System.Core/System.Linq.Expressions/ChangeLog       
2008-01-30 12:43:36 UTC (rev 94368)
+++ trunk/mcs/class/System.Core/System.Linq.Expressions/ChangeLog       
2008-01-30 12:51:26 UTC (rev 94369)
@@ -1,3 +1,8 @@
+2008-01-30  Jb Evain  <[EMAIL PROTECTED]>
+
+       * LambdaExpression.cs, EmitContext.cs: small refactoring.
+       Extract the different EmitContexts to their own file.
+
 2008-01-29  Jb Evain  <[EMAIL PROTECTED]>
 
        * MethodCallExpression.cs, Expression.cs: complete Calls.

Added: trunk/mcs/class/System.Core/System.Linq.Expressions/EmitContext.cs
===================================================================
--- trunk/mcs/class/System.Core/System.Linq.Expressions/EmitContext.cs  
2008-01-30 12:43:36 UTC (rev 94368)
+++ trunk/mcs/class/System.Core/System.Linq.Expressions/EmitContext.cs  
2008-01-30 12:51:26 UTC (rev 94369)
@@ -0,0 +1,165 @@
+//
+// EmitContext.cs
+//
+// Author:
+//   Miguel de Icaza ([EMAIL PROTECTED])
+//   Jb Evain ([EMAIL PROTECTED])
+//
+// (C) 2008 Novell, Inc. (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using System.Reflection.Emit;
+
+namespace System.Linq.Expressions {
+
+       abstract class EmitContext {
+
+               public LambdaExpression owner;
+               public Type [] param_types;
+               public ILGenerator ig;
+
+               public abstract MethodInfo Method { get; }
+
+               static object mlock = new object ();
+               static int method_count;
+
+               protected EmitContext (LambdaExpression lambda)
+               {
+                       this.owner = lambda;
+
+                       param_types = owner.Parameters.Select (p => 
p.Type).ToArray ();
+               }
+
+               public static EmitContext Create (LambdaExpression lambda)
+               {
+                       if (Environment.GetEnvironmentVariable ("LINQ_DBG") != 
null)
+                               return new DebugEmitContext (lambda);
+
+                       return new DynamicEmitContext (lambda);
+               }
+
+               public int GetParameterPosition (ParameterExpression p)
+               {
+                       int position = owner.Parameters.IndexOf (p);
+                       if (position == -1)
+                               throw new InvalidOperationException ("Parameter 
not in scope");
+
+                       return position;
+               }
+
+               public abstract Delegate CreateDelegate ();
+
+               public abstract void Emit ();
+
+               protected static string GenerateName ()
+               {
+                       lock (mlock) {
+                               return "lambda_method-" + (method_count++);
+                       }
+               }
+       }
+
+       class DynamicEmitContext : EmitContext {
+
+               DynamicMethod method;
+
+               public override MethodInfo Method {
+                       get { return method; }
+               }
+
+               public DynamicEmitContext (LambdaExpression lambda)
+                       : base (lambda)
+               {
+                       //
+                       // We probably want to use the 3.5 new API calls to 
associate
+                       // the method with the "sandboxed" Assembly, instead am 
currently
+                       // dumping these types in this class
+                       //
+                       Type owner_of_code = typeof (EmitContext);
+
+                       //
+                       // FIXME: Need to force this to be verifiable, see:
+                       // https://bugzilla.novell.com/show_bug.cgi?id=355005
+                       //
+                       method = new DynamicMethod ("lambda_method", 
lambda.Body.Type, param_types, owner_of_code);
+                       ig = method.GetILGenerator ();
+               }
+
+               public override Delegate CreateDelegate ()
+               {
+                       return method.CreateDelegate (owner.Type);
+               }
+
+               public override void Emit ()
+               {
+                       owner.Emit (this);
+               }
+       }
+
+       class DebugEmitContext : EmitContext {
+
+               AssemblyBuilder assembly;
+               TypeBuilder type;
+               MethodBuilder method;
+
+               public override MethodInfo Method {
+                       get { return method; }
+               }
+
+               public DebugEmitContext (LambdaExpression lambda)
+                       : base (lambda)
+               {
+                       var name = GenerateName ();
+                       var file_name = name + ".dll";
+
+                       assembly = 
AppDomain.CurrentDomain.DefineDynamicAssembly (
+                               new AssemblyName (file_name), 
AssemblyBuilderAccess.RunAndSave, Path.GetTempPath ());
+
+                       type = assembly.DefineDynamicModule (file_name, 
file_name).DefineType ("Linq", TypeAttributes.Public);
+
+                       method = type.DefineMethod (name, 
MethodAttributes.Public | MethodAttributes.Static, owner.Body.Type, 
param_types);
+                       ig = method.GetILGenerator ();
+               }
+
+               public override Delegate CreateDelegate ()
+               {
+                       return Delegate.CreateDelegate (owner.Type, GetMethod 
());
+               }
+
+               MethodInfo GetMethod ()
+               {
+                       return type.GetMethod (method.Name, BindingFlags.Static 
| BindingFlags.Public);
+               }
+
+               public override void Emit ()
+               {
+                       owner.Emit (this);
+
+                       type.CreateType ();
+                       assembly.Save (assembly.GetName ().FullName);
+               }
+       }
+}


Property changes on: 
trunk/mcs/class/System.Core/System.Linq.Expressions/EmitContext.cs
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: 
trunk/mcs/class/System.Core/System.Linq.Expressions/LambdaExpression.cs
===================================================================
--- trunk/mcs/class/System.Core/System.Linq.Expressions/LambdaExpression.cs     
2008-01-30 12:43:36 UTC (rev 94368)
+++ trunk/mcs/class/System.Core/System.Linq.Expressions/LambdaExpression.cs     
2008-01-30 12:51:26 UTC (rev 94369)
@@ -32,77 +32,14 @@
 using System.Collections.Generic;
 using System.Reflection;
 using System.Reflection.Emit;
-using System.Threading;
 
 namespace System.Linq.Expressions {
 
-       internal class EmitContext {
-               internal LambdaExpression Owner;
-               internal Type [] ParamTypes;
-               internal DynamicMethod Method;
-               internal ILGenerator ig;
-
-               static object mlock = new object ();
-               internal static int method_count;
-               static string GenName ()
-               {
-                       lock (mlock){
-                               return "<LINQ-" + method_count++ + ">";
-                       }
-               }
-
-               public EmitContext (LambdaExpression owner)
-               {
-                       Owner = owner;
-
-                       ParamTypes = new Type [Owner.Parameters.Count];
-                       for (int i = 0; i < Owner.Parameters.Count; i++)
-                               ParamTypes [i] = Owner.Parameters [i].Type;
-
-                       //
-                       // We probably want to use the 3.5 new API calls to 
associate
-                       // the method with the "sandboxed" Assembly, instead am 
currently
-                       // dumping these types in this class
-                       //
-                       Type owner_of_code = typeof (EmitContext);
-
-
-                       //
-                       // FIXME: Need to force this to be verifiable, see:
-                       // https://bugzilla.novell.com/show_bug.cgi?id=355005
-                       //
-                       string name = GenName ();
-                       Method = new DynamicMethod (name, Owner.Body.Type, 
ParamTypes, owner_of_code);
-
-                       ig = Method.GetILGenerator ();
-               }
-
-               internal Delegate CreateDelegate ()
-               {
-                       return Method.CreateDelegate (Owner.Type);
-               }
-
-               internal int GetParameterPosition (ParameterExpression p)
-               {
-                       int position = Owner.Parameters.IndexOf (p);
-                       if (position == -1)
-                               throw new InvalidOperationException ("Parameter 
not in scope");
-
-                       return position;
-               }
-       }
-
        public class LambdaExpression : Expression {
 
-               //
-               // LambdaExpression parameters
-               //
                Expression body;
                ReadOnlyCollection<ParameterExpression> parameters;
 
-               // This is set during compilation
-               Delegate lambda_delegate;
-
                public Expression Body {
                        get { return body; }
                }
@@ -154,31 +91,9 @@
 
                public Delegate Compile ()
                {
-                       if (lambda_delegate != null)
-                               return lambda_delegate;
-
-                       var ec = new EmitContext (this);
-
-                       Emit (ec);
-
-                       if (Environment.GetEnvironmentVariable ("LINQ_DBG") != 
null){
-                               string fname = "linq" + 
(EmitContext.method_count-1) + ".dll";
-                               AssemblyBuilder ab = Thread.GetDomain 
().DefineDynamicAssembly (
-                                       new AssemblyName (fname), 
AssemblyBuilderAccess.RunAndSave, "/tmp");
-
-                               ModuleBuilder b = ab.DefineDynamicModule 
(fname, fname);
-                               TypeBuilder tb = b.DefineType ("LINQ", 
TypeAttributes.Public);
-                               MethodBuilder mb = tb.DefineMethod 
("GeneratedMethod", MethodAttributes.Static, Type, ec.ParamTypes);
-                               ec.ig = mb.GetILGenerator ();
-
-                               Emit (ec);
-
-                               tb.CreateType ();
-                               ab.Save (fname);
-                       }
-
-                       lambda_delegate = ec.CreateDelegate ();
-                       return lambda_delegate;
+                       var context = EmitContext.Create (this);
+                       context.Emit ();
+                       return context.CreateDelegate ();
                }
        }
 }

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to