sq_pop(v, 1);
}
-static void print_stack(HSQUIRRELVM v)
+void print_squirrel_stack(HSQUIRRELVM v)
{
printf("--------------------------------------------------------------\n");
int count = sq_gettop(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)
+SquirrelError::SquirrelError(HSQUIRRELVM v, const std::string& message) throw()
{
- // 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);
+ std::ostringstream msg;
+ msg << "SQuirrel error: " << message << " (";
+ const char* lasterr;
+ sq_getlasterror(v);
+ sq_getstring(v, -1, &lasterr);
+ sq_pop(v, 1);
+ msg << lasterr << ")";
+ this->message = msg.str();
+}
- sq_remove(v, -2); // remove class
+SquirrelError::~SquirrelError() throw()
+{}
- // part2 of registration of the instance in the root table
- print_stack(v);
- check(sq_createslot(v, -3));
- sq_pop(v, 1);
+const char*
+SquirrelError::what() const throw()
+{
+ return message.c_str();
}
-