# sqdebug.cpp

    00001 /*
    00002   see copyright notice in squirrel.h
    00003 */
    00004 #include <squirrel.h>
    00005 #include "sqpcheader.h"
    00006 #include <stdarg.h>
    00007 #include "sqvm.h"
    00008 #include "sqfuncproto.h"
    00009 #include "sqclosure.h"
    00010 #include "sqstring.h"
    00011
    00012 SQRESULT sq_stackinfos(HSQUIRRELVM v, SQInteger level, SQStackInfos *si)
    00013 {
    00014   SQInteger cssize = v->_callsstacksize;
    00015   if (cssize > level) {
    00016     memset(si, 0, sizeof(SQStackInfos));
    00017     SQVM::CallInfo &ci = v->_callsstack[cssize-level-1];
    00018     switch (type(ci._closure)) {
    00019     case OT_CLOSURE:{
    00020       SQFunctionProto *func = _funcproto(_closure(ci._closure)->_function);
    00021       if (type(func->_name) == OT_STRING)
    00022         si->funcname = _stringval(func->_name);
    00023       if (type(func->_sourcename) == OT_STRING)
    00024         si->source = _stringval(func->_sourcename);
    00025       si->line = func->GetLine(ci._ip);
    00026             }
    00027       break;
    00028     case OT_NATIVECLOSURE:
    00029       si->source = _SC("NATIVE");
    00030       si->funcname = _SC("unknown");
    00031       if(type(_nativeclosure(ci._closure)->_name) == OT_STRING)
    00032         si->funcname = _stringval(_nativeclosure(ci._closure)->_name);
    00033       si->line = -1;
    00034       break;
    00035     default: break; //shutup compiler
    00036     }
    00037     return SQ_OK;
    00038   }
    00039   return SQ_ERROR;
    00040 }
    00041
    00042 void SQVM::Raise_Error(const SQChar *s, ...)
    00043 {
    00044   va_list vl;
    00045   va_start(vl, s);
    00046   scvsprintf(_sp(rsl((SQInteger)scstrlen(s)+(NUMBER_MAX_CHAR*2))), s, vl);
    00047   va_end(vl);
    00048   _lasterror = SQString::Create(_ss(this),_spval,-1);
    00049 }
    00050
    00051 void SQVM::Raise_Error(SQObjectPtr &desc)
    00052 {
    00053   _lasterror = desc;
    00054 }
    00055
    00056 SQString *SQVM::PrintObjVal(const SQObject &o)
    00057 {
    00058   switch(type(o)) {
    00059   case OT_STRING: return _string(o);
    00060   case OT_INTEGER:
    00061 #if defined(_SQ64)
    00062     scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)), _SC("%ld"), _integer(o));
    00063 #else
    00064     scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)), _SC("%d"), _integer(o));
    00065 #endif
    00066     return SQString::Create(_ss(this), _spval);
    00067     break;
    00068   case OT_FLOAT:
    00069     scsprintf(_sp(rsl(NUMBER_MAX_CHAR+1)), _SC("%.14g"), _float(o));
    00070     return SQString::Create(_ss(this), _spval);
    00071     break;
    00072   default:
    00073     return SQString::Create(_ss(this), GetTypeName(o));
    00074   }
    00075 }
    00076
    00077 void SQVM::Raise_IdxError(SQObject &o)
    00078 {
    00079   SQObjectPtr oval = PrintObjVal(o);
    00080   Raise_Error(_SC("the index '%.50s' does not exist"), _stringval(oval));
    00081 }
    00082
    00083 void SQVM::Raise_CompareError(const SQObject &o1, const SQObject &o2)
    00084 {
    00085   SQObjectPtr oval1 = PrintObjVal(o1), oval2 = PrintObjVal(o2);
    00086   Raise_Error(_SC("comparsion between '%.50s' and '%.50s'"), _stringval(oval1), _stringval(oval2));
    00087 }
    00088
    00089
    00090 void SQVM::Raise_ParamTypeError(SQInteger nparam,SQInteger typemask,SQInteger type)
    00091 {
    00092   SQObjectPtr exptypes = SQString::Create(_ss(this), _SC(""), -1);
    00093   SQInteger found = 0;
    00094   for(SQInteger i=0; i<16; i++)
    00095   {
    00096     SQInteger mask = 0x00000001 << i;
    00097     if(typemask & (mask)) {
    00098       if(found>0) StringCat(exptypes,SQString::Create(_ss(this), _SC("|"), -1), exptypes);
    00099       found ++;
    00100       StringCat(exptypes,SQString::Create(_ss(this), IdType2Name((SQObjectType)mask), -1), exptypes);
    00101     }
    00102   }
    00103   Raise_Error(_SC("parameter %d has an invalid type '%s' ; expected: '%s'"), nparam, IdType2Name((SQObjectType)type), _stringval(exptypes));
    00104 }