Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] New libNode API #405

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 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 All @@ -64,6 +65,7 @@ public static void Main(string[] args)
private JSFunction _jsFunctionCallMethod;
private JSFunction _jsFunctionCallMethodWithArgs;
private JSReference _reference = null!;
private static readonly string[] s_settings = new[] { "node", "--expose-gc" };

/// <summary>
/// Simple class that is exported to JS and used in some benchmarks.
Expand All @@ -84,16 +86,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 = s_settings });

// 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