Skip to content

Commit 0708d44

Browse files
samardzicneoN. Samardzic
authored and
N. Samardzic
committed
use high-level library loading
1 parent def23a5 commit 0708d44

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/NodeApi/Runtime/NodejsPlatform.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Reflection;
56
using System.Runtime.InteropServices;
67

78
namespace Microsoft.JavaScript.NodeApi.Runtime;
@@ -25,25 +26,29 @@ public sealed class NodejsPlatform : IDisposable
2526
/// <summary>
2627
/// Initializes the Node.js platform.
2728
/// </summary>
28-
/// <param name="libnodePath">Path to the `libnode` shared library, including extension.</param>
29+
/// <param name="libnode">Name of the `libnode` shared library.</param>
2930
/// <param name="args">Optional application arguments.</param>
3031
/// <param name="execArgs">Optional platform options.</param>
3132
/// <exception cref="InvalidOperationException">A Node.js platform instance has already been
3233
/// loaded in the current process.</exception>
3334
public NodejsPlatform(
34-
string libnodePath,
35+
string libnode,
3536
string[]? args = null,
3637
string[]? execArgs = null)
3738
{
38-
if (string.IsNullOrEmpty(libnodePath)) throw new ArgumentNullException(nameof(libnodePath));
39-
4039
if (Current != null)
4140
{
4241
throw new InvalidOperationException(
4342
"Only one Node.js platform instance per process is allowed.");
4443
}
4544

46-
nint libnodeHandle = NativeLibrary.Load(libnodePath);
45+
var entryAssembly = Assembly.GetEntryAssembly();
46+
47+
nint libnodeHandle =
48+
entryAssembly == null
49+
? NativeLibrary.Load(libnode, entryAssembly, null)
50+
: NativeLibrary.Load(libnode);
51+
4752
Runtime = new NodejsRuntime(libnodeHandle);
4853

4954
Runtime.CreatePlatform(args, execArgs, (error) => Console.WriteLine(error), out _platform)

0 commit comments

Comments
 (0)