Skip to content

Commit c2663b5

Browse files
authored
[browser] trimming of LegacyExports with WasmEnableLegacyJsInterop (#83664)
1 parent 018a37b commit c2663b5

File tree

29 files changed

+118
-88
lines changed

29 files changed

+118
-88
lines changed

eng/testing/tests.browser.targets

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@
4141
<WasmIncludeFullIcuData>true</WasmIncludeFullIcuData>
4242
</PropertyGroup>
4343

44+
<PropertyGroup Condition="'$(BuildAOTTestsOn)' == 'local'">
45+
<!--
46+
When building for BuildAOTTestsOnHelix=true, the WasmApp*targets are *not* imported, because
47+
they get instead used by the AOT proxy project on helix.
48+
49+
On the build machine only the regular part of the build is run, which includes trimming. But if
50+
WasmApp*targets modify any trimming arguments, then those will not get picked up by this build.
51+
For example - linker substitution files used with simd builds.
52+
53+
So, set those parameters explicitly here.
54+
-->
55+
<_ExtraTrimmerArgs Condition="'$(WasmEnableLegacyJsInterop)' == 'false'">$(_ExtraTrimmerArgs) --substitutions &quot;$(MonoProjectRoot)\wasm\build\ILLink.Substitutions.LegacyJsInterop.xml&quot;</_ExtraTrimmerArgs>
56+
</PropertyGroup>
57+
58+
4459
<!-- On CI this is installed as part of pretest, but it should still be installed
4560
for WBT, and debugger tests -->
4661
<Import Project="$(MSBuildThisFileDirectory)wasm-provisioning.targets"
@@ -125,11 +140,6 @@
125140
<PropertyGroup Condition="'$(BuildAOTTestsOnHelix)' == 'true'">
126141
<!-- wasm targets are not imported at all, in this case, because we run the wasm build on helix -->
127142
</PropertyGroup>
128-
<ItemGroup Condition="'$(BuildAOTTestsOn)' == 'helix'">
129-
<TrimmerRootDescriptor
130-
Condition="'$(WasmEnableLegacyJsInterop)' == 'true'"
131-
Include="$(MonoProjectRoot)\wasm\build\ILLink.Descriptors.LegacyJsInterop.xml" />
132-
</ItemGroup>
133143

134144
<PropertyGroup Condition="'$(IsWasmProject)' == 'true' and '$(BuildAOTTestsOnHelix)' != 'true'">
135145
<WasmBuildOnlyAfterPublish>true</WasmBuildOnlyAfterPublish>

src/installer/pkg/sfx/Microsoft.NETCore.App/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@
253253
<PlatformManifestFileEntry Include="emcc-props.json" IsNative="true" />
254254
<PlatformManifestFileEntry Include="ILLink.Substitutions.WasmIntrinsics.xml" IsNative="true" />
255255
<PlatformManifestFileEntry Include="ILLink.Substitutions.NoWasmIntrinsics.xml" IsNative="true" />
256-
<PlatformManifestFileEntry Include="ILLink.Descriptors.LegacyJsInterop.xml" IsNative="true" />
256+
<PlatformManifestFileEntry Include="ILLink.Substitutions.LegacyJsInterop.xml" IsNative="true" />
257257
<!-- wasi specific -->
258258
<PlatformManifestFileEntry Include="main.c" IsNative="true" />
259259
<PlatformManifestFileEntry Include="driver.h" IsNative="true" />

src/libraries/System.Runtime.InteropServices.JavaScript/src/System.Runtime.InteropServices.JavaScript.csproj

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
<FeatureWasmThreads Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(MonoWasmBuildVariant)' == 'multithread'">true</FeatureWasmThreads>
1313
<WasmEnableLegacyJsInterop Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(WasmEnableLegacyJsInterop)' == ''">true</WasmEnableLegacyJsInterop>
1414
<DefineConstants Condition="'$(FeatureWasmThreads)' == 'true'" >$(DefineConstants);FEATURE_WASM_THREADS</DefineConstants>
15-
<DefineConstants Condition="'$(WasmEnableLegacyJsInterop)' == 'true'" >$(DefineConstants);ENABLE_LEGACY_JS_INTEROP</DefineConstants>
16-
<ILLinkDescriptorsLibraryBuildXml
17-
Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(WasmEnableLegacyJsInterop)' == 'true'"
18-
>$(MonoProjectRoot)wasm\build\ILLink.Descriptors.LegacyJsInterop.xml</ILLinkDescriptorsLibraryBuildXml>
15+
<DefineConstants Condition="'$(WasmEnableLegacyJsInterop)' == 'false'" >$(DefineConstants);DISABLE_LEGACY_JS_INTEROP</DefineConstants>
1916
</PropertyGroup>
2017

2118
<ItemGroup>
@@ -66,7 +63,7 @@
6663
</ItemGroup>
6764

6865
<!-- only include legacy interop when WasmEnableLegacyJsInterop is enabled -->
69-
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(WasmEnableLegacyJsInterop)' == 'true'">
66+
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(WasmEnableLegacyJsInterop)' != 'false'">
7067
<Compile Include="System\Runtime\InteropServices\JavaScript\Interop\LegacyExports.cs" />
7168
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\Runtime.cs" />
7269
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\Array.cs" />

src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/JavaScriptImports.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static void MarshalPromise(Span<JSMarshalerArgument> arguments)
2020
}
2121
}
2222

