605ff1b20de411ca95d7e5f54f10b55623bdd71e
[supertux.git] / src / scripting / wrapper_util.cpp
1 #include <config.h>
2
3 #include <stdexcept>
4 #include <sstream>
5 #include "wrapper_util.hpp"
6
7 std::string squirrel2string(HSQUIRRELVM v, int i)
8 {
9   std::ostringstream os;
10   switch(sq_gettype(v, i))
11     {
12     case OT_NULL:
13       os << "<null>";        
14       break;
15     case OT_BOOL: {
16       SQBool p;
17       sq_getbool(v, i, &p);
18       if (p) 
19         os << "true";
20       else
21         os << "false";
22       break;
23     }
24     case OT_INTEGER: {
25       int val;
26       sq_getinteger(v, i, &val);
27       os << val;
28       break;
29     }
30     case OT_FLOAT: {
31       float val;
32       sq_getfloat(v, i, &val);
33       os << val;
34       break;
35     }
36     case OT_STRING: {
37       const char* val;
38       sq_getstring(v, i, &val);
39       os << "\"" << val << "\"";
40       break;    
41     }
42     case OT_TABLE: {
43       bool first = true;
44       os << "{";
45       sq_pushnull(v);  //null iterator
46       while(SQ_SUCCEEDED(sq_next(v,i-1)))
47         {
48           if (!first) {
49             os << ", ";
50           }
51           first = false;
52
53           //here -1 is the value and -2 is the key
54           os << squirrel2string(v, -2) << " => " 
55              << squirrel2string(v, -1);
56                               
57           sq_pop(v,2); //pops key and val before the nex iteration
58         }
59       sq_pop(v, 1);
60       os << "}";
61       break;
62     }
63     case OT_ARRAY: {
64       bool first = true;
65       os << "[";
66       sq_pushnull(v);  //null iterator
67       while(SQ_SUCCEEDED(sq_next(v,i-1)))
68         {
69           if (!first) {
70             os << ", ";
71           }
72           first = false;
73
74           //here -1 is the value and -2 is the key
75           // we ignore the key, since that is just the index in an array
76           os << squirrel2string(v, -1);
77                               
78           sq_pop(v,2); //pops key and val before the nex iteration
79         }
80       sq_pop(v, 1);
81       os << "]";
82       break;
83     }
84     case OT_USERDATA:
85       os << "<userdata>";
86       break;
87     case OT_CLOSURE:        
88       os << "<closure (function)>";
89       break;
90     case OT_NATIVECLOSURE:
91       os << "<native closure (C function)>";
92       break;
93     case OT_GENERATOR:
94       os << "<generator>";
95       break;
96     case OT_USERPOINTER:
97       os << "userpointer";
98       break;
99     case OT_THREAD:
100       os << "<thread>";
101       break;
102     case OT_CLASS:
103       os << "<class>";
104       break;
105     case OT_INSTANCE:
106       os << "<instance>";
107       break;
108     default:
109       os << "<unknown>";
110       break;
111     }
112   return os.str();
113 }
114
115 void print_squirrel_stack(HSQUIRRELVM v)
116 {
117     printf("--------------------------------------------------------------\n");
118     int count = sq_gettop(v);
119     for(int i = 1; i <= count; ++i) {
120         printf("%d: ",i);
121         switch(sq_gettype(v, i))
122         {
123             case OT_NULL:
124                 printf("null");        
125                 break;
126             case OT_INTEGER: {
127                 int val;
128                 sq_getinteger(v, i, &val);
129                 printf("integer (%d)", val);
130                 break;
131             }
132             case OT_FLOAT: {
133                 float val;
134                 sq_getfloat(v, i, &val);
135                 printf("float (%f)", val);
136                 break;
137             }
138             case OT_STRING: {
139                 const char* val;
140                 sq_getstring(v, i, &val);
141                 printf("string (%s)", val);
142                 break;    
143             }
144             case OT_TABLE:
145                 printf("table");
146                 break;
147             case OT_ARRAY:
148                 printf("array");
149                 break;
150             case OT_USERDATA:
151                 printf("userdata");
152                 break;
153             case OT_CLOSURE:        
154                 printf("closure(function)");    
155                 break;
156             case OT_NATIVECLOSURE:
157                 printf("native closure(C function)");
158                 break;
159             case OT_GENERATOR:
160                 printf("generator");
161                 break;
162             case OT_USERPOINTER:
163                 printf("userpointer");
164                 break;
165             case OT_THREAD:
166                 printf("thread");
167                 break;
168             case OT_CLASS:
169                 printf("class");
170                 break;
171             case OT_INSTANCE:
172                 printf("instance");
173                 break;
174             default:
175                 printf("unknown?!?");
176                 break;
177         }
178         printf("\n");
179     }
180     printf("--------------------------------------------------------------\n");
181 }
182
183 /* EOF */