#include #include #include #include "wrapper_util.hpp" std::string squirrel2string(HSQUIRRELVM v, int i) { 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; } case OT_INTEGER: { SQInteger 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; //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; } 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; //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; } return os.str(); } void print_squirrel_stack(HSQUIRRELVM v) { printf("--------------------------------------------------------------\n"); int count = sq_gettop(v); for(int i = 1; i <= count; ++i) { printf("%d: ",i); switch(sq_gettype(v, i)) { case OT_NULL: printf("null"); break; case OT_INTEGER: { SQInteger val; sq_getinteger(v, i, &val); printf("integer (%d)", static_cast(val)); break; } case OT_FLOAT: { float val; sq_getfloat(v, i, &val); printf("float (%f)", val); break; } case OT_STRING: { const char* val; sq_getstring(v, i, &val); printf("string (%s)", val); break; } case OT_TABLE: printf("table"); break; case OT_ARRAY: printf("array"); break; case OT_USERDATA: printf("userdata"); break; case OT_CLOSURE: printf("closure(function)"); break; case OT_NATIVECLOSURE: printf("native closure(C function)"); break; case OT_GENERATOR: printf("generator"); break; case OT_USERPOINTER: printf("userpointer"); break; case OT_THREAD: printf("thread"); break; case OT_CLASS: printf("class"); break; case OT_INSTANCE: printf("instance"); break; default: printf("unknown?!?"); break; } printf("\n"); } printf("--------------------------------------------------------------\n"); } /* EOF */