#include <stdexcept>
#include <sstream>
-#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 << "<null>";
+ 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 << "<userdata>";
+ break;
+ case OT_CLOSURE:
+ os << "<closure (function)>";
+ break;
+ case OT_NATIVECLOSURE:
+ os << "<native closure (C function)>";
+ break;
+ case OT_GENERATOR:
+ os << "<generator>";
+ break;
+ case OT_USERPOINTER:
+ os << "userpointer";
+ break;
+ case OT_THREAD:
+ os << "<thread>";
+ break;
+ case OT_CLASS:
+ os << "<class>";
+ break;
+ case OT_INSTANCE:
+ os << "<instance>";
+ break;
+ default:
+ os << "<unknown>";
+ 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);
case OT_USERPOINTER:
printf("userpointer");
break;
+ case OT_THREAD:
+ printf("thread");
+ break;
case OT_CLASS:
printf("class");
break;
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 */