From 35e6e06157a232ca95b67e696d3286ae0d71c52d Mon Sep 17 00:00:00 2001 From: Alex Fokas Date: Mon, 11 Sep 2023 11:06:07 +0300 Subject: [PATCH] Ability to step into scripAbility to step into scripts when CompileWithDebug = truets when CompileWithDebug = true --- Westwind.Scripting/CSharpScriptExecution.cs | 48 +++++++++++++++++---- 1 file changed, 40 insertions(+), 8 deletions(-) diff --git a/Westwind.Scripting/CSharpScriptExecution.cs b/Westwind.Scripting/CSharpScriptExecution.cs index f588d9c..70e8c5b 100644 --- a/Westwind.Scripting/CSharpScriptExecution.cs +++ b/Westwind.Scripting/CSharpScriptExecution.cs @@ -874,8 +874,20 @@ public Task ExecuteScriptAsync(string csharpTemplate, TModel public bool CompileAssembly(string source, bool noLoad = false) { ClearErrors(); - - var tree = SyntaxFactory.ParseSyntaxTree(source.Trim()); + + var pePath = $@"{Path.GetTempPath()}\{GeneratedClassName}.cs"; + var pdbPath = Path.ChangeExtension(GeneratedClassName, ".pdb"); + + SourceText sourceText = null; + if (CompileWithDebug) + { + File.WriteAllText(pePath, source); + var buffer = Encoding.UTF8.GetBytes(source); + sourceText = SourceText.From(buffer, buffer.Length, Encoding.UTF8, canBeEmbedded: true); + } + + + var tree = CompileWithDebug ? SyntaxFactory.ParseSyntaxTree(source.Trim(), path: pePath, encoding: Encoding.UTF8) : SyntaxFactory.ParseSyntaxTree(source.Trim()); var optimizationLevel = CompileWithDebug ? OptimizationLevel.Debug : OptimizationLevel.Release; @@ -893,6 +905,7 @@ public bool CompileAssembly(string source, bool noLoad = false) bool isFileAssembly = false; Stream codeStream = null; + var pdbStream = new MemoryStream(); if (string.IsNullOrEmpty(OutputAssembly)) { codeStream = new MemoryStream(); // in-memory assembly @@ -904,15 +917,20 @@ public bool CompileAssembly(string source, bool noLoad = false) } using (codeStream) + using (pdbStream) { EmitResult compilationResult = null; if (CompileWithDebug) { - var debugOptions = CompileWithDebug ? DebugInformationFormat.Embedded : DebugInformationFormat.Pdb; - compilationResult = compilation.Emit(codeStream, - options: new EmitOptions(debugInformationFormat: debugOptions )); + var debugOptions = CompileWithDebug ? DebugInformationFormat.PortablePdb : DebugInformationFormat.Pdb; + + compilationResult = compilation.Emit( + peStream: codeStream, + pdbStream: pdbStream, + embeddedTexts: new List { EmbeddedText.FromSource(pePath, sourceText) }, + options: new EmitOptions(debugInformationFormat: debugOptions, pdbFilePath: pdbPath)); } - else + else compilationResult = compilation.Emit(codeStream); // Compilation Error handling @@ -928,7 +946,7 @@ public bool CompileAssembly(string source, bool noLoad = false) ErrorMessage = sb.ToString(); // no exception here during compilation - return the error - SetErrors(new ApplicationException(ErrorMessage),true); + SetErrors(new ApplicationException(ErrorMessage), true); return false; } } @@ -936,7 +954,10 @@ public bool CompileAssembly(string source, bool noLoad = false) if (!noLoad) { if (!isFileAssembly) - Assembly = LoadAssembly(((MemoryStream) codeStream).ToArray()); + { + if (CompileWithDebug) Assembly = LoadAssembly(((MemoryStream)codeStream).ToArray(), pdbStream.ToArray()); + else Assembly = LoadAssembly(((MemoryStream)codeStream).ToArray()); + } else Assembly = LoadAssemblyFrom(OutputAssembly); } @@ -1731,6 +1752,17 @@ private Assembly LoadAssembly(byte[] rawAssembly) return Assembly.Load(rawAssembly); } + private Assembly LoadAssembly(byte[] rawAssembly, byte[] rawPdb) + { +#if NETCORE + if (AlternateAssemblyLoadContext != null) + { + return AlternateAssemblyLoadContext.LoadFromStream(new MemoryStream(rawAssembly), new MemoryStream(rawPdb)); + } +#endif + return Assembly.Load(rawAssembly, rawPdb); + } + private Assembly LoadAssemblyFrom(string assemblyFile) { #if NETCORE