Skip to content

moonbit: async runtime redesign and coverage expansion#1556

Draft
peter-jerry-ye wants to merge 61 commits intobytecodealliance:mainfrom
peter-jerry-ye:moonbit-async-redesign
Draft

moonbit: async runtime redesign and coverage expansion#1556
peter-jerry-ye wants to merge 61 commits intobytecodealliance:mainfrom
peter-jerry-ye:moonbit-async-redesign

Conversation

@peter-jerry-ye
Copy link
Contributor

Summary

This PR redesigns MoonBit async support in wit-bindgen to align generated bindings/runtime behavior with component-model async semantics, and adds broad runtime coverage for MoonBit async paths.

What Changed

1. MoonBit async runtime foundation

2. Generator changes for async imports/exports

  • Updated MoonBit generator internals in:
  • Key behaviors:
    • Async export wrappers run inside with_waitableset and with_task_group.
    • Async export stubs/signatures include a task_group parameter.
    • FFI builtin emission/cleanup handling was tightened and made explicit.
    • Future/stream lowering/lifting paths were reworked, including nested coverage.

3. MoonBit test toolchain integration updates

4. Runtime and codegen coverage expansion

Design Notes (Intentional)

  • Async export bodies are intentionally executed in a with_task_group child task to model MoonBit structured concurrency.
  • Async export signatures intentionally use task_group : @async.TaskGroup[Unit] as an umbrella concurrency/cancellation context, not as a direct encoding of function return payload type.

Verification

Tested with latest toolchain:

  • wasmtime 43.0.0
  • wasm-tools from main

Commands used:

  1. cargo run test --languages moonbit tests/codegen --artifacts target/artifacts --rust-wit-bindgen-path ./crates/guest-rust --test-threads 1
  2. PATH="target/tools/bin:$PATH" cargo run test --languages rust,moonbit tests/runtime-async --artifacts target/artifacts --rust-wit-bindgen-path ./crates/guest-rust --runner "wasmtime -W component-model-async"
  3. Focus checks:
    • rust-lowered-send
    • ping-pong

Move file writing to each stage to simplify the data structure
Previously we had them all in the pkg that does the exportation, but it made the code generator too complex. Now we move them away to simplify this

As a side effect, there's no unified return_area. The return_area will be freed with each post_return
- Add direction field to FunctionBindgen for proper type resolution
- Add type_name_for_lowering function to use OutFuture/OutStream instead of FutureR/StreamR for exports
- Add sig_string_with_direction for export stub generation
- Pass Direction::Import/Export to FunctionBindgen::new calls appropriately
- Add () to FFI function declarations with no parameters
- Use [export] prefix for lowering intrinsic module names
- Change reinterpret_as_int() to to_int() for handle conversion
- Add let _ = before spawn call to suppress unused result warning
- Make write and close functions async in StreamW generation
- Make spawn function public in coroutine.mbt for test usage
- Make OutStream struct public for test usage
- Change function signatures from async with out parameter to returning OutFuture/OutStream
- Update runner.rs to use new API and handle end-of-stream properly
- Fix stream test to use StreamResult::Complete for end-of-stream detection
- Add task_group parameter to async export function signatures
- Wrap async export calls with @async.with_task_group
- Add async_ field to FunctionBindgen for context tracking
- Update tests to use task_group.spawn_bg for background tasks

This allows exported async functions to spawn background tasks
that will be properly awaited before the function returns.
- Fix MoonBit codegen/runtime for futures/streams\n- Adjust runtime-async MoonBit tests and Rust runner
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.

1 participant