ExecuteUndo() public méthode

public ExecuteUndo ( ) : void
Résultat void
Exemple #1
0
		CompiledMethod CompileBlock (Class host, Undo undo, Report Report)
		{
#if STATIC
			throw new NotSupportedException ();
#else
			string current_debug_name = "eval-" + count + ".dll";
			++count;

			AssemblyDefinitionDynamic assembly;
			AssemblyBuilderAccess access;

			if (Environment.GetEnvironmentVariable ("SAVE") != null) {
				access = AssemblyBuilderAccess.RunAndSave;
				assembly = new AssemblyDefinitionDynamic (module, current_debug_name, current_debug_name);
				assembly.Importer = importer;
			} else {
#if NET_4_0
				access = AssemblyBuilderAccess.RunAndCollect;
#else
				access = AssemblyBuilderAccess.Run;
#endif
				assembly = new AssemblyDefinitionDynamic (module, current_debug_name);
			}

			assembly.Create (AppDomain.CurrentDomain, access);

			Method expression_method;
			if (host != null) {
				var base_class_imported = importer.ImportType (base_class);
				var baseclass_list = new List<FullNamedExpression> (1) {
					new TypeExpression (base_class_imported, host.Location)
				};

				host.AddBasesForPart (baseclass_list);

				host.CreateContainer ();
				host.DefineContainer ();
				host.Define ();

				expression_method = (Method) host.Members[0];
			} else {
				expression_method = null;
			}

			module.CreateContainer ();
			source_file.EnableUsingClausesRedefinition ();
			module.Define ();

			if (Report.Errors != 0){
				if (undo != null)
					undo.ExecuteUndo ();

				return null;
			}

			if (host != null){
				host.EmitContainer ();
			}
			
			module.EmitContainer ();
			if (Report.Errors != 0){
				if (undo != null)
					undo.ExecuteUndo ();
				return null;
			}

			module.CloseContainer ();
			if (host != null)
				host.CloseContainer ();

			if (access == AssemblyBuilderAccess.RunAndSave)
				assembly.Save ();

			if (host == null)
				return null;
			
			//
			// Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
			// work from MethodBuilders.   Retarded, I know.
			//
			var tt = assembly.Builder.GetType (host.TypeBuilder.Name);
			var mi = tt.GetMethod (expression_method.MemberName.Name);

			//
			// We need to then go from FieldBuilder to FieldInfo
			// or reflection gets confused (it basically gets confused, and variables override each
			// other).
			//
			foreach (var member in host.Members) {
				var field = member as Field;
				if (field == null)
					continue;

				var fi = tt.GetField (field.Name);

				Tuple<FieldSpec, FieldInfo> old;

				// If a previous value was set, nullify it, so that we do
				// not leak memory
				if (fields.TryGetValue (field.Name, out old)) {
					if (old.Item1.MemberType.IsStruct) {
						//
						// TODO: Clear fields for structs
						//
					} else {
						try {
							old.Item2.SetValue (null, null);
						} catch {
						}
					}
				}

				fields[field.Name] = Tuple.Create (field.Spec, fi);
			}
			
			return (CompiledMethod) System.Delegate.CreateDelegate (typeof (CompiledMethod), mi);
#endif
		}
Exemple #2
0
		static CompiledMethod CompileBlock (Class host, Undo undo, Report Report)
		{
			RootContext.ResolveTree ();
			if (Report.Errors != 0){
				undo.ExecuteUndo ();
				return null;
			}
			
			RootContext.PopulateTypes ();

			if (Report.Errors != 0){
				undo.ExecuteUndo ();
				return null;
			}

			TypeBuilder tb = null;
			MethodBuilder mb = null;
				
			if (host != null){
				tb = host.TypeBuilder;
				mb = null;
				foreach (MemberCore member in host.Methods){
					if (member.Name != "Host")
						continue;
					
					MethodOrOperator method = (MethodOrOperator) member;
					mb = method.MethodBuilder;
					break;
				}

				if (mb == null)
					throw new Exception ("Internal error: did not find the method builder for the generated method");
			}
			
			RootContext.EmitCode ();
			if (Report.Errors != 0){
				undo.ExecuteUndo ();
				return null;
			}
			
			RootContext.CloseTypes (ctx);

			if (Environment.GetEnvironmentVariable ("SAVE") != null)
				CodeGen.Save (current_debug_name, false, Report);

			if (host == null)
				return null;
			
			//
			// Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
			// work from MethodBuilders.   Retarded, I know.
			//
			var tt = CodeGen.Assembly.Builder.GetType (tb.Name);
			MethodInfo mi = tt.GetMethod (mb.Name);
			
			// Pull the FieldInfos from the type, and keep track of them
			foreach (Field field in queued_fields){
				FieldInfo fi = tt.GetField (field.Name);

				Tuple<FieldSpec, FieldInfo> old;
				
				// If a previous value was set, nullify it, so that we do
				// not leak memory
				if (fields.TryGetValue (field.Name, out old)) {
					if (old.Item1.MemberType.IsStruct) {
						//
						// TODO: Clear fields for structs
						//
					} else {
						try {
							old.Item2.SetValue (null, null);
						} catch {
						}
					}

					fields [field.Name] = Tuple.Create (old.Item1, fi);
				} else {
					fields.Add (field.Name, Tuple.Create (field.Spec, fi));
				}
			}
			//types.Add (tb);

			queued_fields.Clear ();
			
			return (CompiledMethod) System.Delegate.CreateDelegate (typeof (CompiledMethod), mi);
		}
