Skip to content

Commit 282963a

Browse files
authored
Merge pull request #631 from hrumhurum/gp-feature-a
Ability to isolate script execution with AssemblyLoadContext
2 parents b0aadfa + 353609e commit 282963a

19 files changed

+414
-57
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,3 +265,4 @@ project.json
265265
/build/dotnet-script
266266
/dotnet-script
267267
/.vscode
268+
/src/Dotnet.Script/Properties/launchSettings.json

src/Dotnet.Script.Core/Commands/ExecuteCodeCommand.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ public async Task<TReturn> Execute<TReturn>(ExecuteCodeCommandOptions options)
2222
{
2323
var sourceText = SourceText.From(options.Code);
2424
var context = new ScriptContext(sourceText, options.WorkingDirectory ?? Directory.GetCurrentDirectory(), options.Arguments, null, options.OptimizationLevel, ScriptMode.Eval, options.PackageSources);
25-
var compiler = GetScriptCompiler(!options.NoCache, _logFactory);
26-
var runner = new ScriptRunner(compiler, _logFactory, _scriptConsole);
25+
var compiler = new ScriptCompiler(_logFactory, !options.NoCache)
26+
{
27+
#if NETCOREAPP
28+
AssemblyLoadContext = options.AssemblyLoadContext
29+
#endif
30+
};
31+
var runner = new ScriptRunner(compiler, _logFactory, _scriptConsole)
32+
{
33+
#if NETCOREAPP
34+
AssemblyLoadContext = options.AssemblyLoadContext
35+
#endif
36+
};
2737
return await runner.Execute<TReturn>(context);
2838
}
29-
30-
private static ScriptCompiler GetScriptCompiler(bool useRestoreCache, LogFactory logFactory)
31-
{
32-
var compiler = new ScriptCompiler(logFactory, useRestoreCache);
33-
return compiler;
34-
}
3539
}
3640
}

src/Dotnet.Script.Core/Commands/ExecuteCodeCommandOptions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using Microsoft.CodeAnalysis;
2+
#if NETCOREAPP
3+
using System.Runtime.Loader;
4+
#endif
25

36
namespace Dotnet.Script.Core.Commands
47
{
@@ -20,5 +23,14 @@ public ExecuteCodeCommandOptions(string code, string workingDirectory, string[]
2023
public OptimizationLevel OptimizationLevel { get; }
2124
public bool NoCache { get; }
2225
public string[] PackageSources { get; }
26+
27+
#if NETCOREAPP
28+
#nullable enable
29+
/// <summary>
30+
/// Gets or sets a custom assembly load context to use for script execution.
31+
/// </summary>
32+
public AssemblyLoadContext? AssemblyLoadContext { get; init; }
33+
#nullable restore
34+
#endif
2335
}
2436
}

