fun> Fun - Embedding Fun
Embedding the VM from C/Rust, lifecycle, and host integration tips.
This guide outlines how to embed the Fun VM in a host application and extend it from C/Rust.
Overview
- The VM is implemented in C (see ../internals/).
- Optional Rust-based opcodes can be enabled via
FUN_WITH_RUST(see ../rust/).
Embedding from C
While the exact API surface may evolve, a typical embedding flow looks like:
- Initialize the VM/runtime and allocate a context.
- Load/compile Fun source or bytecode.
- Push arguments or set globals as needed.
- Execute entry function or script body.
- Retrieve results and clean up.
See https://git.xw3.org/fun/fun/src/branch/main/src/ and related headers for public entry points and value types.
Hosting considerations
- Threading: share VM state cautiously or create one VM per thread.
- Memory: clarify ownership of strings/buffers crossing the boundary.
- Errors: propagate parse/runtime errors back to the host with useful messages.
Extending with Rust
When FUN_WITH_RUST=ON, a Rust static library from https://git.xw3.org/fun/fun/src/branch/main/src/rust is built and linked. You can:
- Implement new opcodes/functions in Rust.
- Expose a C ABI for the VM to call into.
See ../rust/ for details and example code.

