Generate type-safe MoonBit FFI bindings from C headers.
Dependencies
moonbind sqlite3.h --out examples/sqlite3 --target x86_64Status: 0.1.0. Lexer, preprocessor, C11 parser, record layout for three ABIs, code generation and the command line are complete and tested. What is left is a longer list of refusal reasons, not a missing stage; see Roadmap.
source ──► lex ──► cp ──► cparse ──► layout ──► codegen
│ │ │ │ │
files, tokenize, parse ABI emit
builtin #define, C11 offsets MoonBit
headers #include decls & sizes| Package | Responsibility |
|---|---|
| ast | The declaration model: CType, CRecordDecl, CFnDecl, CTypedef, CEnumDecl |
| source | Header acquisition: disk, embedded standard headers, include resolution |
| lex | C tokenizer (translation phases 1–3: splicing, comments, punctuators) |
| cp | Preprocessor (phase 4): directives, macro expansion, conditional inclusion |
| cparse | C11 declarations: specifiers, declarators, records, enums, bitfields |
| layout | Record layout for SysV x86-64, AArch64 and wasm32 |
| codegen | MoonBit emission: extern declarations, struct mirrors, constants |
| cli | Argument parsing, target selection, file output |
moonbind testdata/layout/bitfields.h --verify
# struct F1 0:0-25 a 4:0-6 b [sizeof=8, align=4]
# struct F2 0:0-25 a 3:2-5 b [sizeof=4, align=4]
# struct F3 0:0-3 a 1:0-5 b [sizeof=2, align=1]int *a[3] chain: [CArr 3, CPtr] fold: Array(Pointer(int), 3)
int (*a)[3] chain: [CPtr, CArr 3] fold: Pointer(Array(int, 3))$ moonbind testdata/sqlite/sqlite3.h --out /tmp/sqlite --report
left out: sqlite3_config — variadic, which MoonBit cannot call
left out: sqlite3_mprintf — variadic, which MoonBit cannot call
left out: sqlite3 — the definition is not in this translation unit
left out: fts5_api — no MoonBit representation: xCreateTokenizer: a pointer has no MoonBit slot
left out: sqlite3_vfs — no MoonBit representation: pNext: a pointer has no MoonBit slot$ moon run examples/sqlite3-demo --target native
craters by size:
Tycho — 5 craters
Copernicus — 3 craters
Kepler — 2 craters
3 rowsmoonbind examples/greet/greet.h --out examples/greet --report$ moonbind examples/greet/greet.h --out examples/greet --report
left out: greet_point_sum — parameter 0 passes greet_point by value, and MoonBit passes records by reference
left out: greet_ctx — the definition is not in this translation unit$ moon run examples/greet-demo --target native
library says: greet 1.0
moonbind greets Tycho
greet_hello returned 1
moonbind greets Kepler
greet_hello returned 2
count: 2
null count: -1$ moonbind testdata/layout/packed.h --verify --target aarch64
struct P1 0:0-25 a 3:2-8 b [sizeof=5, align=1]
struct P2 0:0-25 a 3:2-8 b [sizeof=6, align=2]
union P9 0 c 0:0-19 a [sizeof=4, align=4]moon add icyaaaww/moonbind@0.1.0moon run cmd/main --target wasm-gc -- --help
moon run cmd/main --target wasm-gc -- testdata/layout/packed.h --verifymoon update
moon check --target wasm-gc
moon build --target wasm-gc
moon test --target wasm-gcInstall
Download zipGenerate type-safe MoonBit FFI bindings from C headers.
Dependencies