예제 #1
0
        public static DynamicContext Create()
        {
            if (dc != null)
            {
                return(dc);
            }

            lock (compiler_initializer) {
                if (dc != null)
                {
                    return(dc);
                }

                var settings = new Compiler.CompilerSettings()
                {
                    WarningLevel = 0
                };

                var cc = new Compiler.CompilerContext(settings, ErrorPrinter.Instance)
                {
                    IsRuntimeBinder = true
                };

                //
                // Any later loaded assemblies are handled internally by GetAssemblyDefinition
                // domain.AssemblyLoad cannot be used as that would be too destructive as we
                // would hold all loaded assemblies even if they can be never visited
                //
                // TODO: Remove this code and rely on GetAssemblyDefinition only
                //
                var module = new Compiler.ModuleContainer(cc);
                module.HasTypesFullyDefined = true;

                // Setup fake assembly, it's used mostly to simplify checks like friend-access
                var temp = new Compiler.AssemblyDefinitionDynamic(module, "dynamic");
                module.SetDeclaringAssembly(temp);

                var importer = new Compiler.ReflectionImporter(module, cc.BuiltinTypes)
                {
                    IgnorePrivateMembers         = false,
                    IgnoreCompilerGeneratedField = false
                };

                // Import all currently loaded assemblies
                // TODO: Rewrite this to populate type cache on-demand, that should greatly
                // reduce our start-up cost
                var domain = AppDomain.CurrentDomain;
                foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
                {
                    importer.ImportAssembly(a, module.GlobalRootNamespace);
                }

                cc.BuiltinTypes.CheckDefinitions(module);
                module.InitializePredefinedTypes();

                dc = new DynamicContext(module, importer);
            }

            return(dc);
        }
예제 #2
0
		public static DynamicContext Create ()
		{
			if (dc != null)
				return dc;

			lock (compiler_initializer) {
				if (dc != null)
					return dc;

				var reporter = new Compiler.Report (ErrorPrinter.Instance) {
					WarningLevel = 0
				};

				var cc = new Compiler.CompilerContext (reporter) {
					IsRuntimeBinder = true
				};

				//IList<Compiler.PredefinedTypeSpec> core_types = null;
				//// HACK: To avoid re-initializing static TypeManager types, like string_type
				//if (!Compiler.RootContext.EvalMode) {
				//    core_types = Compiler.TypeManager.InitCoreTypes ();
				//}

				//
				// Any later loaded assemblies are handled internally by GetAssemblyDefinition
				// domain.AssemblyLoad cannot be used as that would be too destructive as we
				// would hold all loaded assemblies even if they can be never visited
				//
				// TODO: Remove this code and rely on GetAssemblyDefinition only
				//
				var module = new Compiler.ModuleContainer (cc);
				var temp = new Compiler.AssemblyDefinitionDynamic (module, "dynamic");
				module.SetDeclaringAssembly (temp);

				// Import all currently loaded assemblies
				var domain = AppDomain.CurrentDomain;

				temp.Create (domain, System.Reflection.Emit.AssemblyBuilderAccess.Run);
				var importer = new Compiler.ReflectionImporter (cc.BuildinTypes) {
					IgnorePrivateMembers = false
				};

				Compiler.RootContext.ToplevelTypes = module;

				foreach (var a in AppDomain.CurrentDomain.GetAssemblies ()) {
					importer.ImportAssembly (a, module.GlobalRootNamespace);
				}

				if (!Compiler.RootContext.EvalMode) {
					cc.BuildinTypes.CheckDefinitions (module);
					module.InitializePredefinedTypes ();
				}

				dc = new DynamicContext (module, importer);
			}

			return dc;
		}
