refactor: Remove inline wasm ABI pointer shifts#1616
Merged
alexcrichton merged 2 commits intoMay 22, 2026
Merged
Conversation
alexcrichton
approved these changes
May 22, 2026
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.
Background
MoonBit initially had a very naive implementation, i.e. the object pointer pointed to the first byte of the object. It may makes sense back then, but it was problematic as people constantly asking, how do I get the data of the
BytesorStringbased on the pointer passed through the FFI. The answer was to add 8 bytes, which is a mysterious number. This is painful, and the other problem is that it doesn't allow us to change the header freely.The new layout has been long tested on the native backend, where the user visible pointers all point to the payload, and the runtime APIs compute the offset to get the actual header. This has been well.
So in the coming release of MoonBit (0.9.3), before we reach 1.0, we are changing the layout for the Wasm backend as well. Now the pointer (
i32passed through the FFI boundary) will point directly at the payload. Only internal in thedecreforincrefwill we compute the offset, which should be invisible to the users.Summary
i32.const 8add/sub pointer adjustments from the MoonBit FFI template.ptr2*helpers now that they no longer need a shifted base-pointer temporary.Validation
cargo fmt --checkcargo test -p wit-bindgen-moonbitcargo run test --languages rust,moonbit --artifacts target/artifacts --rust-wit-bindgen-path ./crates/guest-rust tests/runtimewith an ABI-updated MoonBit toolchain onPATH: component compilation passes 133/133; runtime passes 74/76. The only failures are the twomapruntime cases becausewasmtime 43.0.1rejects-Wcomponent-model-map.git diff --checkrg -n "local\\.tee 2|i32\\.const 8 i32\\.(add|sub)" crates/moonbit/src/ffi.rs crates/moonbit/src/ffi/ffi.mbtreports no matches.This branch is based on
bytecodealliance/wit-bindgenmainand pushed topeter-jerry-ye/wit-bindgenbecause we do not have write access to upstream.