#include #include #include "squirrel/squirrel.h" #include "squirrel/sqstdio.h" #include "squirrel/sqstdaux.h" #pragma comment (lib ,"squirrel.lib") #pragma comment (lib ,"sqstdlib.lib") #ifdef SQUNICODE #define scvprintf vwprintf #else #define scvprintf vprintf #endif void call_foo(HSQUIRRELVM v, int n, float f, const SQChar *s); void printfunc(HSQUIRRELVM v, const SQChar *s, ...); int main(int argc, char* argv[]) { HSQUIRRELVM VirtualMachine = sq_open(1024); // creates a VM with initial stack size 1024 sq_pushroottable(VirtualMachine); //push the root table were to register the lib function sqstd_register_iolib(VirtualMachine); sqstd_seterrorhandlers(VirtualMachine); //registers the default error handlers sq_setprintfunc(VirtualMachine, printfunc); //sets the print function sq_pushroottable(VirtualMachine); //push the root table(were the globals of the script will be stored) // also prints syntax errors if any if (SQ_SUCCEEDED(sqstd_dofile(VirtualMachine, _SC("test.nut"), SQFalse, SQTrue))) call_foo(VirtualMachine, 1, 2.5, _SC("teststring")); sq_pop(VirtualMachine, 1); //pops the root table sq_close(VirtualMachine); return 0; } void printfunc(HSQUIRRELVM v, const SQChar *s, ...) { va_list arglist; va_start(arglist, s); scvprintf(s, arglist); va_end(arglist); } void call_foo(HSQUIRRELVM v, int n, float f, const SQChar *s) { //SQInteger top = sq_gettop(v); //saves the stack size before the call int top = sq_gettop(v); //saves the stack size before the call sq_pushroottable(v); //pushes the global table sq_pushstring(v, _SC("foo2"), -1); sq_get(v,-2); //gets the field 'foo' from the global table if (SQ_SUCCEEDED(sq_get(v, -2))) { sq_pushroottable(v); //push the 'this' (in this case is the global table) sq_pushinteger(v, n); sq_pushfloat(v, f); sq_pushstring(v, s, -1); sq_call(v, 4, SQFalse, SQTrue); //calls the function } sq_settop(v, top); //restores the original stack size }//*/