FFI (Foreign Function Interface)

all header files must import the kairo core from “include/core.hh”, and all code must start within the kairo namespace. Take a look at this C++ example and how to import and call it from kairo:

// project/my_math.hh
#include "include/core.hh"

namespace kairo {
    i32 add(i32 a, i32 b) {
        return a + b;
    }
    
    string greet(string name) {
        return L"Hello, " + name + L"!"; // Note the kairo string type in c++ REQUIRES the use of wide strings (L"")
    }

    f64 multiply(f64 a, f64 b) {
        return a * b;
    }
}

One thing, in the c++ since we have imported the core and are in the kairo namespace, we HAVE to use the kairo types (i32, string, f64, etc) and kairo core std functions (like std::print, etc). The C++ std would still be accessable under libcxx:: namespace if needed.

// project/main.hx
ffi "c++" import "my_math.hh"; // it does get put in the gobal namespace
// you can alias it with `ffi "c++" import "my_math.hh" as my_math;`
//     or put the functions in the header itself in a namespace

fn main() -> i32 {
    var sum:      i32    = add(5, 10);         // calls the c++ add function
    var greeting: string = greet("World");     // calls the c++ greet function
    var product:  f64    = multiply(3.5, 2.0); // calls the c++ multiply function

    std::print(greeting); // prints "Hello, World!"
    std::print(f"Sum: {sum}, Product: {product}");

    return 0;
}