Draft
Conversation
Contributor
|
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors Browser/WASM scheduling and JS interop entrypoints, moving away from legacy mono internal-call/reflection-based paths toward LibraryImport/UnmanagedCallersOnly-style exports, and introduces new scheduling hooks for finalization and diagnostic server work.
Changes:
- Update Browser/WASM scheduling to use idempotent scheduling (
SystemJS_Schedule*) and a unifiedrunBackgroundTimers()runner. - Restructure JS interop exports/binding to use handle-based JSExport invocation (
SystemInteropJS_CallJSExport) and add new native entrypoints (e.g.,BrowserHost_ExecuteAssembly). - Adjust build/pinvoke module lists and remove older mono-only glue code and declarations.
Reviewed changes
Copilot reviewed 38 out of 38 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/native/libs/System.Native.Browser/native/scheduling.ts | Makes background/finalization scheduling idempotent (no reschedule/cancel). |
| src/mono/wasm/build/WasmApp.Common.targets | Adds browser-only native modules to the pinvoke module list. |
| src/mono/mono/utils/mono-threads.h | Removes DISABLE_THREADS declarations for background/ds job scheduling. |
| src/mono/mono/utils/mono-threads-wasm.h | Removes DISABLE_THREADS declarations for background/ds exec entrypoints. |
| src/mono/mono/utils/mono-threads-wasm.c | Removes legacy DISABLE_THREADS background/ds job queue/executors. |
| src/mono/mono/sgen/sgen-client.h | Removes sgen_client_schedule_background_job API declaration. |
| src/mono/mono/mini/mini-wasm.h | Removes DISABLE_THREADS JS-import declarations for timers/background jobs. |
| src/mono/mono/mini/mini-wasm.c | Removes legacy DISABLE_THREADS timer/background job wiring and internal calls. |
| src/mono/mono/metadata/sgen-mono.c | Removes HOST_WASM/DISABLE_THREADS background job scheduling shim. |
| src/mono/mono/metadata/gc.c | Switches DISABLE_THREADS wasm finalization notification to SystemJS_ScheduleFinalization. |
| src/mono/mono/eventpipe/ep-rt-mono.h | Adds browser-only ds job scheduling/execute callback declarations for single-threaded queueing. |
| src/mono/mono/eventpipe/ep-rt-mono.c | Implements browser-only ds job scheduling queue and execute callback. |
| src/mono/browser/runtime/types/internal.ts | Adds JSExportHandle and replaces background/ds helpers with runBackgroundTimers. |
| src/mono/browser/runtime/scheduling.ts | Reworks scheduling: unified runBackgroundTimers + new SystemJS_Schedule* APIs. |
| src/mono/browser/runtime/runtime.h | Removes mono_wasm_assembly_find_method export declaration. |
| src/mono/browser/runtime/runtime.c | Removes mono_wasm_assembly_find_method; hardens token-based lookup with null check. |
| src/mono/browser/runtime/run.ts | Changes main execution to call native BrowserHost_ExecuteAssembly and resolve/reject a JS promise via new callbacks. |
| src/mono/browser/runtime/marshal-to-js.ts | Updates comments to remove call_entry_point reference. |
| src/mono/browser/runtime/managed-exports.ts | Removes legacy managed-exports method lookup/async invoke paths; introduces UCO/handle invoke helpers. |
| src/mono/browser/runtime/loader/exit.ts | Formatting + avoids attempting node stream flush outside Node. |
| src/mono/browser/runtime/invoke-cs.ts | Switches bindings to invoke JSExports via JSExportHandle + handle-based call helper. |
| src/mono/browser/runtime/exports.ts | Exposes runBackgroundTimers instead of legacy background/ds exec helpers. |
| src/mono/browser/runtime/exports-binding.ts | Updates the wasm import list to new scheduling + main-promise resolution hooks. |
| src/mono/browser/runtime/driver.c | Removes legacy invoke_jsexport plumbing; adds BrowserHost_ExecuteAssembly. |
| src/mono/browser/runtime/diagnostics/common.ts | Uses runBackgroundTimers during diagnostic server event loop. |
| src/mono/browser/runtime/cwraps.ts | Updates cwrap signatures to new entrypoints and removes old ones. |
| src/mono/browser/runtime/corebindings.c | Removes legacy internal-call registration and export lookup/bind helpers. |
| src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.cs | Removes legacy managed entrypoint invocation (CallEntrypoint). |
| src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.Mono.cs | Deletes mono-specific BindManagedFunction/BindAssemblyExports implementation. |
| src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/JSHostImplementation.CoreCLR.cs | Improves exception typing + unwraps TargetInvocationException preserving stack. |
| src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/JavaScriptExports.cs | Adds/extends UnmanagedCallersOnly exports and managed-threads handling in exports. |
| src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/JavaScriptExports.Mono.cs | Adds EntryPoint names for select exports; trims legacy entrypoint/bind exports code. |
| src/libraries/System.Runtime.InteropServices.JavaScript/src/System.Runtime.InteropServices.JavaScript.csproj | Switches to unified Interop.Runtime + adjusts included interop/export files. |
| src/libraries/System.Private.CoreLib/src/System/Threading/TimerQueue.Browser.cs | Switches to LibraryImport-based SystemJS_ScheduleTimer for MONO/browser. |
| src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPool.Browser.cs | Switches to LibraryImport-based SystemJS_ScheduleBackgroundJob for MONO/browser. |
| src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncHelpers.Browser.cs | Adds MONO/browser LibraryImport for main-promise resolve/reject hooks. |
| src/libraries/Common/src/Interop/Browser/Interop.Runtime.cs | Removes unused browser interop entrypoint APIs (BindAssemblyExports/GetAssemblyExport/AssemblyGetEntryPoint). |
| src/libraries/Common/src/Interop/Browser/Interop.Runtime.Mono.cs | Deletes mono-specific internal-call declarations for runtime interop. |
Comments suppressed due to low confidence (2)
src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/JavaScriptExports.cs:55
- In the FEATURE_WASM_MANAGED_THREADS branch, this uses
arg_excbut the variable declared in this method isargException. As written this won’t compile; update the reference to use the correct variable when callingAssertCurrentThreadContext().
src/libraries/System.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Interop/JavaScriptExports.cs:139 ctx.ReleasePromiseHolder(arg_1.slot.GCHandle)referencesarg_1, but this method definesarg1(and notarg_1). This is a compile-time error; use the correct argument variable when releasing the holder.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This was referenced Feb 25, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Unify Mono and CoreCLR JS interop code paths for single-threaded Browser WASM
This PR eliminates the separate Mono-specific code paths in the Browser WASM JS interop layer, aligning Mono with the pattern already used by CoreCLR. This is a step toward running CoreCLR on WASM with a shared managed/JS codebase.
Key changes
Replace Mono internal calls with
[LibraryImport]P/InvokesTimerQueue.Browser.cs) and thread pool (ThreadPool.Browser.cs) now use[LibraryImport]for both Mono and CoreCLR instead of Mono-only[MethodImpl(MethodImplOptions.InternalCall)].AsyncHelpers.Browser.csconditionally targetslibSystem.Native.Browser(Mono) or QCall (CoreCLR).Replace
mono_wasm_invoke_jsexportwith[UnmanagedCallersOnly]entry pointsCallDelegate,CompleteTask,ReleaseJSOwnedObjectByGCHandle,GetManagedStackTrace,LoadLazyAssembly,LoadSatelliteAssembly,InstallMainSynchronizationContext,BindAssemblyExports) are now called directly as C-exported functions via[UnmanagedCallersOnly(EntryPoint = "...")]rather than looked up by name and dispatched throughmono_runtime_invoke.cwrapsinstead of via theManagedExportsobject +mono_wasm_invoke_jsexport.JavaScriptExports.CoreCLR.csis renamed toJavaScriptExports.cs(shared) and gains the thread-blocking-mode logic previously only inJavaScriptExports.Mono.cs.New
BrowserHost_ExecuteAssemblyfor entry point executionCallEntrypointmanaged export →AssemblyGetEntryPointicall) with a direct C functionBrowserHost_ExecuteAssemblythat callsmono_runtime_run_main.SystemJS_ResolveMainPromise/SystemJS_RejectMainPromiseexported from managed code.Separate scheduling concerns
SystemJS_ScheduleBackgroundJob(cb)/mono_background_execwhich multiplexed timer, thread pool, finalization, and diagnostics callbacks is split into four dedicated pairs:SystemJS_ScheduleTimer/SystemJS_ExecuteTimerCallbackSystemJS_ScheduleBackgroundJob/SystemJS_ExecuteBackgroundJobCallbackSystemJS_ScheduleFinalization/SystemJS_ExecuteFinalizationCallbackSystemJS_ScheduleDiagnosticServerJob/SystemJS_ExecuteDiagnosticServerCallbackP/Invoke module registration for Mono
WasmApp.Common.targetsaddslibSystem.Native.BrowserandlibSystem.Runtime.InteropServices.JavaScript.Nativeto_WasmPInvokeModulesfor browser projects.Deleted code
Interop.Runtime.Mono.cs— Mono-specific internal call declarationsJSHostImplementation.Mono.cs— Mono-specificBindAssemblyExports/BindManagedFunctionvia icallscorebindings.c—bindings_initialize_internals,SystemInteropJS_AssemblyGetEntryPoint,SystemInteropJS_BindAssemblyExports,SystemInteropJS_GetAssemblyExportdriver.c—mono_wasm_invoke_jsexport, async/sync JSExport dispatchmono-threads-wasm.c—SystemJS_ScheduleBackgroundJob(cb),mono_background_exec,mono_wasm_ds_execmini-wasm.c—SystemJS_ScheduleTimer,mono_wasm_execute_timer, internal call registrationssgen-mono.c—sgen_client_schedule_background_jobOther improvements
mono_wasm_get_method_matchingnow null-checks the result ofmono_get_methodbefore dereferencing.JSHostImplementation.CoreCLR.csunwrapsTargetInvocationExceptionusingExceptionDispatchInfoto preserve original stack traces.ArithmeticExceptiontoArgumentExceptionfor assembly binding errors.Scope
Single-threaded browser only (
DISABLE_THREADS). Multi-threaded paths haveTODO-WASM: unificationmarkers.