-#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);
-}
-