Skip to content
Merged
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
14 changes: 11 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
<Project>
<PropertyGroup>
<TargetFrameworks Condition=" '$(TargetFrameworks)' == '' and ! $([MSBuild]::IsOsPlatform('Windows')) ">net9.0;net8.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(TargetFrameworks)' == '' and $([MSBuild]::IsOsPlatform('Windows')) ">net9.0;net8.0;netstandard2.0;net472</TargetFrameworks>
<TargetFrameworks Condition=" '$(PublishAot)' == 'true' ">net9.0</TargetFrameworks>
<IsDotNet10SdkAvailable Condition="$([MSBuild]::VersionGreaterThanOrEquals('$(MSBuildVersion)', '17.15'))">true</IsDotNet10SdkAvailable>
<AotTargetFramework Condition="'$(IsDotNet10SdkAvailable)' != 'true'">net9.0</AotTargetFramework>
<AotTargetFramework Condition="'$(IsDotNet10SdkAvailable)' == 'true'">net10.0</AotTargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == ''">
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition="'$(IsDotNet10SdkAvailable)' == 'true'">net10.0;$(TargetFrameworks)</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOsPlatform('Windows'))">$(TargetFrameworks);net472</TargetFrameworks>
<TargetFrameworks Condition="'$(PublishAot)' == 'true'">$(AotTargetFramework)</TargetFrameworks>
</PropertyGroup>
<PropertyGroup>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<Configuration Condition="'$(Configuration)'==''">Debug</Configuration>
Expand Down
8 changes: 6 additions & 2 deletions bench/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<Project>
<!-- The main purpose of this file is to prevent use of the root Directory.Build.props file,
because Benchmark.NET does not like the build output paths to be redirected. -->
<PropertyGroup Condition="'$(TargetFramework)' == ''">
<IsDotNet10SdkAvailable Condition="$([MSBuild]::VersionGreaterThanOrEquals('$(MSBuildVersion)', '17.15'))">true</IsDotNet10SdkAvailable>
<TargetFrameworks>net9.0;net8.0;netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition="'$(IsDotNet10SdkAvailable)' == 'true'">net10.0;$(TargetFrameworks)</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOsPlatform('Windows'))">$(TargetFrameworks);net472</TargetFrameworks>
</PropertyGroup>
<PropertyGroup>
<TargetFrameworks Condition=" '$(TargetFrameworks)' == '' and ! $([MSBuild]::IsOsPlatform('Windows')) ">net9.0;net8.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(TargetFrameworks)' == '' and $([MSBuild]::IsOsPlatform('Windows')) ">net9.0;net8.0;net472</TargetFrameworks>
<LangVersion>12</LangVersion>
<Nullable>enable</Nullable>
<PackRelease>false</PackRelease>
Expand Down
4 changes: 2 additions & 2 deletions src/NodeApi.DotNetHost/NodeApi.DotNetHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<ItemGroup>
<ProjectReference Include="..\NodeApi\NodeApi.csproj" GeneratePathProperty="true" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<None Pack="true" PackagePath="\" Include="..\..\README.md" />
</ItemGroup>
Expand All @@ -28,7 +28,7 @@
It's necessary to use <Exec/> here rather than a recursive <MSBuild/> task because
PublishAot modifies properties/targets which would not be re-computed by the task.
-->
<Exec Command="dotnet publish $(MSBuildThisFileDirectory)..\NodeApi\NodeApi.csproj -c $(Configuration) -f net9.0 --self-contained -p:PublishAot=true" />
<Exec Command="dotnet publish $(MSBuildThisFileDirectory)..\NodeApi\NodeApi.csproj -c $(Configuration) -f $(AotTargetFramework) --self-contained -p:PublishAot=true" />
</Target>

<Target Name="NpmPack"
Expand Down
127 changes: 100 additions & 27 deletions src/NodeApi.Generator/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,21 +249,40 @@ private static TypeInfo BuildSymbolicEnumType(
return enumBuilder.CreateTypeInfo()!;
}

private static TypeAttributes GetTypeAttributes(TypeKind typeKind)
private static TypeAttributes GetTypeAttributes(ITypeSymbol typeSymbol)
{
TypeAttributes attributes = TypeAttributes.Public;
if (typeKind == TypeKind.Interface)
{
attributes |= TypeAttributes.Interface;
}
else if (typeKind == TypeKind.Delegate)
{
attributes |= TypeAttributes.Sealed;
}
if (typeKind != TypeKind.Enum)

switch (typeSymbol.TypeKind)
{
attributes |= TypeAttributes.Abstract;
case TypeKind.Interface:
attributes |= TypeAttributes.Interface | TypeAttributes.Abstract;
break;

case TypeKind.Class:
if (typeSymbol.IsAbstract || typeSymbol.IsStatic)
{
attributes |= TypeAttributes.Abstract;
}
if (typeSymbol.IsSealed || typeSymbol.IsStatic)
{
attributes |= TypeAttributes.Sealed;
}
break;

case TypeKind.Struct:
attributes |= TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit;
break;

case TypeKind.Delegate:
attributes |= TypeAttributes.Sealed;
break;

case TypeKind.Enum:
attributes |= TypeAttributes.Abstract;
break;
}

return attributes;
}

