Skip to content
Kavachकवच

Calling C: Callbacks

● Advanced3 min

A C function can call back into Kavach: pass the name of a Kavach function whose type matches C's function pointer.

extern c "c" {
    fn qsort(items: Ptr<Int32>, count: UInt64, size: UInt64, compare: fn(Ptr<Int32>, Ptr<Int32>) -> Int32)
}

fn by_value(a: Ptr<Int32>, b: Ptr<Int32>) -> Int32 {
    unsafe { a.read(0) - b.read(0) }
}

fn main() {
    unsafe {
        const items: Ptr<Int32> = alloc(5)
        const values = [42, 7, 19, 3, 25]
        for i in 0..5 {
            items.write(i, Int32(values[i]))
        }
        qsort(items, 5, 4, by_value)
        for i in 0..5 {
            print(items.read(i))
        }
        free(items)
    }
}

The program ran and printed:

kavach run

37192542
src/main.kav: Kavach code, then what running it prints

C's qsort calls by_value to compare two items. The callback must be a named function that can't fail: a lambda can't be passed, because C has nowhere to keep what it captures.