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 ffe0fe6a0f995b..8ada0d78672ed2 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSExportGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSExportGenerator.cs @@ -241,8 +241,9 @@ 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 + // 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? MemberDeclarationSyntax initializerMethod = MethodDeclaration(PredefinedType(Token(SyntaxKind.VoidKeyword)), Identifier(selfInitName)) .WithAttributeLists(List(new[]{ AttributeList(SingletonSeparatedList(Attribute(IdentifierName(Constants.ModuleInitializerAttributeGlobal)))), @@ -253,8 +254,8 @@ private static NamespaceDeclarationSyntax GenerateRegSource( })) .WithBody(Block( IfStatement(BinaryExpression(SyntaxKind.EqualsExpression, - IdentifierName("Environment.Version.Major"), - LiteralExpression(SyntaxKind.NumericLiteralExpression, Literal(7))), + IdentifierName("System.Runtime.InteropServices.JavaScript.JSHost.GlobalThis"), + LiteralExpression(SyntaxKind.NullLiteralExpression)), Block(SingletonList( ExpressionStatement(InvocationExpression(IdentifierName(initializerName))))))));