Skip to content
24 changes: 13 additions & 11 deletions bench/Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public static void Main(string[] args)
GetCurrentPlatformRuntimeIdentifier(),
"libnode" + GetSharedLibraryExtension());

private napi_env _env;
private NodejsEmbeddingRuntime? _runtime;
private NodejsEmbeddingNodeApiScope? _nodeApiScope;
private JSValue _jsString;
private JSFunction _jsFunction;
private JSFunction _jsFunctionWithArgs;
Expand Down Expand Up @@ -84,16 +85,17 @@ public static void Method() { }
/// </summary>
protected void Setup()
{
NodejsPlatform platform = new(LibnodePath/*, args: new[] { "node", "--expose-gc" }*/);

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

// The new scope instance saves itself as the thread-local JSValueScope.Current.
JSValueScope scope = new(JSValueScopeType.Root, _env, platform.Runtime);
NodejsEmbeddingPlatform platform = new(
LibnodePath,
new NodejsEmbeddingPlatformSettings { Args = new[] { "node", "--expose-gc" } });

// This setup avoids using NodejsEmbeddingThreadRuntime so benchmarks can run on
// the same thread. NodejsEmbeddingThreadRuntime creates a separate thread that would slow
// down the micro-benchmarks.
_runtime = new(platform);
// The nodeApiScope creates JSValueScope instance that saves itself as
// the thread-local JSValueScope.Current.
_nodeApiScope = new(_runtime);

// Create some JS values that will be used by the benchmarks.

Expand Down
2 changes: 1 addition & 1 deletion src/NodeApi.DotNetHost/JSRuntimeContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static T Import<T>(
/// <exception cref="ArgumentNullException">Both <paramref cref="module" /> and
/// <paramref cref="property" /> are null.</exception>
public static T Import<T>(
this NodejsEnvironment nodejs,
this NodejsEmbeddingThreadRuntime nodejs,
string? module,
string? property,
bool esModule,
Expand Down
25 changes: 25 additions & 0 deletions src/NodeApi/NodeApiStatusExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,30 @@ public static T ThrowIfFailed<T>(this napi_status status,
status.ThrowIfFailed(memberName, sourceFilePath, sourceLineNumber);
return value;
}

[StackTraceHidden]
public static void ThrowIfFailed([DoesNotReturnIf(true)] this node_embedding_status status,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
if (status == node_embedding_status.ok)
return;

throw new JSException($"Error in {memberName} at {sourceFilePath}:{sourceLineNumber}");
}

// Throw if status is not napi_ok. Otherwise, return the provided value.
// This function helps writing compact wrappers for the interop calls.
[StackTraceHidden]
public static T ThrowIfFailed<T>(this node_embedding_status status,
T value,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
status.ThrowIfFailed(memberName, sourceFilePath, sourceLineNumber);
return value;
}
}

Loading
Loading