Skip to content

Commit e710739

Browse files
committed
Gluing and propagating custom AssemblyLoadContext through the higher API levels.
1 parent a81c8ef commit e710739

6 files changed

+57
-9
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ public async Task<TReturn> Execute<TReturn>(ExecuteCodeCommandOptions options)
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);
2525
var compiler = GetScriptCompiler(!options.NoCache, _logFactory);
26-
var runner = new ScriptRunner(compiler, _logFactory, _scriptConsole);
26+
var runner = new ScriptRunner(compiler, _logFactory, _scriptConsole)
27+
{
28+
#if NETCOREAPP
29+
AssemblyLoadContext = options.AssemblyLoadContext
30+
#endif
31+
};
2732
return await runner.Execute<TReturn>(context);
2833
}
2934

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/ExecuteLibraryCommand.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ public async Task<TReturn> Execute<TReturn>(ExecuteLibraryCommandOptions options
2626

2727
var absoluteFilePath = options.LibraryPath.GetRootedPath();
2828
var compiler = GetScriptCompiler(!options.NoCache, _logFactory);
29-
var runner = new ScriptRunner(compiler, _logFactory, _scriptConsole);
29+
var runner = new ScriptRunner(compiler, _logFactory, _scriptConsole)
30+
{
31+
#if NETCOREAPP
32+
AssemblyLoadContext = options.AssemblyLoadContext
33+
#endif
34+
};
3035
var result = await runner.Execute<TReturn>(absoluteFilePath, options.Arguments);
3136
return result;
3237
}

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: 8 additions & 7 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)
@@ -124,11 +131,5 @@ public bool TryGetHash(string cacheFolder, out string hash)
124131
hash = File.ReadAllText(pathToHashFile);
125132
return true;
126133
}
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-
}
133134
}
134135
}

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
}

0 commit comments

Comments
 (0)