aspclassic-core/AspClassic.Scripting/Runtime/ScriptDomainManager.cs
Jelle Luteijn 484dbfc9d9 progress
2022-05-15 11:19:49 +02:00

141 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading;
using AspClassic.Scripting.Utils;
namespace AspClassic.Scripting.Runtime;
public sealed class ScriptDomainManager
{
private readonly DynamicRuntimeHostingProvider _hostingProvider;
private readonly SharedIO _sharedIO;
private List<Assembly> _loadedAssemblies = new List<Assembly>();
private int _lastContextId;
private Scope _globals;
private readonly DlrConfiguration _configuration;
public PlatformAdaptationLayer Platform
{
get
{
PlatformAdaptationLayer platformAdaptationLayer = _hostingProvider.PlatformAdaptationLayer;
if (platformAdaptationLayer == null)
{
throw new InvalidImplementationException();
}
return platformAdaptationLayer;
}
}
public SharedIO SharedIO => _sharedIO;
public DynamicRuntimeHostingProvider Host => _hostingProvider;
public DlrConfiguration Configuration => _configuration;
public Scope Globals
{
get
{
return _globals;
}
set
{
_globals = value;
}
}
public event EventHandler<AssemblyLoadedEventArgs> AssemblyLoaded;
public ScriptDomainManager(DynamicRuntimeHostingProvider hostingProvider, DlrConfiguration configuration)
{
ContractUtils.RequiresNotNull(hostingProvider, "hostingProvider");
ContractUtils.RequiresNotNull(configuration, "configuration");
configuration.Freeze();
_hostingProvider = hostingProvider;
_configuration = configuration;
_sharedIO = new SharedIO();
_globals = new Scope();
}
internal ContextId GenerateContextId()
{
return new ContextId(Interlocked.Increment(ref _lastContextId));
}
public LanguageContext GetLanguage(Type providerType)
{
ContractUtils.RequiresNotNull(providerType, "providerType");
return GetLanguageByTypeName(providerType.AssemblyQualifiedName);
}
public LanguageContext GetLanguageByTypeName(string providerAssemblyQualifiedTypeName)
{
ContractUtils.RequiresNotNull(providerAssemblyQualifiedTypeName, "providerAssemblyQualifiedTypeName");
AssemblyQualifiedTypeName providerName = AssemblyQualifiedTypeName.ParseArgument(providerAssemblyQualifiedTypeName, "providerAssemblyQualifiedTypeName");
if (!_configuration.TryLoadLanguage(this, providerName, out var language))
{
throw Error.UnknownLanguageProviderType();
}
return language;
}
public bool TryGetLanguage(string languageName, out LanguageContext language)
{
ContractUtils.RequiresNotNull(languageName, "languageName");
return _configuration.TryLoadLanguage(this, languageName, isExtension: false, out language);
}
public LanguageContext GetLanguageByName(string languageName)
{
if (!TryGetLanguage(languageName, out var language))
{
throw new ArgumentException($"Unknown language name: '{languageName}'");
}
return language;
}
public bool TryGetLanguageByFileExtension(string fileExtension, out LanguageContext language)
{
ContractUtils.RequiresNotEmpty(fileExtension, "fileExtension");
return _configuration.TryLoadLanguage(this, DlrConfiguration.NormalizeExtension(fileExtension), isExtension: true, out language);
}
public LanguageContext GetLanguageByExtension(string fileExtension)
{
if (!TryGetLanguageByFileExtension(fileExtension, out var language))
{
throw new ArgumentException($"Unknown file extension: '{fileExtension}'");
}
return language;
}
public bool LoadAssembly(Assembly assembly)
{
ContractUtils.RequiresNotNull(assembly, "assembly");
lock (_loadedAssemblies)
{
if (_loadedAssemblies.Contains(assembly))
{
return false;
}
_loadedAssemblies.Add(assembly);
}
this.AssemblyLoaded?.Invoke(this, new AssemblyLoadedEventArgs(assembly));
return true;
}
public IList<Assembly> GetLoadedAssemblyList()
{
lock (_loadedAssemblies)
{
return _loadedAssemblies.ToArray();
}
}
}