Example #1
0
        // RunCode_ExecuteCode must throw an exception if he can't execute run method
        // if no error thrown RunCode_ExecuteCode must call RunCode_EndRun()
        private void RunCode_ExecuteCode(Assembly assembly, GenerateCSharpCodeResult codeResult, CompilerProject compilerProject, Compiler compiler, bool useNewThread)
        {
            _runCode                    = new RunCode();
            _runCode.RunAssembly        = assembly;
            _runCode.CompilerAssemblies = compiler.Assemblies;
            //_runCode.RunClassName = codeResult.GetFullClassName();
            //_runCode.RunMethodName = codeResult.RunMethodName;
            _runCode.RunMethodName  = codeResult.GetFullRunMethodName();
            _runCode.InitMethodName = compilerProject.GetInitMethod();  // "Init"
            _runCode.EndMethodName  = compilerProject.GetEndMethod();   // "End"
            _runCode.EndRun        += RunCode_EndRun;

            _executionAborted = false;

            _runCode.Run(useNewThread);
            //_runCodeThread = runCode.RunThread;
        }
Example #2
0
        // RunCode_ExecuteCode must throw an exception if he can't execute run method
        // if no error thrown RunCode_ExecuteCode must call RunCode_EndRun()
        //private void RunCode_ExecuteCode(Assembly assembly, GenerateCSharpCodeResult codeResult, CompilerProjectReader compilerProject, ProjectCompiler compiler, bool runOnMainThread, bool callInit)
        private async Task RunCode_ExecuteCode(Assembly assembly, GenerateCSharpCodeResult codeResult, CompilerProjectReader compilerProject, ProjectCompiler compiler, bool runOnMainThread, bool callInit)
        {
            RunCode runCode = new RunCode(++_runCodeId);

            runCode.RunAssembly = assembly;
            //runCode.CompilerAssemblies = compiler.Assemblies;
            runCode.RunMethodName = codeResult.GetFullRunMethodName();
            runCode.EndRun       += error => RunCode_EndRun(runCode, error);

            //if (forceCallInit)
            //    _runSourceInitEndMethods.CallInit = true;

            _executionAborted = false;

            foreach (CompilerAssembly compilerAssembly in compiler.Assemblies.Values)
            {
                //WriteLine(2, "  Assembly              \"{0}\" resolve {1}", assembly.File, assembly.Resolve);
                if (compilerAssembly.Resolve)
                {
                    AssemblyResolve.Add(compilerAssembly.File, compilerAssembly.ResolveName);
                }
            }

            //_runSourceInitEndMethods.CallInit = callInit;
            //if (callInit)
            //_runSourceInitEndMethods.CallInitMethods(compilerProject.GetInitMethods(), compilerProject.GetEndMethods(), callInit, methodName => runCode.GetMethod(methodName));
            _runSourceInitEndMethods.CallInitMethods(compilerProject.GetInitMethods(), compilerProject.GetEndMethods(), callInit, methodName => zReflection.GetMethod(methodName, assembly, ErrorOptions.TraceWarning));

            // add runCode to _runCodes after call init, if call init fail runCode is not in _runCodes
            if (!_runCodes.TryAdd(runCode.Id, runCode))
            {
                throw new PBException("unable to add RunCode id {0} to ConcurrentDictionary", runCode.Id);
            }

            //runCode.Run(runOnMainThread);
            await runCode.Run_v2(runOnMainThread);

            // problem with AssemblyResolve.Clear() end method may need to resolve assembly
            //AssemblyResolve.Clear();
        }
