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.
25 #include <sqstdmath.h>
26 #include <sqstdblob.h>
27 #include <sqstdstring.h>
30 #include "squirrel_util.hpp"
33 #include "physfs/physfs_stream.hpp"
34 #include "../random_generator.hpp"
37 #include <sqdbg/sqrdbg.h>
39 static HSQREMOTEDBG debugger = NULL;
45 HSQUIRRELVM global_vm = NULL;
47 static void printfunc(HSQUIRRELVM, const char* str, ...)
51 va_start(arglist, str);
52 vsprintf(buf, str, arglist);
53 Console::output << (const char*) buf << std::flush;
57 void init_squirrel(bool enable_debugger)
59 global_vm = sq_open(64);
61 throw std::runtime_error("Couldn't initialize squirrel vm");
65 sq_enabledebuginfo(global_vm, SQTrue);
66 debugger = sq_rdbg_init(global_vm, 1234, SQFalse);
68 throw SquirrelError(global_vm, "Couldn't initialize squirrel debugger");
70 sq_enabledebuginfo(global_vm, SQTrue);
71 log_info << "Waiting for debug client..." << std::endl;
72 if(SQ_FAILED(sq_rdbg_waitforconnections(debugger)))
73 throw SquirrelError(global_vm, "Waiting for debug clients failed");
74 log_info << "debug client connected." << std::endl;
78 sq_pushroottable(global_vm);
79 if(sqstd_register_bloblib(global_vm) < 0)
80 throw SquirrelError(global_vm, "Couldn't register blob lib");
81 if(sqstd_register_mathlib(global_vm) < 0)
82 throw SquirrelError(global_vm, "Couldn't register math lib");
83 if(sqstd_register_stringlib(global_vm) < 0)
84 throw SquirrelError(global_vm, "Couldn't register string lib");
85 // register supertux API
86 register_supertux_wrapper(global_vm);
88 // TODO remove this at some point... it shoud just be functions not an object
89 expose_object(global_vm, -1, new Scripting::Level(), "Level", true);
90 expose_object(global_vm, -1, &systemRandom, "RandomGenerator", false);
94 // register print function
95 sq_setprintfunc(global_vm, printfunc);
96 // register default error handlers
97 sqstd_seterrorhandlers(global_vm);
99 // try to load default script
101 std::string filename = "scripts/default.nut";
102 IFileStream stream(filename);
103 Scripting::compile_and_run(global_vm, stream, filename);
104 } catch(std::exception& e) {
105 log_warning << "Couldn't load default.nut: " << e.what() << std::endl;
112 if(debugger != NULL) {
113 sq_rdbg_shutdown(debugger);
124 void update_debugger()
128 sq_rdbg_update(debugger);
132 std::string squirrel2string(HSQUIRRELVM v, int i)
134 std::ostringstream os;
135 switch(sq_gettype(v, i))
142 sq_getbool(v, i, &p);
151 sq_getinteger(v, i, &val);
157 sq_getfloat(v, i, &val);
163 sq_getstring(v, i, &val);
164 os << "\"" << val << "\"";
170 sq_pushnull(v); //null iterator
171 while(SQ_SUCCEEDED(sq_next(v,i-1)))
178 //here -1 is the value and -2 is the key
179 os << squirrel2string(v, -2) << " => "
180 << squirrel2string(v, -1);
182 sq_pop(v,2); //pops key and val before the nex iteration
191 sq_pushnull(v); //null iterator
192 while(SQ_SUCCEEDED(sq_next(v,i-1)))
199 //here -1 is the value and -2 is the key
200 // we ignore the key, since that is just the index in an array
201 os << squirrel2string(v, -1);
203 sq_pop(v,2); //pops key and val before the nex iteration
215 case OT_NATIVECLOSURE:
216 os << "<native closure>";
243 void print_squirrel_stack(HSQUIRRELVM v)
245 printf("--------------------------------------------------------------\n");
246 int count = sq_gettop(v);
247 for(int i = 1; i <= count; ++i) {
249 switch(sq_gettype(v, i))
256 sq_getinteger(v, i, &val);
257 printf("integer (%d)", val);
262 sq_getfloat(v, i, &val);
263 printf("float (%f)", val);
268 sq_getstring(v, i, &val);
269 printf("string (%s)", val);
282 printf("closure(function)");
284 case OT_NATIVECLOSURE:
285 printf("native closure(C function)");
291 printf("userpointer");
306 printf("unknown?!?");
311 printf("--------------------------------------------------------------\n");
314 static SQInteger squirrel_read_char(SQUserPointer file)
316 std::istream* in = reinterpret_cast<std::istream*> (file);
323 void compile_script(HSQUIRRELVM vm, std::istream& in, const std::string& sourcename)
325 if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in, sourcename.c_str(), true)))
326 throw SquirrelError(vm, "Couldn't parse script");
329 void compile_and_run(HSQUIRRELVM vm, std::istream& in,
330 const std::string& sourcename)
332 compile_script(vm, in, sourcename);
334 int oldtop = sq_gettop(vm);
337 sq_pushroottable(vm);
338 if(SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue)))
339 throw SquirrelError(vm, "Couldn't start script");
341 sq_settop(vm, oldtop);
345 // we can remove the closure in case the script was not suspended
346 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
347 sq_settop(vm, oldtop-1);
351 HSQOBJECT create_thread(HSQUIRRELVM vm)
353 HSQUIRRELVM new_vm = sq_newthread(vm, 64);
355 throw SquirrelError(vm, "Couldn't create new VM");
358 sq_resetobject(&vm_object);
359 if(SQ_FAILED(sq_getstackobj(vm, -1, &vm_object)))
360 throw SquirrelError(vm, "Couldn't get squirrel thread from stack");
361 sq_addref(vm, &vm_object);
368 HSQOBJECT vm_to_object(HSQUIRRELVM vm)
371 sq_resetobject(&object);
372 object._unVal.pThread = vm;
373 object._type = OT_THREAD;
378 HSQUIRRELVM object_to_vm(HSQOBJECT object)
380 if(object._type != OT_THREAD)
383 return object._unVal.pThread;