Exemple #3
0
		CompiledMethod CompileBlock (Class host, Undo undo, Report Report)
		{
#if STATIC
			throw new NotSupportedException ();
#else
			string current_debug_name = "eval-" + count + ".dll";
			++count;

			AssemblyDefinitionDynamic assembly;
			AssemblyBuilderAccess access;

			if (Environment.GetEnvironmentVariable ("SAVE") != null) {
				access = AssemblyBuilderAccess.RunAndSave;
				assembly = new AssemblyDefinitionDynamic (module, current_debug_name, current_debug_name);
				assembly.Importer = importer;
			} else {
#if NET_4_0
				access = AssemblyBuilderAccess.RunAndCollect;
#else
				access = AssemblyBuilderAccess.Run;
#endif
				assembly = new AssemblyDefinitionDynamic (module, current_debug_name);
			}

			assembly.Create (AppDomain.CurrentDomain, access);

			Method expression_method;
			if (host != null) {
				var base_class_imported = importer.ImportType (base_class);
				var baseclass_list = new List<FullNamedExpression> (1) {
					new TypeExpression (base_class_imported, host.Location)
				};

				host.SetBaseTypes (baseclass_list);

				expression_method = (Method) host.Members[0];

				if ((expression_method.ModFlags & Modifiers.ASYNC) != 0) {
					//
					// Host method is async. When WaitOnTask is set we wrap it with wait
					//
					// void AsyncWait (ref object $retval) {
					//	$retval = Host();
					//	((Task)$retval).Wait();  // When WaitOnTask is set
					// }
					//
					var p = new ParametersCompiled (
						new Parameter (new TypeExpression (module.Compiler.BuiltinTypes.Object, Location.Null), "$retval", Parameter.Modifier.REF, null, Location.Null)
					);

					var method = new Method(host, new TypeExpression(module.Compiler.BuiltinTypes.Void, Location.Null),
						Modifiers.PUBLIC | Modifiers.STATIC, new MemberName("AsyncWait"), p, null);

					method.Block = new ToplevelBlock(method.Compiler, p, Location.Null);
					method.Block.AddStatement(new StatementExpression (new SimpleAssign(
						new SimpleName(p [0].Name, Location.Null),
						new Invocation(new SimpleName(expression_method.MemberName.Name, Location.Null), new Arguments(0)),
						Location.Null), Location.Null));

					if (WaitOnTask) {
						var task = new Cast (expression_method.TypeExpression, new SimpleName (p [0].Name, Location.Null), Location.Null);

						method.Block.AddStatement (new StatementExpression (new Invocation (
								new MemberAccess (task, "Wait", Location.Null),
							new Arguments (0)), Location.Null));
					}

					host.AddMember(method);

					expression_method = method;
				}

				host.CreateContainer();
				host.DefineContainer();
				host.Define();

			} else {
				expression_method = null;
			}

			module.CreateContainer ();

			// Disable module and source file re-definition checks
			module.EnableRedefinition ();
			source_file.EnableRedefinition ();

			module.Define ();

			if (Report.Errors != 0){
				if (undo != null)
					undo.ExecuteUndo ();

				return null;
			}

			if (host != null){
				host.PrepareEmit ();
				host.EmitContainer ();
			}
			
			module.EmitContainer ();

			if (Report.Errors != 0){
				if (undo != null)
					undo.ExecuteUndo ();
				return null;
			}

			module.CloseContainer ();
			if (host != null)
				host.CloseContainer ();

			if (access == AssemblyBuilderAccess.RunAndSave)
				assembly.Save ();

			if (host == null)
				return null;
			
			//
			// Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
			// work from MethodBuilders.   Retarded, I know.
			//
			var tt = assembly.Builder.GetType (host.TypeBuilder.Name);
			var mi = tt.GetMethod (expression_method.MemberName.Name);

			//
			// We need to then go from FieldBuilder to FieldInfo
			// or reflection gets confused (it basically gets confused, and variables override each
			// other).
			//
			foreach (var member in host.Members) {
				var field = member as Field;
				if (field == null)
					continue;

				var fi = tt.GetField (field.Name);

				Tuple<FieldSpec, FieldInfo> old;

				// If a previous value was set, nullify it, so that we do
				// not leak memory
				if (fields.TryGetValue (field.Name, out old)) {
					if (old.Item1.MemberType.IsStruct) {
						//
						// TODO: Clear fields for structs
						//
					} else {
						try {
							old.Item2.SetValue (null, null);
						} catch {
						}
					}
				}

				fields[field.Name] = Tuple.Create (field.Spec, fi);
			}
			
			return (CompiledMethod) System.Delegate.CreateDelegate (typeof (CompiledMethod), mi);
#endif
		}
