Skip to content

Commit fd38d02

Browse files
committed
Peachpie.Library.Scripting for net461
fixes #286
1 parent c1dc415 commit fd38d02

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

src/Peachpie.Library.Scripting/Peachpie.Library.Scripting.csproj

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<Import Project="..\..\build\Targets\Settings.props" />
44
<PropertyGroup>
5-
<TargetFrameworks>netstandard2.0</TargetFrameworks>
5+
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
66
<NoWarn>$(NoWarn);1591;1574</NoWarn>
77
<AssemblyName>Peachpie.Library.Scripting</AssemblyName>
88
<PackageId>Peachpie.Library.Scripting</PackageId>
@@ -14,7 +14,11 @@
1414
<ItemGroup>
1515
<PackageReference Include="Devsense.Php.Parser" Version="1.4.51" />
1616
<PackageReference Include="Peachpie.Microsoft.CodeAnalysis" Version="0.9.1" />
17-
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
17+
<!--
18+
System.Runtime.Loader.AssemblyLoadContext cannot be loaded from System.Runtime.Loader 4.0.0 which is hard referenced on full .NET Framework,
19+
you may remove this workaround once it will work using netstandard2.0 well.
20+
-->
21+
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" Condition=" '$(TargetFramework)'=='netstandard2.0' " />
1822
</ItemGroup>
1923

2024
<ItemGroup>

src/Peachpie.Library.Scripting/PhpCompilationFactory.cs

+39-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,45 @@
88

99
namespace Peachpie.Library.Scripting
1010
{
11-
class PhpCompilationFactory : System.Runtime.Loader.AssemblyLoadContext
11+
#if NETSTANDARD
12+
13+
sealed class PhpCompilationFactory : PhpCompilationFactoryBase
14+
{
15+
System.Runtime.Loader.AssemblyLoadContext AssemblyLoadContext => System.Runtime.Loader.AssemblyLoadContext.Default;
16+
17+
public PhpCompilationFactory()
18+
{
19+
//AssemblyLoadContext.Resolving += AssemblyLoadContext_Resolving;
20+
}
21+
22+
//private Assembly AssemblyLoadContext_Resolving(System.Runtime.Loader.AssemblyLoadContext assCtx, AssemblyName assName)
23+
// => TryGetSubmissionAssembly(assName);
24+
25+
protected override Assembly LoadFromStream(MemoryStream peStream, MemoryStream pdbStream)
26+
=> AssemblyLoadContext.LoadFromStream(peStream, pdbStream);
27+
}
28+
29+
#else // NET461, does not have AssemblyLoadContext in System.Runtime.Loader 4.0.0 (we need >= 4.0.1)
30+
31+
sealed class PhpCompilationFactory : PhpCompilationFactoryBase
1232
{
1333
public PhpCompilationFactory()
34+
{
35+
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
36+
}
37+
38+
private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
39+
=> TryGetSubmissionAssembly(new AssemblyName(args.Name));
40+
41+
protected override Assembly LoadFromStream(MemoryStream peStream, MemoryStream pdbStream)
42+
=> Assembly.Load(peStream.ToArray(), pdbStream?.ToArray());
43+
}
44+
45+
#endif
46+
47+
abstract class PhpCompilationFactoryBase
48+
{
49+
public PhpCompilationFactoryBase()
1450
{
1551
_compilation = PhpCompilation.Create("project",
1652
references: MetadataReferences().Select(CreateMetadataReference),
@@ -89,6 +125,8 @@ public Assembly TryGetSubmissionAssembly(AssemblyName assemblyName)
89125
}
90126
}
91127

128+
protected abstract Assembly LoadFromStream(MemoryStream peStream, MemoryStream pdbStream);
129+
92130
public Assembly LoadFromStream(AssemblyName assemblyName, MemoryStream peStream, MemoryStream pdbStream)
93131
{
94132
var assembly = LoadFromStream(peStream, pdbStream);
@@ -100,8 +138,6 @@ public Assembly LoadFromStream(AssemblyName assemblyName, MemoryStream peStream,
100138
return assembly;
101139
}
102140

103-
protected override Assembly Load(AssemblyName assemblyName) => TryGetSubmissionAssembly(assemblyName);
104-
105141
static int _counter = 0;
106142

107143
const string s_submissionAssemblyNamePrefix = "<submission>`";

0 commit comments

Comments
 (0)