Skip to content

Commit bda132b

Browse files
authored
New libNode API (#405)
1 parent 956d1b1 commit bda132b

37 files changed

+2518
-437
lines changed

Directory.Build.targets

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
<Target Name="WriteVersionProps"
1616
BeforeTargets="Pack"
1717
>
18+
<PropertyGroup>
19+
<LibNodePackageVersion Condition="'%(PackageVersion.Identity)' == 'Microsoft.JavaScript.LibNode'">%(PackageVersion.Version)</LibNodePackageVersion>
20+
</PropertyGroup>
1821
<WriteLinesToFile
1922
File="$(PackageOutputPath)version.props"
2023
Overwrite="true"
2124
WriteOnlyWhenDifferent="true"
2225
Lines="
2326
&lt;Project&gt;;
2427
%20%20&lt;PropertyGroup&gt;;
28+
%20%20%20%20&lt;LibNodePackageVersion&gt;$(LibNodePackageVersion)&lt;/LibNodePackageVersion&gt;;
2529
%20%20%20%20&lt;NodeApiDotNetPackageVersion&gt;$(NuGetPackageVersion)&lt;/NodeApiDotNetPackageVersion&gt;;
2630
%20%20&lt;/PropertyGroup&gt;;
2731
&lt;/Project&gt;"

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="7.0.0" />
88
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" /><!-- 4.1.0 is compatible with .NET Standard -->
99
<PackageVersion Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
10+
<PackageVersion Include="Microsoft.JavaScript.LibNode" Version="20.1800.202" />
1011
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
1112
<PackageVersion Include="Microsoft.SourceLink.GitHub" Version="8.0.0" />
1213
<PackageVersion Include="Nerdbank.GitVersioning" Version="3.6.133" />
@@ -16,7 +17,6 @@
1617
<PackageVersion Include="System.Reflection.MetadataLoadContext" Version="6.0.0" />
1718
<PackageVersion Include="xunit" Version="2.4.2" />
1819
<PackageVersion Include="xunit.runner.visualstudio" Version="2.4.5" />
19-
<PackageVersion Include="Xunit.SkippableFact" Version="1.4.13" />
2020
<PackageVersion Include="XmlDocMarkdown.Core" Version="2.9.0" />
2121
</ItemGroup>
2222
</Project>

bench/Benchmarks.cs

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

53-
private napi_env _env;
53+
private NodeEmbeddingRuntime? _runtime;
54+
private NodeEmbeddingNodeApiScope? _nodeApiScope;
5455
private JSValue _jsString;
5556
private JSFunction _jsFunction;
5657
private JSFunction _jsFunctionWithArgs;
@@ -64,6 +65,9 @@ public static void Main(string[] args)
6465
private JSFunction _jsFunctionCallMethod;
6566
private JSFunction _jsFunctionCallMethodWithArgs;
6667
private JSReference _reference = null!;
68+
private static readonly string[] s_settings = new[] { "node", "--expose-gc" };
69+
private static string s_mainScript { get; } =
70+
"globalThis.require = require('module').createRequire(process.execPath);\n";
6771

6872
/// <summary>
6973
/// Simple class that is exported to JS and used in some benchmarks.
@@ -84,16 +88,19 @@ public static void Method() { }
8488
/// </summary>
8589
protected void Setup()
8690
{
87-
NodejsPlatform platform = new(LibnodePath/*, args: new[] { "node", "--expose-gc" }*/);
88-
89-
// This setup avoids using NodejsEnvironment so benchmarks can run on the same thread.
90-
// NodejsEnvironment creates a separate thread that would slow down the micro-benchmarks.
91-
platform.Runtime.CreateEnvironment(
92-
platform, Console.WriteLine, null, NodejsEnvironment.NodeApiVersion, out _env)
93-
.ThrowIfFailed();
94-
95-
// The new scope instance saves itself as the thread-local JSValueScope.Current.
96-
JSValueScope scope = new(JSValueScopeType.Root, _env, platform.Runtime);
91+
NodeEmbeddingPlatform platform = new(
92+
LibnodePath,
93+
new NodeEmbeddingPlatformSettings { Args = s_settings });
94+
95+
// This setup avoids using NodejsEmbeddingThreadRuntime so benchmarks can run on
96+
// the same thread. NodejsEmbeddingThreadRuntime creates a separate thread that would slow
97+
// down the micro-benchmarks.
98+
_runtime = NodeEmbeddingRuntime.Create(platform,
99+
new NodeEmbeddingRuntimeSettings { MainScript = s_mainScript });
100+
101+
// The nodeApiScope creates JSValueScope instance that saves itself as
102+
// the thread-local JSValueScope.Current.
103+
_nodeApiScope = new(_runtime);
97104

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

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/jsdom/jsdom.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,10 @@
1111
<ItemGroup>
1212
<None Include="README.md" />
1313
<Compile Include="*.cs" />
14-
<Content Include="..\..\bin\win-x64\libnode.dll">
15-
<Visible>false</Visible>
16-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
17-
</Content>
1814
</ItemGroup>
1915

2016
<ItemGroup>
17+
<PackageReference Include="Microsoft.JavaScript.LibNode" Version="$(LibNodePackageVersion)" />
2118
<PackageReference Include="Microsoft.JavaScript.NodeApi" Version="$(NodeApiDotnetPackageVersion)" />
2219
</ItemGroup>
2320
</Project>

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!;

examples/winui-fluid/winui-fluid.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,10 @@
4242

4343
<ItemGroup>
4444
<EmbeddedResource Include="README.md" />
45-
<Content Include="..\..\bin\win-x64\libnode.dll">
46-
<Visible>false</Visible>
47-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
48-
</Content>
4945
</ItemGroup>
5046

5147
<ItemGroup>
48+
<PackageReference Include="Microsoft.JavaScript.LibNode" Version="$(LibNodePackageVersion)" />
5249
<PackageReference Include="Microsoft.JavaScript.NodeApi" Version="$(NodeApiDotnetPackageVersion)" />
5350
<PackageReference Include="Microsoft.JavaScript.NodeApi.DotNetHost" Version="$(NodeApiDotnetPackageVersion)" />
5451
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.*" />

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

0 commit comments

Comments
 (0)