7 #include "create_wrapper.hpp"
11 WrapperCreator::create_wrapper(Namespace* ns)
13 std::string fromfile = original_file != "" ? original_file : inputfile;
15 if(selected_namespace != "") {
16 ns_prefix = selected_namespace;
23 << " * WARNING: This file is automatically generated from:\n"
24 << " * '" << fromfile << "'\n"
25 << " * DO NOT CHANGE\n"
27 << "#ifndef __" << modulename << "_WRAPPER_H__\n"
28 << "#define __" << modulename << "_WRAPPER_H__\n"
30 << "#include <squirrel.h>\n"
31 << "#include \"wrapper.interface.hpp\"\n"
33 << "namespace Scripting\n"
37 hppout << "void register_" << modulename << "_wrapper(HSQUIRRELVM v);\n"
40 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
41 i != ns->types.end(); ++i) {
42 AtomicType* type = *i;
43 Class* _class = dynamic_cast<Class*> (type);
47 hppout << "void create_squirrel_instance(HSQUIRRELVM v, "
48 << ns_prefix << _class->name
49 << "* object, bool setup_releasehook = false);\n";
58 << " * WARNING: This file is automatically generated from:\n"
59 << " * '" << fromfile << "'\n"
60 << " * DO NOT CHANGE\n"
62 << "#include <config.h>\n"
65 << "#include <assert.h>\n"
66 << "#include <string>\n"
67 << "#include <sstream>\n"
68 << "#include <squirrel.h>\n"
69 << "#include \"squirrel_error.hpp\"\n"
70 << "#include \"wrapper.interface.hpp\"\n"
72 << "namespace Scripting\n"
74 << "namespace Wrapper\n"
78 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
79 i != ns->types.end(); ++i) {
80 AtomicType* type = *i;
81 Class* _class = dynamic_cast<Class*> (type);
83 create_class_wrapper(_class);
85 for(std::vector<Function*>::iterator i = ns->functions.begin();
86 i != ns->functions.end(); ++i) {
87 create_function_wrapper(0, *i);
90 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";
115 WrapperCreator::create_register_function_code(Function* function, Class* _class)
117 if(function->type == Function::DESTRUCTOR)
120 out << ind << "sq_pushstring(v, \"" << function->name << "\", -1);\n";
121 out << ind << "sq_newclosure(v, &"
122 << (_class != 0 ? _class->name + "_" : "") << function->name
123 << "_wrapper, 0);\n";
125 if(!function->custom) {
126 out << ind << "sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, \"";
130 if(!function->parameters.empty())
132 std::vector<Parameter>::iterator p = function->parameters.begin();
134 // Skip the first parameter since its a HSQUIRRELVM that is
135 // handled internally
136 if (function->suspend)
139 for(; p != function->parameters.end(); ++p) {
140 if(p->type.atomic_type == &BasicType::INT) {
142 } else if(p->type.atomic_type == &BasicType::FLOAT) {
144 } else if(p->type.atomic_type == &BasicType::BOOL) {
146 } else if(p->type.atomic_type == StringType::instance()) {
156 create_register_slot_code("function", function->name);
161 WrapperCreator::create_register_functions_code(Namespace* ns)
163 for(std::vector<Function*>::iterator i = ns->functions.begin();
164 i != ns->functions.end(); ++i) {
165 Function* function = *i;
166 create_register_function_code(function, 0);
171 WrapperCreator::create_register_classes_code(Namespace* ns)
173 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
174 i != ns->types.end(); ++i) {
175 AtomicType* type = *i;
176 Class* _class = dynamic_cast<Class*> (type);
179 if(_class->super_classes.size() > 0)
182 create_register_class_code(_class);
187 WrapperCreator::create_register_class_code(Class* _class)
189 out << ind << "// Register class " << _class->name << "\n";
190 out << ind << "sq_pushstring(v, \""
191 << _class->name << "\", -1);\n";
193 if(_class->super_classes.size() > 0) {
194 if(_class->super_classes.size() > 1) {
195 std::ostringstream msg;
196 msg << "Multiple inheritance not supported (at class '"
197 << _class->name << "')";
198 throw std::runtime_error(msg.str());
201 out << ind << "sq_pushstring(v, \""
202 << _class->super_classes[0]->name << "\", -1);\n";
203 out << ind << "sq_get(v, -3);\n";
205 out << ind << "if(sq_newclass(v, "
206 << (_class->super_classes.size() > 0 ? "SQTrue" : "SQFalse")
208 out << ind << ind << "std::ostringstream msg;\n";
209 out << ind << ind << "msg << \"Couldn't create new class '"
210 << _class->name << "'\";\n";
211 out << ind << ind << "throw SquirrelError(v, msg.str());\n";
214 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
215 i != _class->members.end(); ++i) {
216 ClassMember* member = *i;
217 if(member->visibility != ClassMember::PUBLIC)
219 Function* function = dynamic_cast<Function*> (member);
221 create_register_function_code(function, _class);
223 Field* field = dynamic_cast<Field*> (member);
225 create_register_constant_code(field);
229 create_register_slot_code("class", _class->name);
232 for(std::vector<Class*>::iterator i = _class->sub_classes.begin();
233 i != _class->sub_classes.end(); ++i) {
235 create_register_class_code(_class);
240 WrapperCreator::create_register_constants_code(Namespace* ns)
242 for(std::vector<Field*>::iterator i = ns->fields.begin();
243 i != ns->fields.end(); ++i) {
245 create_register_constant_code(field);
250 WrapperCreator::create_register_constant_code(Field* field)
252 if(!field->has_const_value)
254 out << ind << "sq_pushstring(v, \"" << field->name << "\", -1);\n";
255 if(field->type->atomic_type == &BasicType::INT) {
256 out << ind << "sq_pushinteger(v, " << field->const_int_value << ");\n";
257 } else if(field->type->atomic_type == &BasicType::FLOAT) {
258 out << ind << "sq_pushfloat(v, " << field->const_float_value << ");\n";
259 } else if(field->type->atomic_type == StringType::instance()) {
260 out << ind << "sq_pushstring(v, \""
261 << field->const_string_value << "\", -1);\n";
263 throw std::runtime_error("Constant is not int, float or string");
265 create_register_slot_code("constant", field->name);
270 WrapperCreator::create_register_slot_code(const std::string& what,
271 const std::string& name)
273 out << ind << "if(SQ_FAILED(sq_createslot(v, -3))) {\n";
274 out << ind << ind << "throw SquirrelError(v, \""
275 << "Couldn't register " << what << " '" << name << "'\");\n";
280 WrapperCreator::create_function_wrapper(Class* _class, Function* function)
282 if(function->type == Function::DESTRUCTOR)
285 std::string ns_prefix;
286 if(selected_namespace != "")
287 ns_prefix = selected_namespace + "::";
288 if(function->type == Function::CONSTRUCTOR)
289 function->name = "constructor";
291 out << "static SQInteger ";
293 out << _class->name << "_";
295 out << function->name << "_wrapper(HSQUIRRELVM vm)\n"
298 if(_class == 0 && function->parameters.empty()
299 && function->return_type.is_void()
300 && function->type != Function::CONSTRUCTOR) {
301 out << ind << "(void) vm;\n";
304 // retrieve pointer to class instance
305 if(_class != 0 && function->type != Function::CONSTRUCTOR) {
306 out << ind << "SQUserPointer data;\n";
307 out << ind << "if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0)) || !data) {\n";
308 out << ind << ind << "sq_throwerror(vm, _SC(\"'" << function->name << "' called without instance\"));\n";
309 out << ind << ind << "return SQ_ERROR;\n";
311 out << ind << ns_prefix << _class->name << "* _this = reinterpret_cast<" << ns_prefix << _class->name << "*> (data);\n";
315 if(function->custom) {
316 if(function->type != Function::FUNCTION)
317 throw std::runtime_error(
318 "custom not allow constructor+destructor yet");
319 if(function->return_type.atomic_type != SQIntegerType::instance())
320 throw std::runtime_error("custom function has to return SQInteger");
321 if(function->parameters.size() != 1)
322 throw std::runtime_error(
323 "custom function must have 1 HSQUIRRELVM parameter");
325 out << ind << "return ";
330 out << function->name << "(vm);\n";
336 // declare and retrieve arguments
339 for(std::vector<Parameter>::iterator p = function->parameters.begin();
340 p != function->parameters.end(); ++p) {
341 if(i == 0 && p->type.atomic_type == HSQUIRRELVMType::instance()) {
342 out << ind << "HSQUIRRELVM arg0 = vm;\n";
346 snprintf(argname, sizeof(argname), "arg%d", i);
347 prepare_argument(p->type, i + arg_offset, argname);
354 out << ind << "try {\n";
356 if(!function->return_type.is_void()) {
357 function->return_type.write_c_type(out);
358 out << " return_value = ";
361 if(function->type == Function::CONSTRUCTOR) {
362 out << ns_prefix << _class->name << "* _this = new " << ns_prefix;
369 if(function->type == Function::CONSTRUCTOR) {
370 out << _class->name << "(";
372 out << function->name << "(";
374 for(size_t i = 0; i < function->parameters.size(); ++i) {
377 const Parameter param = function->parameters[i];
378 if(param.type.ref == 0 && param.type.pointer == 0) {
379 if(param.type.atomic_type == &BasicType::INT)
380 out << "static_cast<int> (arg" << i << ")";
381 else if(param.type.atomic_type == &BasicType::FLOAT)
382 out << "static_cast<float> (arg" << i << ")";
383 else if(param.type.atomic_type == &BasicType::BOOL)
384 out << "arg" << i << " == SQTrue";
392 if(function->type == Function::CONSTRUCTOR) {
393 out << ind << "if(SQ_FAILED(sq_setinstanceup(vm, 1, _this))) {\n";
394 out << ind << ind << "sq_throwerror(vm, _SC(\"Couldn't setup instance of '" << _class->name << "' class\"));\n";
395 out << ind << ind << "return SQ_ERROR;\n";
397 out << ind << "sq_setreleasehook(vm, 1, "
398 << _class->name << "_release_hook);\n";
401 // push return value back on stack and return
402 if(function->suspend) {
403 if(!function->return_type.is_void()) {
404 std::stringstream msg;
405 msg << "Function '" << function->name << "' declared as suspend"
406 << " but has a return value.";
407 throw std::runtime_error(msg.str());
409 out << ind << ind << "return sq_suspendvm(vm);\n";
410 } else if(function->return_type.is_void()) {
411 out << ind << ind << "return 0;\n";
413 push_to_stack(function->return_type, "return_value");
414 out << ind << ind << "return 1;\n";
418 out << ind << "} catch(std::exception& e) {\n";
419 out << ind << ind << "sq_throwerror(vm, e.what());\n";
420 out << ind << ind << "return SQ_ERROR;\n";
421 out << ind << "} catch(...) {\n";
422 out << ind << ind << "sq_throwerror(vm, _SC(\"Unexpected exception while executing function '" << function->name << "'\"));\n";
423 out << ind << ind << "return SQ_ERROR;\n";
432 WrapperCreator::prepare_argument(const Type& type, size_t index,
433 const std::string& var)
435 if(type.ref > 0 && type.atomic_type != StringType::instance())
436 throw std::runtime_error("References not handled yet");
438 throw std::runtime_error("Pointers not handled yet");
439 if(type.atomic_type == &BasicType::INT) {
440 out << ind << "SQInteger " << var << ";\n";
441 out << ind << "if(SQ_FAILED(sq_getinteger(vm, " << index << ", &" << var << "))) {\n";
442 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not an integer\"));\n";
443 out << ind << ind << "return SQ_ERROR;\n";
445 } else if(type.atomic_type == &BasicType::FLOAT) {
446 out << ind << "SQFloat " << var << ";\n";
447 out << ind << "if(SQ_FAILED(sq_getfloat(vm, " << index << ", &" << var << "))) {\n";
448 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a float\"));\n";
449 out << ind << ind << "return SQ_ERROR;\n";
451 } else if(type.atomic_type == &BasicType::BOOL) {
452 out << ind << "SQBool " << var << ";\n";
453 out << ind << "if(SQ_FAILED(sq_getbool(vm, " << index << ", &" << var << "))) {\n";
454 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a bool\"));\n";
455 out << ind << ind << "return SQ_ERROR;\n";
457 } else if(type.atomic_type == StringType::instance()) {
458 out << ind << "const SQChar* " << var << ";\n";
459 out << ind << "if(SQ_FAILED(sq_getstring(vm, " << index << ", &" << var << "))) {\n";
460 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a string\"));\n";
461 out << ind << ind << "return SQ_ERROR;\n";
464 std::ostringstream msg;
465 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
466 throw std::runtime_error(msg.str());
471 WrapperCreator::push_to_stack(const Type& type, const std::string& var)
473 if(type.ref > 0 && type.atomic_type != StringType::instance())
474 throw std::runtime_error("References not handled yet");
476 throw std::runtime_error("Pointers not handled yet");
478 if(type.atomic_type == &BasicType::INT) {
479 out << "sq_pushinteger(vm, " << var << ");\n";
480 } else if(type.atomic_type == &BasicType::FLOAT) {
481 out << "sq_pushfloat(vm, " << var << ");\n";
482 } else if(type.atomic_type == &BasicType::BOOL) {
483 out << "sq_pushbool(vm, " << var << ");\n";
484 } else if(type.atomic_type == StringType::instance()) {
485 out << "sq_pushstring(vm, " << var << ".c_str(), "
486 << var << ".size());\n";
488 std::ostringstream msg;
489 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
490 throw std::runtime_error(msg.str());
495 WrapperCreator::create_class_wrapper(Class* _class)
497 create_class_release_hook(_class);
498 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
499 i != _class->members.end(); ++i) {
500 ClassMember* member = *i;
501 if(member->visibility != ClassMember::PUBLIC)
503 Function* function = dynamic_cast<Function*> (member);
506 // don't wrap destructors
507 if(function->type == Function::DESTRUCTOR)
509 create_function_wrapper(_class, function);
514 WrapperCreator::create_squirrel_instance(Class* _class)
516 out << "void create_squirrel_instance(HSQUIRRELVM v, "
517 << ns_prefix << _class->name
518 << "* object, bool setup_releasehook)\n"
520 << ind << "using namespace Wrapper;\n"
522 << ind << "sq_pushroottable(v);\n"
523 << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n"
524 << ind << "if(SQ_FAILED(sq_get(v, -2))) {\n"
525 << ind << ind << "std::ostringstream msg;\n"
526 << ind << ind << "msg << \"Couldn't resolved squirrel type '"
527 << _class->name << "'\";\n"
528 << ind << ind << "throw SquirrelError(v, msg.str());\n"
531 << ind << "if(SQ_FAILED(sq_createinstance(v, -1)) || "
532 << "SQ_FAILED(sq_setinstanceup(v, -1, object))) {\n"
533 << ind << ind << "std::ostringstream msg;\n"
534 << ind << ind << "msg << \"Couldn't setup squirrel instance for "
535 << "object of type '" << _class->name << "'\";\n"
536 << ind << ind << "throw SquirrelError(v, msg.str());\n"
538 << ind << "sq_remove(v, -2); // remove object name\n"
540 << ind << "if(setup_releasehook) {\n"
541 << ind << ind << "sq_setreleasehook(v, -1, "
542 << _class->name << "_release_hook);\n"
545 << ind << "sq_remove(v, -2); // remove root table\n"
551 WrapperCreator::create_class_release_hook(Class* _class)
553 out << "static SQInteger " << _class->name << "_release_hook(SQUserPointer ptr, SQInteger )\n"
555 << ind << ns_prefix << _class->name
556 << "* _this = reinterpret_cast<" << ns_prefix << _class->name
558 << ind << "delete _this;\n"
559 << ind << "return 0;\n"