Exemple #4
0
		static CompiledMethod CompileBlock (Class host, Undo undo, Report Report)
		{
			AssemblyDefinitionDynamic assembly;
			AssemblyBuilderAccess access;

			if (Environment.GetEnvironmentVariable ("SAVE") != null) {
				access = AssemblyBuilderAccess.RunAndSave;
				assembly = new AssemblyDefinitionDynamic (RootContext.ToplevelTypes, current_debug_name, current_debug_name);
				assembly.Importer = loader.Importer;
			} else {
#if NET_4_0
				access = AssemblyBuilderAccess.RunAndCollect;
#else
				access = AssemblyBuilderAccess.Run;
#endif
				assembly = new AssemblyDefinitionDynamic (RootContext.ToplevelTypes, current_debug_name);
			}

			assembly.Create (AppDomain.CurrentDomain, access);

			if (host != null) {
				host.CreateType ();
				host.Define ();
			}

			RootContext.ToplevelTypes.CreateType ();
			RootContext.ToplevelTypes.Define ();

			if (Report.Errors != 0){
				undo.ExecuteUndo ();
				return null;
			}

			TypeBuilder tb = null;
			MethodBuilder mb = null;
				
			if (host != null){
				tb = host.TypeBuilder;
				mb = null;
				foreach (MemberCore member in host.Methods){
					if (member.Name != "Host")
						continue;
					
					MethodOrOperator method = (MethodOrOperator) member;
					mb = method.MethodBuilder;
					break;
				}

				if (mb == null)
					throw new Exception ("Internal error: did not find the method builder for the generated method");

				host.EmitType ();
			}
			
			RootContext.ToplevelTypes.Emit ();
			if (Report.Errors != 0){
				undo.ExecuteUndo ();
				return null;
			}

			RootContext.ToplevelTypes.CloseType ();
			if (host != null)
				host.CloseType ();

			if (access == AssemblyBuilderAccess.RunAndSave)
				assembly.Save ();

			if (host == null)
				return null;
			
			//
			// Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
			// work from MethodBuilders.   Retarded, I know.
			//
			var tt = assembly.Builder.GetType (tb.Name);
			MethodInfo mi = tt.GetMethod (mb.Name);
			
			// Pull the FieldInfos from the type, and keep track of them
			foreach (Field field in queued_fields){
				FieldInfo fi = tt.GetField (field.Name);

				Tuple<FieldSpec, FieldInfo> old;
				
				// If a previous value was set, nullify it, so that we do
				// not leak memory
				if (fields.TryGetValue (field.Name, out old)) {
					if (old.Item1.MemberType.IsStruct) {
						//
						// TODO: Clear fields for structs
						//
					} else {
						try {
							old.Item2.SetValue (null, null);
						} catch {
						}
					}

					fields [field.Name] = Tuple.Create (field.Spec, fi);
				} else {
					fields.Add (field.Name, Tuple.Create (field.Spec, fi));
				}
			}
			queued_fields.Clear ();
			
			return (CompiledMethod) System.Delegate.CreateDelegate (typeof (CompiledMethod), mi);
		}