예제 #3
0
        public static DynamicContext Create()
        {
            if (dc != null)
            {
                return(dc);
            }

            lock (compiler_initializer) {
                if (dc != null)
                {
                    return(dc);
                }

                var reporter = new Compiler.Report(ErrorPrinter.Instance)
                {
                    WarningLevel = 0
                };

                var settings = new Compiler.CompilerSettings();

                var cc = new Compiler.CompilerContext(settings, reporter)
                {
                    IsRuntimeBinder = true
                };

                //IList<Compiler.PredefinedTypeSpec> core_types = null;
                //// HACK: To avoid re-initializing static TypeManager types, like string_type
                //if (!Compiler.RootContext.EvalMode) {
                //    core_types = Compiler.TypeManager.InitCoreTypes ();
                //}

                //
                // Any later loaded assemblies are handled internally by GetAssemblyDefinition
                // domain.AssemblyLoad cannot be used as that would be too destructive as we
                // would hold all loaded assemblies even if they can be never visited
                //
                // TODO: Remove this code and rely on GetAssemblyDefinition only
                //
                var module = new Compiler.ModuleContainer(cc);
                module.HasTypesFullyDefined = true;
                var temp = new Compiler.AssemblyDefinitionDynamic(module, "dynamic");
                module.SetDeclaringAssembly(temp);

                // Import all currently loaded assemblies
                var domain = AppDomain.CurrentDomain;

                temp.Create(domain, System.Reflection.Emit.AssemblyBuilderAccess.Run);
                var importer = new Compiler.ReflectionImporter(module, cc.BuiltinTypes)
                {
                    IgnorePrivateMembers = false
                };

                foreach (var a in AppDomain.CurrentDomain.GetAssemblies())
                {
                    importer.ImportAssembly(a, module.GlobalRootNamespace);
                }

                cc.BuiltinTypes.CheckDefinitions(module);
                module.InitializePredefinedTypes();

                dc = new DynamicContext(module, importer);
            }

            return(dc);
        }
예제 #4
0
        //
        // Main compilation method
        //
        public bool Compile()
        {
            var settings = ctx.Settings;

            //
            // If we are an exe, require a source file for the entry point or
            // if there is nothing to put in the assembly, and we are not a library
            //
            if (settings.FirstSourceFile == null &&
                ((settings.Target == Target.Exe || settings.Target == Target.WinExe || settings.Target == Target.Module) ||
                 settings.Resources == null))
            {
                Report.Error(2008, "No files to compile were specified");
                return(false);
            }

            if (settings.Platform == Platform.AnyCPU32Preferred && (settings.Target == Target.Library || settings.Target == Target.Module))
            {
                Report.Error(4023, "Platform option `anycpu32bitpreferred' is valid only for executables");
                return(false);
            }

            TimeReporter tr = new TimeReporter(settings.Timestamps);

            ctx.TimeReporter = tr;
            tr.StartTotal();

            var module = new ModuleContainer(ctx);

            RootContext.ToplevelTypes = module;

            tr.Start(TimeReporter.TimerType.ParseTotal);
            Parse(module);
            tr.Stop(TimeReporter.TimerType.ParseTotal);

            if (Report.Errors > 0)
            {
                return(false);
            }

            if (settings.TokenizeOnly || settings.ParseOnly)
            {
                tr.StopTotal();
                tr.ShowStats();
                return(true);
            }

            var    output_file = settings.OutputFile;
            string output_file_name;

            if (output_file == null)
            {
                var source_file = settings.FirstSourceFile;

                if (source_file == null)
                {
                    Report.Error(1562, "If no source files are specified you must specify the output file with -out:");
                    return(false);
                }

                output_file_name = source_file.Name;
                int pos = output_file_name.LastIndexOf('.');

                if (pos > 0)
                {
                    output_file_name = output_file_name.Substring(0, pos);
                }

                output_file_name += settings.TargetExt;
                output_file       = output_file_name;
            }
            else
            {
                output_file_name = Path.GetFileName(output_file);

                if (string.IsNullOrEmpty(Path.GetFileNameWithoutExtension(output_file_name)) ||
                    output_file_name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0)
                {
                    Report.Error(2021, "Output file name is not valid");
                    return(false);
                }
            }

#if STATIC
            var importer          = new StaticImporter(module);
            var references_loader = new StaticLoader(importer, ctx);

            tr.Start(TimeReporter.TimerType.AssemblyBuilderSetup);
            var assembly = new AssemblyDefinitionStatic(module, references_loader, output_file_name, output_file);
            assembly.Create(references_loader.Domain);
            tr.Stop(TimeReporter.TimerType.AssemblyBuilderSetup);

            // Create compiler types first even before any referenced
            // assembly is loaded to allow forward referenced types from
            // loaded assembly into compiled builder to be resolved
            // correctly
            tr.Start(TimeReporter.TimerType.CreateTypeTotal);
            module.CreateContainer();
            importer.AddCompiledAssembly(assembly);
            tr.Stop(TimeReporter.TimerType.CreateTypeTotal);

            references_loader.LoadReferences(module);

            tr.Start(TimeReporter.TimerType.PredefinedTypesInit);
            if (!ctx.BuiltinTypes.CheckDefinitions(module))
            {
                return(false);
            }

            tr.Stop(TimeReporter.TimerType.PredefinedTypesInit);

            references_loader.LoadModules(assembly, module.GlobalRootNamespace);
#else
            var assembly = new AssemblyDefinitionDynamic(module, output_file_name, output_file);
            module.SetDeclaringAssembly(assembly);

            var importer = new ReflectionImporter(module, ctx.BuiltinTypes);
            assembly.Importer = importer;

            var loader = new DynamicLoader(importer, ctx);
            loader.LoadReferences(module);

            if (!ctx.BuiltinTypes.CheckDefinitions(module))
            {
                return(false);
            }

            if (!assembly.Create(AppDomain.CurrentDomain, AssemblyBuilderAccess.Save))
            {
                return(false);
            }

            module.CreateContainer();

            loader.LoadModules(assembly, module.GlobalRootNamespace);
#endif
            module.InitializePredefinedTypes();

            tr.Start(TimeReporter.TimerType.ModuleDefinitionTotal);
            module.Define();
            tr.Stop(TimeReporter.TimerType.ModuleDefinitionTotal);

            if (Report.Errors > 0)
            {
                return(false);
            }

            if (settings.DocumentationFile != null)
            {
                var doc = new DocumentationBuilder(module);
                doc.OutputDocComment(output_file, settings.DocumentationFile);
            }

            assembly.Resolve();

            if (Report.Errors > 0)
            {
                return(false);
            }


            tr.Start(TimeReporter.TimerType.EmitTotal);
            assembly.Emit();
            tr.Stop(TimeReporter.TimerType.EmitTotal);

            if (Report.Errors > 0)
            {
                return(false);
            }

            tr.Start(TimeReporter.TimerType.CloseTypes);
            module.CloseContainer();
            tr.Stop(TimeReporter.TimerType.CloseTypes);

            tr.Start(TimeReporter.TimerType.Resouces);
            if (!settings.WriteMetadataOnly)
            {
                assembly.EmbedResources();
            }
            tr.Stop(TimeReporter.TimerType.Resouces);

            if (Report.Errors > 0)
            {
                return(false);
            }

            assembly.Save();

#if STATIC
            references_loader.Dispose();
#endif
            tr.StopTotal();
            tr.ShowStats();

            return(Report.Errors == 0);
        }
예제 #5
0
		public static DynamicContext Create ()
		{
			if (dc != null)
				return dc;

			lock (compiler_initializer) {
				if (dc != null)
					return dc;

				var settings = new Compiler.CompilerSettings () {
					WarningLevel = 0
				};

				var cc = new Compiler.CompilerContext (settings, ErrorPrinter.Instance) {
					IsRuntimeBinder = true
				};

				//
				// Any later loaded assemblies are handled internally by GetAssemblyDefinition
				// domain.AssemblyLoad cannot be used as that would be too destructive as we
				// would hold all loaded assemblies even if they can be never visited
				//
				// TODO: Remove this code and rely on GetAssemblyDefinition only
				//
				var module = new Compiler.ModuleContainer (cc);
				module.HasTypesFullyDefined = true;
				
				// Setup fake assembly, it's used mostly to simplify checks like friend-access
				var temp = new Compiler.AssemblyDefinitionDynamic (module, "dynamic");
				module.SetDeclaringAssembly (temp);

				var importer = new Compiler.ReflectionImporter (module, cc.BuiltinTypes) {
					IgnorePrivateMembers = false
				};

				// Import all currently loaded assemblies
				// TODO: Rewrite this to populate type cache on-demand, that should greatly
				// reduce our start-up cost
				var domain = AppDomain.CurrentDomain;
				foreach (var a in AppDomain.CurrentDomain.GetAssemblies ()) {
					importer.ImportAssembly (a, module.GlobalRootNamespace);
				}

				cc.BuiltinTypes.CheckDefinitions (module);
				module.InitializePredefinedTypes ();

				dc = new DynamicContext (module, importer);
			}

			return dc;
		}
