예제 #1
0
        public T Run <T>(TestCompilerSettings settings, string ns, string type, string method, params object[] parameters)
        {
            CompileTestCode(settings);

            // Find the test method to execute
            RuntimeMethod runtimeMethod = FindMethod(
                ns,
                type,
                method
                );

            Debug.Assert(runtimeMethod != null, runtimeMethod.ToString());
            //Debug.Assert(runtimeMethod.Address != null, runtimeMethod.ToString());
            //Debug.Assert(runtimeMethod.Address != IntPtr.Zero, runtimeMethod.ToString());

            // Get delegate name
            string delegateName;

            if (default(T) is System.ValueType)
            {
                delegateName = "Mosa.Test.Prebuilt.Delegates+" + DelegateUtility.GetDelegteName(default(T), parameters);
            }
            else
            {
                delegateName = "Mosa.Test.Prebuilt.Delegates+" + DelegateUtility.GetDelegteName(null, parameters);
            }

            // Get the prebuilt delegate type
            Type delegateType = Prebuilt.GetType(delegateName);

            Debug.Assert(delegateType != null, delegateName);

            IntPtr address = linker.GetSymbol(runtimeMethod.ToString()).VirtualAddress;

            // Create a delegate for the test method
            Delegate fn = Marshal.GetDelegateForFunctionPointer(
                address,
                delegateType
                );

            // Execute the test method
            object tempResult = fn.DynamicInvoke(parameters);

            try
            {
                if (default(T) is System.ValueType)
                {
                    return((T)tempResult);
                }
                else
                {
                    return(default(T));
                }
            }
            catch (InvalidCastException e)
            {
                Assert.Fail(@"Failed to convert result {0} of type {1} to type {2}.", tempResult, tempResult.GetType(), typeof(T));
                throw e;
            }
        }
예제 #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TestCompilerSettings"/> class.
 /// </summary>
 /// <param name="settings">The settings.</param>
 public TestCompilerSettings(TestCompilerSettings settings)
 {
     language               = settings.language;
     unsafeCode             = settings.unsafeCode;
     doNotReferenceMscorlib = settings.doNotReferenceMscorlib;
     codeSource             = settings.codeSource;
     additionalSource       = settings.additionalSource;
     references             = new List <string>(settings.references);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="TestCompilerSettings"/> class.
 /// </summary>
 /// <param name="settings">The settings.</param>
 public TestCompilerSettings(TestCompilerSettings settings)
 {
     language = settings.language;
     unsafeCode = settings.unsafeCode;
     doNotReferenceMscorlib = settings.doNotReferenceMscorlib;
     codeSource = settings.codeSource;
     additionalSource = settings.additionalSource;
     references = new List<string>(settings.references);
 }
예제 #4
0
        // Might not keep this as a public method
        public void CompileTestCode(TestCompilerSettings settings)
        {
            if (cacheSettings == null || !cacheSettings.IsEqual(settings))
            {
                cacheSettings = new TestCompilerSettings(settings);

                string assembly = RunCodeDomCompiler(settings);

                Console.WriteLine("Executing MOSA compiler...");
                linker = RunMosaCompiler(settings, assembly);
            }
        }
예제 #5
0
        public bool IsEqual(TestCompilerSettings other)
        {
            if (other == null)
            {
                return(false);
            }

            if (this.codeSource != other.codeSource)
            {
                return(false);
            }

            if (this.additionalSource != other.additionalSource)
            {
                return(false);
            }

            if (this.unsafeCode != other.unsafeCode)
            {
                return(false);
            }

            if (this.language != other.language)
            {
                return(false);
            }

            if (this.doNotReferenceMscorlib != other.doNotReferenceMscorlib)
            {
                return(false);
            }

            if (this.references.Count != other.references.Count)
            {
                return(false);
            }

            foreach (string file in this.references)
            {
                if (!other.references.Contains(file))
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #6
0
        private TestAssemblyLinker RunMosaCompiler(TestCompilerSettings settings, string assemblyFile)
        {
            IAssemblyLoader assemblyLoader = new AssemblyLoader();

            assemblyLoader.InitializePrivatePaths(settings.References);

            assemblyLoader.LoadModule(assemblyFile);

            foreach (string file in settings.References)
            {
                assemblyLoader.LoadModule(file);
            }

            typeSystem = new TypeSystem();
            typeSystem.LoadModules(assemblyLoader.Modules);

            TestAssemblyLinker linker = TestCaseAssemblyCompiler.Compile(typeSystem);

            return(linker);
        }
        public bool IsEqual(TestCompilerSettings other)
        {
            if (other == null)
                return false;

            if (this.codeSource != other.codeSource)
                return false;

            if (this.additionalSource != other.additionalSource)
                return false;

            if (this.unsafeCode != other.unsafeCode)
                return false;

            if (this.language != other.language)
                return false;

            if (this.doNotReferenceMscorlib != other.doNotReferenceMscorlib)
                return false;

            if (this.references.Count != other.references.Count)
                return false;

            foreach (string file in this.references)
                if (!other.references.Contains(file))
                    return false;

            return true;
        }
예제 #8
0
        private string RunCodeDomCompiler(TestCompilerSettings settings)
        {
            Console.WriteLine("Executing {0} compiler...", settings.Language);

            CodeDomProvider provider;

            if (!providerCache.TryGetValue(settings.Language, out provider))
            {
                provider = CodeDomProvider.CreateProvider(settings.Language);
                if (provider == null)
                {
                    throw new NotSupportedException("The language '" + settings.Language + "' is not supported on this machine.");
                }
                providerCache.Add(settings.Language, provider);
            }

            string filename = Path.Combine(TempDirectory, Path.ChangeExtension(Path.GetRandomFileName(), "dll"));

            temps.AddFile(filename, false);

            string[] references = new string[settings.References.Count];
            settings.References.CopyTo(references, 0);

            CompilerResults    compileResults;
            CompilerParameters parameters = new CompilerParameters(references, filename, false);

            parameters.CompilerOptions = "/optimize-";

            if (settings.UnsafeCode)
            {
                if (settings.Language == "C#")
                {
                    parameters.CompilerOptions = parameters.CompilerOptions + " /unsafe+";
                }
                else
                {
                    throw new NotSupportedException();
                }
            }

            if (settings.DoNotReferenceMscorlib)
            {
                if (settings.Language == "C#")
                {
                    parameters.CompilerOptions = parameters.CompilerOptions + " /nostdlib";
                }
                else
                {
                    throw new NotSupportedException();
                }
            }

            parameters.GenerateInMemory = false;

            if (settings.CodeSource != null)
            {
                //Console.WriteLine("Code: {0}", settings.CodeSource + settings.AdditionalSource);
                compileResults = provider.CompileAssemblyFromSource(parameters, settings.CodeSource + settings.AdditionalSource);
            }
            else
            {
                throw new NotSupportedException();
            }

            if (compileResults.Errors.HasErrors)
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Code compile errors:");
                foreach (CompilerError error in compileResults.Errors)
                {
                    sb.AppendLine(error.ToString());
                }
                throw new Exception(sb.ToString());
            }

            return(compileResults.PathToAssembly);
        }