5 #include "create_wrapper.hpp"
9 WrapperCreator::create_wrapper(Namespace* ns)
11 std::string fromfile = original_file != "" ? original_file : inputfile;
13 if(selected_namespace != "") {
14 ns_prefix = selected_namespace;
21 << " * WARNING: This file is automatically generated from:\n"
22 << " * '" << fromfile << "'\n"
23 << " * DO NOT CHANGE\n"
25 << "#ifndef __" << modulename << "_WRAPPER_H__\n"
26 << "#define __" << modulename << "_WRAPPER_H__\n"
28 << "#include <squirrel.h>\n"
29 << "#include \"wrapper.interface.hpp\"\n"
31 << "namespace Scripting\n"
35 hppout << "void register_" << modulename << "_wrapper(HSQUIRRELVM v);\n"
38 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
39 i != ns->types.end(); ++i) {
40 AtomicType* type = *i;
41 Class* _class = dynamic_cast<Class*> (type);
45 hppout << "void create_squirrel_instance(HSQUIRRELVM v, "
46 << ns_prefix << _class->name
47 << "* object, bool setup_releasehook = false);\n";
57 << " * WARNING: This file is automatically generated from:\n"
58 << " * '" << fromfile << "'\n"
59 << " * DO NOT CHANGE\n"
61 << "#include <config.h>\n"
64 << "#include <assert.h>\n"
65 << "#include <string>\n"
66 << "#include <sstream>\n"
67 << "#include <squirrel.h>\n"
68 << "#include \"squirrel_error.hpp\"\n"
69 << "#include \"wrapper.interface.hpp\"\n"
71 << "namespace Scripting\n"
73 << "namespace Wrapper\n"
77 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
78 i != ns->types.end(); ++i) {
79 AtomicType* type = *i;
80 Class* _class = dynamic_cast<Class*> (type);
82 create_class_wrapper(_class);
84 for(std::vector<Function*>::iterator i = ns->functions.begin();
85 i != ns->functions.end(); ++i) {
86 create_function_wrapper(0, *i);
89 out << "} // end of namespace Wrapper\n";
92 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
93 i != ns->types.end(); ++i) {
94 AtomicType* type = *i;
95 Class* _class = dynamic_cast<Class*> (type);
97 create_squirrel_instance(_class);
100 out << "void register_" << modulename << "_wrapper(HSQUIRRELVM v)\n"
102 << ind << "using namespace Wrapper;\n"
105 create_register_constants_code(ns);
106 create_register_functions_code(ns);
107 create_register_classes_code(ns);
111 << "} // end of namespace Scripting\n"
116 WrapperCreator::create_register_function_code(Function* function, Class* _class)
118 if(function->type == Function::DESTRUCTOR)
121 out << ind << "sq_pushstring(v, \"" << function->name << "\", -1);\n";
122 out << ind << "sq_newclosure(v, &"
123 << (_class != 0 ? _class->name + "_" : "") << function->name
124 << "_wrapper, 0);\n";
125 create_register_slot_code("function", function->name);
130 WrapperCreator::create_register_functions_code(Namespace* ns)
132 for(std::vector<Function*>::iterator i = ns->functions.begin();
133 i != ns->functions.end(); ++i) {
134 Function* function = *i;
135 create_register_function_code(function, 0);
140 WrapperCreator::create_register_classes_code(Namespace* ns)
142 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
143 i != ns->types.end(); ++i) {
144 AtomicType* type = *i;
145 Class* _class = dynamic_cast<Class*> (type);
148 if(_class->super_classes.size() > 0)
151 create_register_class_code(_class);
156 WrapperCreator::create_register_class_code(Class* _class)
158 out << ind << "// Register class " << _class->name << "\n";
159 out << ind << "sq_pushstring(v, \""
160 << _class->name << "\", -1);\n";
162 if(_class->super_classes.size() > 0) {
163 if(_class->super_classes.size() > 1) {
164 std::ostringstream msg;
165 msg << "Multiple inheritance not supported (at class '"
166 << _class->name << "')";
167 throw std::runtime_error(msg.str());
170 out << ind << "sq_pushstring(v, \""
171 << _class->super_classes[0]->name << "\", -1);\n";
172 out << ind << "sq_get(v, -3);\n";
174 out << ind << "if(sq_newclass(v, "
175 << (_class->super_classes.size() > 0 ? "SQTrue" : "SQFalse")
177 out << ind << ind << "std::ostringstream msg;\n";
178 out << ind << ind << "msg << \"Couldn't create new class '"
179 << _class->name << "'\";\n";
180 out << ind << ind << "throw SquirrelError(v, msg.str());\n";
183 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
184 i != _class->members.end(); ++i) {
185 ClassMember* member = *i;
186 if(member->visibility != ClassMember::PUBLIC)
188 Function* function = dynamic_cast<Function*> (member);
190 create_register_function_code(function, _class);
192 Field* field = dynamic_cast<Field*> (member);
194 create_register_constant_code(field);
198 create_register_slot_code("class", _class->name);
201 for(std::vector<Class*>::iterator i = _class->sub_classes.begin();
202 i != _class->sub_classes.end(); ++i) {
204 create_register_class_code(_class);
209 WrapperCreator::create_register_constants_code(Namespace* ns)
211 for(std::vector<Field*>::iterator i = ns->fields.begin();
212 i != ns->fields.end(); ++i) {
214 create_register_constant_code(field);
219 WrapperCreator::create_register_constant_code(Field* field)
221 if(!field->has_const_value)
223 out << ind << "sq_pushstring(v, \"" << field->name << "\", -1);\n";
224 if(field->type->atomic_type == &BasicType::INT) {
225 out << ind << "sq_pushinteger(v, " << field->const_int_value << ");\n";
226 } else if(field->type->atomic_type == &BasicType::FLOAT) {
227 out << ind << "sq_pushfloat(v, " << field->const_float_value << ");\n";
228 } else if(field->type->atomic_type == StringType::instance()) {
229 out << ind << "sq_pushstring(v, \""
230 << field->const_string_value << "\", -1);\n";
232 throw std::runtime_error("Constant is not int, float or string");
234 create_register_slot_code("constant", field->name);
239 WrapperCreator::create_register_slot_code(const std::string& what,
240 const std::string& name)
242 out << ind << "if(SQ_FAILED(sq_createslot(v, -3))) {\n";
243 out << ind << ind << "throw SquirrelError(v, \""
244 << "Couldn't register " << what << " '" << name << "'\");\n";
249 WrapperCreator::create_function_wrapper(Class* _class, Function* function)
251 if(function->type == Function::DESTRUCTOR)
254 std::string ns_prefix;
255 if(selected_namespace != "")
256 ns_prefix = selected_namespace + "::";
257 if(function->type == Function::CONSTRUCTOR)
258 function->name = "constructor";
260 out << "static int ";
262 out << _class->name << "_";
264 out << function->name << "_wrapper(HSQUIRRELVM vm)\n"
267 if(_class == 0 && function->parameters.empty()
268 && function->return_type.is_void()
269 && function->type != Function::CONSTRUCTOR) {
270 out << ind << "(void) vm;\n";
273 // retrieve pointer to class instance
274 if(_class != 0 && function->type != Function::CONSTRUCTOR) {
275 out << ind << ns_prefix << _class->name << "* _this;\n";
276 out << ind << "if(SQ_FAILED(sq_getinstanceup(vm, 1, reinterpret_cast<SQUserPointer*> (&_this), 0))) {\n";
277 out << ind << ind << "sq_throwerror(vm, _SC(\"'" << function->name << "' called without instance\"));\n";
278 out << ind << ind << "return SQ_ERROR;\n";
283 if(function->custom) {
284 if(function->type != Function::FUNCTION)
285 throw std::runtime_error(
286 "custom not allow constructor+destructor yet");
287 if(function->return_type.atomic_type != &BasicType::INT)
288 throw std::runtime_error("custom function has to return int");
289 if(function->parameters.size() != 1)
290 throw std::runtime_error(
291 "custom function must have 1 HSQUIRRELVM parameter");
293 out << ind << "return ";
298 out << function->name << "(vm);\n";
304 // declare and retrieve arguments
307 for(std::vector<Parameter>::iterator p = function->parameters.begin();
308 p != function->parameters.end(); ++p) {
309 if(i == 0 && p->type.atomic_type == HSQUIRRELVMType::instance()) {
310 out << ind << "HSQUIRRELVM arg0 = vm;\n";
314 snprintf(argname, sizeof(argname), "arg%d", i);
315 prepare_argument(p->type, i + arg_offset, argname);
322 out << ind << "try {\n";
324 if(!function->return_type.is_void()) {
325 function->return_type.write_c_type(out);
326 out << " return_value = ";
329 if(function->type == Function::CONSTRUCTOR) {
330 out << ns_prefix << _class->name << "* _this = new " << ns_prefix;
337 if(function->type == Function::CONSTRUCTOR) {
338 out << _class->name << "(";
340 out << function->name << "(";
342 for(size_t i = 0; i < function->parameters.size(); ++i) {
348 if(function->type == Function::CONSTRUCTOR) {
349 out << ind << "if(SQ_FAILED(sq_setinstanceup(vm, 1, _this))) {\n";
350 out << ind << ind << "sq_throwerror(vm, _SC(\"Couldn't setup instance of '" << _class->name << "' class\"));\n";
351 out << ind << ind << "return SQ_ERROR;\n";
353 out << ind << "sq_setreleasehook(vm, 1, "
354 << _class->name << "_release_hook);\n";
357 // push return value back on stack and return
358 if(function->suspend) {
359 if(!function->return_type.is_void()) {
360 std::stringstream msg;
361 msg << "Function '" << function->name << "' declared as suspend"
362 << " but has a return value.";
363 throw std::runtime_error(msg.str());
365 out << ind << ind << "return sq_suspendvm(vm);\n";
366 } else if(function->return_type.is_void()) {
367 out << ind << ind << "return 0;\n";
369 push_to_stack(function->return_type, "return_value");
370 out << ind << ind << "return 1;\n";
374 out << ind << "} catch(std::exception& e) {\n";
375 out << ind << ind << "sq_throwerror(vm, e.what());\n";
376 out << ind << ind << "return SQ_ERROR;\n";
377 out << ind << "} catch(...) {\n";
378 out << ind << ind << "sq_throwerror(vm, _SC(\"Unexpected exception while executing function '" << function->name << "'\"));\n";
379 out << ind << ind << "return SQ_ERROR;\n";
388 WrapperCreator::prepare_argument(const Type& type, size_t index,
389 const std::string& var)
391 if(type.ref > 0 && type.atomic_type != StringType::instance())
392 throw std::runtime_error("References not handled yet");
394 throw std::runtime_error("Pointers not handled yet");
395 if(type.atomic_type == &BasicType::INT) {
396 out << ind << "int " << var << ";\n";
397 out << ind << "if(SQ_FAILED(sq_getinteger(vm, " << index << ", &" << var << "))) {\n";
398 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not an integer\"));\n";
399 out << ind << ind << "return SQ_ERROR;\n";
401 } else if(type.atomic_type == &BasicType::FLOAT) {
402 out << ind << "float " << var << ";\n";
403 out << ind << "if(SQ_FAILED(sq_getfloat(vm, " << index << ", &" << var << "))) {\n";
404 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a float\"));\n";
405 out << ind << ind << "return SQ_ERROR;\n";
407 } else if(type.atomic_type == &BasicType::BOOL) {
408 out << ind << "SQBool " << var << ";\n";
409 out << ind << "if(SQ_FAILED(sq_getbool(vm, " << index << ", &" << var << "))) {\n";
410 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a bool\"));\n";
411 out << ind << ind << "return SQ_ERROR;\n";
413 } else if(type.atomic_type == StringType::instance()) {
414 out << ind << "const char* " << var << ";\n";
415 out << ind << "if(SQ_FAILED(sq_getstring(vm, " << index << ", &" << var << "))) {\n";
416 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a string\"));\n";
417 out << ind << ind << "return SQ_ERROR;\n";
420 std::ostringstream msg;
421 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
422 throw std::runtime_error(msg.str());
427 WrapperCreator::push_to_stack(const Type& type, const std::string& var)
429 if(type.ref > 0 && type.atomic_type != StringType::instance())
430 throw std::runtime_error("References not handled yet");
432 throw std::runtime_error("Pointers not handled yet");
434 if(type.atomic_type == &BasicType::INT) {
435 out << "sq_pushinteger(vm, " << var << ");\n";
436 } else if(type.atomic_type == &BasicType::FLOAT) {
437 out << "sq_pushfloat(vm, " << var << ");\n";
438 } else if(type.atomic_type == &BasicType::BOOL) {
439 out << "sq_pushbool(vm, " << var << ");\n";
440 } else if(type.atomic_type == StringType::instance()) {
441 out << "sq_pushstring(vm, " << var << ".c_str(), "
442 << var << ".size());\n";
444 std::ostringstream msg;
445 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
446 throw std::runtime_error(msg.str());
451 WrapperCreator::create_class_wrapper(Class* _class)
453 create_class_release_hook(_class);
454 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
455 i != _class->members.end(); ++i) {
456 ClassMember* member = *i;
457 if(member->visibility != ClassMember::PUBLIC)
459 Function* function = dynamic_cast<Function*> (member);
462 // don't wrap destructors
463 if(function->type == Function::DESTRUCTOR)
465 create_function_wrapper(_class, function);
470 WrapperCreator::create_squirrel_instance(Class* _class)
472 out << "void create_squirrel_instance(HSQUIRRELVM v, "
473 << ns_prefix << _class->name
474 << "* object, bool setup_releasehook)\n"
476 << ind << "using namespace Wrapper;\n"
478 << ind << "sq_pushroottable(v);\n"
479 << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n"
480 << ind << "if(SQ_FAILED(sq_get(v, -2))) {\n"
481 << ind << ind << "std::ostringstream msg;\n"
482 << ind << ind << "msg << \"Couldn't resolved squirrel type '"
483 << _class->name << "'\";\n"
484 << ind << ind << "throw SquirrelError(v, msg.str());\n"
487 << ind << "if(SQ_FAILED(sq_createinstance(v, -1)) || "
488 << "SQ_FAILED(sq_setinstanceup(v, -1, object))) {\n"
489 << ind << ind << "std::ostringstream msg;\n"
490 << ind << ind << "msg << \"Couldn't setup squirrel instance for "
491 << "object of type '" << _class->name << "'\";\n"
492 << ind << ind << "throw SquirrelError(v, msg.str());\n"
494 << ind << "sq_remove(v, -2); // remove object name\n"
496 << ind << "if(setup_releasehook) {\n"
497 << ind << ind << "sq_setreleasehook(v, -1, "
498 << _class->name << "_release_hook);\n"
501 << ind << "sq_remove(v, -2); // remove root table\n"
507 WrapperCreator::create_class_release_hook(Class* _class)
509 out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n"
511 << ind << ns_prefix << _class->name
512 << "* _this = reinterpret_cast<" << ns_prefix << _class->name
514 << ind << "delete _this;\n"
515 << ind << "return 0;\n"