예제 #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
            {
#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
        }
예제 #7
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
        }
예제 #8
0
파일: eval.cs 프로젝트: timbriant/runcs
        public string [] GetCompletions(string input, out string prefix)
        {
            prefix = "";
            if (input == null || input.Length == 0)
            {
                return(null);
            }

            try
            {
                invoke_thread = System.Threading.Thread.CurrentThread;
                invoking      = true;

                lock (evaluator_lock)
                {
                    if (!inited)
                    {
                        Init();
                    }

                    bool         partial_input;
                    CSharpParser parser = ParseString(ParseMode.GetCompletions, input, out partial_input);
                    if (parser == null)
                    {
                        if (CSharpParser.yacc_verbose_flag != 0)
                        {
                            Console.WriteLine("DEBUG: No completions available");
                        }
                        return(null);
                    }

                    Class parser_result = parser.InteractiveResult;

#if NET_4_0
                    var access = AssemblyBuilderAccess.Run;
#else
                    var access = AssemblyBuilderAccess.Run;
#endif
                    var a = new AssemblyDefinitionDynamic(module, "completions");
                    a.Create(AppDomain.CurrentDomain, access);
                    module.SetDeclaringAssembly(a);

                    // Need to setup MemberCache
                    parser_result.CreateType();

                    var          method = parser_result.Methods[0] as Method;
                    BlockContext bc     = new BlockContext(method, method.Block, TypeManager.void_type);

                    try
                    {
                        method.Block.Resolve(null, bc, method);
                    }
                    catch (CompletionResult cr)
                    {
                        prefix = cr.BaseText;
                        return(cr.Result);
                    }
                }
            }
            catch (ThreadAbortException e)
            {
                Console.WriteLine("Interrupted!\n{0}", e);
            }
            finally
            {
                invoking = false;
            }

            return(null);
        }
예제 #9
0
파일: eval.cs 프로젝트: liquidboy/ILSpy
        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));
        }
예제 #10
0
파일: eval.cs 프로젝트: liquidboy/ILSpy
        public static string [] GetCompletions(string input, out string prefix)
        {
            prefix = "";
            if (input == null || input.Length == 0)
            {
                return(null);
            }

            lock (evaluator_lock){
                if (!inited)
                {
                    Init();
                }

                bool         partial_input;
                CSharpParser parser = ParseString(ParseMode.GetCompletions, input, out partial_input);
                if (parser == null)
                {
                    if (CSharpParser.yacc_verbose_flag != 0)
                    {
                        Console.WriteLine("DEBUG: No completions available");
                    }
                    return(null);
                }

                Class parser_result = parser.InteractiveResult as Class;

                if (parser_result == null)
                {
                    if (CSharpParser.yacc_verbose_flag != 0)
                    {
                        Console.WriteLine("Do not know how to cope with !Class yet");
                    }
                    return(null);
                }

                try {
                    var a = new AssemblyDefinitionDynamic(RootContext.ToplevelTypes, "temp");
                    a.Create(AppDomain.CurrentDomain, AssemblyBuilderAccess.Run);
                    RootContext.ToplevelTypes.SetDeclaringAssembly(a);
                    RootContext.ToplevelTypes.CreateType();
                    RootContext.ToplevelTypes.Define();
                    if (ctx.Report.Errors != 0)
                    {
                        return(null);
                    }

                    MethodOrOperator method = null;
                    foreach (MemberCore member in parser_result.Methods)
                    {
                        if (member.Name != "Host")
                        {
                            continue;
                        }

                        method = (MethodOrOperator)member;
                        break;
                    }
                    if (method == null)
                    {
                        throw new InternalErrorException("did not find the the Host method");
                    }

                    BlockContext bc = new BlockContext(method, method.Block, method.ReturnType);

                    try {
                        method.Block.Resolve(null, bc, method);
                    } catch (CompletionResult cr) {
                        prefix = cr.BaseText;
                        return(cr.Result);
                    }
                } finally {
                    parser.undo.ExecuteUndo();
                }
            }
            return(null);
        }