GOLF(2gg)
πΈ GOLF(2gg) β the call-extended page, bestie edition πΈ
okay so this is a page from the GOLF(2gg) manual, filed under Development, and it's literally all about call-extended. grab a snack, we're going in π
β¨ NAME β¨
call-extended β (C-language) π«Ά
π― PURPOSE π―
Call an external function or macro β and heads up, this is extended mode only, no cap. π₯
π SYNTAX π
call-extended <function> "( " [ & ]<variable> [ , ... ] " )"
π DESCRIPTION π
So call-extended calls <function> β and <function> can be a function or a macro, we love a flexible icon β with a list of parameter variables. The <function> lives in one of these places:
- a library with C interop β and ngl, that library can be written in a whole bunch of different languages. Most compiled languages can produce libraries like this (Rust, C/C++, and others, we're not gatekeeping), π«
- a
.cfile sitting in the directory that has your application source code β and again, that C file can itself call code from libraries produced by any language that supports C interop (which is most of them, fr fr), - or, if it's a macro, in a
.hfile sitting in the directory that has your application source code.
The <function> must be declared via a C-style declaration in a .h file that lives in the application source code directory. You can use the --lflag and --cflag options of gg to supply the libraries you're using. And bonus: if you need to, you can also have any number of .c and .h files, and they'll be automatically included in your project. A macro must be defined in a .h file. periodt. π
The call-extended statement can only be used in extended mode (peep extended-mode). By default, Golf code runs in safe mode, which does not allow use of the call-extended statement. And listen β using call-extended does not automatically make your application unsafe. Extended code can be written in a memory-safe language (such as Rust), or even if it's written in C it can be made in such a way that it doesn't cause out-of-band memory reads and writes. so you can still be safe and slay π
𧬠C SIGNATURE, INPUT/OUTPUT VARIABLES, TYPES π§¬
Every <variable> can be a C type (or a pointer to a C type):
"gg_num"β a signed integer 8 bytes in length (that's theint64_ttype). This represents a Golf number. π’"char *"β a pointer to an array of characters. This represents a Golf string value. π¦"bool"β a boolean variable. This represents a Golf boolean. β
A <function> should not return a value. Instead, if you want to pass the function's output back to your Golf code, use a variable passed as a pointer. that's the move π«Ά
For more on using C in <function>s, see Server-API.
π CHANGING CURRENT WORKING DIRECTORY π
If you change the current working directory from your C code (like with the C chmod() function), you must set the Golf internal boolean variable "gg_path_changed" to true. don't skip this step bestie, it's giving mandatory π
π WEB FRAMEWORK FOR C PROGRAMMING LANGUAGE π
Note that when you use call-extended, Golf becomes a web-framework-for-C-programming-language. main character energy, honestly π
π₯ EXAMPLES π₯
For instance, consider the C file "calc.c":
#include "golf.h"
// Compute factorial of f, and store result into res
void factorial(gg_num f, gg_num *res)
{
*res = 1;
gg_num i;
for (i = 2; i <= f; i++) {
*res *= i;
}
}
Declare this C function in a header file, for instance "calc.h":
void factorial(gg_num f, gg_num *res);
You can also have macros in a header file, so for example "calc.h" could be:
void factorial(gg_num f, gg_num *res);
#define mod10(n, m) m=(n)%10
In this case you've defined a macro that calculates the modulo of 10 and stores the result into another variable. cute β¨
Use these in your Golf code with the call-extended statement β for instance, to use the function "factorial()":
extended-mode
begin-handler /fact public
set-number fact
call-extended factorial (10, &fact)
print-out fact
end-handler
In the above example, the number "fact" is passed by reference (as a pointer), and it'll contain the value of the factorial of 10 on return. The result printed out is "3628800". ate that π
To use the macro "mod10()":
extended-mode
begin-handler /mod public
set-number mod
call-extended mod10(103, mod)
print-out mod
end-handler
In this example, you're using a C macro, so the number "fact" is assigned a value directly, per C language rules. The result printed out is "3". iconic π
π SEE ALSO π
C language
call-extended extended-mode web-framework-for-C-programming-language See all documentation
and that's the whole page bestie!! this one was rendered from source version $VERSION dated $DATE β shoutout to the GOLF(2gg) manual for having us, mwah πβ¨π
π¦ where this page comes from
- ubuntu questing: golf 601.4.41-1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms