AppendixExamples

Callback Examples

Native and hybrid callback proofs using generated bindings and Kira-owned persistent callback state.

The callback examples prove that Kira can:

  • generate a callback typedef binding from a C header
  • pass a Kira @Native function as a real C ABI callback
  • call through that callback in native code
  • do the same across the hybrid runtime/native boundary
  • keep mutable Kira-owned callback state behind an opaque userdata token

Native Example

Run:

kira run --backend llvm examples/callbacks

Current output:

callbacks
6
9
2
12

That comes from:

  • a manifest under examples/callbacks/NativeLibs/callbacks.toml
  • a generated module at examples/callbacks/callbacks.kira
  • a Kira CounterState boxed with nativeState(...)
  • an opaque token exported with nativeUserData(...)
  • a native callback that mutates the boxed state with nativeRecover(...)

Hybrid Example

The repo also carries a hybrid callback proof in:

tests/pass/run/ffi_callback_hybrid/

That matters because it exercises:

  • runtime entry execution
  • native callback invocation
  • runtime/native bridge marshalling

What This Proves

The callback examples are small, but they prove a lot of the current contract:

  • generated callback typedefs are real Kira types
  • RawPtr is the explicit user-data carrier
  • nativeState(...) avoids forcing globals for callback state
  • nativeRecover(any) is typed recovery, not a raw layout cast
  • @FFI.Struct { layout: c; } remains separate from opaque callback state
  • callback targets must match the generated signature exactly
  • Kira does not invent closure capture semantics at the ABI boundary

On this page