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 8ada0d78672ed2..ffe0fe6a0f995b 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSExportGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSExportGenerator.cs @@ -241,9 +241,8 @@ private static NamespaceDeclarationSyntax GenerateRegSource( .WithModifiers(TokenList(new[] { Token(SyntaxKind.StaticKeyword) })) .WithBody(Block(registerStatements)); - // HACK: protect the code from trimming by putting it behind a comparison we know - // will always evaluate to false at runtime, but the linker can't see through at - // build time. It seems there's no attribute to block trimming of a method? + // 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)))), @@ -254,8 +253,8 @@ private static NamespaceDeclarationSyntax GenerateRegSource( })) .WithBody(Block( IfStatement(BinaryExpression(SyntaxKind.EqualsExpression, - IdentifierName("System.Runtime.InteropServices.JavaScript.JSHost.GlobalThis"), - LiteralExpression(SyntaxKind.NullLiteralExpression)), + IdentifierName("Environment.Version.Major"), + LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(7))), Block(SingletonList( ExpressionStatement(InvocationExpression(IdentifierName(initializerName))))))));