moved back to old acc
[vox.git] / src / core / debug.cpp
blobce29f89fc0ffcb521c58f1ad735a9089e5d134be
2 #include <cstdarg>
3 #include <cassert>
4 #include "pcheader.hpp"
5 #include "vm.hpp"
6 #include "string.hpp"
7 #include "table.hpp"
8 #include "funcproto.hpp"
9 #include "closure.hpp"
10 #include "userdata.hpp"
11 #include "compiler.hpp"
12 #include "funcstate.hpp"
13 #include "class.hpp"
14 #include "debug.hpp"
16 VXInteger vox_aux_invalidtype(VXState* v,VXOType type)
18 return v->ThrowError("unexpected type '%s'", IdType2Name(type));
24 void vox_aux_printcallstack(VXState* v)
26 const char* fn;
27 const char* src;
28 VXPrintFunction pf = v->GetErrorFunc();
29 if(pf)
31 VXStackInfos si;
32 //1 is to skip this function that is level 0
33 VXInteger level=1;
34 while(VX_SUCCEEDED(v->StackInfos(level, &si)))
36 fn = get_funcname(si);
37 src = get_sourcename(si);
38 pf(v, " At '%s' in [%s:%d]\n", fn, src, si.line, si.column);
39 level++;
44 VXInteger vox_aux_printerror(VXState* v)
46 VXPrintFunction pf = v->GetErrorFunc();
47 const char *sErr;
48 if(pf)
50 if(v->GetTop() >= 1)
52 if(VX_FAILED(v->GetString(2, &sErr, NULL)))
54 sErr = "[unknown error]";
56 pf(v, "\nRuntime Error: %s\n", sErr);
57 vox_aux_printcallstack(v);
60 return 0;
63 void vox_aux_compiler_error(VXState* v,
64 const char *sErr,
65 const char *sSource,
66 VXInteger line,
67 VXInteger column)
69 VXPrintFunction pf = v->GetErrorFunc();
70 if(pf)
72 pf(v,
73 "Syntax Error: in %s, line %d, column %d: %s\n",
74 sSource, line, column, sErr);
78 void vox_aux_asserthandler(const char* expr,
79 const char* funct,
80 const char* file,
81 int line)
83 fprintf(stderr, "vox_assert(%s) failed:\n At %s:%d, function '%s'\n",
84 expr, file, line, funct);
85 abort();
90 VXInteger vox_getfunctioninfo(VXState* v,VXInteger level,VXFunctionInfo *fi)
92 VXInteger cssize = v->_callsstacksize;
93 if (cssize > level)
95 VXState::CallInfo &ci = v->_callsstack[cssize-level-1];
96 if(ci._closure.IsClosure())
98 VXForeignClosureObj *c = _closure(ci._closure);
99 VXFuncProtoObj *proto = c->_function;
100 fi->funcid = proto;
101 fi->name = type(proto->_name) == VX_OT_STRING?_stringval(proto->_name):"unknown";
102 fi->source = type(proto->_name) == VX_OT_STRING?_stringval(proto->_sourcename):"unknown";
103 return VX_OK;
106 return v->ThrowError("the object is not a closure");