Exemple #5
0
        CompiledMethod CompileBlock(Class host, Undo undo, Report Report)
        {
            string current_debug_name = "eval-" + count + ".dll";

            ++count;
#if STATIC
            throw new NotSupportedException();
#else
            AssemblyDefinitionDynamic assembly;
            AssemblyBuilderAccess     access;

            if (Environment.GetEnvironmentVariable("SAVE") != null)
            {
                access            = AssemblyBuilderAccess.RunAndSave;
                assembly          = new AssemblyDefinitionDynamic(module, current_debug_name, current_debug_name);
                assembly.Importer = importer;
            }
            else
            {
#if NET_4_0
                access = AssemblyBuilderAccess.RunAndCollect;
#else
                access = AssemblyBuilderAccess.Run;
#endif
                assembly = new AssemblyDefinitionDynamic(module, current_debug_name);
            }

            assembly.Create(AppDomain.CurrentDomain, access);

            Method expression_method;
            if (host != null)
            {
                var base_class_imported = importer.ImportType(base_class);
                var baseclass_list      = new List <FullNamedExpression> (1)
                {
                    new TypeExpression(base_class_imported, host.Location)
                };

                host.AddBasesForPart(host, baseclass_list);

                host.CreateType();
                host.DefineType();
                host.Define();

                expression_method = (Method)host.Methods[0];
            }
            else
            {
                expression_method = null;
            }

            module.CreateType();
            module.Define();

            if (Report.Errors != 0)
            {
                if (undo != null)
                {
                    undo.ExecuteUndo();
                }

                return(null);
            }

            if (host != null)
            {
                host.EmitType();
            }

            module.Emit();
            if (Report.Errors != 0)
            {
                if (undo != null)
                {
                    undo.ExecuteUndo();
                }
                return(null);
            }

            module.CloseType();
            if (host != null)
            {
                host.CloseType();
            }

            if (access == AssemblyBuilderAccess.RunAndSave)
            {
                assembly.Save();
            }

            if (host == null)
            {
                return(null);
            }

            //
            // Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
            // work from MethodBuilders.   Retarded, I know.
            //
            var tt = assembly.Builder.GetType(host.TypeBuilder.Name);
            var mi = tt.GetMethod(expression_method.Name);

            if (host.Fields != null)
            {
                //
                // We need to then go from FieldBuilder to FieldInfo
                // or reflection gets confused (it basically gets confused, and variables override each
                // other).
                //
                foreach (Field field in host.Fields)
                {
                    var fi = tt.GetField(field.Name);

                    Tuple <FieldSpec, FieldInfo> old;

                    // If a previous value was set, nullify it, so that we do
                    // not leak memory
                    if (fields.TryGetValue(field.Name, out old))
                    {
                        if (old.Item1.MemberType.IsStruct)
                        {
                            //
                            // TODO: Clear fields for structs
                            //
                        }
                        else
                        {
                            try {
                                old.Item2.SetValue(null, null);
                            } catch {
                            }
                        }
                    }

                    fields[field.Name] = Tuple.Create(field.Spec, fi);
                }
            }

            return((CompiledMethod)System.Delegate.CreateDelegate(typeof(CompiledMethod), mi));
#endif
        }
