diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Constants.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Constants.cs index 520eeba3ec4e8..a5475e27c70a4 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Constants.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Constants.cs @@ -17,6 +17,7 @@ internal static class Constants public const string ModuleInitializerAttributeGlobal = "global::System.Runtime.CompilerServices.ModuleInitializerAttribute"; public const string CompilerGeneratedAttributeGlobal = "global::System.Runtime.CompilerServices.CompilerGeneratedAttribute"; public const string DynamicDependencyAttributeGlobal = "global::System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute"; + public const string DynamicallyAccessedMemberTypesGlobal = "global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes"; public const string ThreadStaticGlobal = "global::System.ThreadStaticAttribute"; public const string TaskGlobal = "global::System.Threading.Tasks.Task"; public const string SpanGlobal = "global::System.Span"; diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSExportGenerator.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSExportGenerator.cs index ffe0fe6a0f995..ee7eb94cdd3e5 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSExportGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSExportGenerator.cs @@ -215,7 +215,7 @@ private static NamespaceDeclarationSyntax GenerateRegSource( const string generatedNamespace = "System.Runtime.InteropServices.JavaScript"; const string initializerClass = "__GeneratedInitializer"; const string initializerName = "__Register_"; - const string selfInitName = "__Net7SelfInit_"; + const string trimmingPreserveName = "__TrimmingPreserve_"; if (methods.IsEmpty) return NamespaceDeclaration(IdentifierName(generatedNamespace)); @@ -241,22 +241,42 @@ private static NamespaceDeclarationSyntax GenerateRegSource( .WithModifiers(TokenList(new[] { Token(SyntaxKind.StaticKeyword) })) .WithBody(Block(registerStatements)); - // when we are running code generated by .NET8 on .NET7 runtime we need to auto initialize the assembly, because .NET7 doesn't call the registration from JS - // this also keeps the code protected from trimming - MemberDeclarationSyntax initializerMethod = MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(selfInitName)) - .WithAttributeLists(List(new[]{ - AttributeList(SingletonSeparatedList(Attribute(IdentifierName(Constants.ModuleInitializerAttributeGlobal)))), - })) + // HACK: protect the code from trimming with DynamicDependency attached to a ModuleInitializer + MemberDeclarationSyntax initializerMethod = MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(trimmingPreserveName)) + .WithAttributeLists( + SingletonList( + AttributeList( + SeparatedList( + new SyntaxNodeOrToken[]{ + Attribute( + IdentifierName(Constants.ModuleInitializerAttributeGlobal)), + Token(SyntaxKind.CommaToken), + Attribute( + IdentifierName(Constants.DynamicDependencyAttributeGlobal)) + .WithArgumentList( + AttributeArgumentList( + SeparatedList( + new SyntaxNodeOrToken[]{ + AttributeArgument( + BinaryExpression( + SyntaxKind.BitwiseOrExpression, + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName(Constants.DynamicallyAccessedMemberTypesGlobal), + IdentifierName("PublicMethods")), + MemberAccessExpression( + SyntaxKind.SimpleMemberAccessExpression, + IdentifierName(Constants.DynamicallyAccessedMemberTypesGlobal), + IdentifierName("NonPublicMethods")))), + Token(SyntaxKind.CommaToken), + AttributeArgument( + TypeOfExpression( + IdentifierName(initializerClass)))})))})))) .WithModifiers(TokenList(new[] { Token(SyntaxKind.StaticKeyword), Token(SyntaxKind.InternalKeyword) })) - .WithBody(Block( - IfStatement(BinaryExpression(SyntaxKind.EqualsExpression, - IdentifierName("Environment.Version.Major"), - LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(7))), - Block(SingletonList( - ExpressionStatement(InvocationExpression(IdentifierName(initializerName)))))))); + .WithBody(Block()); var ns = NamespaceDeclaration(IdentifierName(generatedNamespace)) .WithMembers(