From 2314e5b8305183b30cdc7472584a83d1d423f139 Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Sat, 9 Mar 2024 21:53:37 +0500 Subject: [PATCH] Add fallback lookup for assemblies from loaded from memory This is important for Blazor WASM workloads. Unfortunately it has limitation, it requires disable WebCLI format using `false`, but at least it's possible to some degree to play with Inter.NET in the Blazor. --- src/Compiler/Infer/ModelCompiler.cs | 10 +++++++- .../TransformFramework/CodeTransformer.cs | 25 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/Compiler/Infer/ModelCompiler.cs b/src/Compiler/Infer/ModelCompiler.cs index 91bab2b4..4cd632a1 100644 --- a/src/Compiler/Infer/ModelCompiler.cs +++ b/src/Compiler/Infer/ModelCompiler.cs @@ -156,6 +156,13 @@ public IAlgorithm Algorithm /// public CompilerChoice CompilerChoice { get; set; } = CompilerChoice.Auto; +#if ROSLYN + /// + /// Resolves reference to assembly for Roslyn. + /// + public Func ReferenceResolver; +#endif + private bool useParallelForLoops = false; /// @@ -822,7 +829,8 @@ internal T CompileWithoutParams(List itds) includeDebugInformation = IncludeDebugInformation, optimizeCode = !IncludeDebugInformation, // tie these together for now showProgress = ShowProgress, - compilerChoice = CompilerChoice + compilerChoice = CompilerChoice, + ReferenceResolver = ReferenceResolver, }; CompilerResults cr = cc.WriteAndCompile(itds); // Examine the compilation results and stop if errors diff --git a/src/Compiler/TransformFramework/CodeTransformer.cs b/src/Compiler/TransformFramework/CodeTransformer.cs index dd49fef8..f22af7bb 100644 --- a/src/Compiler/TransformFramework/CodeTransformer.cs +++ b/src/Compiler/TransformFramework/CodeTransformer.cs @@ -146,6 +146,12 @@ public class CodeCompiler /// public CompilerChoice compilerChoice = CompilerChoice.Auto; +#if ROSLYN + /// + /// Resolves reference for Roslyn + /// + public Func ReferenceResolver; +#endif public List WriteSource(List typeDeclarations, IList filenames, out ICollection referencedAssemblies) { Stopwatch watch = null; @@ -449,7 +455,24 @@ private CompilerResults CompileWithRoslyn(List filenames, List s { try { - references.Add(MetadataReference.CreateFromFile(assembly.Location)); + MetadataReference metadataReference; + if (assembly.Location == "" && ReferenceResolver != null) + { + var bytes = ReferenceResolver(assembly); + if (bytes is null) + { + // do nothing - this assembly does not have a location e.g. it is in memory + continue; + } + + metadataReference = MetadataReference.CreateFromImage(bytes); + } + else + { + metadataReference = MetadataReference.CreateFromFile(assembly.Location); + } + + references.Add(metadataReference); } catch (NotSupportedException) {