Example #3
0
        // RunCode_ExecuteCode must throw an exception if he can't execute run method
        // if no error thrown RunCode_ExecuteCode must call RunCode_EndRun()
        //private void RunCode_ExecuteCode(Assembly assembly, GenerateCSharpCodeResult codeResult, CompilerProject compilerProject, Compiler compiler, bool useNewThread)
        private void RunCode_ExecuteCode(Assembly assembly, GenerateCSharpCodeResult codeResult, CompilerProjectReader compilerProject, ProjectCompiler compiler, bool runOnMainThread, bool callInit)
        {
            RunCode runCode = new RunCode(++_runCodeId);
            runCode.RunAssembly = assembly;
            //runCode.CompilerAssemblies = compiler.Assemblies;
            runCode.RunMethodName = codeResult.GetFullRunMethodName();
            runCode.EndRun += error => RunCode_EndRun(runCode, error);

            //if (forceCallInit)
            //    _runSourceInitEndMethods.CallInit = true;

            _executionAborted = false;

            foreach (CompilerAssembly compilerAssembly in compiler.Assemblies.Values)
            {
                //WriteLine(2, "  Assembly              \"{0}\" resolve {1}", assembly.File, assembly.Resolve);
                if (compilerAssembly.Resolve)
                    AssemblyResolve.Add(compilerAssembly.File, compilerAssembly.ResolveName);
            }

            //_runSourceInitEndMethods.CallInit = callInit;
            //if (callInit)
            //_runSourceInitEndMethods.CallInitMethods(compilerProject.GetInitMethods(), compilerProject.GetEndMethods(), callInit, methodName => runCode.GetMethod(methodName));
            _runSourceInitEndMethods.CallInitMethods(compilerProject.GetInitMethods(), compilerProject.GetEndMethods(), callInit, methodName => zReflection.GetMethod(methodName, assembly, ErrorOptions.TraceWarning));

            // add runCode to _runCodes after call init, if call init fail runCode is not in _runCodes
            if (!_runCodes.TryAdd(runCode.Id, runCode))
                throw new PBException("unable to add RunCode id {0} to ConcurrentDictionary", runCode.Id);

            runCode.Run(runOnMainThread);

            // problem with AssemblyResolve.Clear() end method may need to resolve assembly
            //AssemblyResolve.Clear();
        }
Example #4
0
        // RunCode_ExecuteCode must throw an exception if he can't execute run method
        // if no error thrown RunCode_ExecuteCode must call RunCode_EndRun()
        private void RunCode_ExecuteCode(Assembly assembly, GenerateCSharpCodeResult codeResult, CompilerProject compilerProject, Compiler compiler, bool useNewThread)
        {
            _runCode = new RunCode();
            _runCode.RunAssembly = assembly;
            _runCode.CompilerAssemblies = compiler.Assemblies;
            //_runCode.RunClassName = codeResult.GetFullClassName();
            //_runCode.RunMethodName = codeResult.RunMethodName;
            _runCode.RunMethodName = codeResult.GetFullRunMethodName();
            _runCode.InitMethodName = compilerProject.GetInitMethod();  // "Init"
            _runCode.EndMethodName = compilerProject.GetEndMethod();    // "End"
            _runCode.EndRun += RunCode_EndRun;

            _executionAborted = false;

            _runCode.Run(useNewThread);
            //_runCodeThread = runCode.RunThread;
        }
Example #5
0
        private void _RunCode(string code, bool useNewThread = true, bool compileWithoutProject = false, bool dontRunCode = false)
        {
            if (code == "")
            {
                return;
            }

            // $$RunSourceRunCode_v1
            //if (!dontRunCode && (_runCode != null || _runCodeThread != null))
            if (!dontRunCode && _runCode != null)
            {
                throw new PBException("error program already running");
            }

            bool error    = false;
            bool doEndRun = true;

            _refreshRunSourceConfig = true;
            _refreshProjectConfig   = true;

            //_runCodeChrono = new Chrono();
            try
            {
                CompilerProject compilerProject = null;
                if (!compileWithoutProject)
                {
                    compilerProject = GetProjectCompilerProject();
                }
                if (compilerProject == null)
                {
                    compilerProject = GetDefaultProject();
                }

                //string assemblyFile = RunCode_GetNewAssemblyFile();
                string assemblyFile = GetGenerateAssembly().GetNewAssemblyFile();

                GenerateCSharpCodeResult codeResult = RunCode_GenerateCode(code, compilerProject, assemblyFile);

                Compiler compiler = RunCode_CompileCode(compilerProject, assemblyFile, codeResult.SourceFile);

                if (compiler.HasError())
                {
                    //SetResultCompilerMessages(compiler);
                    SetResult(compiler.GetCompilerMessagesDataTable());
                }
                else
                {
                    // trace warning
                    compiler.TraceMessages();

                    if (!dontRunCode)
                    {
                        RunCode_ExecuteCode(compiler.Results.CompiledAssembly, codeResult, compilerProject, compiler, useNewThread);
                        doEndRun = false;
                    }
                }
            }
            catch
            {
                error = true;
                throw;
            }
            finally
            {
                //if (error && EndRun != null)
                //    EndRun(error);
                // call RunCode_EndRun() only if exception has bin catched (error = true)
                //if (error || dontRunCode)
                //    RunCode_EndRun(error);
                if (doEndRun)
                {
                    RunCode_EndRun(error);
                }
            }
        }
