Update readme.md
[openttd-joker.git] / src / 3rdparty / squirrel / sqstdlib / sqstdaux.cpp
blob540975fb5ccad21d9ef651a8e3ec679aba2de096
1 /* see copyright notice in squirrel.h */
3 #include "../../../stdafx.h"
5 #include <squirrel.h>
6 #include <sqstdaux.h>
8 #include "../../../safeguards.h"
10 void sqstd_printcallstack(HSQUIRRELVM v)
12 SQPRINTFUNCTION pf = sq_getprintfunc(v);
13 if(pf) {
14 SQStackInfos si;
15 SQInteger i;
16 SQBool b;
17 SQFloat f;
18 const SQChar *s;
19 SQInteger level=1; //1 is to skip this function that is level 0
20 const SQChar *name=0;
21 SQInteger seq=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;
28 if(si.source) {
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/");
35 if (src) {
36 src += 4;
37 } else {
38 src = si.source;
41 pf(v,"*FUNCTION [%s()] %s line [%d]\n",fn,src,si.line);
42 level++;
44 level=0;
45 pf(v,"\nLOCALS\n");
47 for(level=0;level<10;level++){
48 seq=0;
49 while((name = sq_getlocal(v,level,seq)))
51 seq++;
52 switch(sq_gettype(v,-1))
54 case OT_NULL:
55 pf(v,"[%s] NULL\n",name);
56 break;
57 case OT_INTEGER:
58 sq_getinteger(v,-1,&i);
59 pf(v,"[%s] %d\n",name,i);
60 break;
61 case OT_FLOAT:
62 sq_getfloat(v,-1,&f);
63 pf(v,"[%s] %.14g\n",name,f);
64 break;
65 case OT_USERPOINTER:
66 pf(v,"[%s] USERPOINTER\n",name);
67 break;
68 case OT_STRING:
69 sq_getstring(v,-1,&s);
70 pf(v,"[%s] \"%s\"\n",name,s);
71 break;
72 case OT_TABLE:
73 pf(v,"[%s] TABLE\n",name);
74 break;
75 case OT_ARRAY:
76 pf(v,"[%s] ARRAY\n",name);
77 break;
78 case OT_CLOSURE:
79 pf(v,"[%s] CLOSURE\n",name);
80 break;
81 case OT_NATIVECLOSURE:
82 pf(v,"[%s] NATIVECLOSURE\n",name);
83 break;
84 case OT_GENERATOR:
85 pf(v,"[%s] GENERATOR\n",name);
86 break;
87 case OT_USERDATA:
88 pf(v,"[%s] USERDATA\n",name);
89 break;
90 case OT_THREAD:
91 pf(v,"[%s] THREAD\n",name);
92 break;
93 case OT_CLASS:
94 pf(v,"[%s] CLASS\n",name);
95 break;
96 case OT_INSTANCE:
97 pf(v,"[%s] INSTANCE\n",name);
98 break;
99 case OT_WEAKREF:
100 pf(v,"[%s] WEAKREF\n",name);
101 break;
102 case OT_BOOL:{
103 sq_getbool(v,-1,&b);
104 pf(v,"[%s] %s\n",name,b?"true":"false");
106 break;
107 default: assert(0); break;
109 sq_pop(v,1);
115 static SQInteger _sqstd_aux_printerror(HSQUIRRELVM v)
117 SQPRINTFUNCTION pf = sq_getprintfunc(v);
118 if(pf) {
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);
124 else{
125 pf(v,"\nAN ERROR HAS OCCURRED [unknown]\n");
127 sqstd_printcallstack(v);
130 return 0;
133 void _sqstd_compiler_error(HSQUIRRELVM v,const SQChar *sErr,const SQChar *sSource,SQInteger line,SQInteger column)
135 SQPRINTFUNCTION pf = sq_getprintfunc(v);
136 if(pf) {
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);