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