Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow host module re-initialization #95

Merged
merged 2 commits into from
Apr 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions src/NodeApi/DotNetHost/NativeHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ internal unsafe partial class NativeHost : IDisposable
private static readonly string s_managedHostTypeName =
typeof(NativeHost).Namespace + ".ManagedHost";

private string? _targetFramework;
private string? _managedHostPath;
private ICLRRuntimeHost* _runtimeHost;
private hostfxr_handle _hostContextHandle;
private JSReference? _exports;

public static bool IsTracingEnabled { get; } =
Environment.GetEnvironmentVariable("TRACE_NODE_API_HOST") == "1";
Expand Down Expand Up @@ -81,20 +84,36 @@ public NativeHost()
/// <returns>JS exports value from the managed host.</returns>
private JSValue InitializeManagedHost(JSCallbackArgs args)
{
string targetFramework = (string)args[0];
string managedHostPath = (string)args[1];

if (_hostContextHandle != default || _runtimeHost is not null)
{
throw new NotSupportedException(
".NET is already initialized in the current process. " +
"Initializing multiple .NET versions is not supported.");
// .NET is already loaded for this host.
if (targetFramework == _targetFramework && managedHostPath == _managedHostPath &&
_exports is not null)
{
// The same version of .NET and same managed host were requested again.
// Just return the same exports object that was initialized the first time.
// Normally this shouldn't happen because the host package initialization
// script would only be loaded once by require(). But certain situations like
// drive letter or path casing inconsistencies can cause it to be loaded twice.
return _exports.GetValue()!.Value;
}
else
{
throw new NotSupportedException(
$".NET ({_targetFramework}) is already initialized in the current process. " +
"Initializing multiple .NET versions is not supported.");
}
}

string targetFramework = (string)args[0];
string managedHostPath = (string)args[1];
JSValue require = args[2];
Trace($"> NativeHost.InitializeManagedHost({targetFramework}, {managedHostPath})");

try
{
JSValue exports;
if (!targetFramework.Contains('.') && targetFramework.StartsWith("net") &&
targetFramework.Length >= 5)
{
Expand All @@ -104,7 +123,7 @@ private JSValue InitializeManagedHost(JSCallbackArgs args)
int.Parse(targetFramework.Substring(4, 1)),
targetFramework.Length == 5 ? 0 :
int.Parse(targetFramework.Substring(5, 1)));
return InitializeFrameworkHost(frameworkVersion, managedHostPath, require);
exports = InitializeFrameworkHost(frameworkVersion, managedHostPath, require);
}
else
{
Expand All @@ -114,8 +133,14 @@ private JSValue InitializeManagedHost(JSCallbackArgs args)
#else
Version dotnetVersion = Version.Parse(targetFramework.AsSpan(3));
#endif
return InitializeDotNetHost(dotnetVersion, managedHostPath, require);
exports = InitializeDotNetHost(dotnetVersion, managedHostPath, require);
}

// Save init parameters and result in case of re-init.
_targetFramework = targetFramework;
_managedHostPath = managedHostPath;
_exports = new JSReference(exports);
return exports;
}
catch (Exception ex)
{
Expand Down