src/Dotnet.Script.Core/Commands/ExecuteInteractiveCommand.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ public ExecuteInteractiveCommand(ScriptConsole scriptConsole, LogFactory logFact
1818

1919
public async Task<int> Execute(ExecuteInteractiveCommandOptions options)
2020
{
21-
var compiler = new ScriptCompiler(_logFactory, useRestoreCache: false);
21+
var compiler = new ScriptCompiler(_logFactory, useRestoreCache: false)
22+
{
23+
#if NETCOREAPP
24+
AssemblyLoadContext = options.AssemblyLoadContext
25+
#endif
26+
};
2227
var runner = new InteractiveRunner(compiler, _logFactory, _scriptConsole, options.PackageSources);
2328

2429
if (options.ScriptFile == null)
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
using Microsoft.CodeAnalysis;
1+
#if NETCOREAPP
2+
using System.Runtime.Loader;
3+
#endif
24

35
namespace Dotnet.Script.Core.Commands
46
{
57
public class ExecuteInteractiveCommandOptions
68
{
7-
public ExecuteInteractiveCommandOptions(ScriptFile scriptFile, string[] arguments ,string[] packageSources)
9+
public ExecuteInteractiveCommandOptions(ScriptFile scriptFile, string[] arguments, string[] packageSources)
810
{
911
ScriptFile = scriptFile;
1012
Arguments = arguments;
@@ -14,5 +16,14 @@ public ExecuteInteractiveCommandOptions(ScriptFile scriptFile, string[] argument
1416
public ScriptFile ScriptFile { get; }
1517
public string[] Arguments { get; }
1618
public string[] PackageSources { get; }
19+
20+
#if NETCOREAPP
21+
#nullable enable
22+
/// <summary>
23+
/// Gets or sets a custom assembly load context to use for script execution.
24+
/// </summary>
25+
public AssemblyLoadContext? AssemblyLoadContext { get; init; }
26+
#nullable restore
27+
#endif
1728
}
1829
}

src/Dotnet.Script.Core/Commands/ExecuteLibraryCommand.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,20 @@ public async Task<TReturn> Execute<TReturn>(ExecuteLibraryCommandOptions options
2525
}
2626

2727
var absoluteFilePath = options.LibraryPath.GetRootedPath();
28-
var compiler = GetScriptCompiler(!options.NoCache, _logFactory);
29-
var runner = new ScriptRunner(compiler, _logFactory, _scriptConsole);
28+
var compiler = new ScriptCompiler(_logFactory, !options.NoCache)
29+
{
30+
#if NETCOREAPP
31+
AssemblyLoadContext = options.AssemblyLoadContext
32+
#endif
33+
};
34+
var runner = new ScriptRunner(compiler, _logFactory, _scriptConsole)
35+
{
36+
#if NETCOREAPP
37+
AssemblyLoadContext = options.AssemblyLoadContext
38+
#endif
39+
};
3040
var result = await runner.Execute<TReturn>(absoluteFilePath, options.Arguments);
3141
return result;
3242
}
33-
34-
private static ScriptCompiler GetScriptCompiler(bool useRestoreCache, LogFactory logFactory)
35-
{
36-
var compiler = new ScriptCompiler(logFactory, useRestoreCache);
37-
return compiler;
38-
}
3943
}
4044
}

src/Dotnet.Script.Core/Commands/ExecuteLibraryCommandOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#if NETCOREAPP
2+
using System.Runtime.Loader;
3+
#endif
4+
15
namespace Dotnet.Script.Core.Commands
26
{
37
public class ExecuteLibraryCommandOptions
@@ -12,5 +16,14 @@ public ExecuteLibraryCommandOptions(string libraryPath, string[] arguments, bool
1216
public string LibraryPath { get; }
1317
public string[] Arguments { get; }
1418
public bool NoCache { get; }
19+
20+
#if NETCOREAPP
21+
#nullable enable
22+
/// <summary>
23+
/// Gets or sets a custom assembly load context to use for script execution.
24+
/// </summary>
25+
public AssemblyLoadContext? AssemblyLoadContext { get; init; }
26+
#nullable restore
27+
#endif
1528
}
1629
}

src/Dotnet.Script.Core/Commands/ExecuteScriptCommand.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ public async Task<TReturn> Run<TReturn, THost>(ExecuteScriptCommandOptions optio
3232
}
3333

3434
var pathToLibrary = GetLibrary(options);
35-
return await ExecuteLibrary<TReturn>(pathToLibrary, options.Arguments, options.NoCache);
35+
36+
var libraryOptions = new ExecuteLibraryCommandOptions(pathToLibrary, options.Arguments, options.NoCache)
37+
{
38+
#if NETCOREAPP
39+
AssemblyLoadContext = options.AssemblyLoadContext
40+
#endif
41+
};
42+
return await new ExecuteLibraryCommand(_scriptConsole, _logFactory).Execute<TReturn>(libraryOptions);
3643
}
3744

