C Functions

A C function has the type int CFunc( sgs_Context* ). It receives the context that called it and must return the number of return values pushed on stack.

Conventions

There are no forced courses of action beyond this point. However, to simplify development, some conventions were established and are used throughout the standard library and are suggested to follow.

General structure
Argument loading
Error handling

A typical function

int sgsfunc_sample( SGS_CTX )
{
    sgs_Int num;
    char* str;
    char* buf;
    sgs_SizeVal bufsize;
    
    if( !sgs_LoadArgs( C, "ism", &num, &str, &buf, &bufsize ) )
        return 0;
    
    if( bufsize == 0 )
        return sgs_Msg( C, SGS_WARNING, "buffer cannot be empty" );
    // sgs_Msg always returns 0
    
    // .. do something with the passed arguments ..
    
    // anything can be returned ..
    // .. but in this case, we want to say ..
    // .. that the action was successful
    sgs_PushBool( C, 1 );
    return 1;
}