1 #include <stdarg.h>
2 #include <stdio.h>
3 
4 #include <squirrel.h>
5 #include <sqstdio.h>
6 #include <sqstdaux.h>
7 
8 #ifdef _MSC_VER
9 #pragma comment (lib ,"squirrel.lib")
10 #pragma comment (lib ,"sqstdlib.lib")
11 #endif
12 
13 #ifdef SQUNICODE
14 
15 #define scvprintf vfwprintf
16 #else
17 
18 #define scvprintf vfprintf
19 #endif
20 
printfunc(HSQUIRRELVM v,const SQChar * s,...)21 void printfunc(HSQUIRRELVM v,const SQChar *s,...)
22 {
23     va_list vl;
24     va_start(vl, s);
25     scvprintf(stdout, s, vl);
26     va_end(vl);
27 }
28 
errorfunc(HSQUIRRELVM v,const SQChar * s,...)29 void errorfunc(HSQUIRRELVM v,const SQChar *s,...)
30 {
31     va_list vl;
32     va_start(vl, s);
33     scvprintf(stderr, s, vl);
34     va_end(vl);
35 }
36 
call_foo(HSQUIRRELVM v,int n,float f,const SQChar * s)37 void call_foo(HSQUIRRELVM v, int n,float f,const SQChar *s)
38 {
39     SQInteger top = sq_gettop(v); //saves the stack size before the call
40     sq_pushroottable(v); //pushes the global table
41     sq_pushstring(v,_SC("foo"),-1);
42     if(SQ_SUCCEEDED(sq_get(v,-2))) { //gets the field 'foo' from the global table
43         sq_pushroottable(v); //push the 'this' (in this case is the global table)
44         sq_pushinteger(v,n);
45         sq_pushfloat(v,f);
46         sq_pushstring(v,s,-1);
47         sq_call(v,4,SQFalse,SQTrue); //calls the function
48     }
49     sq_settop(v,top); //restores the original stack size
50 }
51 
main(int argc,char * argv[])52 int main(int argc, char* argv[])
53 {
54     HSQUIRRELVM v;
55     v = sq_open(1024); // creates a VM with initial stack size 1024
56 
57     //REGISTRATION OF STDLIB
58     //sq_pushroottable(v); //push the root table where the std function will be registered
59     //sqstd_register_iolib(v);  //registers a library
60     // ... call here other stdlibs string,math etc...
61     //sq_pop(v,1); //pops the root table
62     //END REGISTRATION OF STDLIB
63 
64     sqstd_seterrorhandlers(v); //registers the default error handlers
65 
66     sq_setprintfunc(v, printfunc,errorfunc); //sets the print function
67 
68     sq_pushroottable(v); //push the root table(were the globals of the script will be stored)
69     if(SQ_SUCCEEDED(sqstd_dofile(v, _SC("test.nut"), SQFalse, SQTrue))) // also prints syntax errors if any
70     {
71         call_foo(v,1,2.5,_SC("teststring"));
72     }
73 
74     sq_pop(v,1); //pops the root table
75     sq_close(v);
76 
77     return 0;
78 }
79