Skip to content

Commit 85d8c86

Browse files
authored
Update for Node.js embedding API changes (#219)
1 parent def23a5 commit 85d8c86

File tree

8 files changed

+32
-22
lines changed

8 files changed

+32
-22
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ jobs:
8787
continue-on-error: true
8888

8989
- name: Upload test logs
90-
uses: actions/upload-artifact@v4
90+
# upload-artifact@v4 breaks the test reporter: https://github.com/dorny/test-reporter/issues/343
91+
uses: actions/upload-artifact@v3
9192
with:
9293
name: test-logs-${{ matrix.os }}-${{matrix.dotnet-version}}-node${{matrix.node-version}}-${{matrix.configuration}}
9394
path: |

bench/Benchmarks.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ protected void Setup()
8888

8989
// This setup avoids using NodejsEnvironment so benchmarks can run on the same thread.
9090
// NodejsEnvironment creates a separate thread that would slow down the micro-benchmarks.
91-
platform.Runtime.CreateEnvironment(platform, Console.WriteLine, null, out _env)
91+
platform.Runtime.CreateEnvironment(
92+
platform, Console.WriteLine, null, NodejsEnvironment.NodeApiVersion, out _env)
9293
.ThrowIfFailed();
9394

9495
// The new scope instance saves itself as the thread-local JSValueScope.Current.

src/NodeApi/Runtime/JSRuntime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,14 +521,14 @@ public virtual napi_status GetBufferInfo(
521521

522522
public virtual napi_status CreatePlatform(
523523
string[]? args,
524-
string[]? execArgs,
525524
Action<string>? errorHandler,
526525
out napi_platform result) => throw NS();
527526
public virtual napi_status DestroyPlatform(napi_platform platform) => throw NS();
528527
public virtual napi_status CreateEnvironment(
529528
napi_platform platform,
530529
Action<string>? errorHandler,
531530
string? mainScript,
531+
int apiVersion,
532532
out napi_env result) => throw NS();
533533
public virtual napi_status DestroyEnvironment(napi_env env, out int exitCode) => throw NS();
534534
public virtual napi_status RunEnvironment(napi_env env) => throw NS();

src/NodeApi/Runtime/NodejsEnvironment.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ namespace Microsoft.JavaScript.NodeApi.Runtime;
2323
/// </remarks>
2424
public sealed class NodejsEnvironment : IDisposable
2525
{
26+
/// <summary>
27+
/// Corresponds to NAPI_VERSION from js_native_api.h.
28+
/// </summary>
29+
public const int NodeApiVersion = 8;
30+
2631
private readonly JSValueScope _scope;
2732
private readonly Thread _thread;
2833
private readonly TaskCompletionSource<bool> _completion = new();
@@ -44,6 +49,7 @@ internal NodejsEnvironment(NodejsPlatform platform, string? mainScript)
4449
(napi_platform)platform,
4550
(error) => Console.WriteLine(error),
4651
mainScript,
52+
NodeApiVersion,
4753
out napi_env env).ThrowIfFailed();
4854

4955
// The new scope instance saves itself as the thread-local JSValueScope.Current.

src/NodeApi/Runtime/NodejsPlatform.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,12 @@ public sealed class NodejsPlatform : IDisposable
2626
/// Initializes the Node.js platform.
2727
/// </summary>
2828
/// <param name="libnodePath">Path to the `libnode` shared library, including extension.</param>
29-
/// <param name="args">Optional application arguments.</param>
30-
/// <param name="execArgs">Optional platform options.</param>
29+
/// <param name="args">Optional platform arguments.</param>
3130
/// <exception cref="InvalidOperationException">A Node.js platform instance has already been
3231
/// loaded in the current process.</exception>
3332
public NodejsPlatform(
3433
string libnodePath,
35-
string[]? args = null,
36-
string[]? execArgs = null)
34+
string[]? args = null)
3735
{
3836
if (string.IsNullOrEmpty(libnodePath)) throw new ArgumentNullException(nameof(libnodePath));
3937

@@ -46,7 +44,7 @@ public NodejsPlatform(
4644
nint libnodeHandle = NativeLibrary.Load(libnodePath);
4745
Runtime = new NodejsRuntime(libnodeHandle);
4846

49-
Runtime.CreatePlatform(args, execArgs, (error) => Console.WriteLine(error), out _platform)
47+
Runtime.CreatePlatform(args, (error) => Console.WriteLine(error), out _platform)
5048
.ThrowIfFailed();
5149
Current = this;
5250
}

src/NodeApi/Runtime/NodejsRuntime.Embedding.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ public unsafe partial class NodejsRuntime
1111
{
1212
#pragma warning disable IDE1006 // Naming: missing prefix '_'
1313

14-
private delegate* unmanaged[Cdecl]<
15-
int, nint, int, nint, napi_error_message_handler, nint, napi_status>
14+
private delegate* unmanaged[Cdecl]<int, nint, napi_error_message_handler, nint, napi_status>
1615
napi_create_platform;
1716

1817
public override napi_status CreatePlatform(
1918
string[]? args,
20-
string[]? execArgs,
2119
Action<string>? errorHandler,
2220
out napi_platform result)
2321
{
@@ -29,7 +27,6 @@ public override napi_status CreatePlatform(
2927
});
3028

3129
nint args_ptr = StringsToHGlobalUtf8(args, out int args_count);
32-
nint exec_args_ptr = StringsToHGlobalUtf8(execArgs, out int exec_args_count);
3330

3431
try
3532
{
@@ -39,23 +36,20 @@ public override napi_status CreatePlatform(
3936
if (napi_create_platform == null)
4037
{
4138
napi_create_platform = (delegate* unmanaged[Cdecl]<
42-
int, nint, int, nint, napi_error_message_handler, nint, napi_status>)
39+
int, nint, napi_error_message_handler, nint, napi_status>)
4340
Import(nameof(napi_create_platform));
4441
}
4542

4643
return napi_create_platform(
4744
args_count,
4845
args_ptr,
49-
exec_args_count,
50-
exec_args_ptr,
5146
native_error_handler,
5247
(nint)result_ptr);
5348
}
5449
}
5550
finally
5651
{
5752
FreeStringsHGlobal(args_ptr, args_count);
58-
FreeStringsHGlobal(exec_args_ptr, exec_args_count);
5953
}
6054
}
6155

@@ -68,13 +62,14 @@ public override napi_status DestroyPlatform(napi_platform platform)
6862
}
6963

