- Avoid some expensive SDL_GetTicks() calls
[supertux.git] / src / squirrel / squirrel / sqdebug.cpp
1 /*\r
2         see copyright notice in squirrel.h\r
3 */\r
4 #include "sqpcheader.h"\r
5 #include <stdarg.h>\r
6 #include "sqvm.h"\r
7 #include "sqfuncproto.h"\r
8 #include "sqclosure.h"\r
9 #include "sqstring.h"\r
10 \r
11 SQRESULT sq_stackinfos(HSQUIRRELVM v, SQInteger level, SQStackInfos *si)\r
12 {\r
13         SQInteger cssize = v->_callsstack.size();\r
14         if (cssize > level) {\r
15                 memset(si, 0, sizeof(SQStackInfos));\r
16                 SQVM::CallInfo &ci = v->_callsstack[cssize-level-1];\r
17                 switch (type(ci._closure)) {\r
18                 case OT_CLOSURE:{\r
19                         SQFunctionProto *func = _funcproto(_closure(ci._closure)->_function);\r
20                         if (type(func->_name) == OT_STRING)\r
21                                 si->funcname = _stringval(func->_name);\r
22                         if (type(func->_sourcename) == OT_STRING)\r
23                                 si->source = _stringval(func->_sourcename);\r
24                         si->line = func->GetLine(ci._ip);\r
25                                                 }\r
26                         break;\r
27                 case OT_NATIVECLOSURE:\r
28                         si->source = _SC("NATIVE");\r
29                         si->funcname = _SC("unknown");\r
30                         if(type(_nativeclosure(ci._closure)->_name) == OT_STRING)\r
31                                 si->funcname = _stringval(_nativeclosure(ci._closure)->_name);\r
32                         si->line = -1;\r
33                         break;\r
34                 default: break; //shutup compiler\r
35                 }\r
36                 return SQ_OK;\r
37         }\r
38         return SQ_ERROR;\r
39 }\r
40 \r
41 void SQVM::Raise_Error(const SQChar *s, ...)\r
42 {\r
43         va_list vl;\r
44         va_start(vl, s);\r
45         scvsprintf(_sp(rsl((SQInteger)scstrlen(s)+(NUMBER_MAX_CHAR*2))), s, vl);\r
46         va_end(vl);\r
47         _lasterror = SQString::Create(_ss(this),_spval,-1);\r
48 }\r
49 \r
50 void SQVM::Raise_Error(SQObjectPtr &desc)\r
51 {\r
52         _lasterror = desc;\r
53 }\r
54 \r
55 SQString *SQVM::PrintObjVal(const SQObject &o)\r
56 {\r
57         switch(type(o)) {\r
58         case OT_STRING: return _string(o);\r
59         case OT_INTEGER:\r
60                 scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)), _SC("%d"), _integer(o));\r
61                 return SQString::Create(_ss(this), _spval);\r
62                 break;\r
63         case OT_FLOAT:\r
64                 scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)), _SC("%.14g"), _float(o));\r
65                 return SQString::Create(_ss(this), _spval);\r
66                 break;\r
67         default:\r
68                 return SQString::Create(_ss(this), GetTypeName(o));\r
69         }\r
70 }\r
71 \r
72 void SQVM::Raise_IdxError(SQObject &o)\r
73 {\r
74         SQObjectPtr oval = PrintObjVal(o);\r
75         Raise_Error(_SC("the index '%.50s' does not exist"), _stringval(oval));\r
76 }\r
77 \r
78 void SQVM::Raise_CompareError(const SQObject &o1, const SQObject &o2)\r
79 {\r
80         SQObjectPtr oval1 = PrintObjVal(o1), oval2 = PrintObjVal(o2);\r
81         Raise_Error(_SC("comparsion between '%.50s' and '%.50s'"), _stringval(oval1), _stringval(oval2));\r
82 }\r
83 \r
84 \r
85 void SQVM::Raise_ParamTypeError(SQInteger nparam,SQInteger typemask,SQInteger type)\r
86 {\r
87         SQObjectPtr exptypes = SQString::Create(_ss(this), _SC(""), -1);\r
88         SQInteger found = 0;    \r
89         for(SQInteger i=0; i<16; i++)\r
90         {\r
91                 SQInteger mask = 0x00000001 << i;\r
92                 if(typemask & (mask)) {\r
93                         if(found>0) StringCat(exptypes,SQString::Create(_ss(this), _SC("|"), -1), exptypes);\r
94                         found ++;\r
95                         StringCat(exptypes,SQString::Create(_ss(this), IdType2Name((SQObjectType)mask), -1), exptypes);\r
96                 }\r
97         }\r
98         Raise_Error(_SC("parameter %d has an invalid type '%s' ; expected: '%s'"), nparam, IdType2Name((SQObjectType)type), _stringval(exptypes));\r
99 }\r