GOLF(2gg)

πŸ“¦ version 601.4.41, in ubuntu questing

🌸 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 .c file 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 .h file 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 the int64_t type). 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