Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to step into scripts when CompileWithDebug = true #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions Westwind.Scripting/CSharpScriptExecution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -874,8 +874,20 @@ public Task<string> ExecuteScriptAsync<TModelType>(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;

Expand All @@ -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
Expand All @@ -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> { EmbeddedText.FromSource(pePath, sourceText) },
options: new EmitOptions(debugInformationFormat: debugOptions, pdbFilePath: pdbPath));
}
else
else
compilationResult = compilation.Emit(codeStream);

// Compilation Error handling
Expand All @@ -928,15 +946,18 @@ 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;
}
}

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);
}
Expand Down Expand Up @@ -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
Expand Down