Code samples - the C API

Primary operations

// create the context
sgs_Context* C = sgs_CreateEngine();

// load the built-in math library
sgs_LoadLib_Math( C );

// load a file
sgs_ExecFile( C, "script.sgs" );

// call a global function with 0 arguments, expecting 0 values returned
sgs_GlobalCall( C, "myFunction", 0, 0 );

// destroy the context
sgs_DestroyEngine( C );

Data in, data out

sgs_Context* C = sgs_CreateEngine();
sgs_LoadLib_Math( C );

// push a variable on the stack
sgs_PushReal( C, 3.14 );

// call a global function with 1 argument, expecting 1 value returned
sgs_GlobalCall( C, "sin", 1, 1 );

// check the returned value
printf( "returned value: %f\n", sgs_GetReal( C, -1 ) );

// clean the stack
sgs_SetStackSize( C, 0 );