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(SQ_FAILED(sqstd_register_bloblib(global_vm)))
80 throw SquirrelError(global_vm, "Couldn't register blob lib");
81 if(SQ_FAILED(sqstd_register_mathlib(global_vm)))
82 throw SquirrelError(global_vm, "Couldn't register math lib");
83 if(SQ_FAILED(sqstd_register_stringlib(global_vm)))
84 throw SquirrelError(global_vm, "Couldn't register string lib");
86 // remove rand and srand calls from sqstdmath, we'll provide our own
87 sq_pushstring(global_vm, "srand", -1);
88 sq_deleteslot(global_vm, -2, SQFalse);
89 sq_pushstring(global_vm, "rand", -1);
90 sq_deleteslot(global_vm, -2, SQFalse);
92 // register supertux API
93 register_supertux_wrapper(global_vm);
95 // TODO remove this at some point... it shoud just be functions not an object
96 expose_object(global_vm, -1, new Scripting::Level(), "Level", true);
100 // register print function
101 sq_setprintfunc(global_vm, printfunc);
102 // register default error handlers
103 sqstd_seterrorhandlers(global_vm);
105 // try to load default script
107 std::string filename = "scripts/default.nut";
108 IFileStream stream(filename);
109 Scripting::compile_and_run(global_vm, stream, filename);
110 } catch(std::exception& e) {
111 log_warning << "Couldn't load default.nut: " << e.what() << std::endl;
118 if(debugger != NULL) {
119 sq_rdbg_shutdown(debugger);
130 void update_debugger()
134 sq_rdbg_update(debugger);
138 std::string squirrel2string(HSQUIRRELVM v, SQInteger i)
140 std::ostringstream os;
141 switch(sq_gettype(v, i))
148 sq_getbool(v, i, &p);
157 sq_getinteger(v, i, &val);
163 sq_getfloat(v, i, &val);
169 sq_getstring(v, i, &val);
170 os << "\"" << val << "\"";
176 sq_pushnull(v); //null iterator
177 while(SQ_SUCCEEDED(sq_next(v,i-1)))
184 //here -1 is the value and -2 is the key
185 os << squirrel2string(v, -2) << " => "
186 << squirrel2string(v, -1);
188 sq_pop(v,2); //pops key and val before the nex iteration
197 sq_pushnull(v); //null iterator
198 while(SQ_SUCCEEDED(sq_next(v,i-1)))
205 //here -1 is the value and -2 is the key
206 // we ignore the key, since that is just the index in an array
207 os << squirrel2string(v, -1);
209 sq_pop(v,2); //pops key and val before the nex iteration
221 case OT_NATIVECLOSURE:
222 os << "<native closure>";
249 void print_squirrel_stack(HSQUIRRELVM v)
251 printf("--------------------------------------------------------------\n");
252 int count = sq_gettop(v);
253 for(int i = 1; i <= count; ++i) {
255 switch(sq_gettype(v, i))
262 sq_getinteger(v, i, &val);
263 printf("integer (%d)", static_cast<int> (val));
268 sq_getfloat(v, i, &val);
269 printf("float (%f)", val);
274 sq_getstring(v, i, &val);
275 printf("string (%s)", val);
288 printf("closure(function)");
290 case OT_NATIVECLOSURE:
291 printf("native closure(C function)");
297 printf("userpointer");
312 printf("unknown?!?");
317 printf("--------------------------------------------------------------\n");
320 static SQInteger squirrel_read_char(SQUserPointer file)
322 std::istream* in = reinterpret_cast<std::istream*> (file);
329 void compile_script(HSQUIRRELVM vm, std::istream& in, const std::string& sourcename)
331 if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in, sourcename.c_str(), true)))
332 throw SquirrelError(vm, "Couldn't parse script");
335 void compile_and_run(HSQUIRRELVM vm, std::istream& in,
336 const std::string& sourcename)
338 compile_script(vm, in, sourcename);
340 SQInteger oldtop = sq_gettop(vm);
343 sq_pushroottable(vm);
344 if(SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue)))
345 throw SquirrelError(vm, "Couldn't start script");
347 sq_settop(vm, oldtop);
351 // we can remove the closure in case the script was not suspended
352 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
353 sq_settop(vm, oldtop-1);
357 HSQOBJECT create_thread(HSQUIRRELVM vm)
359 HSQUIRRELVM new_vm = sq_newthread(vm, 64);
361 throw SquirrelError(vm, "Couldn't create new VM");
364 sq_resetobject(&vm_object);
365 if(SQ_FAILED(sq_getstackobj(vm, -1, &vm_object)))
366 throw SquirrelError(vm, "Couldn't get squirrel thread from stack");
367 sq_addref(vm, &vm_object);
374 HSQOBJECT vm_to_object(HSQUIRRELVM vm)
377 sq_resetobject(&object);
378 object._unVal.pThread = vm;
379 object._type = OT_THREAD;
384 HSQUIRRELVM object_to_vm(HSQOBJECT object)
386 if(object._type != OT_THREAD)
389 return object._unVal.pThread;