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"
36 #include <sqdbg/sqrdbg.h>
38 static HSQREMOTEDBG debugger = NULL;
44 HSQUIRRELVM global_vm = NULL;
46 static void printfunc(HSQUIRRELVM, const char* str, ...)
50 va_start(arglist, str);
51 vsprintf(buf, str, arglist);
52 Console::output << (const char*) buf << std::flush;
56 void init_squirrel(bool enable_debugger)
58 global_vm = sq_open(64);
60 throw std::runtime_error("Couldn't initialize squirrel vm");
64 sq_enabledebuginfo(global_vm, SQTrue);
65 debugger = sq_rdbg_init(global_vm, 1234, SQFalse);
67 throw SquirrelError(global_vm, "Couldn't initialize squirrel debugger");
69 sq_enabledebuginfo(global_vm, SQTrue);
70 log_info << "Waiting for debug client..." << std::endl;
71 if(SQ_FAILED(sq_rdbg_waitforconnections(debugger)))
72 throw SquirrelError(global_vm, "Waiting for debug clients failed");
73 log_info << "debug client connected." << std::endl;
77 sq_pushroottable(global_vm);
78 if(sqstd_register_bloblib(global_vm) < 0)
79 throw SquirrelError(global_vm, "Couldn't register blob lib");
80 if(sqstd_register_mathlib(global_vm) < 0)
81 throw SquirrelError(global_vm, "Couldn't register math lib");
82 if(sqstd_register_stringlib(global_vm) < 0)
83 throw SquirrelError(global_vm, "Couldn't register string lib");
84 // register supertux API
85 register_supertux_wrapper(global_vm);
87 // TODO remove this at some point... it shoud just be functions not an object
88 expose_object(global_vm, -1, new Scripting::Level(), "Level", true);
92 // register print function
93 sq_setprintfunc(global_vm, printfunc);
94 // register default error handlers
95 sqstd_seterrorhandlers(global_vm);
97 // try to load default script
99 std::string filename = "scripts/default.nut";
100 IFileStream stream(filename);
101 Scripting::compile_and_run(global_vm, stream, filename);
102 } catch(std::exception& e) {
103 log_warning << "Couldn't load default.nut: " << e.what() << std::endl;
110 if(debugger != NULL) {
111 sq_rdbg_shutdown(debugger);
122 void update_debugger()
126 sq_rdbg_update(debugger);
130 std::string squirrel2string(HSQUIRRELVM v, int i)
132 std::ostringstream os;
133 switch(sq_gettype(v, i))
140 sq_getbool(v, i, &p);
149 sq_getinteger(v, i, &val);
155 sq_getfloat(v, i, &val);
161 sq_getstring(v, i, &val);
162 os << "\"" << val << "\"";
168 sq_pushnull(v); //null iterator
169 while(SQ_SUCCEEDED(sq_next(v,i-1)))
176 //here -1 is the value and -2 is the key
177 os << squirrel2string(v, -2) << " => "
178 << squirrel2string(v, -1);
180 sq_pop(v,2); //pops key and val before the nex iteration
189 sq_pushnull(v); //null iterator
190 while(SQ_SUCCEEDED(sq_next(v,i-1)))
197 //here -1 is the value and -2 is the key
198 // we ignore the key, since that is just the index in an array
199 os << squirrel2string(v, -1);
201 sq_pop(v,2); //pops key and val before the nex iteration
213 case OT_NATIVECLOSURE:
214 os << "<native closure>";
241 void print_squirrel_stack(HSQUIRRELVM v)
243 printf("--------------------------------------------------------------\n");
244 int count = sq_gettop(v);
245 for(int i = 1; i <= count; ++i) {
247 switch(sq_gettype(v, i))
254 sq_getinteger(v, i, &val);
255 printf("integer (%d)", val);
260 sq_getfloat(v, i, &val);
261 printf("float (%f)", val);
266 sq_getstring(v, i, &val);
267 printf("string (%s)", val);
280 printf("closure(function)");
282 case OT_NATIVECLOSURE:
283 printf("native closure(C function)");
289 printf("userpointer");
304 printf("unknown?!?");
309 printf("--------------------------------------------------------------\n");
312 static SQInteger squirrel_read_char(SQUserPointer file)
314 std::istream* in = reinterpret_cast<std::istream*> (file);
321 void compile_script(HSQUIRRELVM vm, std::istream& in, const std::string& sourcename)
323 if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in, sourcename.c_str(), true)))
324 throw SquirrelError(vm, "Couldn't parse script");
327 void compile_and_run(HSQUIRRELVM vm, std::istream& in,
328 const std::string& sourcename)
330 compile_script(vm, in, sourcename);
332 int oldtop = sq_gettop(vm);
335 sq_pushroottable(vm);
336 if(SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue)))
337 throw SquirrelError(vm, "Couldn't start script");
339 sq_settop(vm, oldtop);
343 // we can remove the closure in case the script was not suspended
344 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
345 sq_settop(vm, oldtop-1);
349 HSQOBJECT create_thread(HSQUIRRELVM vm)
351 HSQUIRRELVM new_vm = sq_newthread(vm, 64);
353 throw SquirrelError(vm, "Couldn't create new VM");
356 sq_resetobject(&vm_object);
357 if(SQ_FAILED(sq_getstackobj(vm, -1, &vm_object)))
358 throw SquirrelError(vm, "Couldn't get squirrel thread from stack");
359 sq_addref(vm, &vm_object);
366 HSQOBJECT vm_to_object(HSQUIRRELVM vm)
369 sq_resetobject(&object);
370 object._unVal.pThread = vm;
371 object._type = OT_THREAD;
376 HSQUIRRELVM object_to_vm(HSQOBJECT object)
378 if(object._type != OT_THREAD)
381 return object._unVal.pThread;