refactored some supertux mainloops
[supertux.git] / src / scripting / wrapper_util.cpp
index 8b67d34..605ff1b 100644 (file)
@@ -4,6 +4,114 @@
 #include <sstream>
 #include "wrapper_util.hpp"
 
+std::string squirrel2string(HSQUIRRELVM v, int i)
+{
+  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;
+    }
+    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;
+
+          //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 << "<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;
+    }
+  return os.str();
+}
+
 void print_squirrel_stack(HSQUIRRELVM v)
 {
     printf("--------------------------------------------------------------\n");
@@ -72,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 */