Exemple #6
0
        CompiledMethod CompileBlock(Class host, Undo undo, Report Report)
        {
#if STATIC
            throw new NotSupportedException();
#else
            string current_debug_name = "eval-" + count + ".dll";
            ++count;

            AssemblyDefinitionDynamic assembly;
            AssemblyBuilderAccess     access;

            if (Environment.GetEnvironmentVariable("SAVE") != null)
            {
                access            = AssemblyBuilderAccess.RunAndSave;
                assembly          = new AssemblyDefinitionDynamic(module, current_debug_name, current_debug_name);
                assembly.Importer = importer;
            }
            else
            {
                access   = AssemblyBuilderAccess.RunAndCollect;
                assembly = new AssemblyDefinitionDynamic(module, current_debug_name);
            }

            assembly.Create(AppDomain.CurrentDomain, access);

            Method expression_method;
            if (host != null)
            {
                var base_class_imported = importer.ImportType(base_class);
                var baseclass_list      = new List <FullNamedExpression> (1)
                {
                    new TypeExpression(base_class_imported, host.Location)
                };

                host.SetBaseTypes(baseclass_list);

                expression_method = (Method)host.Members[0];

                if ((expression_method.ModFlags & Modifiers.ASYNC) != 0)
                {
                    //
                    // Host method is async. When WaitOnTask is set we wrap it with wait
                    //
                    // void AsyncWait (ref object $retval) {
                    //	$retval = Host();
                    //	((Task)$retval).Wait();  // When WaitOnTask is set
                    // }
                    //
                    var p = new ParametersCompiled(
                        new Parameter(new TypeExpression(module.Compiler.BuiltinTypes.Object, Location.Null), "$retval", Parameter.Modifier.REF, null, Location.Null)
                        );

                    var method = new Method(host, new TypeExpression(module.Compiler.BuiltinTypes.Void, Location.Null),
                                            Modifiers.PUBLIC | Modifiers.STATIC, new MemberName("AsyncWait"), p, null);

                    method.Block = new ToplevelBlock(method.Compiler, p, Location.Null);
                    method.Block.AddStatement(new StatementExpression(new SimpleAssign(
                                                                          new SimpleName(p [0].Name, Location.Null),
                                                                          new Invocation(new SimpleName(expression_method.MemberName.Name, Location.Null), new Arguments(0)),
                                                                          Location.Null), Location.Null));

                    if (WaitOnTask)
                    {
                        var task = new Cast(expression_method.TypeExpression, new SimpleName(p [0].Name, Location.Null), Location.Null);

                        method.Block.AddStatement(new StatementExpression(new Invocation(
                                                                              new MemberAccess(task, "Wait", Location.Null),
                                                                              new Arguments(0)), Location.Null));
                    }

                    host.AddMember(method);

                    expression_method = method;
                }

                host.CreateContainer();
                host.DefineContainer();
                host.Define();
            }
            else
            {
                expression_method = null;
            }

            module.CreateContainer();

            // Disable module and source file re-definition checks
            module.EnableRedefinition();
            source_file.EnableRedefinition();

            module.Define();

            if (Report.Errors != 0)
            {
                if (undo != null)
                {
                    undo.ExecuteUndo();
                }

                return(null);
            }

            if (host != null)
            {
                host.PrepareEmit();
                host.EmitContainer();
            }

            module.EmitContainer();

            if (Report.Errors != 0)
            {
                if (undo != null)
                {
                    undo.ExecuteUndo();
                }
                return(null);
            }

            module.CloseContainerEarlyForReflectionEmit();
            module.CloseContainer();
            if (host != null)
            {
                host.CloseContainer();
            }

            if (access == AssemblyBuilderAccess.RunAndSave)
            {
                assembly.Save();
            }

            if (host == null)
            {
                return(null);
            }

            //
            // Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
            // work from MethodBuilders.   Retarded, I know.
            //
            var tt = assembly.Builder.GetType(host.TypeBuilder.Name);
            var mi = tt.GetMethod(expression_method.MemberName.Name);

            //
            // We need to then go from FieldBuilder to FieldInfo
            // or reflection gets confused (it basically gets confused, and variables override each
            // other).
            //
            foreach (var member in host.Members)
            {
                var field = member as Field;
                if (field == null)
                {
                    continue;
                }

                var fi = tt.GetField(field.Name);

                Tuple <FieldSpec, FieldInfo> old;

                // If a previous value was set, nullify it, so that we do
                // not leak memory
                if (fields.TryGetValue(field.Name, out old))
                {
                    if (old.Item1.MemberType.IsStruct)
                    {
                        //
                        // TODO: Clear fields for structs
                        //
                    }
                    else
                    {
                        try {
                            old.Item2.SetValue(null, null);
                        } catch {
                        }
                    }
                }

                fields[field.Name] = Tuple.Create(field.Spec, fi);
            }

            return((CompiledMethod)System.Delegate.CreateDelegate(typeof(CompiledMethod), mi));
#endif
        }
