Repository navigation
Expand file tree
/
Copy pathScript.cs
More file actions
520 lines (463 loc) · 19.2 KB
/
Copy pathScript.cs
File metadata and controls
520 lines (463 loc) · 19.2 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// 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.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis.Scripting
{
/// <summary>
/// A delegate that will run a script when invoked.
/// </summary>
/// <param name="globals">An object instance whose members can be accessed by the script as global variables.</param>
public delegate object ScriptRunner(object globals = null);
/// <summary>
/// A class that represents a script that you can run.
///
/// Create a script using a language specific script class such as CSharpScript or VisualBasicScript.
/// </summary>
public abstract class Script
{
private readonly string _code;
private readonly string _path;
private readonly ScriptOptions _options;
private readonly Type _globalsType;
private readonly Type _returnType;
private readonly Script _previous;
private ScriptBuilder _lazyBuilder;
private Compilation _lazyCompilation;
private Func<object[], object> _lazyExecutor;
private Func<ScriptExecutionState, object> _lazyAggrateScriptExecutor;
internal Script(string code, string path, ScriptOptions options, Type globalsType, Type returnType, ScriptBuilder builder, Script previous)
{
_code = code ?? "";
_path = path ?? "";
_options = options ?? ScriptOptions.Default;
_globalsType = globalsType;
_returnType = returnType ?? typeof(object);
_previous = previous;
if (_previous != null && builder != null && _previous._lazyBuilder != builder)
{
throw new ArgumentException("Incompatible script builder.");
}
_lazyBuilder = builder;
}
/// <summary>
/// A script that will run first when this script is run.
/// Any declarations made in the previous script can be referenced in this script.
/// The end state from running this script includes all declarations made by both scripts.
/// </summary>
public Script Previous
{
get { return _previous; }
}
/// <summary>
/// The options used by this script.
/// </summary>
public ScriptOptions Options
{
get { return _options; }
}
/// <summary>
/// The source code of the script.
/// </summary>
public string Code
{
get { return _code; }
}
/// <summary>
/// The path to the source if it originated from a file.
/// </summary>
public string Path
{
get { return _path; }
}
/// <summary>
/// The type of an object whose members can be accessed by the script as global variables.
/// </summary>
public Type GlobalsType
{
get { return _globalsType; }
}
/// <summary>
/// The expected return type of the script.
/// </summary>
public Type ReturnType
{
get { return _returnType; }
}
/// <summary>
/// The <see cref="ScriptBuilder"/> that will be used to build the script before running.
/// </summary>
internal ScriptBuilder Builder
{
get
{
if (_lazyBuilder == null)
{
ScriptBuilder tmp;
if (_previous != null)
{
tmp = _previous.Builder;
}
else
{
tmp = new ScriptBuilder();
}
Interlocked.CompareExchange(ref _lazyBuilder, tmp, null);
}
return _lazyBuilder;
}
}
/// <summary>
/// Creates a new version of this script with the specified options.
/// </summary>
public Script WithOptions(ScriptOptions options)
{
return this.With(options: options);
}
/// <summary>
/// Creates a new version of this script with the source code specified.
/// </summary>
/// <param name="code">The source code of the script.</param>
public Script WithCode(string code)
{
return this.With(code: code ?? "");
}
/// <summary>
/// Creates a new version of this script with the path specified.
/// The path is optional. It can be used to associate the script code with a file path.
/// </summary>
public Script WithPath(string path)
{
return this.With(path: path ?? "");
}
/// <summary>
/// Creates a new version of this script with the specified globals type.
/// The members of this type can be accessed by the script as global variables.
/// </summary>
/// <param name="globalsType">The type that defines members that can be accessed by the script.</param>
public Script WithGlobalsType(Type globalsType)
{
return this.With(globalsType: globalsType);
}
/// <summary>
/// Creates a new version of this script with the specified return type.
/// The default return type for a script is <see cref="System.Object"/>.
/// Specifying a return type may be necessary for proper understanding of some scripts.
/// </summary>
public Script WithReturnType(Type returnType)
{
return this.With(returnType: returnType);
}
/// <summary>
/// Creates a new version of this script with the previous script specified.
/// </summary>
public Script WithPrevious(Script script)
{
if (script != null)
{
return this.With(previous: script, globalsType: script.GlobalsType);
}
else
{
return this.With(previous: script);
}
}
/// <summary>
/// Creates a new verion of this script with the <see cref="ScriptBuilder"/> specified.
/// </summary>
internal Script WithBuilder(ScriptBuilder builder)
{
return this.With(builder: builder);
}
private Script With(
Optional<string> code = default(Optional<string>),
Optional<string> path = default(Optional<string>),
Optional<ScriptOptions> options = default(Optional<ScriptOptions>),
Optional<Type> globalsType = default(Optional<Type>),
Optional<Type> returnType = default(Optional<Type>),
Optional<ScriptBuilder> builder = default(Optional<ScriptBuilder>),
Optional<Script> previous = default(Optional<Script>))
{
var newCode = code.HasValue ? code.Value : _code;
var newPath = path.HasValue ? path.Value : _path;
var newOptions = options.HasValue ? options.Value : _options;
var newGlobalsType = globalsType.HasValue ? globalsType.Value : _globalsType;
var newReturnType = returnType.HasValue ? returnType.Value : _returnType;
var newBuilder = builder.HasValue ? builder.Value : _lazyBuilder;
var newPrevious = previous.HasValue ? previous.Value : _previous;
if (ReferenceEquals(newCode, _code) &&
ReferenceEquals(newPath, _path) &&
newOptions == _options &&
newGlobalsType == _globalsType &&
newReturnType == _returnType &&
newBuilder == _lazyBuilder &&
newPrevious == this.Previous)
{
return this;
}
else
{
return this.Make(newCode, newPath, newOptions, newGlobalsType, newReturnType, newBuilder, newPrevious);
}
}
/// <summary>
/// Creates a new instance of a script of this type.
/// </summary>
internal abstract Script Make(string code, string path, ScriptOptions options, Type globalsType, Type returnType, ScriptBuilder builder, Script previous);
/// <summary>
/// Runs this script.
/// </summary>
/// <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>
/// <returns>A <see cref="ScriptState"/> that represents the state after running the script, including all declared variables and return value.</returns>
public ScriptState Run(object globals = null)
{
var state = globals as ScriptState;
if (state != null)
{
if (state.Script == this)
{
// this state is already the output of running this script.
return state;
}
else if (this.Previous == null)
{
// if this script is unbound (no previous script) then run this script bound to the state's script
return this.WithPrevious(state.Script).Run(state);
}
else
{
// attempt to run script forward from the point after the specified state was computed.
ScriptExecutionState executionState;
object value;
if (this.TryRunFrom(state, out executionState, out value))
{
return new ScriptState(executionState, value, this);
}
else
{
throw new InvalidOperationException(ScriptingResources.StartingStateIncompatible);
}
}
}
else
{
if (_globalsType != null && globals == null)
{
throw new ArgumentNullException("globals");
}
else if (globals != null && _globalsType != null)
{
var runtimeType = globals.GetType();
if (!_globalsType.IsAssignableFrom(runtimeType))
{
throw new ArgumentException(string.Format(ScriptingResources.GlobalsNotAssignable, runtimeType, _globalsType));
}
}
// make sure we are running from a script with matching globals type
if (globals != null && _globalsType == null)
{
return this.WithGlobalsType(globals.GetType()).Run(globals);
}
// run this script from the start with the specified globals
var executionState = ScriptExecutionState.Create(globals);
if (this.Previous == null)
{
// only single submission, so just execute it directly.
var executor = this.GetExecutor(CancellationToken.None);
var value = executionState.RunSubmission(executor);
return new ScriptState(executionState, value, this);
}
else
{
// otherwise run the aggregate script.
var executor = this.GetAggregateScriptExecutor(CancellationToken.None);
var value = executor(executionState);
return new ScriptState(executionState, value, this);
}
}
}
///<summary>
/// Continue running script from the point after the intermediate state was produced.
///</summary>
private bool TryRunFrom(ScriptState state, out ScriptExecutionState executionState, out object value)
{
if (state.Script == this)
{
value = state.ReturnValue;
executionState = state.ExecutionState.FreezeAndClone();
return true;
}
else if (_previous != null && _previous.TryRunFrom(state, out executionState, out value))
{
var executor = this.GetExecutor(CancellationToken.None);
value = executionState.RunSubmission(executor);
return true;
}
else
{
// couldn't find starting point to continue running from.
value = null;
executionState = null;
return false;
}
}
/// <summary>
/// Get's the <see cref="Compilation"/> that represents the semantics of the script.
/// </summary>
public Compilation GetCompilation()
{
if (_lazyCompilation == null)
{
var compilation = this.CreateCompilation();
Interlocked.CompareExchange(ref _lazyCompilation, compilation, null);
}
return _lazyCompilation;
}
/// <summary>
/// Forces the script through the build step.
/// If not called directly, the build step will occur on the first call to Run.
/// </summary>
public void Build()
{
this.GetExecutor(CancellationToken.None);
}
/// <summary>
/// Gets the references that need to be assigned to the compilation.
/// This can be different than the list of references defined by the <see cref="ScriptOptions"/> instance.
/// </summary>
protected ImmutableArray<MetadataReference> GetReferencesForCompilation()
{
var references = _options.References;
if (this.GlobalsType != null)
{
var globalsTypeAssembly = MetadataReference.CreateFromAssembly(this.GlobalsType.Assembly);
if (!references.Contains(globalsTypeAssembly))
{
references = references.Add(globalsTypeAssembly);
}
}
if (_previous == null)
{
return references;
}
else
{
// TODO (tomat): RESOLVED? bound imports should be reused from previous submission instead of passing
// them to every submission in the chain. See bug #7802.
var compilation = _previous.GetCompilation();
return ImmutableArray.CreateRange(references.Union(compilation.References));
}
}
/// <summary>
/// Creates a <see cref="Compilation"/> instances based on script members.
/// </summary>
protected abstract Compilation CreateCompilation();
/// <summary>
/// Gets the executor that will run this portion of the script only. (does not include any previous scripts).
/// </summary>
private Func<object[], object> GetExecutor(CancellationToken cancellationToken)
{
if (_lazyExecutor == null)
{
var compilation = this.GetCompilation();
var diagnostics = DiagnosticBag.GetInstance();
try
{
// get compilation diagnostics first.
diagnostics.AddRange(compilation.GetParseDiagnostics());
if (diagnostics.HasAnyErrors())
{
CompilationError(diagnostics);
}
diagnostics.Clear();
var executor = this.Builder.Build(this, diagnostics, cancellationToken);
if (diagnostics.HasAnyErrors())
{
CompilationError(diagnostics);
}
if (executor == null)
{
executor = (s) => null;
}
Interlocked.CompareExchange(ref _lazyExecutor, executor, null);
}
finally
{
diagnostics.Free();
}
}
return _lazyExecutor;
}
private void CompilationError(DiagnosticBag diagnostics)
{
var resolvedLocalDiagnostics = diagnostics.AsEnumerable();
var firstError = resolvedLocalDiagnostics.FirstOrDefault(d => d.Severity == DiagnosticSeverity.Error);
if (firstError != null)
{
throw new CompilationErrorException(FormatDiagnostic(firstError, CultureInfo.CurrentCulture),
(resolvedLocalDiagnostics.AsImmutable()));
}
}
protected abstract string FormatDiagnostic(Diagnostic diagnostic, CultureInfo culture);
/// <summary>
/// Creates a delegate that will execute this script when invoked.
/// </summary>
public ScriptRunner CreateDelegate(CancellationToken cancellationToken = default(CancellationToken))
{
var executor = this.GetAggregateScriptExecutor(cancellationToken);
return (globals) =>
{
var executionState = ScriptExecutionState.Create(globals);
return executor(executionState);
};
}
/// <summary>
/// Creates an executor that while run the entire aggregate script (all submissions).
/// </summary>
private Func<ScriptExecutionState, object> GetAggregateScriptExecutor(CancellationToken cancellationToken)
{
if (_lazyAggrateScriptExecutor == null)
{
Func<ScriptExecutionState, object> aggregateExecutor;
if (_previous == null)
{
// only one submission, just use the submission's entry point.
var executor = this.GetExecutor(cancellationToken);
aggregateExecutor = state => state.RunSubmission(executor);
}
else
{
// make a function to runs through all submissions in order.
var executors = new List<Func<object[], object>>();
this.GatherSubmissionExecutors(executors, cancellationToken);
aggregateExecutor = state =>
{
object result = null;
foreach (var exec in executors)
{
result = state.RunSubmission(exec);
}
return result;
};
}
Interlocked.CompareExchange(ref _lazyAggrateScriptExecutor, aggregateExecutor, null);
}
return _lazyAggrateScriptExecutor;
}
private void GatherSubmissionExecutors(List<Func<object[], object>> executors, CancellationToken cancellationToken)
{
if (_previous != null)
{
_previous.GatherSubmissionExecutors(executors, cancellationToken);
}
executors.Add(this.GetExecutor(cancellationToken));
}
}
}