89663ba4c054c63563e87f7faf02c002cb6b42bf
[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, int level, SQStackInfos *si)\r
12 {\r
13         int 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                 }\r
35                 return SQ_OK;\r
36         }\r
37         return SQ_ERROR;\r
38 }\r
39 \r
40 void SQVM::Raise_Error(const SQChar *s, ...)\r
41 {\r
42         va_list vl;\r
43         va_start(vl, s);\r
44         scvsprintf(_sp(rsl(scstrlen(s)+(NUMBER_MAX_CHAR*2))), s, vl);\r
45         va_end(vl);\r
46         _lasterror = SQString::Create(_ss(this),_spval,-1);\r
47 }\r
48 \r
49 void SQVM::Raise_Error(SQObjectPtr &desc)\r
50 {\r
51         _lasterror = desc;\r
52 }\r
53 \r
54 SQString *SQVM::PrintObjVal(const SQObject &o)\r
55 {\r
56         switch(type(o)) {\r
57         case OT_STRING: return _string(o);\r
58         case OT_INTEGER:\r
59                 scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)), _SC("%d"), _integer(o));\r
60                 return SQString::Create(_ss(this), _spval);\r
61                 break;\r
62         case OT_FLOAT:\r
63                 scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)), _SC("%.14g"), _float(o));\r
64                 return SQString::Create(_ss(this), _spval);\r
65                 break;\r
66         default:\r
67                 return SQString::Create(_ss(this), GetTypeName(o));\r
68         }\r
69 }\r
70 \r
71 void SQVM::Raise_IdxError(SQObject &o)\r
72 {\r
73         SQObjectPtr oval = PrintObjVal(o);\r
74         Raise_Error(_SC("the index '%.50s' does not exist"), _stringval(oval));\r
75 }\r
76 \r
77 void SQVM::Raise_CompareError(const SQObject &o1, const SQObject &o2)\r
78 {\r
79         SQObjectPtr oval1 = PrintObjVal(o1), oval2 = PrintObjVal(o2);\r
80         Raise_Error(_SC("comparsion between '%.50s' and '%.50s'"), _stringval(oval1), _stringval(oval2));\r
81 }\r
82 \r
83 \r
84 void SQVM::Raise_ParamTypeError(int nparam,int typemask,int type)\r
85 {\r
86         SQObjectPtr exptypes = SQString::Create(_ss(this), _SC(""), -1);\r
87         int found = 0;  \r
88         for(int i=0; i<16; i++)\r
89         {\r
90                 int mask = 0x00000001 << i;\r
91                 if(typemask & (mask)) {\r
92                         if(found>0) StringCat(exptypes,SQString::Create(_ss(this), _SC("|"), -1), exptypes);\r
93                         found ++;\r
94                         StringCat(exptypes,SQString::Create(_ss(this), IdType2Name((SQObjectType)mask), -1), exptypes);\r
95                 }\r
96         }\r
97         Raise_Error(_SC("parameter %d has an invalid type '%s' ; expected: '%s'"), nparam, IdType2Name((SQObjectType)type), _stringval(exptypes));\r
98 }\r