Skip to content

FFI (Foreign Function Interface)

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

project/my_math.hh
#include "include/core.hh"
namespace helix {
i32 add(i32 a, i32 b) {
return a + b;
}
string greet(string name) {
return L"Hello, " + name + L"!"; // Note the helix 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 helix namespace, we HAVE to use the helix types (i32, string, f64, etc) and helix 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;
}