Repository navigation
Expand file tree
/
Copy pathScript.cs
More file actions
586 lines (513 loc) · 21.5 KB
/
Copy pathScript.cs
File metadata and controls
586 lines (513 loc) · 21.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
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// 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.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
using Microsoft.CodeAnalysis;
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 Task<T> ScriptRunner<T>(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 Script _previous;
private ScriptBuilder _lazyBuilder;
private Compilation _lazyCompilation;
internal Script(string code, string path, ScriptOptions options, Type globalsType, ScriptBuilder builder, Script previous)
{
_code = code ?? "";
_path = path ?? "";
_options = options ?? ScriptOptions.Default;
_globalsType = globalsType;
_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 abstract Type ReturnType
{
get;
}
/// <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;
}
}
internal ScriptBuilder LazyBuilder
{
get { return _lazyBuilder; }
}
/// <summary>
/// Creates a new version of this script with the specified options.
/// </summary>
public Script WithOptions(ScriptOptions options)
{
return this.WithOptionsInternal(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.WithCodeInternal(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.WithPathInternal(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.WithGlobalsTypeInternal(globalsType);
}
/// <summary>
/// Creates a new version of this script with the previous script specified.
/// </summary>
public Script WithPrevious(Script previous)
{
return this.WithPreviousInternal(previous);
}
internal abstract Script WithOptionsInternal(ScriptOptions options);
internal abstract Script WithCodeInternal(string code);
internal abstract Script WithPathInternal(string path);
internal abstract Script WithGlobalsTypeInternal(Type globalsType);
internal abstract Script WithPreviousInternal(Script previous);
/// <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>
/// Creates a <see cref="Compilation"/> instances based on script members.
/// </summary>
protected abstract Compilation CreateCompilation();
/// <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>
/// <param name="cancellationToken">Cancellation token.</param>
/// <returns>A <see cref="ScriptState"/> that represents the state after running the script, including all declared variables and return value.</returns>
public ScriptState RunAsync(object globals = null, CancellationToken cancellationToken = default(CancellationToken))
{
return RunInternalAsync(globals, cancellationToken);
}
protected abstract ScriptState RunInternalAsync(object globals, CancellationToken cancellationToken);
/// <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.GetExecutorInternal(CancellationToken.None);
}
internal abstract Func<object[], Task> GetExecutorInternal(CancellationToken cancellationToken);
internal void GatherSubmissionExecutors(ArrayBuilder<Func<object[], Task>> executors, CancellationToken cancellationToken)
{
var previous = this.Previous;
if (previous != null)
{
previous.GatherSubmissionExecutors(executors, cancellationToken);
}
executors.Add(this.GetExecutorInternal(cancellationToken));
}
///<summary>
/// Continue running script from the point after the intermediate state was produced.
///</summary>
internal bool TryRunFrom(ScriptState state, out ScriptExecutionState executionState, out object value)
{
if (state.Script == this)
{
value = state.ReturnValue;
executionState = state.ExecutionState.FreezeAndClone();
return true;
}
var previous = this.Previous;
if (previous != null && previous.TryRunFrom(state, out executionState, out value))
{
value = this.RunSubmission(executionState);
return true;
}
else
{
// couldn't find starting point to continue running from.
value = null;
executionState = null;
return false;
}
}
internal abstract object RunSubmission(ScriptExecutionState executionState);
}
public abstract class Script<T> : Script
{
private Func<object[], Task<T>> _lazyExecutor;
private Func<ScriptExecutionState, Task<T>> _lazyAggrateScriptExecutor;
internal Script(string code, string path, ScriptOptions options, Type globalsType, ScriptBuilder builder, Script previous) :
base(code, path, options, globalsType, builder, previous)
{
}
public sealed override Type ReturnType
{
get { return typeof(T); }
}
protected sealed override ScriptState RunInternalAsync(object globals, CancellationToken cancellationToken)
{
return RunAsync(globals, cancellationToken);
}
public new ScriptState<T> RunAsync(object globals = null, CancellationToken cancellationToken = default(CancellationToken))
{
var state = globals as ScriptState;
if (state != null)
{
if (state.Script == this)
{
// this state is already the output of running this script.
return (ScriptState<T>)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 (ScriptState<T>)this.WithPrevious(state.Script).RunAsync(state, cancellationToken);
}
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<T>(executionState, (Task<T>)value, this);
}
else
{
throw new InvalidOperationException(ScriptingResources.StartingStateIncompatible);
}
}
}
else
{
var globalsType = this.GlobalsType;
if (globalsType != null)
{
if (globals == null)
{
throw new ArgumentNullException(nameof(globals));
}
else
{
var runtimeType = globals.GetType().GetTypeInfo();
var globalsTypeInfo = globalsType.GetTypeInfo();
if (!globalsTypeInfo.IsAssignableFrom(runtimeType))
{
throw new ArgumentException(string.Format(ScriptingResources.GlobalsNotAssignable, runtimeType, globalsTypeInfo));
}
}
}
else if (globals != null)
{
// make sure we are running from a script with matching globals type
return (ScriptState<T>)this.WithGlobalsType(globals.GetType()).RunAsync(globals, cancellationToken);
}
// 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<T>(executionState, value, this);
}
else
{
// otherwise run the aggregate script.
var executor = this.GetAggregateScriptExecutor(CancellationToken.None);
var value = executor(executionState);
return new ScriptState<T>(executionState, value, this);
}
}
}
internal sealed override object RunSubmission(ScriptExecutionState executionState)
{
var executor = this.GetExecutor(CancellationToken.None);
return executionState.RunSubmission(executor);
}
public new Script<T> WithOptions(ScriptOptions options)
{
return (options == this.Options) ?
this :
this.Make(this.Code, this.Path, options, this.GlobalsType, this.LazyBuilder, this.Previous);
}
public new Script<T> WithCode(string code)
{
if (code == null)
{
code = "";
}
return (code == this.Code) ?
this :
this.Make(code, this.Path, this.Options, this.GlobalsType, this.LazyBuilder, this.Previous);
}
public new Script<T> WithPath(string path)
{
if (path == null)
{
path = "";
}
return (path == this.Path) ?
this :
this.Make(this.Code, path, this.Options, this.GlobalsType, this.LazyBuilder, this.Previous);
}
public new Script<T> WithGlobalsType(Type globalsType)
{
return (globalsType == this.GlobalsType) ?
this :
this.Make(this.Code, this.Path, this.Options, globalsType, this.LazyBuilder, this.Previous);
}
public new Script<T> WithPrevious(Script previous)
{
var globalsType = (previous == null) ? this.GlobalsType : previous.GlobalsType;
return (globalsType == this.GlobalsType) && (previous == this.Previous) ?
this :
this.Make(this.Code, this.Path, this.Options, globalsType, this.LazyBuilder, previous);
}
internal sealed override Script WithOptionsInternal(ScriptOptions options)
{
return this.WithOptions(options);
}
internal sealed override Script WithCodeInternal(string code)
{
return this.WithCode(code);
}
internal sealed override Script WithPathInternal(string path)
{
return this.WithPath(path);
}
internal sealed override Script WithGlobalsTypeInternal(Type globalsType)
{
return this.WithGlobalsType(globalsType);
}
internal sealed override Script WithPreviousInternal(Script previous)
{
return this.WithPrevious(previous);
}
/// <summary>
/// Creates a new version of this script with the <see cref="ScriptBuilder"/> specified.
/// </summary>
internal Script<T> WithBuilder(ScriptBuilder builder)
{
return (builder == this.LazyBuilder) ?
this :
this.Make(this.Code, this.Path, this.Options, this.GlobalsType, builder, this.Previous);
}
/// <summary>
/// Creates a new instance of a script of this type.
/// </summary>
internal abstract Script<T> Make(string code, string path, ScriptOptions options, Type globalsType, ScriptBuilder builder, Script previous);
/// <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 = this.Options.References;
if (this.GlobalsType != null)
{
var globalsTypeAssembly = MetadataReference.CreateFromAssemblyInternal(this.GlobalsType.GetTypeInfo().Assembly);
if (!references.Contains(globalsTypeAssembly))
{
references = references.Add(globalsTypeAssembly);
}
}
var previous = this.Previous;
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>
/// Gets the executor that will run this portion of the script only. (does not include any previous scripts).
/// </summary>
private Func<object[], Task<T>> 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<T>(this, diagnostics, cancellationToken);
// emit can fail due to compilation errors or because there is nothing to emit:
if (diagnostics.HasAnyErrors())
{
CompilationError(diagnostics);
}
if (executor == null)
{
executor = (s) => Task.FromResult(default(T));
}
Interlocked.CompareExchange(ref _lazyExecutor, executor, null);
}
finally
{
diagnostics.Free();
}
}
return _lazyExecutor;
}
internal sealed override Func<object[], Task> GetExecutorInternal(CancellationToken cancellationToken)
{
return this.GetExecutor(cancellationToken);
}
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<T> CreateDelegate(CancellationToken cancellationToken = default(CancellationToken))
{
var executor = this.GetAggregateScriptExecutor(cancellationToken);
return globals => executor(ScriptExecutionState.Create(globals));
}
/// <summary>
/// Creates an executor that while run the entire aggregate script (all submissions).
/// </summary>
private Func<ScriptExecutionState, Task<T>> GetAggregateScriptExecutor(CancellationToken cancellationToken)
{
if (_lazyAggrateScriptExecutor == null)
{
var builder = ArrayBuilder<Func<object[], Task>>.GetInstance();
this.GatherSubmissionExecutors(builder, cancellationToken);
var executors = builder.ToImmutableAndFree();
Debug.Assert(executors.Length > 0);
// make a function to run through all submissions in order.
Func<ScriptExecutionState, Task<T>> aggregateExecutor = state =>
{
Task result = null;
foreach (var executor in executors)
{
result = state.RunSubmission(executor);
Debug.Assert(result != null);
}
return (Task<T>)result;
};
Interlocked.CompareExchange(ref _lazyAggrateScriptExecutor, aggregateExecutor, null);
}
return _lazyAggrateScriptExecutor;
}
}
}