Code::Blocks  SVN r11506
sqdebug.cpp
Go to the documentation of this file.
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_getfunctioninfo(HSQUIRRELVM v,SQInteger level,SQFunctionInfo *fi)
12 {
13  SQInteger cssize = v->_callsstacksize;
14  if (cssize > level) {
15  SQVM::CallInfo &ci = v->_callsstack[cssize-level-1];
16  if(sq_isclosure(ci._closure)) {
17  SQClosure *c = _closure(ci._closure);
18  SQFunctionProto *proto = _funcproto(c->_function);
19  fi->funcid = proto;
20  fi->name = type(proto->_name) == OT_STRING?_stringval(proto->_name):_SC("unknown");
21  fi->source = type(proto->_name) == OT_STRING?_stringval(proto->_sourcename):_SC("unknown");
22  return SQ_OK;
23  }
24  }
25  return sq_throwerror(v,_SC("the object is not a closure"));
26 }
27 
28 SQRESULT sq_stackinfos(HSQUIRRELVM v, SQInteger level, SQStackInfos *si)
29 {
30  SQInteger cssize = v->_callsstacksize;
31  if (cssize > level) {
32  memset(si, 0, sizeof(SQStackInfos));
33  SQVM::CallInfo &ci = v->_callsstack[cssize-level-1];
34  switch (type(ci._closure)) {
35  case OT_CLOSURE:{
36  SQFunctionProto *func = _funcproto(_closure(ci._closure)->_function);
37  if (type(func->_name) == OT_STRING)
38  si->funcname = _stringval(func->_name);
39  if (type(func->_sourcename) == OT_STRING)
40  si->source = _stringval(func->_sourcename);
41  si->line = func->GetLine(ci._ip);
42  }
43  break;
44  case OT_NATIVECLOSURE:
45  si->source = _SC("NATIVE");
46  si->funcname = _SC("unknown");
47  if(type(_nativeclosure(ci._closure)->_name) == OT_STRING)
48  si->funcname = _stringval(_nativeclosure(ci._closure)->_name);
49  si->line = -1;
50  break;
51  default: break; //shutup compiler
52  }
53  return SQ_OK;
54  }
55  return SQ_ERROR;
56 }
57 
58 void SQVM::Raise_Error(const SQChar *s, ...)
59 {
60  va_list vl;
61  va_start(vl, s);
62  scvsprintf(_sp(rsl((SQInteger)scstrlen(s)+(NUMBER_MAX_CHAR*2))), s, vl);
63  va_end(vl);
64  _lasterror = SQString::Create(_ss(this),_spval,-1);
65 }
66 
67 void SQVM::Raise_Error(SQObjectPtr &desc)
68 {
69  _lasterror = desc;
70 }
71 
72 SQString *SQVM::PrintObjVal(const SQObject &o)
73 {
74  switch(type(o)) {
75  case OT_STRING: return _string(o);
76  case OT_INTEGER:
77  // C::B patch: Support for Windows 64 bit
78  #if defined(_WIN64)
79  scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)), _SC("%I64d"), _integer(o));
80  // C::B patch: Support for Linux 64 bit
81  #elif defined(_SQ64)
82  scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)), _SC("%ld"), _integer(o));
83  #else
84  scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)), _SC("%d"), _integer(o));
85  #endif
86  return SQString::Create(_ss(this), _spval);
87  break;
88  case OT_FLOAT:
89  scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)), _SC("%.14g"), _float(o));
90  return SQString::Create(_ss(this), _spval);
91  break;
92  default:
93  return SQString::Create(_ss(this), GetTypeName(o));
94  }
95 }
96 
97 void SQVM::Raise_IdxError(SQObject &o)
98 {
99  SQObjectPtr oval = PrintObjVal(o);
100  Raise_Error(_SC("the index '%.50s' does not exist"), _stringval(oval));
101 }
102 
103 void SQVM::Raise_CompareError(const SQObject &o1, const SQObject &o2)
104 {
105  SQObjectPtr oval1 = PrintObjVal(o1), oval2 = PrintObjVal(o2);
106  Raise_Error(_SC("comparsion between '%.50s' and '%.50s'"), _stringval(oval1), _stringval(oval2));
107 }
108 
109 
110 void SQVM::Raise_ParamTypeError(SQInteger nparam,SQInteger typemask,SQInteger type)
111 {
112  SQObjectPtr exptypes = SQString::Create(_ss(this), _SC(""), -1);
113  SQInteger found = 0;
114  for(SQInteger i=0; i<16; i++)
115  {
116  SQInteger mask = 0x00000001 << i;
117  if(typemask & (mask)) {
118  if(found>0) StringCat(exptypes,SQString::Create(_ss(this), _SC("|"), -1), exptypes);
119  found ++;
120  StringCat(exptypes,SQString::Create(_ss(this), IdType2Name((SQObjectType)mask), -1), exptypes);
121  }
122  }
123  Raise_Error(_SC("parameter %d has an invalid type '%s' ; expected: '%s'"), nparam, IdType2Name((SQObjectType)type), _stringval(exptypes));
124 }
SQRESULT sq_throwerror(HSQUIRRELVM v, const SQChar *err)
Definition: sqapi.cpp:918
const SQChar * GetTypeName(const SQObjectPtr &obj1)
Definition: sqobject.cpp:43
SQRESULT sq_getfunctioninfo(HSQUIRRELVM v, SQInteger level, SQFunctionInfo *fi)
Definition: sqdebug.cpp:11
const SQChar * IdType2Name(SQObjectType type)
Definition: sqobject.cpp:15
char SQChar
SQRESULT sq_stackinfos(HSQUIRRELVM v, SQInteger level, SQStackInfos *si)
Definition: sqdebug.cpp:28