forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharpScriptCompilerEngine.cs
More file actions
119 lines (99 loc) · 4.66 KB
/
CSharpScriptCompilerEngine.cs
File metadata and controls
119 lines (99 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using ScriptCs.Contracts;
using ScriptCs.Exceptions;
using Microsoft.CodeAnalysis.Emit;
namespace ScriptCs.Engine.Roslyn
{
public abstract class CSharpScriptCompilerEngine : CommonScriptEngine
{
private const string CompiledScriptClass = "Submission#0";
private const string CompiledScriptMethod = "<Factory>";
private readonly ILog _log;
protected CSharpScriptCompilerEngine(IScriptHostFactory scriptHostFactory, ILogProvider logProvider)
: base(scriptHostFactory, logProvider)
{
Guard.AgainstNullArgument("logProvider", logProvider);
_log = logProvider.ForCurrentType();
}
protected abstract bool ShouldCompile();
protected abstract Assembly LoadAssembly(byte[] exeBytes, byte[] pdbBytes);
protected abstract Assembly LoadAssemblyFromCache();
protected override ScriptResult Execute(string code, object globals, SessionState<ScriptState> sessionState)
{
return ShouldCompile()
? CompileAndExecute(code, globals)
: InvokeEntryPointMethod(globals, LoadAssemblyFromCache());
}
protected override ScriptState GetScriptState(string code, object globals)
{
return null;
}
protected ScriptResult CompileAndExecute(string code, object globals)
{
_log.Debug("Compiling submission");
try
{
var script = CSharpScript.Create(code, ScriptOptions, globals.GetType());
var compilation = script.GetCompilation();
using (var exeStream = new MemoryStream())
using (var pdbStream = new MemoryStream())
{
var result = compilation.Emit(exeStream, pdbStream: pdbStream, options: new EmitOptions().
WithDebugInformationFormat(GetPlatformSpecificDebugInformationFormat()));
if (result.Success)
{
_log.Debug("Compilation was successful.");
var assembly = LoadAssembly(exeStream.ToArray(), pdbStream.ToArray());
return InvokeEntryPointMethod(globals, assembly);
}
var errors = string.Join(Environment.NewLine, result.Diagnostics.Select(x => x.ToString()));
_log.ErrorFormat("Error occurred when compiling: {0})", errors);
return new ScriptResult(compilationException: new ScriptCompilationException(errors));
}
}
catch (Exception compileException)
{
//we catch Exception rather than CompilationErrorException because there might be issues with assembly loading too
return new ScriptResult(compilationException: new ScriptCompilationException(compileException.Message, compileException));
}
}
private ScriptResult InvokeEntryPointMethod(object globals, Assembly assembly)
{
_log.Debug("Retrieving compiled script class (reflection).");
// the following line can throw NullReferenceException, if that happens it's useful to notify that an error ocurred
var type = assembly.GetType(CompiledScriptClass);
_log.Debug("Retrieving compiled script method (reflection).");
var method = type.GetMethod(CompiledScriptMethod, BindingFlags.Static | BindingFlags.Public);
try
{
_log.Debug("Invoking method.");
var submissionStates = new object[2];
submissionStates[0] = globals;
var result = method.Invoke(null, new[] {submissionStates}) as Task<object>;
return new ScriptResult(returnValue: result.GetAwaiter().GetResult());
}
catch (Exception executeException)
{
_log.Error("An error occurred when executing the scripts.");
var ex = executeException.InnerException ?? executeException;
return new ScriptResult(executionException: ex);
}
}
private static DebugInformationFormat GetPlatformSpecificDebugInformationFormat()
{
// Mono, use PortablePdb
if (Type.GetType("Mono.Runtime") != null)
{
return DebugInformationFormat.PortablePdb;
}
// otherwise standard PDB
return DebugInformationFormat.Pdb;
}
}
}