X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fscripting%2Fwrapper_util.cpp;h=605ff1b20de411ca95d7e5f54f10b55623bdd71e;hb=1486ceaaf9dd7a9d2d7e3654550b9a2768df2a56;hp=a16953ad7bd842034bbcf4167005f2e7a364afc0;hpb=60908c905544776c376421b8d3e12eeb936c068f;p=supertux.git diff --git a/src/scripting/wrapper_util.cpp b/src/scripting/wrapper_util.cpp index a16953ad7..605ff1b20 100644 --- a/src/scripting/wrapper_util.cpp +++ b/src/scripting/wrapper_util.cpp @@ -2,44 +2,117 @@ #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(); } -static void print_stack(HSQUIRRELVM v) +void print_squirrel_stack(HSQUIRRELVM v) { printf("--------------------------------------------------------------\n"); int count = sq_gettop(v); @@ -89,6 +162,9 @@ static void print_stack(HSQUIRRELVM v) case OT_USERPOINTER: printf("userpointer"); break; + case OT_THREAD: + printf("thread"); + break; case OT_CLASS: printf("class"); break; @@ -104,49 +180,4 @@ static void print_stack(HSQUIRRELVM v) printf("--------------------------------------------------------------\n"); } -#define check(x) \ - if((x) < 0) { \ - std::stringstream msg; \ - sq_getlasterror(v); \ - const char* error; \ - sq_getstring(v, -1, &error); \ - msg << "Error: " << error; \ - throw std::runtime_error(msg.str()); \ - } - -void expose_object(HSQUIRRELVM v, void* object, const char* type, - const char* name) -{ - // part1 of registration of the instance in the root table - sq_pushroottable(v); - sq_pushstring(v, name, -1); - - // resolve class name - sq_pushroottable(v); - sq_pushstring(v, type, -1); - print_stack(v); - if(sq_get(v, -2) < 0) { - std::ostringstream msg; - msg << "Couldn't resolve squirrel type '" << type << "'."; - throw std::runtime_error(msg.str()); - } - sq_remove(v, -2); // remove roottable - print_stack(v); - - // create an instance and set pointer to c++ object - print_stack(v); - check(sq_createinstance(v, -1)); - printf("after creatinstance\n"); - print_stack(v); - check(sq_setinstanceup(v, -1, object)); - printf("after setinstanceup\n"); - print_stack(v); - - sq_remove(v, -2); // remove class - - // part2 of registration of the instance in the root table - print_stack(v); - check(sq_createslot(v, -3)); - sq_pop(v, 1); -} - +/* EOF */