Exemple #7
0
        static CompiledMethod CompileBlock(Class host, Undo undo, Report Report)
        {
            RootContext.ResolveTree();
            if (Report.Errors != 0)
            {
                undo.ExecuteUndo();
                return(null);
            }

            RootContext.PopulateTypes();

            if (Report.Errors != 0)
            {
                undo.ExecuteUndo();
                return(null);
            }

            TypeBuilder   tb = null;
            MethodBuilder mb = null;

            if (host != null)
            {
                tb = host.TypeBuilder;
                mb = null;
                foreach (MemberCore member in host.Methods)
                {
                    if (member.Name != "Host")
                    {
                        continue;
                    }

                    MethodOrOperator method = (MethodOrOperator)member;
                    mb = method.MethodBuilder;
                    break;
                }

                if (mb == null)
                {
                    throw new Exception("Internal error: did not find the method builder for the generated method");
                }
            }

            RootContext.EmitCode();
            if (Report.Errors != 0)
            {
                return(null);
            }

            RootContext.CloseTypes();

            if (Environment.GetEnvironmentVariable("SAVE") != null)
            {
                CodeGen.Save(current_debug_name, false, Report);
            }

            if (host == null)
            {
                return(null);
            }

            //
            // Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
            // work from MethodBuilders.   Retarded, I know.
            //
            Type       tt = CodeGen.Assembly.Builder.GetType(tb.Name);
            MethodInfo mi = tt.GetMethod(mb.Name);

            // Pull the FieldInfos from the type, and keep track of them
            foreach (Field field in queued_fields)
            {
                FieldInfo fi = tt.GetField(field.Name);

                FieldInfo old = (FieldInfo)fields [field.Name];

                // If a previous value was set, nullify it, so that we do
                // not leak memory
                if (old != null)
                {
                    if (TypeManager.IsStruct(old.FieldType))
                    {
                        //
                        // TODO: Clear fields for structs
                        //
                    }
                    else
                    {
                        try {
                            old.SetValue(null, null);
                        } catch {
                        }
                    }
                }

                fields [field.Name] = fi;
            }
            //types.Add (tb);

            queued_fields.Clear();

            return((CompiledMethod)System.Delegate.CreateDelegate(typeof(CompiledMethod), mi));
        }
Exemple #8
0
        static CompiledMethod CompileBlock(Class host, Undo undo, Report Report)
        {
            AssemblyDefinitionDynamic assembly;

            if (Environment.GetEnvironmentVariable("SAVE") != null)
            {
                assembly          = new AssemblyDefinitionDynamic(RootContext.ToplevelTypes, current_debug_name, current_debug_name);
                assembly.Importer = loader.Importer;
            }
            else
            {
                assembly = new AssemblyDefinitionDynamic(RootContext.ToplevelTypes, current_debug_name);
            }

            assembly.Create(AppDomain.CurrentDomain, AssemblyBuilderAccess.RunAndSave);
            RootContext.ToplevelTypes.CreateType();
            RootContext.ToplevelTypes.Define();

            if (Report.Errors != 0)
            {
                undo.ExecuteUndo();
                return(null);
            }

            TypeBuilder   tb = null;
            MethodBuilder mb = null;

            if (host != null)
            {
                tb = host.TypeBuilder;
                mb = null;
                foreach (MemberCore member in host.Methods)
                {
                    if (member.Name != "Host")
                    {
                        continue;
                    }

                    MethodOrOperator method = (MethodOrOperator)member;
                    mb = method.MethodBuilder;
                    break;
                }

                if (mb == null)
                {
                    throw new Exception("Internal error: did not find the method builder for the generated method");
                }
            }

            RootContext.ToplevelTypes.Emit();
            if (Report.Errors != 0)
            {
                undo.ExecuteUndo();
                return(null);
            }

            RootContext.ToplevelTypes.CloseType();

            if (Environment.GetEnvironmentVariable("SAVE") != null)
            {
                assembly.Save();
            }

            if (host == null)
            {
                return(null);
            }

            //
            // Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
            // work from MethodBuilders.   Retarded, I know.
            //
            var        tt = assembly.Builder.GetType(tb.Name);
            MethodInfo mi = tt.GetMethod(mb.Name);

            // Pull the FieldInfos from the type, and keep track of them
            foreach (Field field in queued_fields)
            {
                FieldInfo fi = tt.GetField(field.Name);

                Tuple <FieldSpec, FieldInfo> old;

                // If a previous value was set, nullify it, so that we do
                // not leak memory
                if (fields.TryGetValue(field.Name, out old))
                {
                    if (old.Item1.MemberType.IsStruct)
                    {
                        //
                        // TODO: Clear fields for structs
                        //
                    }
                    else
                    {
                        try {
                            old.Item2.SetValue(null, null);
                        } catch {
                        }
                    }

                    fields [field.Name] = Tuple.Create(field.Spec, fi);
                }
                else
                {
                    fields.Add(field.Name, Tuple.Create(field.Spec, fi));
                }
            }
            //types.Add (tb);

            queued_fields.Clear();

            return((CompiledMethod)System.Delegate.CreateDelegate(typeof(CompiledMethod), mi));
        }