Expand Down Expand Up @@ -291,7 +310,7 @@ private static Type BuildSymbolicObjectType(
{
typeBuilder = ModuleBuilder.DefineType(
name: typeFullName,
GetTypeAttributes(typeSymbol.TypeKind),
GetTypeAttributes(typeSymbol),
parent: baseType);

if (typeSymbol.TypeParameters.Length > 0)
Expand Down Expand Up @@ -347,7 +366,15 @@ static PropertyInfo GetAttributeProperty(Type type, string name)
return thisType;
}

return typeBuilder.CreateTypeInfo()!;
try
{
return typeBuilder.CreateTypeInfo()!;
}
catch (Exception ex)
{
throw new InvalidOperationException(
$"Failed to create type info for type: {typeSymbol.Name}", ex);
}
}

/// <summary>
Expand Down Expand Up @@ -390,7 +417,15 @@ static void BuildType(
if (SymbolicTypes.TryGetValue(typeFullName, out Type? type) &&
type is TypeBuilder typeBuilder)
{
typeBuilder.CreateTypeInfo();
try
{
typeBuilder.CreateTypeInfo();
}
catch (Exception ex)
{
throw new InvalidOperationException(
$"Failed to create type info for type: {typeSymbol.Name}", ex);
}
}
}

Expand Down Expand Up @@ -449,7 +484,10 @@ private static void BuildSymbolicTypeMembers(
}

foreach (ISymbol memberSymbol in typeSymbol.GetMembers()
.Where((m) => m.DeclaredAccessibility == Accessibility.Public))
.Where((m) => m.DeclaredAccessibility == Accessibility.Public ||
(m.DeclaredAccessibility == Accessibility.Private &&
(m is IMethodSymbol ms && ms.ExplicitInterfaceImplementations.Length > 0) ||
(m is IPropertySymbol ps && ps.ExplicitInterfaceImplementations.Length > 0))))
{
if (memberSymbol is IMethodSymbol constructorSymbol &&
constructorSymbol.MethodKind == MethodKind.Constructor)
Expand All @@ -458,6 +496,7 @@ private static void BuildSymbolicTypeMembers(
}
else if (memberSymbol is IMethodSymbol methodSymbol &&
(methodSymbol.MethodKind == MethodKind.Ordinary ||
methodSymbol.MethodKind == MethodKind.ExplicitInterfaceImplementation ||
methodSymbol.MethodKind == MethodKind.DelegateInvoke))
{
BuildSymbolicMethod(typeBuilder, methodSymbol, genericTypeParameters);
Expand All @@ -476,7 +515,7 @@ private static void BuildSymbolicTypeMembers(
}
else if (memberSymbol is INamedTypeSymbol nestedTypeSymbol)
{
TypeAttributes attributes = GetTypeAttributes(nestedTypeSymbol.TypeKind);
TypeAttributes attributes = GetTypeAttributes(nestedTypeSymbol);
attributes &= ~TypeAttributes.Public;
attributes |= TypeAttributes.NestedPublic;

Expand Down Expand Up @@ -533,9 +572,17 @@ private static void BuildSymbolicMethod(
Type[]? genericTypeParameters)
{
bool isDelegateMethod = typeBuilder.BaseType == typeof(MulticastDelegate);
MethodAttributes attributes = MethodAttributes.Public | (methodSymbol.IsStatic ?
MethodAttributes.Static : MethodAttributes.Virtual | (isDelegateMethod ?
MethodAttributes.HideBySig : MethodAttributes.Abstract));
// All nonstatic methods are declared virtual on symbolic types.
// This allows any struct/class methods to implement interface methods.
MethodAttributes attributes =
(methodSymbol.DeclaredAccessibility == Accessibility.Public ?
MethodAttributes.Public : MethodAttributes.Private) |
(methodSymbol.IsStatic ? MethodAttributes.Static : MethodAttributes.Virtual) |
(methodSymbol.IsAbstract ? MethodAttributes.Abstract : default) |
(methodSymbol.ExplicitInterfaceImplementations.Length > 0 ?
MethodAttributes.Final : default) |
(isDelegateMethod ? MethodAttributes.HideBySig : default);

MethodBuilder methodBuilder = typeBuilder.DefineMethod(
methodSymbol.Name,
attributes,
Expand Down Expand Up @@ -565,29 +612,41 @@ private static void BuildSymbolicMethod(
methodBuilder.SetImplementationFlags(
MethodImplAttributes.Runtime | MethodImplAttributes.Managed);
}
else if (methodSymbol.IsStatic)
else if (!methodSymbol.IsAbstract)
{
// Static methods cannot be abstract; emit a minimal body.
// Emit a minimal method body.
methodBuilder.GetILGenerator().Emit(OpCodes.Ret);
}

if (methodSymbol.ExplicitInterfaceImplementations.Length > 0)
{
MethodInfo interfaceMethod =
methodSymbol.ExplicitInterfaceImplementations[0].AsMethodInfo();
typeBuilder.DefineMethodOverride(methodBuilder, interfaceMethod);
}
}

private static void BuildSymbolicProperty(
TypeBuilder typeBuilder,
IPropertySymbol propertySymbol,
Type[]? genericTypeParameters)
{
MethodAttributes attributes = MethodAttributes.SpecialName |
(propertySymbol.DeclaredAccessibility == Accessibility.Public ?
MethodAttributes.Public : MethodAttributes.Private) |
(propertySymbol.IsStatic ? MethodAttributes.Static : MethodAttributes.Virtual) |
(propertySymbol.IsAbstract ? MethodAttributes.Abstract : default) |
(propertySymbol.IsVirtual ? MethodAttributes.Virtual : default) |
(propertySymbol.ExplicitInterfaceImplementations.Length > 0 ?
MethodAttributes.Final : default);

PropertyBuilder propertyBuilder = typeBuilder.DefineProperty(
propertySymbol.Name,
PropertyAttributes.None,
propertySymbol.Type.AsType(genericTypeParameters, buildType: false),
propertySymbol.Parameters.Select(
(p) => p.Type.AsType(genericTypeParameters, buildType: false)).ToArray());

MethodAttributes attributes = MethodAttributes.SpecialName | MethodAttributes.Public |
(propertySymbol.IsStatic ? MethodAttributes.Static :
MethodAttributes.Abstract | MethodAttributes.Virtual);

if (propertySymbol.GetMethod != null)
{
MethodBuilder getMethodBuilder = typeBuilder.DefineMethod(
Expand All @@ -598,8 +657,15 @@ private static void BuildSymbolicProperty(
propertySymbol.GetMethod.Parameters.Select(
(p) => p.Type.AsType(genericTypeParameters, buildType: false)).ToArray());
BuildSymbolicParameters(getMethodBuilder, propertySymbol.GetMethod.Parameters);
if (propertySymbol.IsStatic) getMethodBuilder.GetILGenerator().Emit(OpCodes.Ret);
if (!propertySymbol.IsAbstract) getMethodBuilder.GetILGenerator().Emit(OpCodes.Ret);
propertyBuilder.SetGetMethod(getMethodBuilder);

if (propertySymbol.ExplicitInterfaceImplementations.Length > 0)
{
MethodInfo interfaceGetMethod =
propertySymbol.ExplicitInterfaceImplementations[0].GetMethod!.AsMethodInfo();
typeBuilder.DefineMethodOverride(getMethodBuilder, interfaceGetMethod);
}
}

if (propertySymbol.SetMethod != null)
Expand All @@ -612,8 +678,15 @@ private static void BuildSymbolicProperty(
propertySymbol.SetMethod.Parameters.Select(
(p) => p.Type.AsType(genericTypeParameters, buildType: false)).ToArray());
BuildSymbolicParameters(setMethodBuilder, propertySymbol.SetMethod.Parameters);
if (propertySymbol.IsStatic) setMethodBuilder.GetILGenerator().Emit(OpCodes.Ret);
if (!propertySymbol.IsAbstract) setMethodBuilder.GetILGenerator().Emit(OpCodes.Ret);
propertyBuilder.SetSetMethod(setMethodBuilder);

if (propertySymbol.ExplicitInterfaceImplementations.Length > 0)
{
MethodInfo interfaceSetMethod =
propertySymbol.ExplicitInterfaceImplementations[0].SetMethod!.AsMethodInfo();
typeBuilder.DefineMethodOverride(setMethodBuilder, interfaceSetMethod);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/NodeApi/NodeApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<UseSystemResourceKeys>true</UseSystemResourceKeys><!-- Trim detailed system exception messages. -->
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net9.0' ">
<PropertyGroup Condition=" '$(TargetFramework)' == '$(AotTargetFramework)' ">
<!-- Enable AOT compatibility checks during compilation even when not publishing the AOT binary. -->
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>
Expand Down
4 changes: 3 additions & 1 deletion src/node-api-dotnet/pack.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ if (!packageName || !configuration || rids.length === 0) {
const assemblyName = 'Microsoft.JavaScript.NodeApi';

const targetFrameworks = ['net9.0', 'net8.0'];
const dotnetGlobalJson = require('../../global.json');
if (dotnetGlobalJson.sdk.version.startsWith('10.')) targetFrameworks.unshift('net10.0');
if (process.platform === 'win32') targetFrameworks.push('net472');

const fs = require('fs');
Expand Down Expand Up @@ -169,7 +171,7 @@ function copyFrameworkSpecificBinaries(targetFrameworks, packageStageDir, ...bin
binFileName.startsWith('System.') &&
!binFileName.includes('MetadataLoadContext')
) return;

// Exclude Microsoft.Bcl.AsyncInterfaces from new platforms
if (
tfm.includes('.') &&
Expand Down
16 changes: 14 additions & 2 deletions test/JSProjectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,19 @@ public void Test(string id)

string compileLogFilePath = GetBuildLogFilePath(
projectName + "-" + moduleName, "projects");
string tsConfigFile = "tsconfig." + Path.GetFileNameWithoutExtension(moduleName) + ".json";

string tsConfigFile = "tsconfig.json";
if (IsCurrentTargetFramework(moduleName))
{
tsConfigFile = $"tsconfig.{moduleName}.json";
File.WriteAllText(
Path.Combine(ProjectDir(projectName), tsConfigFile),
$"{{\n" +
$" \"extends\": \"./tsconfig.json\",\n" +
$" \"include\": [\"{moduleName}.ts\", \"{moduleName}.js\", \"bin/*.js\"]\n" +
$"}}");
}

BuildTestProjectTypeScript(projectName,
compileLogFilePath,
File.Exists(Path.Combine(ProjectDir(projectName), tsConfigFile)) ?
Expand Down Expand Up @@ -165,7 +177,7 @@ private static void BuildTestProjectTypeScript(
};

logWriter.WriteLine();
logWriter.WriteLine("tsc");
logWriter.WriteLine("node " + nodeArgs);

Process nodeProcess = Process.Start(nodeStartInfo)!;
errorOutput = LogOutput(nodeProcess, logWriter);
Expand Down
4 changes: 0 additions & 4 deletions test/NativeAotTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#if NET9_0_OR_GREATER

using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -107,5 +105,3 @@ private static void BuildTestModuleTypeScript(string _ /*testCaseName*/)
// Reference the generated type definitions from the C#?
}
}

#endif // NET9_0_OR_GREATER
14 changes: 11 additions & 3 deletions test/NodeApi.Test.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsDotNet10SdkAvailable Condition="$([MSBuild]::VersionGreaterThanOrEquals('$(MSBuildVersion)', '17.15'))">true</IsDotNet10SdkAvailable>
<AotTargetFramework Condition="'$(IsDotNet10SdkAvailable)' != 'true'">net9.0</AotTargetFramework>
<AotTargetFramework Condition="'$(IsDotNet10SdkAvailable)' == 'true'">net10.0</AotTargetFramework>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == ''">
<!-- Exclude netstandard2.0 from target frameworks for tests. -->
<TargetFrameworks Condition=" ! $([MSBuild]::IsOsPlatform('Windows')) ">net9.0;net8.0</TargetFrameworks>
<TargetFrameworks Condition=" $([MSBuild]::IsOsPlatform('Windows')) ">net9.0;net8.0;net472</TargetFrameworks>
<TargetFrameworks>net9.0;net8.0</TargetFrameworks>
<TargetFrameworks Condition="'$(IsDotNet10SdkAvailable)' == 'true'">net10.0;$(TargetFrameworks)</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOsPlatform('Windows'))">$(TargetFrameworks);net472</TargetFrameworks>
</PropertyGroup>
<PropertyGroup>
<TestTfmsInParallel>false</TestTfmsInParallel>

<RootNamespace>Microsoft.JavaScript.NodeApi.Test</RootNamespace>
<AssemblyName>Microsoft.JavaScript.NodeApi.Test</AssemblyName>
<IsPublishable>false</IsPublishable>
Expand All @@ -17,6 +24,7 @@
<None Include="TestCases\**\*.ts" />
<None Include="TestCases\**\*.js" />
<Compile Remove="TestCases\**" />
<Compile Condition="'$(TargetFramework)' != '$(AotTargetFramework)'" Remove="NativeAotTests.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading
Loading