23-
#if ENABLE_LEGACY_JS_INTEROP
23+
#if !DISABLE_LEGACY_JS_INTEROP
2424
#region legacy
2525

2626
public static object GetGlobalObject(string? str = null)

src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/LegacyExports.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,34 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics;
5+
using System.Diagnostics.CodeAnalysis;
56
using System.Reflection;
67
using System.Runtime.CompilerServices;
78
using System.Text;
89
using System.Threading.Tasks;
910

1011
namespace System.Runtime.InteropServices.JavaScript
1112
{
12-
// the public methods are protected from trimming by ILLink.Descriptors.LegacyJsInterop.xml
13+
internal static unsafe partial class LegacyExportsTrimmingRoot
14+
{
15+
// the public methods are used from JavaScript, but the trimmer doesn't know about it.
16+
// It's protected by DynamicDependencyAttribute on JSFunctionBinding.BindJSFunction.
17+
public static void TrimWhenNotWasmEnableLegacyJsInterop()
18+
{
19+
// if MSBuild property WasmEnableLegacyJsInterop==false this call would be substituted away and LegacyExports would be trimmed.
20+
LegacyExports.PreventTrimming();
21+
}
22+
}
23+
1324
internal static unsafe partial class LegacyExports
1425
{
26+
// the public methods of this class are used from JavaScript, but the trimmer doesn't know about it.
27+
// They are protected by LegacyExportsTrimmingRoot.PreventTrimming and JSFunctionBinding.BindJSFunction.
28+
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(LegacyExports))]
29+
internal static void PreventTrimming()
30+
{
31+
}
32+
1533
public static void GetCSOwnedObjectByJSHandleRef(nint jsHandle, int shouldAddInflight, out JSObject? result)
1634
{
1735
lock (JSHostImplementation.s_csOwnedObjects)

src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSFunctionBinding.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,8 @@ public static void InvokeJS(JSFunctionBinding signature, Span<JSMarshalerArgumen
142142
/// <exception cref="PlatformNotSupportedException">The method is executed on an architecture other than WebAssembly.</exception>
143143
// JavaScriptExports need to be protected from trimming because they are used from C/JS code which IL linker can't see
144144
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, "System.Runtime.InteropServices.JavaScript.JavaScriptExports", "System.Runtime.InteropServices.JavaScript")]
145-
// TODO make this DynamicDependency conditional again, see https://github.com/dotnet/runtime/pull/82826
146-
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, "System.Runtime.InteropServices.JavaScript.LegacyExports", "System.Runtime.InteropServices.JavaScript")]
145+
// Same for legacy, but the type could be explicitly trimmed by setting WasmEnableLegacyJsInterop=false which would use ILLink.Descriptors.LegacyJsInterop.xml
146+
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, "System.Runtime.InteropServices.JavaScript.LegacyExportsTrimmingRoot", "System.Runtime.InteropServices.JavaScript")]
147147
public static JSFunctionBinding BindJSFunction(string functionName, string moduleName, ReadOnlySpan<JSMarshalerType> signatures)
148148
{
149149
if (RuntimeInformation.OSArchitecture != Architecture.Wasm)

src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSObject.References.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public partial class JSObject
1010
{
1111
internal nint JSHandle;
1212

13-
#if ENABLE_LEGACY_JS_INTEROP
13+
#if !DISABLE_LEGACY_JS_INTEROP
1414
internal GCHandle? InFlight;
1515
internal int InFlightCounter;
1616
#endif
@@ -21,7 +21,7 @@ internal JSObject(IntPtr jsHandle)
2121
JSHandle = jsHandle;
2222
}
2323

24-
#if ENABLE_LEGACY_JS_INTEROP
24+
#if !DISABLE_LEGACY_JS_INTEROP
2525
internal void AddInFlight()
2626
{
2727
ObjectDisposedException.ThrowIf(IsDisposed, this);

src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.Legacy.UnitTests/System.Runtime.InteropServices.JavaScript.Legacy.UnitTests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
<WasmXHarnessArgs>$(WasmXHarnessArgs) --engine-arg=--expose-gc --web-server-use-cop</WasmXHarnessArgs>
77
<NoWarn>0612</NoWarn>
88
<WasmEnableLegacyJsInterop Condition="'$(WasmEnableLegacyJsInterop)' == ''">true</WasmEnableLegacyJsInterop>
9-
<DefineConstants Condition="'$(WasmEnableLegacyJsInterop)' == 'true'">$(DefineConstants);ENABLE_LEGACY_JS_INTEROP</DefineConstants>
9+
<DefineConstants Condition="'$(WasmEnableLegacyJsInterop)' == 'false'">$(DefineConstants);DISABLE_LEGACY_JS_INTEROP</DefineConstants>
1010
</PropertyGroup>
1111

12-
<ItemGroup Condition="'$(WasmEnableLegacyJsInterop)' == 'true'">
12+
<ItemGroup Condition="'$(WasmEnableLegacyJsInterop)' != 'false'">
1313
<Compile Include="System\Runtime\InteropServices\JavaScript\JavaScriptTests.cs" />
1414
<Compile Include="System\Runtime\InteropServices\JavaScript\DataViewTests.cs" />
1515
<Compile Include="System\Runtime\InteropServices\JavaScript\MemoryTests.cs" />

src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System.Runtime.InteropServices.JavaScript.Tests.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
<TargetFrameworks>$(NetCoreAppCurrent)-browser</TargetFrameworks>
55
<TestRuntime>true</TestRuntime>
66
<WasmXHarnessArgs>$(WasmXHarnessArgs) --engine-arg=--expose-gc --web-server-use-cop</WasmXHarnessArgs>
7-
<WasmEnableLegacyJsInterop Condition="'$(WasmEnableLegacyJsInterop)' == ''">true</WasmEnableLegacyJsInterop>
8-
<DefineConstants Condition="'$(WasmEnableLegacyJsInterop)' == 'true'">$(DefineConstants);ENABLE_LEGACY_JS_INTEROP</DefineConstants>
7+
<EnableAggressiveTrimming>true</EnableAggressiveTrimming>
8+
<PublishTrimmed>true</PublishTrimmed>
9+
<WasmEnableLegacyJsInterop>false</WasmEnableLegacyJsInterop>
10+
<DefineConstants Condition="'$(WasmEnableLegacyJsInterop)' == 'false'">$(DefineConstants);DISABLE_LEGACY_JS_INTEROP</DefineConstants>
911
<!-- Use following lines to write the generated files to disk. -->
1012
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
1113
</PropertyGroup>

src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportExportTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public unsafe void GlobalThis()
5858
[Fact]
5959
public unsafe void DotnetInstance()
6060
{
61-
#if ENABLE_LEGACY_JS_INTEROP
61+
#if !DISABLE_LEGACY_JS_INTEROP
6262
Assert.True(JSHost.DotnetInstance.HasProperty("MONO"));
6363
Assert.Equal("object", JSHost.DotnetInstance.GetTypeOfProperty("MONO"));
6464
#endif

0 commit comments

Comments
 (0)