Example #6
0
        //private CompilerProject GetRunSourceConfigCompilerDefaultValues()
        //{
        //    return CompilerProject.Create(GetRunSourceConfig().zGetConfigElement("CompilerDefaultValues"));
        //}

        //private void _RunCode(string code, bool runOnMainThread = false, bool compileWithoutProject = false, bool allowMultipleRun = false, bool dontRunCode = false, bool callInit = false)
        private async Task _RunCode(string code, bool runOnMainThread = false, bool compileWithoutProject = false, bool allowMultipleRun = false, bool dontRunCode = false, bool callInit = false)
        {
            if (code == "")
            {
                return;
            }

            //if (!dontRunCode && _runCode != null)
            //    throw new PBException("error program already running");
            //if (!dontRunCode && _runCodes.Count > 0 && !_allowMultipleExecution)
            if (!dontRunCode && _runCodes.Count > 0 && !allowMultipleRun)
            {
                throw new PBException("error program already running and multiple execution is not allowed");
            }

            bool error    = false;
            bool doEndRun = true;

            _refreshRunSourceConfig = true;
            _refreshProjectConfig   = true;

            try
            {
                CompilerProjectReader projectReader = null;
                if (!compileWithoutProject)
                {
                    projectReader = GetProjectCompilerProject();
                }
                if (projectReader == null)
                {
                    projectReader = GetDefaultProject();
                    //Trace.WriteLine($"compile with default project \"{projectReader.GetProjectCompilerFile().File}\"");
                    Trace.WriteLine($"compile with default project \"{projectReader.ProjectFile}\"");
                }

                string assemblyFile = GetGenerateAssembly().GetNewAssemblyFile();

                GenerateCSharpCodeResult codeResult = RunCode_GenerateCode(code, projectReader, assemblyFile);

                ProjectCompiler compiler = RunCode_CompileCode(projectReader, assemblyFile, codeResult.SourceFile);

                //if (compiler.HasError())
                if (!compiler.Success)
                {
                    SetResult(compiler.GetCompilerMessagesDataTable());
                }
                else
                {
                    // trace warning
                    //compiler.TraceMessages(_messageFilter);
                    compiler.TraceMessages(message => _messageFilter(message) || message.FileName != codeResult.SourceFile);

                    if (!dontRunCode)
                    {
                        //RunCode_ExecuteCode(compiler.Results.LoadAssembly(), codeResult, projectReader, compiler, runOnMainThread, callInit);
                        await RunCode_ExecuteCode(compiler.Results.LoadAssembly(), codeResult, projectReader, compiler, runOnMainThread, callInit);

                        doEndRun = false;
                    }
                }
            }
            //catch
            //{
            //    error = true;
            //    throw;
            //}
            catch (Exception ex)
            {
                error = true;
                if (ex is ProjectCompilerException)
                {
                    Error.WriteMessage(ErrorOptions.TraceError, ex.Message);
                }
                else
                {
                    throw;
                }
            }
            finally
            {
                if (doEndRun)
                {
                    RunCode_EndRun(null, error);
                }
            }
        }