Skip to content

Commit 6c836e8

Browse files
authored
[browser] legacy JS interop optional via WasmEnableLegacyJsInterop - managed (#82826)
* WasmEnableLegacyJsInterop and ILLink.Descriptors.LegacyJsInterop.xml * revert back to USE_PTHREADS symbol * feedback
1 parent ceb2344 commit 6c836e8

File tree

21 files changed

+99
-37
lines changed

21 files changed

+99
-37
lines changed

eng/testing/tests.browser.targets

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
<GetNuGetsToBuildForWorkloadTestingDependsOn>_GetRuntimePackNuGetsToBuild;_GetNugetsForAOT;$(GetNuGetsToBuildForWorkloadTestingDependsOn)</GetNuGetsToBuildForWorkloadTestingDependsOn>
3232
<_BundleAOTTestWasmAppForHelixDependsOn>$(_BundleAOTTestWasmAppForHelixDependsOn);PrepareForWasmBuildApp;_PrepareForAOTOnHelix</_BundleAOTTestWasmAppForHelixDependsOn>
33+
<WasmEnableLegacyJsInterop Condition="'$(WasmEnableLegacyJsInterop)' == ''">true</WasmEnableLegacyJsInterop>
3334
</PropertyGroup>
3435

3536
<!-- We expect WASM users to indicate they would like to have bigger download size by adding WasmIncludeFullIcuData, -->
@@ -126,6 +127,11 @@
126127
<PropertyGroup Condition="'$(BuildAOTTestsOnHelix)' == 'true'">
127128
<!-- wasm targets are not imported at all, in this case, because we run the wasm build on helix -->
128129
</PropertyGroup>
130+
<ItemGroup Condition="'$(BuildAOTTestsOn)' == 'helix'">
131+
<TrimmerRootDescriptor
132+
Condition="'$(WasmEnableLegacyJsInterop)' == 'true'"
133+
Include="$(MonoProjectRoot)\wasm\build\ILLink.Descriptors.LegacyJsInterop.xml" />
134+
</ItemGroup>
129135

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
<PlatformManifestFileEntry Include="emcc-props.json" IsNative="true" />
253253
<PlatformManifestFileEntry Include="ILLink.Substitutions.WasmIntrinsics.xml" IsNative="true" />
254254
<PlatformManifestFileEntry Include="ILLink.Substitutions.NoWasmIntrinsics.xml" IsNative="true" />
255+
<PlatformManifestFileEntry Include="ILLink.Descriptors.LegacyJsInterop.xml" IsNative="true" />
255256
<!-- wasi specific -->
256257
<PlatformManifestFileEntry Include="main.c" IsNative="true" />
257258
<PlatformManifestFileEntry Include="driver.h" IsNative="true" />

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,20 @@
22
<PropertyGroup>
33
<TargetFrameworks>$(NetCoreAppCurrent)-browser;$(NetCoreAppCurrent)</TargetFrameworks>
44
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
5-
<FeatureWasmThreads Condition="'$(TargetOS)' == 'browser' and '$(MonoWasmBuildVariant)' == 'multithread'">true</FeatureWasmThreads>
6-
<DefineConstants Condition="'$(FeatureWasmThreads)' == 'true'" >$(DefineConstants);FEATURE_WASM_THREADS</DefineConstants>
75
<EnableAOTAnalyzer>false</EnableAOTAnalyzer>
86
</PropertyGroup>
97

108
<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
119
<PropertyGroup>
1210
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
1311
<GeneratePlatformNotSupportedAssemblyMessage Condition="'$(TargetPlatformIdentifier)' != 'browser'">SR.SystemRuntimeInteropServicesJavaScript_PlatformNotSupported</GeneratePlatformNotSupportedAssemblyMessage>
12+
<FeatureWasmThreads Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(MonoWasmBuildVariant)' == 'multithread'">true</FeatureWasmThreads>
13+
<WasmEnableLegacyJsInterop Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(WasmEnableLegacyJsInterop)' == ''">true</WasmEnableLegacyJsInterop>
14+
<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>
1419
</PropertyGroup>
1520

1621
<ItemGroup>
@@ -22,15 +27,6 @@
2227
<Compile Include="System\Runtime\InteropServices\JavaScript\Interop\JavaScriptImports.Generated.cs" />
2328
<Compile Include="System\Runtime\InteropServices\JavaScript\Interop\JavaScriptExports.cs" />
2429
<Compile Include="System\Runtime\InteropServices\JavaScript\Interop\JavaScriptImports.cs" />
25-
<Compile Include="System\Runtime\InteropServices\JavaScript\Interop\LegacyExports.cs" />
26-
27-
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\Runtime.cs" />
28-
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\Array.cs" />
29-
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\ArrayBuffer.cs" />
30-
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\DataView.cs" />
31-
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\Function.cs" />
32-
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\Uint8Array.cs" />
33-
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\LegacyHostImplementation.cs" />
3430

3531
<Compile Include="System\Runtime\InteropServices\JavaScript\JSHost.cs" />
3632
<Compile Include="System\Runtime\InteropServices\JavaScript\JSMarshalerType.cs" />
@@ -69,6 +65,18 @@
6965
<Compile Include="System\Runtime\InteropServices\JavaScript\JSSynchronizationContext.cs" />
7066
</ItemGroup>
7167

68+
<!-- only include legacy interop when WasmEnableLegacyJsInterop is enabled -->
69+
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'browser' and '$(WasmEnableLegacyJsInterop)' == 'true'">
70+
<Compile Include="System\Runtime\InteropServices\JavaScript\Interop\LegacyExports.cs" />
71+
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\Runtime.cs" />
72+
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\Array.cs" />
73+
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\ArrayBuffer.cs" />
74+
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\DataView.cs" />
75+
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\Function.cs" />
76+
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\Uint8Array.cs" />
77+
<Compile Include="System\Runtime\InteropServices\JavaScript\Legacy\LegacyHostImplementation.cs" />
78+
</ItemGroup>
79+
7280
<ItemGroup>
7381
<Reference Include="System.Collections" />
7482
<Reference Include="System.Memory" />

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

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

23+
#if ENABLE_LEGACY_JS_INTEROP
2324
#region legacy
2425

2526
public static object GetGlobalObject(string? str = null)
@@ -30,7 +31,7 @@ public static object GetGlobalObject(string? str = null)
3031
if (exception != 0)
3132
throw new JSException(SR.Format(SR.ErrorResolvingFromGlobalThis, str));
3233

33-
JSHostImplementation.ReleaseInFlight(jsObj);
34+
LegacyHostImplementation.ReleaseInFlight(jsObj);
3435
return jsObj;
3536
}
3637

