Skip to content

fix(moonbit)!: separate exported resource objects from handles - #1742

Draft
peter-jerry-ye wants to merge 2 commits into
bytecodealliance:mainfrom
peter-jerry-ye:codex/moonbit-managed-resources
Draft

peter-jerry-ye wants to merge 2 commits into
bytecodealliance:mainfrom
peter-jerry-ye:codex/moonbit-managed-resources

Conversation

@peter-jerry-ye

@peter-jerry-ye peter-jerry-ye commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Exported resource methods and destructors receive resource representations, but the MoonBit bindings currently wrap those values as handles. Calling rep() on them can trap or resolve the wrong resource.

This change keeps the original resource name, such as Counter, for the application-defined object and generates an opaque CounterHandle for owned exported resources. A generated table translates incoming representations to objects, while owned values retain handles for transfer and destruction. Constructors return objects, including Result[Counter, E] for fallible constructors; generated adapters register successful results. Handle methods forward to the object implementation, including async methods.

Ownership transfer invalidates the handle wrapper and its aliases. Destruction releases the table slot before application cleanup, allowing cleanup to allocate or destroy other resources safely. Imported resources keep their existing type and method names, with opaque representations and generated conversion helpers for use across packages.

This is a breaking change to generated MoonBit source interfaces; the WIT interface and component ABI are unchanged. The migration guide explains constructor results, owned parameters, visibility, and cleanup. Borrowed objects remain ordinary MoonBit references, and owned resources still require explicit transfer or drop.

The changes are confined to the MoonBit generator, documentation, and resource fixtures. Existing resource tests cover ordinary calls and ownership transfers; the shared borrowed-record fixture gains a MoonBit implementation, and the existing payload fixture covers nested ownership and async forwarding. The new exported-resource-lifecycle fixture covers only fallible construction, alias invalidation after transfer or drop, and reentrant destruction with slot reuse. A code-generation fixture covers generated-name collisions across interfaces.

Fixes #1724.

This branch has not been deployed

No deployments
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.

moonbit: self of exported resource methods and dtor is a rep, but rep() treats it as a handle and traps

1 participant