Skip to content

Commit cafd480

Browse files
committed
Preserve register method using DynamicDependency
1 parent 96666c5 commit cafd480

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal static class Constants
1717
public const string ModuleInitializerAttributeGlobal = "global::System.Runtime.CompilerServices.ModuleInitializerAttribute";
1818
public const string CompilerGeneratedAttributeGlobal = "global::System.Runtime.CompilerServices.CompilerGeneratedAttribute";
1919
public const string DynamicDependencyAttributeGlobal = "global::System.Diagnostics.CodeAnalysis.DynamicDependencyAttribute";
20+
public const string DynamicallyAccessedMemberTypesGlobal = "global::System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes";
2021
public const string ThreadStaticGlobal = "global::System.ThreadStaticAttribute";
2122
public const string TaskGlobal = "global::System.Threading.Tasks.Task";
2223
public const string SpanGlobal = "global::System.Span";

src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSExportGenerator.cs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -245,19 +245,40 @@ private static NamespaceDeclarationSyntax GenerateRegSource(
245245
// will always evaluate to false at runtime, but the linker can't see through at
246246
// build time. It seems there's no attribute to block trimming of a method?
247247
MemberDeclarationSyntax initializerMethod = MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(selfInitName))
248-
.WithAttributeLists(List(new[]{
249-
AttributeList(SingletonSeparatedList(Attribute(IdentifierName(Constants.ModuleInitializerAttributeGlobal)))),
250-
}))
248+
.WithAttributeLists(
249+
SingletonList<AttributeListSyntax>(
250+
AttributeList(
251+
SeparatedList<AttributeSyntax>(
252+
new SyntaxNodeOrToken[]{
253+
Attribute(
254+
IdentifierName(Constants.ModuleInitializerAttributeGlobal)),
255+
Token(SyntaxKind.CommaToken),
256+
Attribute(
257+
IdentifierName(Constants.DynamicDependencyAttributeGlobal))
258+
.WithArgumentList(
259+
AttributeArgumentList(
260+
SeparatedList<AttributeArgumentSyntax>(
261+
new SyntaxNodeOrToken[]{
262+
AttributeArgument(
263+
BinaryExpression(
264+
SyntaxKind.BitwiseOrExpression,
265+
MemberAccessExpression(
266+
SyntaxKind.SimpleMemberAccessExpression,
267+
IdentifierName(Constants.DynamicallyAccessedMemberTypesGlobal),
268+
IdentifierName("PublicMethods")),
269+
MemberAccessExpression(
270+
SyntaxKind.SimpleMemberAccessExpression,
271+
IdentifierName(Constants.DynamicallyAccessedMemberTypesGlobal),
272+
IdentifierName("NonPublicMethods")))),
273+
Token(SyntaxKind.CommaToken),
274+
AttributeArgument(
275+
TypeOfExpression(
276+
IdentifierName(initializerClass)))})))}))))
251277
.WithModifiers(TokenList(new[] {
252278
Token(SyntaxKind.StaticKeyword),
253279
Token(SyntaxKind.InternalKeyword)
254280
}))
255-
.WithBody(Block(
256-
IfStatement(BinaryExpression(SyntaxKind.EqualsExpression,
257-
IdentifierName("System.Runtime.InteropServices.JavaScript.JSHost.GlobalThis"),
258-
LiteralExpression(SyntaxKind.NullLiteralExpression)),
259-
Block(SingletonList<StatementSyntax>(
260-
ExpressionStatement(InvocationExpression(IdentifierName(initializerName))))))));
281+
.WithBody(Block());
261282

262283
var ns = NamespaceDeclaration(IdentifierName(generatedNamespace))
263284
.WithMembers(

0 commit comments

Comments
 (0)