3845
private async Task<TReturn> DownloadAndRunCode<TReturn>(ExecuteScriptCommandOptions executeOptions)
@@ -57,7 +64,12 @@ private string GetLibrary(ExecuteScriptCommandOptions executeOptions)
5764
return pathToLibrary;
5865
}
5966

60-
var options = new PublishCommandOptions(executeOptions.File, executionCacheFolder, "script", PublishType.Library, executeOptions.OptimizationLevel, executeOptions.PackageSources, null, executeOptions.NoCache);
67+
var options = new PublishCommandOptions(executeOptions.File, executionCacheFolder, "script", PublishType.Library, executeOptions.OptimizationLevel, executeOptions.PackageSources, null, executeOptions.NoCache)
68+
{
69+
#if NETCOREAPP
70+
AssemblyLoadContext = executeOptions.AssemblyLoadContext
71+
#endif
72+
};
6173
new PublishCommand(_scriptConsole, _logFactory).Execute(options);
6274
if (hash != null)
6375
{
@@ -124,11 +136,5 @@ public bool TryGetHash(string cacheFolder, out string hash)
124136
hash = File.ReadAllText(pathToHashFile);
125137
return true;
126138
}
127-
128-
private async Task<TReturn> ExecuteLibrary<TReturn>(string pathToLibrary, string[] arguments, bool noCache)
129-
{
130-
var options = new ExecuteLibraryCommandOptions(pathToLibrary, arguments, noCache);
131-
return await new ExecuteLibraryCommand(_scriptConsole, _logFactory).Execute<TReturn>(options);
132-
}
133139
}
134140
}

src/Dotnet.Script.Core/Commands/ExecuteScriptCommandOptions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
using Microsoft.CodeAnalysis;
2+
#if NETCOREAPP
3+
using System.Runtime.Loader;
4+
#endif
25

36
namespace Dotnet.Script.Core.Commands
47
{
@@ -20,5 +23,14 @@ public ExecuteScriptCommandOptions(ScriptFile file, string[] arguments, Optimiza
2023
public string[] PackageSources { get; }
2124
public bool IsInteractive { get; }
2225
public bool NoCache { get; }
26+
27+
#if NETCOREAPP
28+
#nullable enable
29+
/// <summary>
30+
/// Gets or sets a custom assembly load context to use for script execution.
31+
/// </summary>
32+
public AssemblyLoadContext? AssemblyLoadContext { get; init; }
33+
#nullable restore
34+
#endif
2335
}
2436
}

src/Dotnet.Script.Core/Commands/PublishCommand.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ public void Execute(PublishCommandOptions options)
2828
(options.PublishType == PublishType.Library ? Path.Combine(Path.GetDirectoryName(absoluteFilePath), "publish") : Path.Combine(Path.GetDirectoryName(absoluteFilePath), "publish", options.RuntimeIdentifier));
2929

3030
var absolutePublishDirectory = publishDirectory.GetRootedPath();
31-
var compiler = GetScriptCompiler(!options.NoCache, _logFactory);
31+
var compiler = new ScriptCompiler(_logFactory, !options.NoCache)
32+
{
33+
#if NETCOREAPP
34+
AssemblyLoadContext = options.AssemblyLoadContext
35+
#endif
36+
};
3237
var scriptEmitter = new ScriptEmitter(_scriptConsole, compiler);
3338
var publisher = new ScriptPublisher(_logFactory, scriptEmitter);
3439
var code = absoluteFilePath.ToSourceText();
@@ -43,12 +48,5 @@ public void Execute(PublishCommandOptions options)
4348
publisher.CreateExecutable<int, CommandLineScriptGlobals>(context, _logFactory, options.RuntimeIdentifier, options.LibraryName);
4449
}
4550
}
46-
47-
private static ScriptCompiler GetScriptCompiler(bool useRestoreCache, LogFactory logFactory)
48-
{
49-
50-
var compiler = new ScriptCompiler(logFactory, useRestoreCache);
51-
return compiler;
52-
}
5351
}
5452
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy