sqstdaux.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* see copyright notice in squirrel.h */
  2. #include <squirrel.h>
  3. #include <sqstdaux.h>
  4. #include <assert.h>
  5. void sqstd_printcallstack(HSQUIRRELVM v)
  6. {
  7. SQPRINTFUNCTION pf = sq_geterrorfunc(v);
  8. if(pf) {
  9. SQStackInfos si;
  10. SQInteger i;
  11. SQFloat f;
  12. const SQChar *s;
  13. SQInteger level=1; //1 is to skip this function that is level 0
  14. const SQChar *name=0;
  15. SQInteger seq=0;
  16. pf(v,_SC("\nCALLSTACK\n"));
  17. while(SQ_SUCCEEDED(sq_stackinfos(v,level,&si)))
  18. {
  19. const SQChar *fn=_SC("unknown");
  20. const SQChar *src=_SC("unknown");
  21. if(si.funcname)fn=si.funcname;
  22. if(si.source)src=si.source;
  23. pf(v,_SC("*FUNCTION [%s()] %s line [%d]\n"),fn,src,si.line);
  24. level++;
  25. }
  26. level=0;
  27. pf(v,_SC("\nLOCALS\n"));
  28. for(level=0;level<10;level++){
  29. seq=0;
  30. while((name = sq_getlocal(v,level,seq)))
  31. {
  32. seq++;
  33. switch(sq_gettype(v,-1))
  34. {
  35. case OT_NULL:
  36. pf(v,_SC("[%s] NULL\n"),name);
  37. break;
  38. case OT_INTEGER:
  39. sq_getinteger(v,-1,&i);
  40. pf(v,_SC("[%s] %d\n"),name,i);
  41. break;
  42. case OT_FLOAT:
  43. sq_getfloat(v,-1,&f);
  44. pf(v,_SC("[%s] %.14g\n"),name,f);
  45. break;
  46. case OT_USERPOINTER:
  47. pf(v,_SC("[%s] USERPOINTER\n"),name);
  48. break;
  49. case OT_STRING:
  50. sq_getstring(v,-1,&s);
  51. pf(v,_SC("[%s] \"%s\"\n"),name,s);
  52. break;
  53. case OT_TABLE:
  54. pf(v,_SC("[%s] TABLE\n"),name);
  55. break;
  56. case OT_ARRAY:
  57. pf(v,_SC("[%s] ARRAY\n"),name);
  58. break;
  59. case OT_CLOSURE:
  60. pf(v,_SC("[%s] CLOSURE\n"),name);
  61. break;
  62. case OT_NATIVECLOSURE:
  63. pf(v,_SC("[%s] NATIVECLOSURE\n"),name);
  64. break;
  65. case OT_GENERATOR:
  66. pf(v,_SC("[%s] GENERATOR\n"),name);
  67. break;
  68. case OT_USERDATA:
  69. pf(v,_SC("[%s] USERDATA\n"),name);
  70. break;
  71. case OT_THREAD:
  72. pf(v,_SC("[%s] THREAD\n"),name);
  73. break;
  74. case OT_CLASS:
  75. pf(v,_SC("[%s] CLASS\n"),name);
  76. break;
  77. case OT_INSTANCE:
  78. pf(v,_SC("[%s] INSTANCE\n"),name);
  79. break;
  80. case OT_WEAKREF:
  81. pf(v,_SC("[%s] WEAKREF\n"),name);
  82. break;
  83. case OT_BOOL:{
  84. SQBool bval;
  85. sq_getbool(v,-1,&bval);
  86. pf(v,_SC("[%s] %s\n"),name,bval == SQTrue ? _SC("true"):_SC("false"));
  87. }
  88. break;
  89. default: assert(0); break;
  90. }
  91. sq_pop(v,1);
  92. }
  93. }
  94. }
  95. }
  96. static SQInteger _sqstd_aux_printerror(HSQUIRRELVM v)
  97. {
  98. SQPRINTFUNCTION pf = sq_geterrorfunc(v);
  99. if(pf) {
  100. const SQChar *sErr = 0;
  101. if(sq_gettop(v)>=1) {
  102. if(SQ_SUCCEEDED(sq_getstring(v,2,&sErr))) {
  103. pf(v,_SC("\nAN ERROR HAS OCCURED [%s]\n"),sErr);
  104. }
  105. else{
  106. pf(v,_SC("\nAN ERROR HAS OCCURED [unknown]\n"));
  107. }
  108. sqstd_printcallstack(v);
  109. }
  110. }
  111. return 0;
  112. }
  113. void _sqstd_compiler_error(HSQUIRRELVM v,const SQChar *sErr,const SQChar *sSource,SQInteger line,SQInteger column)
  114. {
  115. SQPRINTFUNCTION pf = sq_geterrorfunc(v);
  116. if(pf) {
  117. pf(v,_SC("%s line = (%d) column = (%d) : error %s\n"),sSource,line,column,sErr);
  118. }
  119. }
  120. void sqstd_seterrorhandlers(HSQUIRRELVM v)
  121. {
  122. sq_setcompilererrorhandler(v,_sqstd_compiler_error);
  123. sq_newclosure(v,_sqstd_aux_printerror,0);
  124. sq_seterrorhandler(v);
  125. }