1 /* see copyright notice in squirrel.h */
3 #include "../../../stdafx.h"
8 #include "../../../safeguards.h"
10 void sqstd_printcallstack(HSQUIRRELVM v
)
12 SQPRINTFUNCTION pf
= sq_getprintfunc(v
);
19 SQInteger level
=1; //1 is to skip this function that is level 0
22 pf(v
,"\nCALLSTACK\n");
23 while(SQ_SUCCEEDED(sq_stackinfos(v
,level
,&si
)))
25 const SQChar
*fn
="unknown";
26 const SQChar
*src
="unknown";
27 if(si
.funcname
)fn
=si
.funcname
;
29 /* We don't want to bother users with absolute paths to all AI files.
30 * Since the path only reaches NoAI code in a formatted string we have
31 * to strip it here. Let's hope nobody installs openttd in a subdirectory
32 * of a directory named /ai/. */
33 src
= strstr(si
.source
, "\\ai\\");
34 if (!src
) src
= strstr(si
.source
, "/ai/");
41 pf(v
,"*FUNCTION [%s()] %s line [%d]\n",fn
,src
,si
.line
);
47 for(level
=0;level
<10;level
++){
49 while((name
= sq_getlocal(v
,level
,seq
)))
52 switch(sq_gettype(v
,-1))
55 pf(v
,"[%s] NULL\n",name
);
58 sq_getinteger(v
,-1,&i
);
59 pf(v
,"[%s] %d\n",name
,i
);
63 pf(v
,"[%s] %.14g\n",name
,f
);
66 pf(v
,"[%s] USERPOINTER\n",name
);
69 sq_getstring(v
,-1,&s
);
70 pf(v
,"[%s] \"%s\"\n",name
,s
);
73 pf(v
,"[%s] TABLE\n",name
);
76 pf(v
,"[%s] ARRAY\n",name
);
79 pf(v
,"[%s] CLOSURE\n",name
);
81 case OT_NATIVECLOSURE
:
82 pf(v
,"[%s] NATIVECLOSURE\n",name
);
85 pf(v
,"[%s] GENERATOR\n",name
);
88 pf(v
,"[%s] USERDATA\n",name
);
91 pf(v
,"[%s] THREAD\n",name
);
94 pf(v
,"[%s] CLASS\n",name
);
97 pf(v
,"[%s] INSTANCE\n",name
);
100 pf(v
,"[%s] WEAKREF\n",name
);
104 pf(v
,"[%s] %s\n",name
,b
?"true":"false");
107 default: assert(0); break;
115 static SQInteger
_sqstd_aux_printerror(HSQUIRRELVM v
)
117 SQPRINTFUNCTION pf
= sq_getprintfunc(v
);
119 const SQChar
*sErr
= 0;
120 if(sq_gettop(v
)>=1) {
121 if(SQ_SUCCEEDED(sq_getstring(v
,2,&sErr
))) {
122 pf(v
,"\nAN ERROR HAS OCCURRED [%s]\n",sErr
);
125 pf(v
,"\nAN ERROR HAS OCCURRED [unknown]\n");
127 sqstd_printcallstack(v
);
133 void _sqstd_compiler_error(HSQUIRRELVM v
,const SQChar
*sErr
,const SQChar
*sSource
,SQInteger line
,SQInteger column
)
135 SQPRINTFUNCTION pf
= sq_getprintfunc(v
);
137 pf(v
,"%s line = (%d) column = (%d) : error %s\n",sSource
,line
,column
,sErr
);
141 void sqstd_seterrorhandlers(HSQUIRRELVM v
)
143 sq_setcompilererrorhandler(v
,_sqstd_compiler_error
);
144 sq_newclosure(v
,_sqstd_aux_printerror
,0);
145 sq_seterrorhandler(v
);