Repository navigation
fix(moonbit)!: separate exported resource objects from handles - #1742
Draft
peter-jerry-ye wants to merge 5 commits into
Draft
peter-jerry-ye wants to merge 5 commits into
peter-jerry-ye wants to merge 5 commits into
Conversation
peter-jerry-ye
force-pushed
the
codex/moonbit-managed-resources
branch
from
October 20, 2026 08:07
e9f84f3 to
d40dde6
Compare
This branch has not been deployed
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.
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 opaqueCounterHandlefor owned exported resources. A generated table translates incoming representations to objects, while owned values retain handles for transfer and destruction. Constructors return objects, includingResult[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 implementation changes are confined to the MoonBit generator; documentation and resource fixtures describe and exercise the new interfaces. 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 exported-resource-lifecycle fixture adds focused coverage for fallible construction, alias invalidation after transfer or drop, reentrant destruction with slot reuse, and owned handles kept alive through background computation. The async ownership example distinguishes method-scoped endpoint cleanup from group-scoped handle destruction, allowing CM task return before background work completes. A code-generation fixture covers generated-name collisions across interfaces. Known unsupported collisions in Rust, C++, and D use the harness's existing expected-failure mechanism. The lifecycle runner is also MoonBit, so other language jobs do not select it without its implementation.
Fixes #1724.