diff options
| author | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-08-09 17:26:12 +0300 |
|---|---|---|
| committer | Roy Ben Shabat <Roy.mail.net@gmail.com> | 2020-08-09 17:26:12 +0300 |
| commit | 02d8fb3fe734e38308226bddbd8f584bf957035c (patch) | |
| tree | 8e6af011ec96feeb67c1c051c4521ce0ac23e825 /Software/Visual_Studio/Scripting | |
| parent | 9aad7ef417c421d2516aeab86e7094198aa242cb (diff) | |
| download | Tango-02d8fb3fe734e38308226bddbd8f584bf957035c.tar.gz Tango-02d8fb3fe734e38308226bddbd8f584bf957035c.zip | |
Optimized procedure auto compilation frequency.
Some more work on proc_doc.
Diffstat (limited to 'Software/Visual_Studio/Scripting')
| -rw-r--r-- | Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs | 165 |
1 files changed, 101 insertions, 64 deletions
diff --git a/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs b/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs index 2bd438ff8..7500e404f 100644 --- a/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs +++ b/Software/Visual_Studio/Scripting/Tango.Scripting.Basic/Project.cs @@ -21,6 +21,8 @@ namespace Tango.Scripting.Basic { public class Project<T> : ExtendedObject where T : IContext { + private object _compileLock = new object(); + public String ID { get; set; } private String _name; @@ -37,6 +39,22 @@ namespace Tango.Scripting.Basic set { _description = value; RaisePropertyChangedAuto(); } } + private bool _isRunning; + [JsonIgnore] + public bool IsRunning + { + get { return _isRunning; } + set { _isRunning = value; RaisePropertyChangedAuto(); } + } + + private bool _isCompiling; + [JsonIgnore] + public bool IsCompiling + { + get { return _isCompiling; } + set { _isCompiling = value; RaisePropertyChangedAuto(); } + } + public ApartmentState ApartmentState { get; set; } public ObservableCollection<ReferenceAssembly> ReferenceAssemblies { get; set; } @@ -52,6 +70,7 @@ namespace Tango.Scripting.Basic } } + [JsonIgnore] public List<ScriptBreakPoint> BreakPoints { get; set; } public Project() @@ -72,97 +91,113 @@ namespace Tango.Scripting.Basic { return Task.Factory.StartNew<CompilationResult>(() => { - var result = new CompilationResult(); - var tempFolder = TemporaryManager.CreateFolder(Name + "_" + ID); - result.TemporaryProjectPath = tempFolder; + lock (_compileLock) + { + try + { + IsCompiling = true; + var result = new CompilationResult(); + var tempFolder = TemporaryManager.CreateFolder(Name + "_" + ID); + result.TemporaryProjectPath = tempFolder; - String mainScriptCode = String.Empty; + String mainScriptCode = String.Empty; - foreach (var script in Scripts) - { - script.LoadCount = 0; - script.LoadCharCount = 0; - String code = script.Code; - String codeFile = Path.Combine(tempFolder, script.Name); + foreach (var script in Scripts) + { + script.LoadCount = 0; + script.LoadCharCount = 0; + String code = script.Code; + String codeFile = Path.Combine(tempFolder, script.Name); - String loadingString = String.Empty; + String loadingString = String.Empty; - foreach (var file in Scripts.Where(x => !x.IsEntryPoint && script != x).Select(x => Path.Combine(tempFolder, x.Name))) - { - loadingString += $"#load \"{file}\"\n"; - script.LoadCount++; - } + foreach (var file in Scripts.Where(x => !x.IsEntryPoint && script != x).Select(x => Path.Combine(tempFolder, x.Name))) + { + loadingString += $"#load \"{file}\"\n"; + script.LoadCount++; + } - script.LoadCharCount += loadingString.Length; + script.LoadCharCount += loadingString.Length; - code = loadingString + code; + code = loadingString + code; - int debugLinesLength = 0; + int debugLinesLength = 0; - foreach (var breakPoint in BreakPoints.Where(x => x.Script == script).OrderBy(x => x.LineNumber)) - { - var debugLine = $"context.BreakPoint(\"{script.Name}\",{breakPoint.LineNumber}"; + foreach (var breakPoint in BreakPoints.Where(x => x.Script == script).OrderBy(x => x.LineNumber)) + { + var debugLine = $"context.BreakPoint(\"{script.Name}\",{breakPoint.LineNumber}"; - foreach (var symbol in breakPoint.ContextSymbols) - { - debugLine += $",\"{symbol.Name}\",{symbol.Offset},{symbol.Length},{symbol.Name}"; - } + foreach (var symbol in breakPoint.ContextSymbols) + { + debugLine += $",\"{symbol.Name}\",{symbol.Offset},{symbol.Length},{symbol.Name}"; + } - debugLine += ");"; + debugLine += ");"; - StringBuilder builder = new StringBuilder(code); - builder.Insert(breakPoint.LineStartOffset + loadingString.Length + debugLinesLength, debugLine); - code = builder.ToString(); + StringBuilder builder = new StringBuilder(code); + builder.Insert(breakPoint.LineStartOffset + loadingString.Length + debugLinesLength, debugLine); + code = builder.ToString(); - debugLinesLength += debugLine.Length; - } + debugLinesLength += debugLine.Length; + } - if (!script.IsEntryPoint) - { - File.WriteAllText(codeFile, code); - } - else - { - code += Environment.NewLine + Environment.NewLine + "new Program().OnExecute(GlobalContext);"; - mainScriptCode = code; - } - } + if (!script.IsEntryPoint) + { + File.WriteAllText(codeFile, code); + } + else + { + code += Environment.NewLine + Environment.NewLine + "new Program().OnExecute(GlobalContext);"; + mainScriptCode = code; + } + } - var scriptOptions = ScriptOptions.Default.WithReferences(LoadReferenceAssemblies()).WithEmitDebugInformation(true); + var scriptOptions = ScriptOptions.Default.WithReferences(LoadReferenceAssemblies()).WithEmitDebugInformation(true); - var s = CSharpScript.Create<object>(mainScriptCode, scriptOptions, typeof(GlobalObject<T>)); + var s = CSharpScript.Create<object>(mainScriptCode, scriptOptions, typeof(GlobalObject<T>)); - result.Script = s; + result.Script = s; - var compileResults = s.Compile(); + var compileResults = s.Compile(); - GC.Collect(); + GC.Collect(); - foreach (var error in compileResults.Where(x => x.Severity == Microsoft.CodeAnalysis.DiagnosticSeverity.Error)) - { - CompilationError cError = new CompilationError(); - cError.File = System.IO.Path.GetFileName(error.Location.SourceTree.FilePath); - if (cError.File == String.Empty) + foreach (var error in compileResults.Where(x => x.Severity == Microsoft.CodeAnalysis.DiagnosticSeverity.Error)) + { + CompilationError cError = new CompilationError(); + cError.File = System.IO.Path.GetFileName(error.Location.SourceTree.FilePath); + if (cError.File == String.Empty) + { + cError.File = Scripts.Single(x => x.IsEntryPoint).Name; + } + Script errorScript = Scripts.Single(x => x.Name == cError.File); + cError.Message = error.GetMessage(); + cError.Severity = error.Severity; + cError.Position = error.Location.SourceSpan.Start - (errorScript != null ? errorScript.LoadCharCount : 0); + var line = error.Location.GetMappedLineSpan(); + cError.Line = line.StartLinePosition.Line + 1 - (errorScript != null ? errorScript.LoadCount : 0); + cError.Column = line.StartLinePosition.Character + 1; + cError.Length = line.EndLinePosition.Character - line.StartLinePosition.Character; + result.Errors.Add(cError); + } + + return result; + } + catch (Exception) + { + throw; + } + finally { - cError.File = Scripts.Single(x => x.IsEntryPoint).Name; + IsCompiling = false; } - Script errorScript = Scripts.Single(x => x.Name == cError.File); - cError.Message = error.GetMessage(); - cError.Severity = error.Severity; - cError.Position = error.Location.SourceSpan.Start - (errorScript != null ? errorScript.LoadCharCount : 0); - var line = error.Location.GetMappedLineSpan(); - cError.Line = line.StartLinePosition.Line + 1 - (errorScript != null ? errorScript.LoadCount : 0); - cError.Column = line.StartLinePosition.Character + 1; - cError.Length = line.EndLinePosition.Character - line.StartLinePosition.Character; - result.Errors.Add(cError); } - - return result; }); } public async Task<ProjectSession<T>> Run(T context) { + IsRunning = true; var result = await Compile(); if (result.Errors.Count > 0) @@ -183,6 +218,7 @@ namespace Tango.Scripting.Basic try { var runResult = result.Script.RunAsync(globals: new GlobalObject<T>() { GlobalContext = context }).Result; + IsRunning = false; session.Completed(runResult.ReturnValue); } catch (ThreadAbortException) @@ -197,6 +233,7 @@ namespace Tango.Scripting.Basic { BreakPoints.Clear(); GC.Collect(); + IsRunning = false; } }); |
