fix: handle wbg_cast functions by rewriting patch module #5026
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.
Fixes #5016
Rewrite the wbg_cast functions to call the indirect functions from the original module.
This is necessary because wasm-bindgen uses these calls to perform dynamic type casting through
the JS layer. If we don't rewrite these, they end up as calls to
breaks_if_inlinedfunctionswhich are no-ops and get rewritten by the wbindgen post-processing step.
Here, we find the corresponding wbg_cast function in the old module by name and then rewrite
the patch module's cast function to call the indirect function from the original module.
See the wbg_cast implementation in wasm-bindgen for more details:
https://github.com/wasm-bindgen/wasm-bindgen/blob/f61a588f674304964a2062b2307edb304aed4d16/src/rt/mod.rs#L30
wbg changed their handling of wbg_cast functions to include more type information in the JS <-> Rust shims. This now leads to more functions in the callstack of the descriptor functions, which we weren't handling properly.
Instead of relying on the import map for all information here, we instead look for the wbg_cast functions explicitly and then rebind them to their original calls in the main module. This prevents the
breaks_if_inlinedfunction from being retained in the patch module, and thus fixes hot-patching on the newer wasm-bindgen versions.