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