From 7a4be9072a8a4d848f86f9cd9e60f4d33ce22d38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Thu, 13 Jun 2024 10:17:51 +0200 Subject: [PATCH 1/2] Use DynamicDependencyAttribute contructor with string values for typeName and assemblyName --- .../gen/JSImportGenerator/JSExportGenerator.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) 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 ee7eb94cdd3e51..9925f2c86be856 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSExportGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/gen/JSImportGenerator/JSExportGenerator.cs @@ -34,6 +34,8 @@ public static class StepNames public void Initialize(IncrementalGeneratorInitializationContext context) { + var assemblyName = context.CompilationProvider.Select(static (c, _) => c.AssemblyName); + // Collect all methods adorned with JSExportAttribute var attributedMethods = context.SyntaxProvider .ForAttributeWithMetadataName(Constants.JSExportAttribute, @@ -96,7 +98,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context) .Collect(); IncrementalValueProvider registration = regSyntax - .Select(static (data, ct) => GenerateRegSource(data)) + .Combine(assemblyName) + .Select(static (data, ct) => GenerateRegSource(data.Left, data.Right)) .Select(static (data, ct) => data.NormalizeWhitespace().ToFullString()); IncrementalValueProvider> generated = generateSingleStub @@ -210,7 +213,7 @@ private static IncrementalStubGenerationContext CalculateStubInformation( } private static NamespaceDeclarationSyntax GenerateRegSource( - ImmutableArray<(StatementSyntax Registration, AttributeListSyntax Attribute)> methods) + ImmutableArray<(StatementSyntax Registration, AttributeListSyntax Attribute)> methods, string assemblyName) { const string generatedNamespace = "System.Runtime.InteropServices.JavaScript"; const string initializerClass = "__GeneratedInitializer"; @@ -270,8 +273,13 @@ private static NamespaceDeclarationSyntax GenerateRegSource( IdentifierName("NonPublicMethods")))), Token(SyntaxKind.CommaToken), AttributeArgument( - TypeOfExpression( - IdentifierName(initializerClass)))})))})))) + LiteralExpression(SyntaxKind.StringLiteralExpression, Literal($"{generatedNamespace}.{initializerClass}")) + ), + Token(SyntaxKind.CommaToken), + AttributeArgument( + LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(assemblyName)) + ) + })))})))) .WithModifiers(TokenList(new[] { Token(SyntaxKind.StaticKeyword), Token(SyntaxKind.InternalKeyword) From b6a11d1206c0ffb73feb663d30094b8d3cf5f825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Fi=C5=A1era?= Date: Thu, 13 Jun 2024 14:57:42 +0200 Subject: [PATCH 2/2] Test case --- .../JavaScriptLibrary/JavaScriptInterop.cs | 15 +++++++++++ .../JavaScriptLibrary.csproj | 26 +++++++++++++++++++ ...me.InteropServices.JavaScript.Tests.csproj | 1 + .../JavaScript/JSExportTest.cs | 6 +++++ .../JavaScript/JavaScriptTestHelper.cs | 3 +++ .../JavaScript/JavaScriptTestHelper.mjs | 5 ++++ 6 files changed, 56 insertions(+) create mode 100644 src/libraries/System.Runtime.InteropServices.JavaScript/tests/JavaScriptLibrary/JavaScriptInterop.cs create mode 100644 src/libraries/System.Runtime.InteropServices.JavaScript/tests/JavaScriptLibrary/JavaScriptLibrary.csproj diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/JavaScriptLibrary/JavaScriptInterop.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/JavaScriptLibrary/JavaScriptInterop.cs new file mode 100644 index 00000000000000..79f6af9ebe43a5 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/JavaScriptLibrary/JavaScriptInterop.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.InteropServices.JavaScript; + +namespace JavaScriptLibrary; + +public partial class JavaScriptInterop +{ + [JSExport] + public static int ExportedMethod(int a, int b) => a + b; + + internal static int ValidationMethod(int a, int b) => a + b; +} \ No newline at end of file diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/JavaScriptLibrary/JavaScriptLibrary.csproj b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/JavaScriptLibrary/JavaScriptLibrary.csproj new file mode 100644 index 00000000000000..9d1b9c04271991 --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/JavaScriptLibrary/JavaScriptLibrary.csproj @@ -0,0 +1,26 @@ + + + $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) + true + $(NetCoreAppCurrent)-browser + + true + + + + true + false + + + + + + + + + + + + + + diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System.Runtime.InteropServices.JavaScript.Tests.csproj b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System.Runtime.InteropServices.JavaScript.Tests.csproj index 135b1b78297ab8..eb19722912195e 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System.Runtime.InteropServices.JavaScript.Tests.csproj +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System.Runtime.InteropServices.JavaScript.Tests.csproj @@ -34,6 +34,7 @@ + diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSExportTest.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSExportTest.cs index d8c3f71ff73ec9..17f75816c31c6c 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSExportTest.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSExportTest.cs @@ -430,5 +430,11 @@ public void JsExportCallback_FunctionIntIntThrow() res = invoke(value, echoName); Assert.Equal(value, res); } + + [Fact] + public async Task InternalsVisibleToDoesntBreak() + { + Assert.Equal(JavaScriptLibrary.JavaScriptInterop.ValidationMethod(5, 6), await JavaScriptTestHelper.callJavaScriptLibrary(5, 6)); + } } } diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.cs index aa0c3f4cb86c42..db770def63ad62 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.cs @@ -1019,6 +1019,9 @@ public static JSObject EchoIJSObject([JSMarshalAs] JSObject arg1) { return arg1; } + + [JSImport("callJavaScriptLibrary", "JavaScriptTestHelper")] + public static partial Task callJavaScriptLibrary(int a, int b); [JSImport("echopromise", "JavaScriptTestHelper")] [return: JSMarshalAs>] diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.mjs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.mjs index 266017afdb2d5c..a7d3e3a1ae38fe 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.mjs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.mjs @@ -427,6 +427,11 @@ export async function backbackAsync(arg1, arg2, arg3) { return arg1(arg2, arg3); } +export async function callJavaScriptLibrary(a, b) { + const exports = await App.runtime.getAssemblyExports("JavaScriptLibrary.dll"); + return exports.JavaScriptLibrary.JavaScriptInterop.ExportedMethod(a, b); +} + export const instance = {} globalThis.javaScriptTestHelper = instance;