X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fscripting%2Fwrapper_util.cpp;h=605ff1b20de411ca95d7e5f54f10b55623bdd71e;hb=1486ceaaf9dd7a9d2d7e3654550b9a2768df2a56;hp=b25930f7b6fde6c25fba09fb94c578d5b5d60708;hpb=2f9e19ce4e01dc769ae7b2a7129109e0e81a6b5b;p=supertux.git diff --git a/src/scripting/wrapper_util.cpp b/src/scripting/wrapper_util.cpp index b25930f7b..605ff1b20 100644 --- a/src/scripting/wrapper_util.cpp +++ b/src/scripting/wrapper_util.cpp @@ -4,52 +4,114 @@ #include #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 - if(sq_createslot(v, -3) < 0) { - std::stringstream msg; - msg << "Couldn't register function '" << name << "'"; - throw SquirrelError(v, msg.str()); + 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; } -} - -static void register_class(HSQUIRRELVM v, WrappedClass* wclass) -{ - sq_pushstring(v, wclass->name, -1); - sq_newclass(v, false); - register_functions(v, wclass->functions); - register_constants(v, wclass->int_consts); - register_constants(v, wclass->float_consts); - register_constants(v, wclass->string_consts); - - if(sq_createslot(v, -3) < 0) { - std::stringstream msg; - msg << "Couldn't register function '" << wclass->name << "'"; - throw SquirrelError(v, msg.str()); + 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) { printf("--------------------------------------------------------------\n"); @@ -118,29 +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); - if(sq_gettype(v, -1) != OT_STRING) { - lasterr = "no error info"; - } else { - 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 */