minimal.c 2.0 KB

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