From 250c4ed7363dd596c274beb71800f528bb31d229 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Tue, 5 Mar 2024 15:42:41 +0100 Subject: [PATCH 1/3] use current pthread ptr instead of targetContext thread ptr --- .../InteropServices/JavaScript/JSFunctionBinding.cs | 6 +++--- .../InteropServices/JavaScript/JSExportTest.cs | 2 ++ .../InteropServices/JavaScript/JSImportTest.cs | 1 + .../JavaScript/JavaScriptTestHelper.cs | 11 +++++++++++ src/mono/browser/runtime/invoke-js.ts | 2 +- 5 files changed, 18 insertions(+), 4 deletions(-) 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 4f14a575fb1c0d..797ed2c8c44151 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] 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 b2a352cf5e94e5..c13250fbe0ba4a 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 { From b203baf12318dd097712b15ac73a3a1c8978b807 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Tue, 5 Mar 2024 16:12:29 +0100 Subject: [PATCH 2/3] test --- .../Common/tests/TestUtilities/System/PlatformDetection.cs | 1 + .../Runtime/InteropServices/JavaScript/JSExportTest.cs | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs index 510b4b8e321805..f55730baf8824a 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 IsNotWasmBackgroundExec => IsBrowser && !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/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 22a36496989f32..dd9af88ef3586f 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 @@ -14,6 +14,13 @@ namespace System.Runtime.InteropServices.JavaScript.Tests { public class JSExportAsyncTest : JSInteropTestBase, IAsyncLifetime { + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmBackgroundExec))] + public void SyncJsImportJsExportThrows() + { + var ex = Assert.Throws(()=>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) From b0774064addef3221c2db3739a12638231c6c902 Mon Sep 17 00:00:00 2001 From: pavelsavara Date: Tue, 5 Mar 2024 18:50:34 +0100 Subject: [PATCH 3/3] more --- .../Common/tests/TestUtilities/System/PlatformDetection.cs | 2 +- .../System/Runtime/InteropServices/JavaScript/JSExportTest.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs index f55730baf8824a..210e8b6fc99f7c 100644 --- a/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs +++ b/src/libraries/Common/tests/TestUtilities/System/PlatformDetection.cs @@ -136,7 +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 IsNotWasmBackgroundExec => IsBrowser && !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/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 dd9af88ef3586f..2d7b7cbf6429cd 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 @@ -14,7 +14,7 @@ namespace System.Runtime.InteropServices.JavaScript.Tests { public class JSExportAsyncTest : JSInteropTestBase, IAsyncLifetime { - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmBackgroundExec))] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupportedNotBrowserBackgroundExec))] public void SyncJsImportJsExportThrows() { var ex = Assert.Throws(()=>JavaScriptTestHelper.invoke1_Boolean(true, nameof(JavaScriptTestHelper.EchoBoolean)));