diff --git a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs index 510b4b8e32180..210e8b6fc99f7 100644 --- a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs +++ b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs @@ -136,6 +136,7 @@ public static int SlowRuntimeTimeoutModifier public static bool IsWasmThreadingSupported => IsBrowser && IsEnvironmentVariableTrue("IsBrowserThreadingSupported"); public static bool IsNotWasmThreadingSupported => !IsWasmThreadingSupported; public static bool IsWasmBackgroundExec => IsBrowser && IsEnvironmentVariableTrue("IsWasmBackgroundExec"); + public static bool IsThreadingSupportedNotBrowserBackgroundExec => IsWasmThreadingSupported && !IsWasmBackgroundExec; public static bool IsWasmBackgroundExecOrSingleThread => IsWasmBackgroundExec || IsNotWasmThreadingSupported; public static bool IsThreadingSupportedOrBrowserBackgroundExec => IsWasmBackgroundExec || !IsBrowser; public static bool IsBinaryFormatterSupported => IsNotMobile && !IsNativeAot; diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSFunctionBinding.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSFunctionBinding.cs index 618c5e02e0d05..550956cce3309 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSFunctionBinding.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSFunctionBinding.cs @@ -207,7 +207,7 @@ internal static unsafe void InvokeJSFunction(JSObject jsFunction, Span(()=>JavaScriptTestHelper.invoke1_Boolean(true, nameof(JavaScriptTestHelper.EchoBoolean))); + Assert.Contains("Cannot call synchronous C# method", ex.Message); + } + [Theory] [MemberData(nameof(MarshalBooleanCases))] public async Task JsExportBooleanAsync(bool value) @@ -150,6 +157,7 @@ public void JsExportIntPtr(IntPtr value) [MemberData(nameof(MarshalIntPtrCases))] public unsafe void JsExportVoidPtr(IntPtr xvalue) { + JavaScriptTestHelper.AssertWasmBackgroundExec(); void* value = (void*)xvalue; void* res = JavaScriptTestHelper.invoke1_VoidPtr(value, nameof(JavaScriptTestHelper.EchoVoidPtr)); Assert.True(value == res); @@ -180,6 +188,7 @@ public void JsExportDateTimeOffset(DateTimeOffset value) [MemberData(nameof(MarshalNullableBooleanCases))] public void JsExportNullableBoolean(bool? value) { + JavaScriptTestHelper.AssertWasmBackgroundExec(); JsExportTest(value, JavaScriptTestHelper.invoke1_NullableBoolean, nameof(JavaScriptTestHelper.EchoNullableBoolean), diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportTest.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportTest.cs index 89ec5cbb08978..f3ce777e82456 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportTest.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportTest.cs @@ -116,6 +116,7 @@ public unsafe void OutOfRange() [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsWasmBackgroundExecOrSingleThread))] public unsafe void OptimizedPaths() { + JavaScriptTestHelper.AssertWasmBackgroundExec(); JavaScriptTestHelper.optimizedReached = 0; JavaScriptTestHelper.invoke0V(); Assert.Equal(1, JavaScriptTestHelper.optimizedReached); 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 e5ea3a887e24b..232397defef1d 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 @@ -1020,14 +1020,24 @@ public static JSObject EchoIJSObject([JSMarshalAs] JSObject arg1) [JSImport("INTERNAL.forceDisposeProxies")] internal static partial void ForceDisposeProxies(bool disposeMethods, bool verbose); + public static void AssertWasmBackgroundExec() + { + if (PlatformDetection.IsWasmBackgroundExec && Environment.CurrentManagedThreadId == 1) + { + throw new Exception("With WasmBackgroundExec we are expecting to run tests on the thread pool"); + } + } + static JSObject _module; public static async Task InitializeAsync() { + AssertWasmBackgroundExec(); if (_module == null) { _module = await JSHost.ImportAsync("JavaScriptTestHelper", "../JavaScriptTestHelper.mjs"); ; await Setup(); } + AssertWasmBackgroundExec(); #if FEATURE_WASM_MANAGED_THREADS // are we in the UI thread ? @@ -1037,6 +1047,7 @@ public static async Task InitializeAsync() // this gives browser chance to serve UI thread event loop before every test await Task.Yield(); } + AssertWasmBackgroundExec(); } public static Task DisposeAsync() diff --git a/src/mono/browser/runtime/invoke-js.ts b/src/mono/browser/runtime/invoke-js.ts index b2a352cf5e94e..c13250fbe0ba4 100644 --- a/src/mono/browser/runtime/invoke-js.ts +++ b/src/mono/browser/runtime/invoke-js.ts @@ -142,7 +142,7 @@ function bind_js_import(signature: JSFunctionSignature): Function { try { forceThreadMemoryViewRefresh(); const caller_tid = get_caller_native_tid(args); - runtimeHelpers.isPendingSynchronousCall = runtimeHelpers.currentThreadTID === caller_tid; + runtimeHelpers.isPendingSynchronousCall = runtimeHelpers.managedThreadTID === caller_tid; bound_fn(args); } finally {