Skip to content

refactor: Remove inline wasm ABI pointer shifts#1616

Merged
alexcrichton merged 2 commits into
bytecodealliance:mainfrom
peter-jerry-ye:codex-remove-abi-pointer-shifts
May 22, 2026
Merged

refactor: Remove inline wasm ABI pointer shifts#1616
alexcrichton merged 2 commits into
bytecodealliance:mainfrom
peter-jerry-ye:codex-remove-abi-pointer-shifts

Conversation

@peter-jerry-ye
Copy link
Copy Markdown
Contributor

@peter-jerry-ye peter-jerry-ye commented May 22, 2026

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 Bytes or String based 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 (i32 passed through the FFI boundary) will point directly at the payload. Only internal in the decref or incref will we compute the offset, which should be invisible to the users.

Summary

  • Remove inline wasm i32.const 8 add/sub pointer adjustments from the MoonBit FFI template.
  • Simplify ptr2* helpers now that they no longer need a shifted base-pointer temporary.
  • Update the Rust generator string constants so newly generated MoonBit bindings no longer encode the old 8-byte ABI shift.
  • Document the silent MoonBit ABI compatibility risk in the README so users know to regenerate bindings with a matching toolchain/generator pair.

Validation

  • cargo fmt --check
  • cargo test -p wit-bindgen-moonbit
  • cargo run test --languages rust,moonbit --artifacts target/artifacts --rust-wit-bindgen-path ./crates/guest-rust tests/runtime with an ABI-updated MoonBit toolchain on PATH: component compilation passes 133/133; runtime passes 74/76. The only failures are the two map runtime cases because wasmtime 43.0.1 rejects -Wcomponent-model-map.
  • git diff --check
  • rg -n "local\\.tee 2|i32\\.const 8 i32\\.(add|sub)" crates/moonbit/src/ffi.rs crates/moonbit/src/ffi/ffi.mbt reports no matches.

This branch is based on bytecodealliance/wit-bindgen main and pushed to peter-jerry-ye/wit-bindgen because we do not have write access to upstream.

@peter-jerry-ye peter-jerry-ye changed the title [codex] Remove inline wasm ABI pointer shifts refactor: Remove inline wasm ABI pointer shifts May 22, 2026
@peter-jerry-ye peter-jerry-ye marked this pull request as ready for review May 22, 2026 06:09
@alexcrichton alexcrichton added this pull request to the merge queue May 22, 2026
Merged via the queue into bytecodealliance:main with commit 1c9bfc6 May 22, 2026
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants