Repository navigation
[WASM] Bind CoreCLR JSExports lazily using assembly type-map providers - #136924
Open
pavelsavara wants to merge 37 commits into
Open
pavelsavara wants to merge 37 commits into
pavelsavara wants to merge 37 commits into
Conversation
Two changes that do not need new public API. CoreCLR already binds exports by storing the generated wrapper in JSProxyContext.JSExportByHandle and letting JavaScript call CallJSExport with that handle. Mono instead looked the wrapper up natively by name and invoked the MonoMethod directly, so the two runtimes disagreed about how a bound export is reached, and a wrapper signature change would not be diagnosed: mono_class_get_method_from_name is passed -1 for the signature. Move BindManagedFunction into a file shared by both flavors, give Mono a CallJSExport and a handle-dispatching invoke in driver.c, and delete the SystemInteropJS_GetAssemblyExport internal call. BindAssemblyExports stays per-flavor: that is assembly bootstrap rather than dispatch, and the two implementations differ in whether an assembly without exports is an error. The generated JSImport stub no longer opens an unsafe context or calls Unsafe.SkipInit, so a project using only [JSImport] no longer needs AllowUnsafeBlocks and SYSLIB1074 is gone. [JSExport] still generates a pointer-based wrapper, so SYSLIB1075 remains until that changes. Also drop Interop.Runtime.BindAssemblyExports and GetAssemblyExport from the CoreCLR declarations. They were already unreachable: GetAssemblyExport has no implementation in the CoreCLR native library, and BindAssemblyExports there is the reverse thunk into managed code, so calling it as a P/Invoke would recurse.
Generated JSExport registration now passes the wrapper as a method group to a new BindManagedFunction overload, so the binding no longer needs reflection, DynamicDependency, or unsafe code in user assemblies. API (proposal pending): - JSExportCallback(Span<JSMarshalerArgument>) delegate - JSFunctionBinding.BindManagedFunction(..., JSExportCallback) - JSFunctionBinding.RegisterAssemblyExports(string, Action) The wrapper now takes Span<JSMarshalerArgument> rather than a pointer, so the runtime constructs the span and generated code stays safe. The argument count travels with the callback via JSProxyContext.JSExportEntry so the runtime can size the span. Assemblies register their export initializer from __TrimmingPreserve_; BindAssemblyExports consults that registry first and falls back to the reflection path for assemblies built with an older generator. Types nested in a type that is not referenceable by a method group (generic or file-local containers) keep the legacy reflection path and report the new SYSLIB1076 warning. SYSLIB1075 (AllowUnsafeBlocks required for [JSExport]) is no longer reported. Shared interop generator gains SignatureBehavior.RefToNativeType and ValueBoundaryBehavior.RefNativeIdentifier so the inner stub can take its argument slots by ref instead of by pointer.
The wasmbrowser template and the two JS interop test libraries enabled unsafe code only because the [JSImport]/[JSExport] generators used to emit it. Neither contains hand-written unsafe code, so the flag can go. Left in place where unsafe code is genuinely used: WasmBasicTestApp/App (fixed block in MemoryTest), the JS interop test project (void* imports and stackalloc), and the PInvoke test assets.
PR #134507 landed on main, so the dispatch-unification work and its review fixes now come from upstream. Conflicts were in the three files both sides touched: - JSProxyContext: keep the JSExportEntry value (callback + frame size) from this branch, inside the private fields and locked accessors that landed on main. AllocJSExportHandle/TryGetJSExport now carry JSExportEntry. - JSHostImplementation.Exports (add/add): keep the assembly-exports registry, the 4-arg BindManagedFunction and BindManagedFunctionCore from this branch, and take main's reflection class-name fix, BindingContextOrMain and locked registration. - JavaScriptExports.Mono: main's locked TryGetJSExport lookup with this branch's Span-based callback invocation. Note main reverted the AllowUnsafeBlocks removal, so SYSLIB1074 and the SkipInit emission are back. This branch still carries its own AllowUnsafeBlocks removal, which is now reconsidered separately. Co-authored-by: Copilot <[email protected]>
Generated JSExport registration now passes each wrapper's function pointer to a new BindManagedFunction overload. The runtime stores that pointer in the per-context handle table and invokes it directly, removing the reflection, object array and Pointer.Box allocations from export dispatch. Legacy wrappers retain their existing ABI and are resolved once through MethodHandle.GetFunctionPointer, so they use the same direct dispatch path. Assemblies self-register their export entry point through the module initializer; BindAssemblyExports runs the module constructor and retains reflection only as a fallback for legacy and non-referenceable wrappers. AllowUnsafeBlocks remains required. The pointer-generation changes and SYSLIB1074/SYSLIB1075 requirements are restored to main's behavior. Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Build a library with the released .NET 10 targeting pack and run its legacy JSExports in a trimmed current-framework browser application. Cover marshaling, nested exports, exceptions, and async returns in the no-workload CI lane. Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Apply EditorBrowsable(Never) and DebuggerNonUserCode to the new JSFunctionBinding binding and assembly registration APIs, including their reference declarations. Co-authored-by: Copilot <[email protected]>
Replace module-initializer callback registration with a generated JSExportProvider and a type-map entry per assembly. Preserve the provider constructor and native binding bootstrap during trimming, and configure the browser type-map entry assembly before managed Main runs. Retain managed function-pointer dispatch and the legacy generated-code fallback. Cover JS-only library discovery in trimmed and untrimmed apps. Co-authored-by: Copilot <[email protected]>
Separate name discovery from export binding using a name-only assembly catalog and one type-map provider per method. Publish stable deferred JavaScript functions and bind through the existing managed delegate bridge only when an export is first invoked. Preserve overload aliases and legacy registration, manage binder lifetime, and test trimming, unused providers, first-call exceptions and disposal. Co-authored-by: Copilot <[email protected]>
Combine export-name discovery and first-call binding in one generated assembly-keyed type-map provider. Dispatch Bind(string methodId) using invariant decimal catalog indexes, retaining ordinary JS objects, stable overload aliases, cached bindings and legacy compatibility. Co-authored-by: Copilot <[email protected]>
|
Azure Pipelines: Successfully started running 4 pipeline(s). 11 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
This was referenced Nov 3, 2026
pavelsavara
temporarily deployed
to
copilot-pat-pool
November 3, 2026 19:15 — with
GitHub Actions
Inactive
pavelsavara
temporarily deployed
to
copilot-pat-pool
November 3, 2026 19:15 — with
GitHub Actions
Inactive
Contributor
|
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara |
This was referenced Nov 3, 2026
Co-authored-by: Copilot <[email protected]>
Configure the ILLink argument at target execution time so WebAssembly SDK imports can provide the root after the linker targets have been evaluated. Add a shared duplicate-safe Browser fallback until the ILLink fix flows through supported SDKs, and include it in both WebAssembly packages. Verify exact-once root forwarding in the existing LibraryMode publish test. Co-authored-by: Copilot <[email protected]>
This was referenced Nov 13, 2026
Member
Author
|
CI issue |
maraf
reviewed
Nov 16, 2026
Keep shared JSExport trimming configuration independent of native relinking and move checkout-specific imports into the in-tree and local-build adapters. Track removal of the SDK-flow forwarding fallback in #137651. Reuse WasmBasicTestApp with opt-in provider and legacy fixtures, preserve single-file JavaScript bundling, and use the stable .NET 11 targeting pack. Verify LibraryMode forwarding through the actual ILLink binlog arguments. Validated on CoreCLR and Mono: provider/legacy 7/7, LibraryMode 4/4, and the existing bundled initializer 1/1 per runtime. Both SDK package builds passed for each runtime. Co-authored-by: Copilot <[email protected]>
This was referenced Nov 16, 2026
Co-authored-by: Copilot <[email protected]>
Remove Mono-specific assembly discovery and JavaScript binding changes. Keep the existing legacy Mono dispatch in shared managed sources, and scope provider configuration and new regression tests to CoreCLR. Move the export-name parser into CoreCLR interop and remove the Mono-only provider accommodations, resources, and package dependency. Validated after merging fresh main: full CoreCLR Browser Debug build, 46 generator tests, 372 ILLink task tests, 522 interop tests passed with 2 existing skips, and 12 focused WBT cases passed. Co-authored-by: Copilot <[email protected]>
lewing
reviewed
Nov 20, 2026
Use the existing reflection fallback for inaccessible struct exports instead of emitting an unsupported UnsafeAccessorType target. Keep the configured type-map entry assembly authoritative when raw linker arguments specify a different root. Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Discover JSExport maps when ILLink marks their assemblies instead of scanning references in the generator. Libraries reached only from JavaScript still need an explicit publish root. Co-authored-by: Copilot <[email protected]>
Keep explicit runtime type-map roots unless TypeMapEntryAssembly overrides them. Drain unused Promise arguments on binding failure, and avoid direct wrapper references through error-obsolete types. Run the secondary runtime disposal test only in browsers and cover root precedence and the binding regressions. Co-authored-by: Copilot <[email protected]>
Read the runtimeconfig template before defaulting to the app assembly. Track the effective root so changing or clearing it reruns ILLink without invalidating unchanged publishes, including with older SDK linker targets. Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Use the shared no-workload Wasm build test targets to select the current reference pack and generator for both child projects. Co-authored-by: Copilot <[email protected]>
This branch was previously deployed
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.
Summary
The CoreCLR Browser/Wasm JSExport generator emits one approved
JSExportProviderand one local type-map entry per exporting assembly. It does not scan referenced assemblies or generate links to their export maps.getAssemblyExports()reads only the requested assembly's metadata or ReadyToRun map, without loading unrelated lazy assemblies. Public application-wide type-map queries still follow explicit assembly-target edges.Trimming and compatibility
TrimmerRootDescriptor. Embedding a descriptor in an otherwise-unrooted library does not keep that library.TypeMapEntryAssemblyif set, then the explicitSystem.Runtime.InteropServices.TypeMappingEntryAssemblyruntime host option, then the app assembly. The selected root takes precedence over a raw linker argument and works for library-mode apps that have no managed entry point.Discovery and invocation
Activator.CreateInstancewith generated constructor dependencies.Validation
The trimmed Chrome suite exercises a managed-referenced library's JSExport with in-tree ILLink. The LibraryMode cases verify explicit runtime root preservation and linker argument precedence. These runs do not establish that an ordinary SDK publish selects a linker with local JSExport discovery.
Resolves #136464
Resolves #120215
Note
This pull request description was generated with GitHub Copilot.