@@ -215,7 +215,7 @@ private static NamespaceDeclarationSyntax GenerateRegSource(
215
215
const string generatedNamespace = "System.Runtime.InteropServices.JavaScript" ;
216
216
const string initializerClass = "__GeneratedInitializer" ;
217
217
const string initializerName = "__Register_" ;
218
- const string selfInitName = "__Net7SelfInit_ " ;
218
+ const string trimmingPreserveName = "__TrimmingPreserve_ " ;
219
219
220
220
if ( methods . IsEmpty ) return NamespaceDeclaration ( IdentifierName ( generatedNamespace ) ) ;
221
221
@@ -241,22 +241,42 @@ private static NamespaceDeclarationSyntax GenerateRegSource(
241
241
. WithModifiers ( TokenList ( new [ ] { Token ( SyntaxKind . StaticKeyword ) } ) )
242
242
. WithBody ( Block ( registerStatements ) ) ;
243
243
244
- // 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
245
- // this also keeps the code protected from trimming
246
- MemberDeclarationSyntax initializerMethod = MethodDeclaration ( PredefinedType ( Token ( SyntaxKind . VoidKeyword ) ) , Identifier ( selfInitName ) )
247
- . WithAttributeLists ( List ( new [ ] {
248
- AttributeList ( SingletonSeparatedList ( Attribute ( IdentifierName ( Constants . ModuleInitializerAttributeGlobal ) ) ) ) ,
249
- } ) )
244
+ // HACK: protect the code from trimming with DynamicDependency attached to a ModuleInitializer
245
+ MemberDeclarationSyntax initializerMethod = MethodDeclaration ( PredefinedType ( Token ( SyntaxKind . VoidKeyword ) ) , Identifier ( trimmingPreserveName ) )
246
+ . WithAttributeLists (
247
+ SingletonList < AttributeListSyntax > (
248
+ AttributeList (
249
+ SeparatedList < AttributeSyntax > (
250
+ new SyntaxNodeOrToken [ ] {
251
+ Attribute (
252
+ IdentifierName ( Constants . ModuleInitializerAttributeGlobal ) ) ,
253
+ Token ( SyntaxKind . CommaToken ) ,
254
+ Attribute (
255
+ IdentifierName ( Constants . DynamicDependencyAttributeGlobal ) )
256
+ . WithArgumentList (
257
+ AttributeArgumentList (
258
+ SeparatedList < AttributeArgumentSyntax > (
259
+ new SyntaxNodeOrToken [ ] {
260
+ AttributeArgument (
261
+ BinaryExpression (
262
+ SyntaxKind . BitwiseOrExpression ,
263
+ MemberAccessExpression (
264
+ SyntaxKind . SimpleMemberAccessExpression ,
265
+ IdentifierName ( Constants . DynamicallyAccessedMemberTypesGlobal ) ,
266
+ IdentifierName ( "PublicMethods" ) ) ,
267
+ MemberAccessExpression (
268
+ SyntaxKind . SimpleMemberAccessExpression ,
269
+ IdentifierName ( Constants . DynamicallyAccessedMemberTypesGlobal ) ,
270
+ IdentifierName ( "NonPublicMethods" ) ) ) ) ,
271
+ Token ( SyntaxKind . CommaToken ) ,
272
+ AttributeArgument (
273
+ TypeOfExpression (
274
+ IdentifierName ( initializerClass ) ) ) } ) ) ) } ) ) ) )
250
275
. WithModifiers ( TokenList ( new [ ] {
251
276
Token ( SyntaxKind . StaticKeyword ) ,
252
277
Token ( SyntaxKind . InternalKeyword )
253
278
} ) )
254
- . WithBody ( Block (
255
- IfStatement ( BinaryExpression ( SyntaxKind . EqualsExpression ,
256
- IdentifierName ( "Environment.Version.Major" ) ,
257
- LiteralExpression ( SyntaxKind . NumericLiteralExpression , Literal ( 7 ) ) ) ,
258
- Block ( SingletonList < StatementSyntax > (
259
- ExpressionStatement ( InvocationExpression ( IdentifierName ( initializerName ) ) ) ) ) ) ) ) ;
279
+ . WithBody ( Block ( ) ) ;
260
280
261
281
var ns = NamespaceDeclaration ( IdentifierName ( generatedNamespace ) )
262
282
. WithMembers (
0 commit comments