示例#1
0
        public object Test(string source, string typeName, string method, object[] parameters, bool convertToTac, ProviderType providerType)
        {
            Compiler compiler = new Compiler();
            string   output   = compiler.CompileSource(source);

            Host host = new Host();
            //PlatformTypes.Resolve(host);
            ILoader provider = CreateProvider(providerType, host);

            provider.LoadAssembly(output);

            if (convertToTac)
            {
                IEnumerable <MethodDefinition> allDefinedMethods = from a in host.Assemblies
                                                                   from t in a.RootNamespace.GetAllTypes()
                                                                   from m in t.Members.OfType <MethodDefinition>()
                                                                   where m.HasBody
                                                                   select m;

                foreach (MethodDefinition definedMethod in allDefinedMethods)
                {
                    MethodDefinition mainMethod           = definedMethod;
                    MethodBody       originalBytecodeBody = mainMethod.Body;
                    Utils.TransformToTac(mainMethod);
                    originalBytecodeBody.Kind = MethodBodyKind.ThreeAddressCode;

                    Assembler  assembler    = new Assembler(mainMethod.Body);
                    MethodBody bytecodeBody = assembler.Execute();
                    mainMethod.Body      = bytecodeBody;
                    mainMethod.Body.Kind = MethodBodyKind.Bytecode;
                }
            }

            CodeGenerator.CecilCodeGenerator.CecilCodeGenerator exporter = new CodeGenerator.CecilCodeGenerator.CecilCodeGenerator(host);
            string outputDir = Utils.GetTemporaryDirectory();

            exporter.WriteAssemblies(outputDir);

            // hack: we are assuming it is always a .dll
            output = output + ".dll";

            var filePath = Path.Combine(outputDir, Path.GetFileName(output));

            VerifyAssembly(filePath);
            System.Reflection.Assembly DLL = System.Reflection.Assembly.LoadFile(filePath);

            Type type = DLL.GetType(typeName);

            System.Reflection.MethodInfo methodInfoStatic = type.GetMethod(method);
            if (methodInfoStatic == null)
            {
                throw new Exception("No such static method exists.");
            }

            // Invoke static method
            object result = methodInfoStatic.Invoke(null, parameters);

            return(result);
        }
示例#2
0
        public void DSA_Sil2Cil()
        {
            Model.Host    host     = new Model.Host();
            Model.ILoader provider = new CecilProvider.Loader(host);
            string        buildDir =
                Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(CecilProvider.Loader))
                                      .Location);

            // create temporary directory where we place generated dlls
            string tempDir = Utils.GetTemporaryDirectory();

            // sanity check for needed libraries
            // resourceDSALibrary is the library that is going to be processed
            // resourceDSANUnit has test cases for the DSA library
            string resourceDSALibrary = Path.Combine(buildDir, "Resources/DSA/Library/DSA.dll");
            string resourceDSANUnit   = Path.Combine(buildDir, "Resources/DSA/NUnit/DSANUnitTests.dll");

            if (!File.Exists(resourceDSALibrary) || !File.Exists((resourceDSANUnit)))
            {
                throw new FileNotFoundException();
            }

            // read the DSA library and re compile it using our framework
            provider.LoadAssembly(resourceDSALibrary);
            CodeGenerator.CecilCodeGenerator.CecilCodeGenerator exporter = new CodeGenerator.CecilCodeGenerator.CecilCodeGenerator(host);
            exporter.WriteAssemblies(tempDir);

            // copy nunit test library to temp dir
            string dsaNUnit = Path.Combine(tempDir, "DSANUnitTests.dll");

            File.Copy(resourceDSANUnit, dsaNUnit);

            // execute nunit test suite
            NUnitLite.AutoRun autoRun   = new NUnitLite.AutoRun(System.Reflection.Assembly.LoadFrom(dsaNUnit));
            string            outputTxt = Path.Combine(tempDir, "output.txt");
            string            outputCmd = "--out=" + outputTxt;

            autoRun.Execute(new string[1] {
                outputCmd
            });

            // check results
            string output = File.ReadAllText(outputTxt);

            Assert.IsTrue(output.Contains("Test Count: 618, Passed: 618"));
        }