3: C Interaction [✓] - royal-lang/rl GitHub Wiki
Royal is able to directly call C functions by including header files with the include keyword. To use a function specified in a header file its signature must be specified using the internal keyword prior to the function definition.
To include a header file simply create an include statement within the module's scope.
include HEADERFILE;
The header file value must be a string.
Example:
include "stdio.h";
To define a function from a header file simply use the internal keyword to specify the function signature.
This is superior to just allowing directly usage of the functions because restrictions can be put onto the definition etc. which can ensure safety of otherwise unsafe functions.
internal ROYAL_FUNCTION_DEFINITION;
Example:
alias CSTRING = ptr:byte;
internal fn printf(CSTRING message);
You can create safe wrappers around functions that you import internally from C.
Example:
// An actual implementation of printf() in Royal.
fn printf(string:const message)
{
include "stdio.h";
alias CSTRING = ptr:byte;
internal fn printf(CSTRING message);
var cstring = message.toStringz();
printf(cstring); // Calls the internal printf() and not itself.
}