MoonBit C Binding Guide
Step-by-step workflow for binding any C library to MoonBit using native FFI.
When To Use
Use this skill when:
- Adding declarations for a C library
- Writing C stub files (, )
- Configuring for native builds (, )
- Choosing vs ownership transfer for FFI parameters
- Wrapping C handles with external objects and finalizers
- Implementing callback trampolines (closures or )
- Converting strings between MoonBit (UTF-16) and C (UTF-8)
- Running AddressSanitizer to catch memory bugs in bindings
Plan Mode Instructions
If you are in plan mode and creating a plan for moonbit-c-binding work:
- In your final plan file, include an explicit instruction at the beginning that tells the executing agent to read the moonbit-c-binding skill before starting implementation
- The instruction should look like this:
**Before starting implementation:** Use the Skill tool to load the moonbit-c-binding skill, which provides comprehensive guidance on FFI declarations, ownership annotations, C stubs, and AddressSanitizer validation.
- This ensures the executing agent has access to all the critical patterns and workflows documented in this skill
Type Mapping
Map C types to MoonBit types before writing any declarations.
| C Type | MoonBit Type | Notes |
|---|
| , | | 32-bit signed |
| | 32-bit unsigned |
| | 64-bit signed |
| | 64-bit unsigned |
| | 32-bit float |
| | 64-bit float |
| | Passed as in the C ABI (not C99 ) |
| , | | Single byte |
| | Return type only |
| (opaque, GC-managed) | (opaque) | External object with finalizer |
| (opaque, C-managed) | with annotation | No GC tracking; C manages lifetime |
| , | or | Use if C doesn't store it |
| (UTF-8 string) | | Null-terminated by runtime; pass directly to C |
| (small, no cleanup) | | Value-as-Bytes pattern |
| (needs cleanup) | (opaque) | External object with finalizer |
| (enum/flags) | , , or constant | enum Foo { A = 0; B = 1; C = 10 }
maps to |
| callback function pointer | or closure | See @references/callbacks.md |
| output | | Borrow the Ref |
Workflow
Follow these 4 phases in order.
Phase 1: Project Setup
Set up
and
for native compilation.
Module configuration (): Add
"preferred-target": "native"
so that
,
, and
default to the native backend:
json
{
"preferred-target": "native"
}
Package configuration ():
moonbit
options(
"native-stub": ["stub.c"],
targets: {
"ffi.mbt": ["native"]
},
)
Key fields:
| Field | Purpose |
|---|
| C source files to compile. Must be in the same directory as . |
| Gate files to backends: |
link(native("cc-flags": ...))
| Compile flags (, ). Only for system libraries. |
link(native("cc-link-flags": ...))
| Linker flags (, ). Only for system libraries. |
link(native("stub-cc-flags": ...))
| Compile flags for stub files only |
link(native(exports: ...))
| Export MoonBit functions to C (reverse direction) |
Warning — : Avoid
supported-targets: ["native"]
. It prevents downstream packages from building on other targets. Use
to gate individual files instead.
Warning — / portability: Setting
disables TCC for debug builds. Setting
with
/
breaks Windows portability. Only set these for system libraries.
Including library sources: All files in
must be in the same directory as
. For inclusion strategies (flattening, header-only, system library linking), see @references/including-c-sources.md.
Phase 2: FFI Layer
Write extern declarations and C stubs together. Keep externs private; expose safe wrappers in Phase 3. Both
and
are valid — choose one casing and be consistent (e.g., match
if also targeting JS).
External object pattern (C handle with cleanup, GC-managed):
mbt
// ffi.mbt (gated to native in targets)
///|
type Parser // opaque type backed by external object
///|
extern "c" fn ts_parser_new() -> Parser = "moonbit_ts_parser_new"
///|
#borrow(parser)
extern "c" fn ts_parser_language(parser : Parser) -> Language = "moonbit_ts_parser_language"
c
// stub.c
#include "tree_sitter/api.h"
#include <moonbit.h>
typedef struct { TSParser *parser; } MoonBitTSParser;
static void moonbit_ts_parser_destroy(void *ptr) {
ts_parser_delete(((MoonBitTSParser *)ptr)->parser);
// Do NOT free ptr -- GC manages the container
}
MOONBIT_FFI_EXPORT
MoonBitTSParser *moonbit_ts_parser_new(void) {
MoonBitTSParser *p = (MoonBitTSParser *)moonbit_make_external_object(
moonbit_ts_parser_destroy, sizeof(TSParser *)
);
p->parser = ts_parser_new();
return p;
}
annotation pattern (C pointer, C-managed lifetime):
When C fully manages the pointer's lifetime (no GC cleanup needed), annotate the type with
. The pointer is passed as raw
without reference counting:
mbt
///|
#external
type RawPtr // void*, not GC-tracked
///|
extern "c" fn raw_create() -> RawPtr = "lib_create"
///|
extern "c" fn raw_destroy(ptr : RawPtr) = "lib_destroy"
is an annotation (like
and
) — it goes on its own line before the
declaration, not on the same line.
No C stub wrapper or
moonbit_make_external_object
is needed — the MoonBit extern calls the C function directly. Use this when the C API has explicit create/destroy functions and you want manual lifetime control.
Ownership annotations:
| Annotation | When to use |
|---|
| C only reads during the call, does not store a reference |
| Ownership transfers to C; C must when done |
Rules:
- Annotate every non-primitive parameter as or .
- Primitives (, , , , etc.) are passed by value — no annotation needed.
- If unsure whether C stores a reference, do NOT use .
- Use with for output parameters where C writes a value back.
For detailed ownership semantics, see @references/ownership-and-memory.md.
String conversion across FFI:
MoonBit
is null-terminated by the runtime, so it can be passed directly to C functions expecting
. For the reverse direction (C string to MoonBit), use
+
:
c
// C side: return a C string as MoonBit Bytes
MOONBIT_FFI_EXPORT
moonbit_bytes_t moonbit_get_name(void *handle) {
const char *str = lib_get_name(handle);
int32_t len = strlen(str);
moonbit_bytes_t bytes = moonbit_make_bytes(len, 0);
memcpy(bytes, str, len);
return bytes; // if str was malloc'd, free(str) before returning
}
mbt
// MoonBit side: decode UTF-8 Bytes to String
// Requires import "moonbitlang/core/encoding/utf8" in moon.pkg
///|
pub fn get_name(handle : Handle) -> String {
@utf8.decode_lossy(get_name_ffi(handle))
}
Value-as-Bytes pattern (small struct, no cleanup):
c
MOONBIT_FFI_EXPORT
void *moonbit_settings_new(void) {
return moonbit_make_bytes(sizeof(settings_t), 0);
}
mbt
///|
struct Settings(Bytes) // backed by GC-managed Bytes, no finalizer
| API | Purpose |
|---|
moonbit_make_external_object(finalizer, size)
| GC-tracked object with cleanup finalizer |
moonbit_make_bytes(len, init)
| GC-managed byte array (MoonBit ) |
| Prevent GC collection of C-held object |
| Release C's reference (pair with incref) |
Moonbit_array_length(arr)
| Length of GC-managed array or Bytes |
| Required macro on all exported functions |
For the full API, read
(default
is
).
Phase 3: MoonBit API
Build safe public wrappers over the raw externs.
Type declarations:
mbt
///|
type Parser // opaque, backed by external object (has finalizer)
///|
struct Settings(Bytes) // value type, backed by GC-managed Bytes
///|
struct Node(Bytes) // small value struct
Safe constructors and methods:
mbt
///|
pub fn Parser::new() -> Parser {
ts_parser_new()
}
///|
pub fn Parser::set_language(self : Parser, language : Language) -> Bool {
ts_parser_set_language(self, language)
}
Error mapping:
mbt
///|
pub fn result_from_status(status : Int) -> Unit raise {
if status < 0 {
raise MyLibError(status)
}
}
For callback patterns (FuncRef, closures, trampolines), see @references/callbacks.md.
Phase 4: Testing
bash
moon test --target native -v
Run with AddressSanitizer to catch memory bugs:
bash
python3 scripts/run-asan.py \
--repo-root <project-root> \
--pkg moon.pkg \
--pkg main/moon.pkg
See @references/asan-validation.md for details.
Decision Table
| Situation | Pattern | Key Action |
|---|
| C reads pointer only during call | | No decref in C |
| C takes ownership of pointer | | C must |
| C handle needs cleanup on GC | External object + finalizer | moonbit_make_external_object
|
| C pointer, C manages lifetime | annotation on | No GC tracking; call C destroy explicitly |
| Small C struct, no cleanup | Value-as-Bytes | + |
| C returns null on failure | Nullable wrapper | Check null, return or raise error |
| Callback with data parameter | FuncRef + Callback trick | See @references/callbacks.md |
| Callback without data parameter | FuncRef only | See @references/callbacks.md |
| C string (UTF-8) output | across FFI | + in C; in MoonBit |
| Output parameter () | with | C writes into Ref, MoonBit reads |
Common Pitfalls
-
Using when C stores the pointer. The GC may collect the object while C holds a stale reference. Only borrow for call-scoped access.
-
Forgetting on owned parameters. Every non-borrowed, non-primitive parameter transfers ownership to C. Missing decrefs leak memory.
-
Calling on external object containers. The GC manages the container. Finalizers must only release the inner C resource.
-
Using for structs with inner pointers. Bytes have no finalizer, so inner heap allocations leak. Use external objects instead.
-
Missing before callback invocation. When C calls back into MoonBit, the GC may run. Incref MoonBit-managed objects before the call; decref afterward.
-
Forgetting the macro. Without it, the function is invisible to the MoonBit linker.
References
@references/ownership-and-memory.md
@references/callbacks.md
@references/including-c-sources.md
@references/asan-validation.md