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