4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "wrapper_util.hpp"
29 std::string squirrel2string(HSQUIRRELVM v, int i)
31 std::ostringstream os;
32 switch(sq_gettype(v, i))
48 sq_getinteger(v, i, &val);
54 sq_getfloat(v, i, &val);
60 sq_getstring(v, i, &val);
61 os << "\"" << val << "\"";
67 sq_pushnull(v); //null iterator
68 while(SQ_SUCCEEDED(sq_next(v,i-1)))
75 //here -1 is the value and -2 is the key
76 os << squirrel2string(v, -2) << " => "
77 << squirrel2string(v, -1);
79 sq_pop(v,2); //pops key and val before the nex iteration
88 sq_pushnull(v); //null iterator
89 while(SQ_SUCCEEDED(sq_next(v,i-1)))
96 //here -1 is the value and -2 is the key
97 // we ignore the key, since that is just the index in an array
98 os << squirrel2string(v, -1);
100 sq_pop(v,2); //pops key and val before the nex iteration
110 os << "<closure (function)>";
112 case OT_NATIVECLOSURE:
113 os << "<native closure (C function)>";
137 void print_squirrel_stack(HSQUIRRELVM v)
139 printf("--------------------------------------------------------------\n");
140 int count = sq_gettop(v);
141 for(int i = 1; i <= count; ++i) {
143 switch(sq_gettype(v, i))
150 sq_getinteger(v, i, &val);
151 printf("integer (%d)", val);
156 sq_getfloat(v, i, &val);
157 printf("float (%f)", val);
162 sq_getstring(v, i, &val);
163 printf("string (%s)", val);
176 printf("closure(function)");
178 case OT_NATIVECLOSURE:
179 printf("native closure(C function)");
185 printf("userpointer");
197 printf("unknown?!?");
202 printf("--------------------------------------------------------------\n");
205 static SQInteger squirrel_read_char(SQUserPointer file)
207 std::istream* in = reinterpret_cast<std::istream*> (file);
214 void compile_script(HSQUIRRELVM vm, std::istream& in, const std::string& sourcename)
216 if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in, sourcename.c_str(), true)))
217 throw SquirrelError(vm, "Couldn't parse script");
220 void compile_and_run(HSQUIRRELVM vm, std::istream& in, const std::string& sourcename)
222 compile_script(vm, in, sourcename);
224 int oldtop = sq_gettop(vm);
227 sq_pushroottable(vm);
228 if(SQ_FAILED(sq_call(vm, 1, false)))
229 throw SquirrelError(vm, "Couldn't start script");
231 sq_settop(vm, oldtop);
235 sq_settop(vm, oldtop);