Repository navigation
Expand file tree
/
Copy pathCSharpScript.cs
More file actions
238 lines (213 loc) · 11.5 KB
/
Copy pathCSharpScript.cs
File metadata and controls
238 lines (213 loc) · 11.5 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Globalization;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp;
namespace Microsoft.CodeAnalysis.Scripting.CSharp
{
/// <summary>
/// A factory for creating and running C# scripts.
/// </summary>
public static class CSharpScript
{
/// <summary>
/// Create a new C# script.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
/// <typeparam name="T">The return type of the script</typeparam>
public static Script<T> Create<T>(string code, ScriptOptions options)
{
return new CSharpScript<T>(code, null, options, null, null, null);
}
/// <summary>
/// Create a new C# script.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
public static Script<object> Create(string code, ScriptOptions options)
{
return Create<object>(code, options);
}
/// <summary>
/// Create a new C# script.
/// </summary>
/// <param name="code">The source code of the script.</param>
public static Script<object> Create(string code)
{
return Create<object>(code, null);
}
/// <summary>
/// Run a C# script.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
/// <param name="globals">An object instance whose members can be accessed by the script as global variables,
/// or a <see cref="ScriptState"/> instance that was the output from a previously run script.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <typeparam name="T">The return type of the submission</typeparam>
public static ScriptState<T> RunAsync<T>(string code, ScriptOptions options, object globals, CancellationToken cancellationToken = default(CancellationToken))
{
return Create<T>(code, options).RunAsync(globals, cancellationToken);
}
/// <summary>
/// Run a C# script.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
/// <param name="globals">An object instance whose members can be accessed by the script as global variables,
/// or a <see cref="ScriptState"/> instance that was the output from a previously run script.</param>
/// <param name="cancellationToken">Cancellation token.</param>
public static ScriptState<object> RunAsync(string code, ScriptOptions options, object globals, CancellationToken cancellationToken = default(CancellationToken))
{
return RunAsync<object>(code, options, globals, cancellationToken);
}
/// <summary>
/// Run a C# script.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
/// <param name="cancellationToken">Cancellation token.</param>
public static ScriptState<object> RunAsync(string code, ScriptOptions options, CancellationToken cancellationToken = default(CancellationToken))
{
return RunAsync<object>(code, options, null, cancellationToken);
}
/// <summary>
/// Run a C# script.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="globals">An object instance whose members can be accessed by the script as global variables,
/// or a <see cref="ScriptState"/> instance that was the output from a previously run script.</param>
/// <param name="cancellationToken">Cancellation token.</param>
public static ScriptState<object> RunAsync(string code, object globals, CancellationToken cancellationToken = default(CancellationToken))
{
return RunAsync<object>(code, null, globals, cancellationToken);
}
/// <summary>
/// Run a C# script.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="cancellationToken">Cancellation token.</param>
public static ScriptState<object> RunAsync(string code, CancellationToken cancellationToken = default(CancellationToken))
{
return RunAsync<object>(code, null, null, cancellationToken);
}
/// <summary>
/// Run a C# script and return its resulting value.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
/// <param name="globals">An object instance whose members can be accessed by the script as global variables,
/// or a <see cref="ScriptState"/> instance that was the output from a previously run script.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <typeparam name="T">The return type of the submission</typeparam>
/// <return>Returns the value returned by running the script.</return>
public static Task<T> EvaluateAsync<T>(string code, ScriptOptions options, object globals, CancellationToken cancellationToken = default(CancellationToken))
{
return RunAsync<T>(code, options, globals, cancellationToken).ReturnValue;
}
/// <summary>
/// Run a C# script and return its resulting value.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
/// <param name="globals">An object instance whose members can be accessed by the script as global variables,
/// or a <see cref="ScriptState"/> instance that was the output from a previously run script.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <return>Returns the value returned by running the script.</return>
public static Task<object> EvaluateAsync(string code, ScriptOptions options, object globals, CancellationToken cancellationToken = default(CancellationToken))
{
return EvaluateAsync<object>(code, options, globals, cancellationToken);
}
/// <summary>
/// Run a C# script and return its resulting value.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <return>Returns the value returned by running the script.</return>
public static Task<object> EvaluateAsync(string code, ScriptOptions options, CancellationToken cancellationToken = default(CancellationToken))
{
return EvaluateAsync<object>(code, options, null, cancellationToken);
}
/// <summary>
/// Run a C# script and return its resulting value.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="globals">An object instance whose members can be accessed by the script as global variables,
/// or a <see cref="ScriptState"/> instance that was the output from a previously run script.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <return>Returns the value returned by running the script.</return>
public static Task<object> EvaluateAsync(string code, object globals, CancellationToken cancellationToken = default(CancellationToken))
{
return EvaluateAsync<object>(code, null, globals, cancellationToken);
}
/// <summary>
/// Run a C# script and return its resulting value.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="cancellationToken">Cancellation token.</param>
/// <return>Returns the value returned by running the script.</return>
public static Task<object> EvaluateAsync(string code, CancellationToken cancellationToken = default(CancellationToken))
{
return EvaluateAsync<object>(code, null, null, cancellationToken);
}
}
internal sealed class CSharpScript<T> : Script<T>
{
internal CSharpScript(string code, string path, ScriptOptions options, Type globalsType, ScriptBuilder builder, Script previous)
: base(code, path, options, globalsType, builder, previous)
{
}
internal override Script<T> Make(string code, string path, ScriptOptions options, Type globalsType, ScriptBuilder builder, Script previous)
{
return new CSharpScript<T>(code, path, options, globalsType, builder, previous);
}
#region Compilation
private static readonly CSharpParseOptions s_defaultInteractive = new CSharpParseOptions(languageVersion: LanguageVersion.CSharp6, kind: SourceCodeKind.Interactive);
private static readonly CSharpParseOptions s_defaultScript = new CSharpParseOptions(languageVersion: LanguageVersion.CSharp6, kind: SourceCodeKind.Script);
protected override Compilation CreateCompilation()
{
Compilation previousSubmission = null;
if (this.Previous != null)
{
previousSubmission = this.Previous.GetCompilation();
}
var references = this.GetReferencesForCompilation();
var parseOptions = this.Options.IsInteractive ? s_defaultInteractive : s_defaultScript;
var tree = SyntaxFactory.ParseSyntaxTree(this.Code, parseOptions, path: this.Path);
string assemblyName, submissionTypeName;
this.Builder.GenerateSubmissionId(out assemblyName, out submissionTypeName);
var compilation = CSharpCompilation.CreateSubmission(
assemblyName,
tree,
references,
new CSharpCompilationOptions(
outputKind: OutputKind.DynamicallyLinkedLibrary,
mainTypeName: null,
scriptClassName: submissionTypeName,
usings: this.Options.Namespaces,
optimizationLevel: OptimizationLevel.Debug, // TODO
checkOverflow: false, // TODO
allowUnsafe: true, // TODO
platform: Platform.AnyCpu,
warningLevel: 4,
xmlReferenceResolver: null, // don't support XML file references in interactive (permissions & doc comment includes)
sourceReferenceResolver: SourceFileResolver.Default, // TODO
metadataReferenceResolver: this.Options.ReferenceResolver,
assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default
),
previousSubmission,
this.ReturnType,
this.GlobalsType
);
return compilation;
}
protected override string FormatDiagnostic(Diagnostic diagnostic, CultureInfo culture)
{
return CSharpDiagnosticFormatter.Instance.Format(diagnostic, culture);
}
#endregion
}
}