FFI (Foreign Function Interface)
Read about FFI at Wikipedia
This documentation page is written for AARCH64_MacOS
target and may be irrelevant for some underneath implementations on different targets, like FFI naming conventions on different assemblers
Gofra is capable of calling functions that written in other languages and externally defined, for example using libraries written for/in C
or other language, or using system libraries.
To do that assembler after code generation at linker stage should be acknowledged of external libraries (via CLI linker flags) and Gofra source code should specify which functions being external for FFI.
extern
marker
Functions marked with extern
keyword/marker will be treated as externally defined via FFI for example external function puts
from libc
is declared as:
extern func int _puts[ptr]
This is written according to libc
library C
interface:
int puts(const char *s){ ... }
External functions in Gofra CANNOT have any tokens inside otherwise compiler will throw an error
Type contracts for FFI
To specify which data is expected for and from external function you specify type contract for that functions (like default functions in Gofra)
Every argument will be treated by compiler and will be passed to that function, as well as external function return data will be pushed onto stack for you
Variadic arguments (type contracts)
For now, there is no way to pass variadic arguments to external function (Hack or workaround for that is only to create non-variadic wrappers with different number of arguments to pass to underlying external function which type contract is expanded for max required amount of arguments)
Linker stage
For letting linker know about that dependencies (Libraries with FFI), you can pass -L
flag(s) to the compiler
For example linking with raylib
and using their functions:
-L=-lraylib -L=-L/opt/homebrew/lib
Additional flags: @
By using @
flag you may specify for example against which function to link your external:
extern func int _puts[ptr]@link[_puts]
In that case is straighforward same name, but you can change that, and