Repository navigation
Expand file tree
/
Copy pathScriptEngine.cs
More file actions
312 lines (260 loc) · 11.1 KB
/
Copy pathScriptEngine.cs
File metadata and controls
312 lines (260 loc) · 11.1 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// 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.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.CodeAnalysis;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Scripting
{
/// <summary>
/// Represents a runtime execution context for C# scripts.
/// </summary>
internal abstract class ScriptEngine
{
public static readonly ImmutableArray<string> DefaultReferenceSearchPaths;
// state captured by session at creation time:
private ScriptOptions _options = ScriptOptions.Default;
private ScriptBuilder _builder;
static ScriptEngine()
{
DefaultReferenceSearchPaths = ImmutableArray.Create<string>(RuntimeEnvironment.GetRuntimeDirectory());
}
internal ScriptEngine(MetadataFileReferenceProvider metadataReferenceProvider, AssemblyLoader assemblyLoader)
{
if (metadataReferenceProvider == null)
{
metadataReferenceProvider = _options.AssemblyResolver.Provider;
}
if (assemblyLoader == null)
{
assemblyLoader = new InteractiveAssemblyLoader();
}
_builder = new ScriptBuilder(assemblyLoader);
_options = _options.WithReferenceProvider(metadataReferenceProvider);
string initialBaseDirectory;
try
{
initialBaseDirectory = Directory.GetCurrentDirectory();
}
catch
{
initialBaseDirectory = null;
}
_options = _options.WithBaseDirectory(initialBaseDirectory);
}
public MetadataFileReferenceProvider MetadataReferenceProvider
{
get { return _options.AssemblyResolver.Provider; }
}
public AssemblyLoader AssemblyLoader
{
get { return _builder.AssemblyLoader; }
}
internal ScriptBuilder Builder
{
get { return _builder; }
}
// TODO (tomat): Consider exposing FileResolver and removing BaseDirectory.
// We would need WithAssemblySearchPaths on FileResolver to implement SetReferenceSearchPaths
internal MetadataFileReferenceResolver MetadataReferenceResolver
{
get
{
return _options.AssemblyResolver.PathResolver;
}
// for testing
set
{
Debug.Assert(value != null);
_options = _options.WithReferenceResolver(value);
}
}
internal string AssemblyNamePrefix
{
get { return _builder.AssemblyNamePrefix; }
}
#region Script
internal abstract Script Create(string code, ScriptOptions options, Type globalsType, Type returnType);
#endregion
#region Session
// for testing only:
// TODO (tomat): Sessions generate uncollectible code since we don't know whether they would execute just one submissions or multiple.
// We need to address code collectibility of multi-submission sessions at some point (the CLR needs to be fixed). Meanwhile use this helper
// to force collectible code generation in order to keep test coverage.
internal Session CreateCollectibleSession()
{
return new Session(this, _options.WithIsCollectible(true), null);
}
internal Session CreateCollectibleSession<THostObject>(THostObject hostObject)
{
return new Session(this, _options.WithIsCollectible(true), hostObject, typeof(THostObject));
}
public Session CreateSession() // TODO (tomat): bool isCancellable = false
{
return new Session(this, _options, null);
}
public Session CreateSession(object hostObject) // TODO (tomat): bool isCancellable = false
{
if (hostObject == null)
{
throw new ArgumentNullException("hostObject");
}
return new Session(this, _options, hostObject, hostObject.GetType());
}
public Session CreateSession(object hostObject, Type hostObjectType) // TODO (tomat): bool isCancellable = false
{
if (hostObject == null)
{
throw new ArgumentNullException("hostObject");
}
if (hostObjectType == null)
{
throw new ArgumentNullException("hostObjectType");
}
Type actualType = hostObject.GetType();
if (!hostObjectType.IsAssignableFrom(actualType))
{
throw new ArgumentException(String.Format(ScriptingResources.CantAssignTo, actualType, hostObjectType), "hostObjectType");
}
return new Session(this, _options, hostObject, hostObjectType);
}
public Session CreateSession<THostObject>(THostObject hostObject) // TODO (tomat): bool isCancellable = false
where THostObject : class
{
if (hostObject == null)
{
throw new ArgumentNullException("hostObject");
}
return new Session(this, _options, hostObject, typeof(THostObject));
}
#endregion
#region State
/// <summary>
/// The base directory used to resolve relative paths to assembly references and
/// relative paths that appear in source code compiled by this script engine.
/// </summary>
/// <remarks>
/// If null relative paths won't be resolved and an error will be reported when the compiler encountrs such paths.
/// The value can be changed at any point in time. However the new value doesn't affect already compiled submissions.
/// The initial value is the current working directory if the current process, or null if not available.
/// Changing the base directory doesn't affect the process current working directory used by <see cref="System.IO"/> APIs.
/// </remarks>
public string BaseDirectory
{
get
{
return _options.BaseDirectory;
}
set
{
_options = _options.WithBaseDirectory(value);
}
}
public ImmutableArray<string> ReferenceSearchPaths
{
get { return _options.SearchPaths; }
}
public void SetReferenceSearchPaths(params string[] paths)
{
SetReferenceSearchPaths(ImmutableArray.CreateRange<string>(paths));
}
public void SetReferenceSearchPaths(IEnumerable<string> paths)
{
SetReferenceSearchPaths(ImmutableArray.CreateRange<string>(paths));
}
public void SetReferenceSearchPaths(ImmutableArray<string> paths)
{
MetadataFileReferenceResolver.ValidateSearchPaths(paths, "paths");
_options = _options.WithSearchPaths(paths);
}
/// <summary>
/// Returns a list of assemblies that are currently referenced by the engine.
/// </summary>
public ImmutableArray<MetadataReference> GetReferences()
{
return _options.References;
}
/// <summary>
/// Adds a reference to specified assembly.
/// </summary>
/// <param name="assemblyDisplayNameOrPath">Assembly display name or path.</param>
/// <exception cref="ArgumentNullException"><paramref name="assemblyDisplayNameOrPath"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="assemblyDisplayNameOrPath"/> is empty.</exception>
/// <exception cref="FileNotFoundException">Assembly file can't be found.</exception>
public void AddReference(string assemblyDisplayNameOrPath)
{
if (assemblyDisplayNameOrPath == null)
{
throw new ArgumentNullException(nameof(assemblyDisplayNameOrPath));
}
_options = _options.AddReferences(assemblyDisplayNameOrPath);
}
/// <summary>
/// Adds a reference to specified assembly.
/// </summary>
/// <param name="assembly">Runtime assembly. The assembly must be loaded from a file on disk. In-memory assemblies are not supported.</param>
/// <exception cref="ArgumentNullException"><paramref name="assembly"/> is null.</exception>
public void AddReference(Assembly assembly)
{
if (assembly == null)
{
throw new ArgumentNullException(nameof(assembly));
}
_options = _options.AddReferences(assembly);
}
/// <summary>
/// Adds a reference to specified assembly.
/// </summary>
/// <param name="reference">Assembly reference.</param>
/// <exception cref="ArgumentException"><paramref name="reference"/> is not an assembly reference (it's a module).</exception>
/// <exception cref="ArgumentNullException"><paramref name="reference"/> is null.</exception>
public void AddReference(MetadataReference reference)
{
if (reference == null)
{
throw new ArgumentNullException(nameof(reference));
}
if (reference.Properties.Kind != MetadataImageKind.Assembly)
{
throw new ArgumentException(ScriptingResources.ExpectedAnAssemblyReference, nameof(reference));
}
_options = _options.AddReferences(reference);
}
/// <summary>
/// Returns a list of imported namespaces.
/// </summary>
public ImmutableArray<string> GetImportedNamespaces()
{
return _options.Namespaces;
}
/// <summary>
/// Imports a namespace, an equivalent of executing "using <paramref name="namespace"/>;" (C#) or "Imports <paramref name="namespace"/>" (VB).
/// </summary>
/// <exception cref="ArgumentNullException"><paramref name="namespace"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="namespace"/> is not a valid namespace name.</exception>
public void ImportNamespace(string @namespace)
{
ValidateNamespace(@namespace);
// we don't report duplicates to get the same behavior as evaluating "using NS;" twice.
_options = _options.AddNamespaces(@namespace);
}
internal static void ValidateNamespace(string @namespace)
{
if (@namespace == null)
{
throw new ArgumentNullException(nameof(@namespace));
}
// Only check that the namespace is a CLR namespace name.
// If the namespace doesn't exist an error will be reported when compiling the next submission.
if (!@namespace.IsValidClrNamespaceName())
{
throw new ArgumentException("Invalid namespace name", "namespace");
}
}
#endregion
}
}