@@ -44,5 +45,6 @@ public static IntPtr CreateCSOwnedObject(string typeName, object[] parms)
4445
}
4546

4647
#endregion
48+
#endif
4749
}
4850
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
namespace System.Runtime.InteropServices.JavaScript
1111
{
12-
// this maps to src\mono\wasm\runtime\legacy\corebindings.ts
13-
// the public methods are protected from trimming by DynamicDependency on JSFunctionBinding.BindJSFunction
12+
// the public methods are protected from trimming by ILLink.Descriptors.LegacyJsInterop.xml
1413
internal static unsafe partial class LegacyExports
1514
{
1615
public static void GetCSOwnedObjectByJSHandleRef(nint jsHandle, int shouldAddInflight, out JSObject? result)

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,6 @@ 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
146-
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, "System.Runtime.InteropServices.JavaScript.LegacyExports", "System.Runtime.InteropServices.JavaScript")]
147145
public static JSFunctionBinding BindJSFunction(string functionName, string moduleName, ReadOnlySpan<JSMarshalerType> signatures)
148146
{
149147
if (RuntimeInformation.OSArchitecture != Architecture.Wasm)

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ public static void ReleaseCSOwnedObject(nint jsHandle)
4242
throw new InvalidOperationException();
4343
}
4444

45-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
46-
public static void ReleaseInFlight(object obj)
47-
{
48-
JSObject? jsObj = obj as JSObject;
49-
jsObj?.ReleaseInFlight();
50-
}
51-
5245
// A JSOwnedObject is a managed object with its lifetime controlled by javascript.
5346
// The managed side maintains a strong reference to the object, while the JS side
5447
// maintains a weak reference and notifies the managed side if the JS wrapper object

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

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

13+
#if ENABLE_LEGACY_JS_INTEROP
1314
internal GCHandle? InFlight;
1415
internal int InFlightCounter;
16+
#endif
1517
private bool _isDisposed;
1618

1719
internal JSObject(IntPtr jsHandle)
1820
{
1921
JSHandle = jsHandle;
20-
InFlight = null;
21-
InFlightCounter = 0;
2222
}
2323

24+
#if ENABLE_LEGACY_JS_INTEROP
2425
internal void AddInFlight()
2526
{
2627
ObjectDisposedException.ThrowIf(IsDisposed, this);
@@ -53,6 +54,7 @@ internal void ReleaseInFlight()
5354
}
5455
}
5556
}
57+
#endif
5658

5759
/// <inheritdoc />
5860
public override bool Equals([NotNullWhen(true)] object? obj) => obj is JSObject other && JSHandle == other.JSHandle;

src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/Array.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public object this[int i]
9191

9292
if (exception != 0)
9393
throw new JSException((string)indexValue);
94-
JSHostImplementation.ReleaseInFlight(indexValue);
94+
LegacyHostImplementation.ReleaseInFlight(indexValue);
9595
return indexValue;
9696
}
9797
set

src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Legacy/LegacyHostImplementation.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ namespace System.Runtime.InteropServices.JavaScript
1010
[SupportedOSPlatform("browser")]
1111
internal static class LegacyHostImplementation
1212
{
13+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
14+
public static void ReleaseInFlight(object obj)
15+
{
16+
JSObject? jsObj = obj as JSObject;
17+
jsObj?.ReleaseInFlight();
18+
}
19+
1320
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1421
public static void RegisterCSOwnedObject(JSObject proxy)
1522
{

0 commit comments

Comments
 (0)