Repository navigation
Expand file tree
/
Copy pathCSharpScript.cs
More file actions
189 lines (167 loc) · 7.97 KB
/
Copy pathCSharpScript.cs
File metadata and controls
189 lines (167 loc) · 7.97 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
// 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.Diagnostics;
using System.Globalization;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
namespace Microsoft.CodeAnalysis.Scripting.CSharp
{
/// <summary>
/// A factory for creating and running csharp scripts.
/// </summary>
public sealed class CSharpScript : Script
{
private CSharpScript(string code, string path, ScriptOptions options, Type globalsType, Type returnType, ScriptBuilder builder, Script previous)
: base(code, path, options, globalsType, returnType, builder, previous)
{
}
internal override Script Make(string code, string path, ScriptOptions options, Type globalsType, Type returnType, ScriptBuilder builder, Script previous)
{
return new CSharpScript(code, path, options, globalsType, returnType, builder, previous);
}
/// <summary>
/// Create a new C# script.
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
/// </summary>
public static Script Create(string code, ScriptOptions options)
{
return new CSharpScript(code, null, options, null, typeof(object), null, null);
}
/// <summary>
/// Create a new C# script.
/// <param name="code">The source code of the script.</param>
/// </summary>
public static Script Create(string code)
{
return Create(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>
public static ScriptState Run(string code, ScriptOptions options, object globals)
{
return Create(code, options).Run(globals);
}
/// <summary>
/// Run a C# script.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <param name="options">The script options.</param>
public static ScriptState Run(string code, ScriptOptions options)
{
return Run(code, options, globals: null);
}
/// <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>
public static ScriptState Run(string code, object globals)
{
return Run(code, options: null, globals: globals);
}
/// <summary>
/// Run a C# script.
/// </summary>
/// <param name="code">The source code of the script.</param>
public static ScriptState Run(string code)
{
return Run(code, null, null);
}
/// <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>
/// <return>Returns the value returned by running the script.</return>
public static object Eval(string code, ScriptOptions options, object globals)
{
return Run(code, options, globals).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>
/// <return>Returns the value returned by running the script.</return>
public static object Eval(string code, ScriptOptions options)
{
return Run(code, options).ReturnValue;
}
/// <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>
/// <return>Returns the value returned by running the script.</return>
public static object Eval(string code, object globals)
{
return Run(code, globals).ReturnValue;
}
/// <summary>
/// Run a C# script and return its resulting value.
/// </summary>
/// <param name="code">The source code of the script.</param>
/// <return>Returns the value returned by running the script.</return>
public static object Eval(string code)
{
return Run(code).ReturnValue;
}
#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
}
}