Skip to content

Commit

Permalink
Refactor native APIs (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasongin authored Nov 5, 2023
1 parent 0c245a8 commit 8907200
Show file tree
Hide file tree
Showing 76 changed files with 7,105 additions and 6,394 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ csharp_space_between_square_brackets = false
dotnet_diagnostic.CA1510.severity = none
dotnet_diagnostic.CA1513.severity = none

dotnet_diagnostic.IDE0290.severity = none # Use primary constructor
dotnet_diagnostic.IDE0065.severity = none # Using directives must be placed outside of namespace

# Collection initialization can be simplified.
# This relies on implicit conversions in ways that are sometimes undesirable.
dotnet_diagnostic.IDE0028.severity = none # new Dictionary<K,V>() => [] (and other collections)
dotnet_diagnostic.IDE0300.severity = none # new[] { a, b } -> [a, b] (breaks some attributes)
dotnet_diagnostic.IDE0302.severity = none # stackalloc T[N] { a, b } -> [a, b]
dotnet_diagnostic.IDE0305.severity = none # collection.ToArray() -> [..collection]

# License header
file_header_template = Copyright (c) Microsoft Corporation.\nLicensed under the MIT License.

Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<TargetFrameworks Condition=" '$(TargetFrameworks)' == '' and ! $([MSBuild]::IsOsPlatform('Windows')) ">net8.0;net6.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(TargetFrameworks)' == '' and $([MSBuild]::IsOsPlatform('Windows')) ">net8.0;net6.0;net472</TargetFrameworks>
<TargetFrameworks Condition=" '$(PublishAot)' == 'true' ">net8.0</TargetFrameworks>
<LangVersion>10</LangVersion>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<Configuration Condition="'$(Configuration)'==''">Debug</Configuration>
<BaseOutputPath>$(MSBuildThisFileDirectory)out/</BaseOutputPath>
Expand Down
9 changes: 5 additions & 4 deletions bench/Benchmarks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
using BenchmarkDotNet.Running;
using Microsoft.JavaScript.NodeApi.DotNetHost;
using Microsoft.JavaScript.NodeApi.Interop;
using Microsoft.JavaScript.NodeApi.Runtimes;
using static Microsoft.JavaScript.NodeApi.JSNativeApi.Interop;
using Microsoft.JavaScript.NodeApi.Runtime;
using static Microsoft.JavaScript.NodeApi.Runtime.JSRuntime;
using static Microsoft.JavaScript.NodeApi.Test.TestUtils;

namespace Microsoft.JavaScript.NodeApi.Bench;
Expand Down Expand Up @@ -87,10 +87,11 @@ protected void Setup()

// 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.
_env = JSNativeApi.CreateEnvironment(platform, (error) => Console.WriteLine(error), null);
platform.Runtime.CreateEnvironment(platform, Console.WriteLine, null, out _env)
.ThrowIfFailed();

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

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

Expand Down
7 changes: 4 additions & 3 deletions examples/hermes-engine/HermesApi.Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
using System.Runtime.InteropServices;
using System.Security;
using Microsoft.JavaScript.NodeApi;
using Microsoft.JavaScript.NodeApi.Runtime;
using static Hermes.Example.HermesApi.Interop;
using static Microsoft.JavaScript.NodeApi.JSNativeApi.Interop;
using static Microsoft.JavaScript.NodeApi.Runtime.JSRuntime;

namespace Hermes.Example;

public static class HermesApi
{
public static void Load(string hermesEnginePath)
public static JSRuntime Load(string hermesEnginePath)
{
nint hermesLib = NativeLibrary.Load(hermesEnginePath);
Interop.Initialize(hermesLib);
JSNativeApi.Interop.Initialize(hermesLib);
return new NodejsRuntime(hermesLib);
}

public static void ThrowIfFailed(
Expand Down
17 changes: 9 additions & 8 deletions examples/hermes-engine/HermesRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

using Microsoft.JavaScript.NodeApi;
using Microsoft.JavaScript.NodeApi.Interop;
using Microsoft.JavaScript.NodeApi.Runtime;
using static Hermes.Example.HermesApi.Interop;
using static Microsoft.JavaScript.NodeApi.JSNativeApi.Interop;
using static Microsoft.JavaScript.NodeApi.Runtime.JSRuntime;

namespace Hermes.Example;

Expand All @@ -26,10 +27,10 @@ public sealed class HermesRuntime : IDisposable
private HermesRuntime(JSDispatcherQueue dispatcherQueue)
{
_dispatcherQueue = dispatcherQueue;
HermesApi.Load("hermes.dll");
JSRuntime runtime = HermesApi.Load("hermes.dll");
using HermesConfig tempConfig = new();
hermes_create_runtime((hermes_config)tempConfig, out _runtime).ThrowIfFailed();
_rootScope = new JSValueScope(JSValueScopeType.Root, (napi_env)this);
_rootScope = new JSValueScope(JSValueScopeType.Root, (napi_env)this, runtime);
CreatePolyfills();
}

Expand Down Expand Up @@ -103,37 +104,37 @@ private void CreatePolyfills()
JSValue global = JSValue.Global;
global["global"] = global;

global["setImmediate"] = (JSCallback)(args =>
global["setImmediate"] = new JSFunction("setImmediate", args =>
{
JSValue immediateCallback = AsFunction(args, 0);
int taskId = AddImmediateTask(immediateCallback);
return taskId;
});

global["clearImmediate"] = (JSCallback)(args =>
global["clearImmediate"] = new JSFunction("clearImmediate", args =>
{
int taskId = AsInt32(args, 0);
RemoveImmediateTask(taskId);
return default;
});

global["setTimeout"] = (JSCallback)(args =>
global["setTimeout"] = new JSFunction("setTimeout", args =>
{
JSValue timeoutCallback = AsFunction(args, 0);
int delayInMs = AsInt32(args, 1);
int taskId = AddTimerTask(timeoutCallback, delayInMs);
return taskId;
});

global["clearTimeout"] = (JSCallback)(args =>
global["clearTimeout"] = new JSFunction("clearTimeout", args =>
{
int taskId = AsInt32(args, 0);
RemoveTimerTask(taskId);
return default;
});

var console = new JSObject();
console["log"] = (JSCallback)(args =>
console["log"] = new JSFunction("log", args =>
{
Console.WriteLine((string)args[0].CoerceToString());
return default;
Expand Down
2 changes: 1 addition & 1 deletion examples/hermes-engine/hermes-engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.JavaScript.NodeApi" Version="0.2.*-*" PrivateAssets="all" />
<PackageReference Include="Microsoft.JavaScript.NodeApi" Version="0.4.*-*" PrivateAssets="all" />
<PackageReference Include="Microsoft.JavaScript.Hermes" Version="0.1.6" PrivateAssets="all" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion examples/winui-fluid/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.JavaScript.NodeApi.Runtimes;
using Microsoft.JavaScript.NodeApi.Runtime;

namespace Microsoft.JavaScript.NodeApi.Examples;

Expand Down
2 changes: 1 addition & 1 deletion examples/winui-fluid/CollabEditBox.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes;
using Microsoft.JavaScript.NodeApi.Runtimes;
using Microsoft.JavaScript.NodeApi.Runtime;

namespace Microsoft.JavaScript.NodeApi.Examples;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Platform>arm64</Platform>
<RuntimeIdentifier>win10-arm64</RuntimeIdentifier>
<RuntimeIdentifier>win-arm64</RuntimeIdentifier>
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
<SelfContained>true</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Platform>x64</Platform>
<RuntimeIdentifier>win10-x64</RuntimeIdentifier>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
<SelfContained>true</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ https://go.microsoft.com/fwlink/?LinkID=208121.
<PropertyGroup>
<PublishProtocol>FileSystem</PublishProtocol>
<Platform>x86</Platform>
<RuntimeIdentifier>win10-x86</RuntimeIdentifier>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<PublishDir>bin\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\publish\</PublishDir>
<SelfContained>true</SelfContained>
<PublishSingleFile>False</PublishSingleFile>
Expand Down
8 changes: 4 additions & 4 deletions examples/winui-fluid/winui-fluid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>net8.0-windows10.0.22000.0</TargetFramework>
<TargetPlatformMinVersion>10.0.22000.0</TargetPlatformMinVersion>
<OutputType>WinExe</OutputType><!-- TODO: Change back to WinExe -->
<OutputType>WinExe</OutputType>
<ManagePackageVersionsCentrally>false</ManagePackageVersionsCentrally>

<!-- WinUI 3 -->
Expand All @@ -24,7 +24,7 @@
<Platform Condition=" '$(Platform)' == 'AnyCPU' ">x64</Platform>
<ApplicationManifest>app.manifest</ApplicationManifest>
<PublishProfile>win10-$(Platform).pubxml</PublishProfile>
<RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
<RuntimeIdentifiers>win-x86;win-x64;win-arm64</RuntimeIdentifiers>
</PropertyGroup>
<ItemGroup>
<None Remove="CollabEditBox.xaml" />
Expand All @@ -50,8 +50,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.JavaScript.NodeApi" Version="0.2.*-*" />
<PackageReference Include="Microsoft.JavaScript.NodeApi.DotNetHost" Version="0.2.*-*" />
<PackageReference Include="Microsoft.JavaScript.NodeApi" Version="0.4.*-*" />
<PackageReference Include="Microsoft.JavaScript.NodeApi.DotNetHost" Version="0.4.*-*" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.*" />
<PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.755" />
<Manifest Include="$(ApplicationManifest)" />
Expand Down
4 changes: 2 additions & 2 deletions src/NodeApi.DotNetHost/JSMarshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2212,7 +2212,7 @@ private LambdaExpression BuildConvertToJSValueExpression(Type fromType)
statements = new[]
{
Expression.Convert(
Expression.New(typeof(JSArray).GetInstanceConstructor(Array.Empty<Type>())),
Expression.New(typeof(JSArray).GetInstanceConstructor([])),
typeof(JSValue)),
};
}
Expand Down Expand Up @@ -2280,7 +2280,7 @@ Expression TupleItem(int index) => InlineOrInvoke(
statements = new[]
{
Expression.Convert(
Expression.New(typeof(JSArray).GetInstanceConstructor(Array.Empty<Type>())),
Expression.New(typeof(JSArray).GetInstanceConstructor([])),
typeof(JSValue)),
};
}
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 @@ -3,7 +3,7 @@

using System;
using Microsoft.JavaScript.NodeApi.Interop;
using Microsoft.JavaScript.NodeApi.Runtimes;
using Microsoft.JavaScript.NodeApi.Runtime;

namespace Microsoft.JavaScript.NodeApi.DotNetHost;

Expand Down
Loading

0 comments on commit 8907200

Please sign in to comment.