7064
private delegate* unmanaged[Cdecl]<
71-
napi_platform, napi_error_message_handler, nint, nint, napi_status>
65+
napi_platform, napi_error_message_handler, nint, int, nint, napi_status>
7266
napi_create_environment;
7367

7468
public override napi_status CreateEnvironment(
7569
napi_platform platform,
7670
Action<string>? errorHandler,
7771
string? mainScript,
72+
int apiVersion,
7873
out napi_env result)
7974
{
8075
napi_error_message_handler native_error_handler = errorHandler == null ? default :
@@ -91,7 +86,7 @@ public override napi_status CreateEnvironment(
9186
fixed (napi_env* result_ptr = &result)
9287
{
9388
return Import(ref napi_create_environment)(
94-
platform, native_error_handler, main_script_ptr, (nint)result_ptr);
89+
platform, native_error_handler, main_script_ptr, apiVersion, (nint)result_ptr);
9590
}
9691
}
9792
finally

src/NodeApi/Runtime/TracingJSRuntime.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2648,15 +2648,14 @@ public override napi_status GetNodeVersion(napi_env env, out napi_node_version r
26482648
#region Embedding
26492649

26502650
public override napi_status CreatePlatform(
2651-
string[]? args, string[]? execArgs, Action<string>? errorHandler, out napi_platform result)
2651+
string[]? args, Action<string>? errorHandler, out napi_platform result)
26522652
{
26532653
napi_platform resultValue = default;
26542654
napi_status status = TraceCall(
26552655
[
26562656
$"[{string.Join(", ", args ?? [])}]",
2657-
$"[{string.Join(", ", execArgs ?? [])}]",
26582657
],
2659-
() => (_runtime.CreatePlatform(args, execArgs, errorHandler, out resultValue),
2658+
() => (_runtime.CreatePlatform(args, errorHandler, out resultValue),
26602659
Format(resultValue)));
26612660
result = resultValue;
26622661
return status;
@@ -2673,12 +2672,14 @@ public override napi_status CreateEnvironment(
26732672
napi_platform platform,
26742673
Action<string>? errorHandler,
26752674
string? mainScript,
2675+
int apiVersion,
26762676
out napi_env result)
26772677
{
26782678
napi_env resultValue = default;
26792679
napi_status status = TraceCall(
26802680
[Format(platform), Format(mainScript)],
2681-
() => (_runtime.CreateEnvironment(platform, errorHandler, mainScript, out resultValue),
2681+
() => (_runtime.CreateEnvironment(
2682+
platform, errorHandler, mainScript, apiVersion, out resultValue),
26822683
Format(resultValue)));
26832684
result = resultValue;
26842685
return status;

test/NodejsEmbeddingTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ public void NodejsStart()
4646
Assert.Equal(0, nodejs.ExitCode);
4747
}
4848

49+
[SkippableFact]
50+
public void NodejsRestart()
51+
{
52+
// Create and destory a Node.js environment twice, using the same platform instance.
53+
NodejsStart();
54+
NodejsStart();
55+
}
56+
4957
[SkippableFact]
5058
public void NodejsCallFunction()
5159
{

0 commit comments

Comments
 (0)