5 #include "wrapper_util.h"
7 static void register_function(HSQUIRRELVM v, SQFUNCTION func, const char* name)
9 sq_pushstring(v, name, -1);
10 sq_newclosure(v, func, 0); //create a new function
14 static void register_class(HSQUIRRELVM v, WrappedClass* wclass)
16 sq_pushstring(v, wclass->name, -1);
17 sq_newclass(v, false);
18 for(WrappedFunction* func = wclass->functions; func->name != 0; ++func) {
19 register_function(v, func->f, func->name);
24 void register_functions(HSQUIRRELVM v, WrappedFunction* functions)
27 for(WrappedFunction* func = functions; func->name != 0; ++func) {
28 register_function(v, func->f, func->name);
33 void register_classes(HSQUIRRELVM v, WrappedClass* classes)
36 for(WrappedClass* wclass = classes; wclass->name != 0; ++wclass) {
37 register_class(v, wclass);
42 void print_squirrel_stack(HSQUIRRELVM v)
44 printf("--------------------------------------------------------------\n");
45 int count = sq_gettop(v);
46 for(int i = 1; i <= count; ++i) {
48 switch(sq_gettype(v, i))
55 sq_getinteger(v, i, &val);
56 printf("integer (%d)", val);
61 sq_getfloat(v, i, &val);
62 printf("float (%f)", val);
67 sq_getstring(v, i, &val);
68 printf("string (%s)", val);
81 printf("closure(function)");
83 case OT_NATIVECLOSURE:
84 printf("native closure(C function)");
90 printf("userpointer");
104 printf("--------------------------------------------------------------\n");
107 void expose_object(HSQUIRRELVM v, void* object, const char* type,
110 // part1 of registration of the instance in the root table
112 sq_pushstring(v, name, -1);
114 // resolve class name
116 sq_pushstring(v, type, -1);
117 if(sq_get(v, -2) < 0) {
118 std::ostringstream msg;
119 msg << "Couldn't resolve squirrel type '" << type << "'.";
120 throw std::runtime_error(msg.str());
122 sq_remove(v, -2); // remove roottable
124 // create an instance and set pointer to c++ object
125 if(sq_createinstance(v, -1) < 0 || sq_setinstanceup(v, -1, object)) {
126 std::ostringstream msg;
127 msg << "Couldn't setup squirrel instance for object '"
128 << name << "' of type '" << type << "'.";
129 throw SquirrelError(v, msg.str());
132 sq_remove(v, -2); // remove class from stack
134 // part2 of registration of the instance in the root table
135 if(sq_createslot(v, -3) < 0)
136 throw SquirrelError(v, "Couldn't register object in squirrel root table");
140 //----------------------------------------------------------------------------
142 SquirrelError::SquirrelError(HSQUIRRELVM v, const std::string& message) throw()
144 std::ostringstream msg;
145 msg << "SQuirrel error: " << message << " (";
148 sq_getstring(v, -1, &lasterr);
150 msg << lasterr << ")";
151 this->message = msg.str();
154 SquirrelError::~SquirrelError() throw()
158 SquirrelError::what() const throw()
160 return message.c_str();