Skip to content

Commit 8907200

Browse files
authored
Refactor native APIs (#159)
1 parent 0c245a8 commit 8907200

76 files changed

Lines changed: 7105 additions & 6394 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.editorconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ csharp_space_between_square_brackets = false
156156
dotnet_diagnostic.CA1510.severity = none
157157
dotnet_diagnostic.CA1513.severity = none
158158

159+
dotnet_diagnostic.IDE0290.severity = none # Use primary constructor
160+
dotnet_diagnostic.IDE0065.severity = none # Using directives must be placed outside of namespace
161+
162+
# Collection initialization can be simplified.
163+
# This relies on implicit conversions in ways that are sometimes undesirable.
164+
dotnet_diagnostic.IDE0028.severity = none # new Dictionary<K,V>() => [] (and other collections)
165+
dotnet_diagnostic.IDE0300.severity = none # new[] { a, b } -> [a, b] (breaks some attributes)
166+
dotnet_diagnostic.IDE0302.severity = none # stackalloc T[N] { a, b } -> [a, b]
167+
dotnet_diagnostic.IDE0305.severity = none # collection.ToArray() -> [..collection]
168+
159169
# License header
160170
file_header_template = Copyright (c) Microsoft Corporation.\nLicensed under the MIT License.
161171

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<TargetFrameworks Condition=" '$(TargetFrameworks)' == '' and ! $([MSBuild]::IsOsPlatform('Windows')) ">net8.0;net6.0</TargetFrameworks>
44
<TargetFrameworks Condition=" '$(TargetFrameworks)' == '' and $([MSBuild]::IsOsPlatform('Windows')) ">net8.0;net6.0;net472</TargetFrameworks>
55
<TargetFrameworks Condition=" '$(PublishAot)' == 'true' ">net8.0</TargetFrameworks>
6-
<LangVersion>10</LangVersion>
6+
<LangVersion>12</LangVersion>
77
<Nullable>enable</Nullable>
88
<Configuration Condition="'$(Configuration)'==''">Debug</Configuration>
99
<BaseOutputPath>$(MSBuildThisFileDirectory)out/</BaseOutputPath>

bench/Benchmarks.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
using BenchmarkDotNet.Running;
1010
using Microsoft.JavaScript.NodeApi.DotNetHost;
1111
using Microsoft.JavaScript.NodeApi.Interop;
12-
using Microsoft.JavaScript.NodeApi.Runtimes;
13-
using static Microsoft.JavaScript.NodeApi.JSNativeApi.Interop;
12+
using Microsoft.JavaScript.NodeApi.Runtime;
13+
using static Microsoft.JavaScript.NodeApi.Runtime.JSRuntime;
1414
using static Microsoft.JavaScript.NodeApi.Test.TestUtils;
1515

1616
namespace Microsoft.JavaScript.NodeApi.Bench;
@@ -87,10 +87,11 @@ protected void Setup()
8787

8888
// This setup avoids using NodejsEnvironment so benchmarks can run on the same thread.
8989
// NodejsEnvironment creates a separate thread that would slow down the micro-benchmarks.
90-
_env = JSNativeApi.CreateEnvironment(platform, (error) => Console.WriteLine(error), null);
90+
platform.Runtime.CreateEnvironment(platform, Console.WriteLine, null, out _env)
91+
.ThrowIfFailed();
9192

9293
// The new scope instance saves itself as the thread-local JSValueScope.Current.
93-
JSValueScope scope = new(JSValueScopeType.Root, _env);
94+
JSValueScope scope = new(JSValueScopeType.Root, _env, platform.Runtime);
9495

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

examples/hermes-engine/HermesApi.Interop.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
using System.Runtime.InteropServices;
77
using System.Security;
88
using Microsoft.JavaScript.NodeApi;
9+
using Microsoft.JavaScript.NodeApi.Runtime;
910
using static Hermes.Example.HermesApi.Interop;
10-
using static Microsoft.JavaScript.NodeApi.JSNativeApi.Interop;
11+
using static Microsoft.JavaScript.NodeApi.Runtime.JSRuntime;
1112

1213
namespace Hermes.Example;
1314

1415
public static class HermesApi
1516
{
16-
public static void Load(string hermesEnginePath)
17+
public static JSRuntime Load(string hermesEnginePath)
1718
{
1819
nint hermesLib = NativeLibrary.Load(hermesEnginePath);
1920
Interop.Initialize(hermesLib);
20-
JSNativeApi.Interop.Initialize(hermesLib);
21+
return new NodejsRuntime(hermesLib);
2122
}
2223

2324
public static void ThrowIfFailed(

examples/hermes-engine/HermesRuntime.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
using Microsoft.JavaScript.NodeApi;
55
using Microsoft.JavaScript.NodeApi.Interop;
6+
using Microsoft.JavaScript.NodeApi.Runtime;
67
using static Hermes.Example.HermesApi.Interop;
7-
using static Microsoft.JavaScript.NodeApi.JSNativeApi.Interop;
8+
using static Microsoft.JavaScript.NodeApi.Runtime.JSRuntime;
89

910
namespace Hermes.Example;
1011

@@ -26,10 +27,10 @@ public sealed class HermesRuntime : IDisposable
2627
private HermesRuntime(JSDispatcherQueue dispatcherQueue)
2728
{
2829
_dispatcherQueue = dispatcherQueue;
29-
HermesApi.Load("hermes.dll");
30+
JSRuntime runtime = HermesApi.Load("hermes.dll");
3031
using HermesConfig tempConfig = new();
3132
hermes_create_runtime((hermes_config)tempConfig, out _runtime).ThrowIfFailed();
32-
_rootScope = new JSValueScope(JSValueScopeType.Root, (napi_env)this);
33+
_rootScope = new JSValueScope(JSValueScopeType.Root, (napi_env)this, runtime);
3334
CreatePolyfills();
3435
}
3536

@@ -103,37 +104,37 @@ private void CreatePolyfills()
103104
JSValue global = JSValue.Global;
104105
global["global"] = global;
105106

106-
global["setImmediate"] = (JSCallback)(args =>
107+
global["setImmediate"] = new JSFunction("setImmediate", args =>
107108
{
108109
JSValue immediateCallback = AsFunction(args, 0);
109110
int taskId = AddImmediateTask(immediateCallback);
110111
return taskId;
111112
});
112113

113-
global["clearImmediate"] = (JSCallback)(args =>
114+
global["clearImmediate"] = new JSFunction("clearImmediate", args =>
114115
{
115116
int taskId = AsInt32(args, 0);
116117
RemoveImmediateTask(taskId);
117118
return default;
118119
});
119120

120-
global["setTimeout"] = (JSCallback)(args =>
121+
global["setTimeout"] = new JSFunction("setTimeout", args =>
121122
{
122123
JSValue timeoutCallback = AsFunction(args, 0);
123124
int delayInMs = AsInt32(args, 1);
124125
int taskId = AddTimerTask(timeoutCallback, delayInMs);
125126
return taskId;
126127
});
127128

128-
global["clearTimeout"] = (JSCallback)(args =>
129+
global["clearTimeout"] = new JSFunction("clearTimeout", args =>
129130
{
130131
int taskId = AsInt32(args, 0);
131132
RemoveTimerTask(taskId);
132133
return default;
133134
});
134135

135136
var console = new JSObject();
136-
console["log"] = (JSCallback)(args =>
137+
console["log"] = new JSFunction("log", args =>
137138
{
138139
Console.WriteLine((string)args[0].CoerceToString());
139140
return default;

examples/hermes-engine/hermes-engine.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
</PropertyGroup>
1212

1313
<ItemGroup>
14-
<PackageReference Include="Microsoft.JavaScript.NodeApi" Version="0.2.*-*" PrivateAssets="all" />
14+
<PackageReference Include="Microsoft.JavaScript.NodeApi" Version="0.4.*-*" PrivateAssets="all" />
1515
<PackageReference Include="Microsoft.JavaScript.Hermes" Version="0.1.6" PrivateAssets="all" />
1616
</ItemGroup>
1717

examples/winui-fluid/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System;
55
using System.Diagnostics;
66
using System.IO;
7-
using Microsoft.JavaScript.NodeApi.Runtimes;
7+
using Microsoft.JavaScript.NodeApi.Runtime;
88

99
namespace Microsoft.JavaScript.NodeApi.Examples;
1010

examples/winui-fluid/CollabEditBox.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
using Microsoft.UI.Xaml.Controls;
1717
using Microsoft.UI.Xaml.Media;
1818
using Microsoft.UI.Xaml.Shapes;
19-
using Microsoft.JavaScript.NodeApi.Runtimes;
19+
using Microsoft.JavaScript.NodeApi.Runtime;
2020

2121
namespace Microsoft.JavaScript.NodeApi.Examples;
2222

examples/winui-fluid/Properties/PublishProfiles/win10-arm64.pubxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
66
<PropertyGroup>
77
<PublishProtocol>FileSystem</PublishProtocol>
88
<Platform>arm64</Platform>
9-
<RuntimeIdentifier>win10-arm64</RuntimeIdentifier>
9+
<RuntimeIdentifier>win-arm64</RuntimeIdentifier>
1010
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
1111
<SelfContained>true</SelfContained>
1212
<PublishSingleFile>False</PublishSingleFile>

examples/winui-fluid/Properties/PublishProfiles/win10-x64.pubxml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
66
<PropertyGroup>
77
<PublishProtocol>FileSystem</PublishProtocol>
88
<Platform>x64</Platform>
9-
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
9+
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
1010
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
1111
<SelfContained>true</SelfContained>
1212
<PublishSingleFile>False</PublishSingleFile>

0 commit comments

Comments
 (0)