minimal.c

00001 #include <stdarg.h>
00002 #include <stdio.h>
00003 
00004 #include <squirrel.h>
00005 #include <sqstdio.h>
00006 #include <sqstdaux.h>
00007 
00008 #ifdef _MSC_VER
00009 #pragma comment (lib ,"squirrel.lib")
00010 #pragma comment (lib ,"sqstdlib.lib")
00011 #endif
00012 
00013 #ifdef SQUNICODE
00014 #define scvprintf vwprintf
00015 #else
00016 #define scvprintf vprintf
00017 #endif
00018 
00019 void printfunc(HSQUIRRELVM v, const SQChar *s, ...)
00020 {
00021 va_list arglist;
00022 va_start(arglist, s);
00023 scvprintf(s, arglist);
00024 va_end(arglist);
00025 }
00026 
00027 void call_foo(HSQUIRRELVM v, int n,float f,const SQChar *s)
00028 {
00029   SQInteger top = sq_gettop(v); //saves the stack size before the call
00030   sq_pushroottable(v); //pushes the global table
00031   sq_pushstring(v,_SC("foo"),-1);
00032   if(SQ_SUCCEEDED(sq_get(v,-2))) { //gets the field 'foo' from the global table
00033     sq_pushroottable(v); //push the 'this' (in this case is the global table)
00034     sq_pushinteger(v,n);
00035     sq_pushfloat(v,f);
00036     sq_pushstring(v,s,-1);
00037     sq_call(v,4,SQFalse,SQTrue); //calls the function
00038   }
00039   sq_settop(v,top); //restores the original stack size
00040 }
00041 
00042 int main(int argc, char* argv[])
00043 {
00044   HSQUIRRELVM v;
00045   v = sq_open(1024); // creates a VM with initial stack size 1024
00046 
00047   //sq_pushroottable(v); //push the root table were to register the lib function
00048   //sqstd_register_iolib(v);
00049   sqstd_seterrorhandlers(v); //registers the default error handlers
00050 
00051   sq_setprintfunc(v, printfunc); //sets the print function
00052 
00053   sq_pushroottable(v); //push the root table(were the globals of the script will be stored)
00054   if(SQ_SUCCEEDED(sqstd_dofile(v, _SC("test.nut"), SQFalse, SQTrue))) // also prints syntax errors if any
00055   {
00056     call_foo(v,1,2.5,_SC("teststring"));
00057   }
00058 
00059   sq_pop(v,1); //pops the root table
00060   sq_close(v);
00061 
00062   return 0;
00063 }

Generated on Sun Nov 15 15:40:08 2009 for OpenTTD by  doxygen 1.5.6