-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathNodejsPlatform.cs
More file actions
104 lines (86 loc) · 3.44 KB
/
NodejsPlatform.cs
File metadata and controls
104 lines (86 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;
namespace Microsoft.JavaScript.NodeApi.Runtime;
using static JSRuntime;
/// <summary>
/// Manages a Node.js platform instance, provided by `libnode`.
/// </summary>
/// <remarks>
/// Only one Node.js platform instance can be created per process. Once the platform is disposed,
/// another platform instance cannot be re-initialized. One or more <see cref="NodejsEnvironment" />
/// instances may be created using the platform.
/// </remarks>
public sealed class NodejsPlatform : IDisposable
{
private readonly napi_platform _platform;
public static implicit operator napi_platform(NodejsPlatform platform) => platform._platform;
/// <summary>
/// Initializes the Node.js platform.
/// </summary>
/// <param name="libnode">
/// Name of the `libnode` shared library.
/// Has to be a full file path when using .NET Framework.
/// </param>
/// <param name="args">Optional application arguments.</param>
/// <param name="execArgs">Optional platform options.</param>
/// <exception cref="InvalidOperationException">A Node.js platform instance has already been
/// loaded in the current process.</exception>
public NodejsPlatform(
string libnode,
string[]? args = null,
string[]? execArgs = null)
{
if (Current != null)
{
throw new InvalidOperationException(
"Only one Node.js platform instance per process is allowed.");
}
#if NET7_0_OR_GREATER
var entryAssembly = System.Reflection.Assembly.GetEntryAssembly();
nint libnodeHandle =
entryAssembly != null
? NativeLibrary.Load(libnode, entryAssembly, null)
: NativeLibrary.Load(libnode);
#else
if (string.IsNullOrEmpty(libnode))
throw new ArgumentNullException(nameof(libnode));
nint libnodeHandle = NativeLibrary.Load(libnode);
#endif
Runtime = new NodejsRuntime(libnodeHandle);
Runtime.CreatePlatform(args, execArgs, (error) => Console.WriteLine(error), out _platform)
.ThrowIfFailed();
Current = this;
}
/// <summary>
/// Gets the Node.js platform instance for the current process, or null if not initialized.
/// </summary>
public static NodejsPlatform? Current { get; private set; }
public JSRuntime Runtime { get; }
/// <summary>
/// Gets a value indicating whether the current platform has been disposed.
/// </summary>
public bool IsDisposed { get; private set; }
/// <summary>
/// Disposes the platform. After disposal, another platform instance may not be initialized
/// in the current process.
/// </summary>
public void Dispose()
{
if (IsDisposed) return;
IsDisposed = true;
Runtime.DestroyPlatform(_platform);
}
/// <summary>
/// Creates a new Node.js environment with a dedicated main thread.
/// </summary>
/// <param name="mainScript">Optional script to run in the environment. (Literal script content,
/// not a path to a script file.)</param>
/// <returns>A new <see cref="NodejsEnvironment" /> instance.</returns>
public NodejsEnvironment CreateEnvironment(string? mainScript = null)
{
if (IsDisposed) throw new ObjectDisposedException(nameof(NodejsPlatform));
return new NodejsEnvironment(this, mainScript);
}
}