Skip to content

Commit a2a59f5

Browse files
committed
Update API
1 parent 536b612 commit a2a59f5

35 files changed

+1884
-1678
lines changed

.ado/publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ extends:
259259
- script: >
260260
dotnet test -f ${{ MatrixEntry.DotNetVersion }}
261261
--configuration Release
262+
--property:ParallelizeTestCollections=false
262263
--logger trx
263264
--results-directory "$(Build.StagingDirectory)/test/${{ MatrixEntry.DotNetVersion }}-Release"
264265
displayName: Run tests

.github/workflows/build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ jobs:
7777
run: >
7878
dotnet test -f ${{ matrix.dotnet-version }}
7979
--configuration ${{ matrix.configuration }}
80+
--property:ParallelizeTestCollections=false
8081
--logger trx
8182
--results-directory "out/test/${{matrix.dotnet-version}}-node${{matrix.node-version}}-${{matrix.configuration}}"
8283
continue-on-error: true

bench/Benchmarks.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ public static void Main(string[] args)
5050
GetCurrentPlatformRuntimeIdentifier(),
5151
"libnode" + GetSharedLibraryExtension());
5252

53-
private NodejsEmbeddingRuntime? _runtime;
54-
private NodejsEmbeddingNodeApiScope? _nodeApiScope;
53+
private NodeEmbeddingRuntime? _runtime;
54+
private NodeEmbeddingNodeApiScope? _nodeApiScope;
5555
private JSValue _jsString;
5656
private JSFunction _jsFunction;
5757
private JSFunction _jsFunctionWithArgs;
@@ -86,9 +86,9 @@ public static void Method() { }
8686
/// </summary>
8787
protected void Setup()
8888
{
89-
NodejsEmbeddingPlatform platform = new(
89+
NodeEmbeddingPlatform platform = new(
9090
LibnodePath,
91-
new NodejsEmbeddingPlatformSettings { Args = s_settings });
91+
new NodeEmbeddingPlatformSettings { Args = s_settings });
9292

9393
// This setup avoids using NodejsEmbeddingThreadRuntime so benchmarks can run on
9494
// the same thread. NodejsEmbeddingThreadRuntime creates a separate thread that would slow

examples/jsdom/Program.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ public static void Main()
1111
{
1212
string appDir = Path.GetDirectoryName(typeof(Program).Assembly.Location)!;
1313
string libnodePath = Path.Combine(appDir, "libnode.dll");
14-
using NodejsPlatform nodejsPlatform = new(libnodePath);
15-
using NodejsEnvironment nodejs = nodejsPlatform.CreateEnvironment(appDir);
14+
using NodeEmbeddingPlatform nodejsPlatform = new(libnodePath, null);
15+
using NodeEmbeddingThreadRuntime nodejs = nodejsPlatform.CreateThreadRuntime(appDir,
16+
new NodeEmbeddingRuntimeSettings
17+
{
18+
MainScript =
19+
"globalThis.require = require('module').createRequire(process.execPath);\n"
20+
});
1621
if (Debugger.IsAttached)
1722
{
1823
int pid = Process.GetCurrentProcess().Id;
@@ -25,7 +30,7 @@ public static void Main()
2530
Console.WriteLine(content);
2631
}
2732

28-
private static string GetContent(NodejsEnvironment nodejs, string html)
33+
private static string GetContent(NodeEmbeddingThreadRuntime nodejs, string html)
2934
{
3035
JSValue jsdomClass = nodejs.Import(module: "jsdom", property: "JSDOM");
3136
JSValue dom = jsdomClass.CallAsConstructor(html);

examples/winui-fluid/App.xaml.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ public App()
2626

2727
string appDir = Path.GetDirectoryName(typeof(App).Assembly.Location)!;
2828
string libnodePath = Path.Combine(appDir, "libnode.dll");
29-
NodejsPlatform nodejsPlatform = new(libnodePath);
30-
31-
Nodejs = nodejsPlatform.CreateEnvironment(appDir);
29+
NodeEmbeddingPlatform nodejsPlatform = new(libnodePath, null);
30+
Nodejs = nodejsPlatform.CreateThreadRuntime(appDir, new NodeEmbeddingRuntimeSettings
31+
{
32+
MainScript =
33+
"globalThis.require = require('module').createRequire(process.execPath);\n"
34+
});
3235
if (Debugger.IsAttached)
3336
{
3437
int pid = Process.GetCurrentProcess().Id;
@@ -60,5 +63,5 @@ private void OnMainWindowClosed(object sender, WindowEventArgs args)
6063

6164
public static new App Current => (App)Application.Current;
6265

63-
public NodejsEnvironment Nodejs { get; }
66+
public NodeEmbeddingThreadRuntime Nodejs { get; }
6467
}

examples/winui-fluid/CollabEditBox.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class CollabEditBox : UserControl
2828
private const string FluidServiceUri = "http://localhost:7070/";
2929

3030
private readonly SynchronizationContext uiSyncContext;
31-
private readonly NodejsEnvironment nodejs;
31+
private readonly NodeEmbeddingThreadRuntime nodejs;
3232
private readonly JSMarshaller marshaller;
3333

3434
private ITinyliciousClient fluidClient = null!;

src/NodeApi.DotNetHost/JSRuntimeContextExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static T Import<T>(
5454
/// <exception cref="ArgumentNullException">Both <paramref cref="module" /> and
5555
/// <paramref cref="property" /> are null.</exception>
5656
public static T Import<T>(
57-
this NodejsEmbeddingThreadRuntime nodejs,
57+
this NodeEmbeddingThreadRuntime nodejs,
5858
string? module,
5959
string? property,
6060
bool esModule,

src/NodeApi/Interop/EmptyAttributes.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,23 @@ public CallerArgumentExpressionAttribute(string parameterName)
5555

5656
public string ParameterName { get; }
5757
}
58+
59+
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Field | AttributeTargets.Property
60+
| AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
61+
public sealed class RequiredMemberAttribute : Attribute
62+
{
63+
}
64+
65+
[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)]
66+
public sealed class CompilerFeatureRequiredAttribute : Attribute
67+
{
68+
public CompilerFeatureRequiredAttribute (string featureName)
69+
{
70+
FeatureName = featureName;
71+
}
72+
73+
public string FeatureName { get; }
74+
}
5875
}
5976

6077
namespace System.Diagnostics

src/NodeApi/NodeApiStatusExtensions.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
using System.Diagnostics;
55
using System.Diagnostics.CodeAnalysis;
66
using System.Runtime.CompilerServices;
7+
using Microsoft.JavaScript.NodeApi.Runtime;
78
using static Microsoft.JavaScript.NodeApi.Runtime.JSRuntime;
9+
using static Microsoft.JavaScript.NodeApi.Runtime.NodejsRuntime;
810

911
namespace Microsoft.JavaScript.NodeApi;
1012

@@ -61,28 +63,17 @@ public static T ThrowIfFailed<T>(this napi_status status,
6163
}
6264

6365
[StackTraceHidden]
64-
public static void ThrowIfFailed([DoesNotReturnIf(true)] this node_embedding_status status,
66+
public static void ThrowIfFailed([DoesNotReturnIf(true)] this NodeEmbeddingStatus status,
6567
[CallerMemberName] string memberName = "",
6668
[CallerFilePath] string sourceFilePath = "",
6769
[CallerLineNumber] int sourceLineNumber = 0)
6870
{
69-
if (status == node_embedding_status.ok)
71+
if (status == NodeEmbeddingStatus.OK)
7072
return;
71-
72-
throw new JSException($"Error in {memberName} at {sourceFilePath}:{sourceLineNumber}");
73-
}
74-
75-
// Throw if status is not napi_ok. Otherwise, return the provided value.
76-
// This function helps writing compact wrappers for the interop calls.
77-
[StackTraceHidden]
78-
public static T ThrowIfFailed<T>(this node_embedding_status status,
79-
T value,
80-
[CallerMemberName] string memberName = "",
81-
[CallerFilePath] string sourceFilePath = "",
82-
[CallerLineNumber] int sourceLineNumber = 0)
83-
{
84-
status.ThrowIfFailed(memberName, sourceFilePath, sourceLineNumber);
85-
return value;
73+
throw new JSException($"""
74+
Error in {memberName} at {sourceFilePath}:{sourceLineNumber}
75+
{NodeEmbedding.JSRuntime.EmbeddingGetLastErrorMessage()}
76+
""");
8677
}
8778
}
8879

0 commit comments

Comments
 (0)