X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fscripting%2Fwrapper_util.cpp;h=605ff1b20de411ca95d7e5f54f10b55623bdd71e;hb=1486ceaaf9dd7a9d2d7e3654550b9a2768df2a56;hp=86e7c4db4a98865d483fdebfb5bf3d91cc2a7af3;hpb=864c93e01ec366f730b3ebad08d5c52d6a9363b6;p=supertux.git diff --git a/src/scripting/wrapper_util.cpp b/src/scripting/wrapper_util.cpp index 86e7c4db4..605ff1b20 100644 --- a/src/scripting/wrapper_util.cpp +++ b/src/scripting/wrapper_util.cpp @@ -2,41 +2,114 @@ #include #include -#include "wrapper_util.h" +#include "wrapper_util.hpp" -static void register_function(HSQUIRRELVM v, SQFUNCTION func, const char* name) +std::string squirrel2string(HSQUIRRELVM v, int i) { - sq_pushstring(v, name, -1); - sq_newclosure(v, func, 0); //create a new function - sq_createslot(v, -3); -} - -static void register_class(HSQUIRRELVM v, WrappedClass* wclass) -{ - sq_pushstring(v, wclass->name, -1); - sq_newclass(v, false); - for(WrappedFunction* func = wclass->functions; func->name != 0; ++func) { - register_function(v, func->f, func->name); + std::ostringstream os; + switch(sq_gettype(v, i)) + { + case OT_NULL: + os << ""; + break; + case OT_BOOL: { + SQBool p; + sq_getbool(v, i, &p); + if (p) + os << "true"; + else + os << "false"; + break; } - sq_createslot(v, -3); -} + case OT_INTEGER: { + int val; + sq_getinteger(v, i, &val); + os << val; + break; + } + case OT_FLOAT: { + float val; + sq_getfloat(v, i, &val); + os << val; + break; + } + case OT_STRING: { + const char* val; + sq_getstring(v, i, &val); + os << "\"" << val << "\""; + break; + } + case OT_TABLE: { + bool first = true; + os << "{"; + sq_pushnull(v); //null iterator + while(SQ_SUCCEEDED(sq_next(v,i-1))) + { + if (!first) { + os << ", "; + } + first = false; -void register_functions(HSQUIRRELVM v, WrappedFunction* functions) -{ - sq_pushroottable(v); - for(WrappedFunction* func = functions; func->name != 0; ++func) { - register_function(v, func->f, func->name); + //here -1 is the value and -2 is the key + os << squirrel2string(v, -2) << " => " + << squirrel2string(v, -1); + + sq_pop(v,2); //pops key and val before the nex iteration + } + sq_pop(v, 1); + os << "}"; + break; } - sq_pop(v, 1); -} + case OT_ARRAY: { + bool first = true; + os << "["; + sq_pushnull(v); //null iterator + while(SQ_SUCCEEDED(sq_next(v,i-1))) + { + if (!first) { + os << ", "; + } + first = false; -void register_classes(HSQUIRRELVM v, WrappedClass* classes) -{ - sq_pushroottable(v); - for(WrappedClass* wclass = classes; wclass->name != 0; ++wclass) { - register_class(v, wclass); + //here -1 is the value and -2 is the key + // we ignore the key, since that is just the index in an array + os << squirrel2string(v, -1); + + sq_pop(v,2); //pops key and val before the nex iteration + } + sq_pop(v, 1); + os << "]"; + break; + } + case OT_USERDATA: + os << ""; + break; + case OT_CLOSURE: + os << ""; + break; + case OT_NATIVECLOSURE: + os << ""; + break; + case OT_GENERATOR: + os << ""; + break; + case OT_USERPOINTER: + os << "userpointer"; + break; + case OT_THREAD: + os << ""; + break; + case OT_CLASS: + os << ""; + break; + case OT_INSTANCE: + os << ""; + break; + default: + os << ""; + break; } - sq_pop(v, 1); + return os.str(); } void print_squirrel_stack(HSQUIRRELVM v) @@ -89,6 +162,9 @@ void print_squirrel_stack(HSQUIRRELVM v) case OT_USERPOINTER: printf("userpointer"); break; + case OT_THREAD: + printf("thread"); + break; case OT_CLASS: printf("class"); break; @@ -104,25 +180,4 @@ void print_squirrel_stack(HSQUIRRELVM v) printf("--------------------------------------------------------------\n"); } -//---------------------------------------------------------------------------- - -SquirrelError::SquirrelError(HSQUIRRELVM v, const std::string& message) throw() -{ - std::ostringstream msg; - msg << "SQuirrel error: " << message << " ("; - const char* lasterr; - sq_getlasterror(v); - sq_getstring(v, -1, &lasterr); - sq_pop(v, 1); - msg << lasterr << ")"; - this->message = msg.str(); -} - -SquirrelError::~SquirrelError() throw() -{} - -const char* -SquirrelError::what() const throw() -{ - return message.c_str(); -} +/* EOF */