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"
32 << "namespace Scripting\n"
36 hppout << "void register_" << modulename << "_wrapper(HSQUIRRELVM v);\n"
39 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
40 i != ns->types.end(); ++i) {
41 AtomicType* type = *i;
42 Class* _class = dynamic_cast<Class*> (type);
46 hppout << "class " << _class->name << ";\n";
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, " << function->parameter_spec << ");\n";
128 out << ind << "sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, \"";
132 if(!function->parameters.empty())
134 std::vector<Parameter>::iterator p = function->parameters.begin();
136 // Skip the first parameter since its a HSQUIRRELVM that is
137 // handled internally
138 if (function->suspend) {
140 } else if (p->type.atomic_type == HSQUIRRELVMType::instance()) {
144 for(; p != function->parameters.end(); ++p) {
145 if(p->type.atomic_type == &BasicType::INT) {
147 } else if(p->type.atomic_type == &BasicType::FLOAT) {
149 } else if(p->type.atomic_type == &BasicType::BOOL) {
151 } else if(p->type.atomic_type == StringType::instance()) {
161 create_register_slot_code("function", function->name);
166 WrapperCreator::create_register_functions_code(Namespace* ns)
168 for(std::vector<Function*>::iterator i = ns->functions.begin();
169 i != ns->functions.end(); ++i) {
170 Function* function = *i;
171 create_register_function_code(function, 0);
176 WrapperCreator::create_register_classes_code(Namespace* ns)
178 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
179 i != ns->types.end(); ++i) {
180 AtomicType* type = *i;
181 Class* _class = dynamic_cast<Class*> (type);
184 if(_class->super_classes.size() > 0)
187 create_register_class_code(_class);
192 WrapperCreator::create_register_class_code(Class* _class)
194 out << ind << "// Register class " << _class->name << "\n";
195 out << ind << "sq_pushstring(v, \""
196 << _class->name << "\", -1);\n";
198 if(_class->super_classes.size() > 0) {
199 if(_class->super_classes.size() > 1) {
200 std::ostringstream msg;
201 msg << "Multiple inheritance not supported (at class '"
202 << _class->name << "')";
203 throw std::runtime_error(msg.str());
206 out << ind << "sq_pushstring(v, \""
207 << _class->super_classes[0]->name << "\", -1);\n";
208 out << ind << "sq_get(v, -3);\n";
210 out << ind << "if(sq_newclass(v, "
211 << (_class->super_classes.size() > 0 ? "SQTrue" : "SQFalse")
213 out << ind << ind << "std::ostringstream msg;\n";
214 out << ind << ind << "msg << \"Couldn't create new class '"
215 << _class->name << "'\";\n";
216 out << ind << ind << "throw SquirrelError(v, msg.str());\n";
219 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
220 i != _class->members.end(); ++i) {
221 ClassMember* member = *i;
222 if(member->visibility != ClassMember::PUBLIC)
224 Function* function = dynamic_cast<Function*> (member);
226 create_register_function_code(function, _class);
228 Field* field = dynamic_cast<Field*> (member);
230 create_register_constant_code(field);
234 create_register_slot_code("class", _class->name);
237 for(std::vector<Class*>::iterator i = _class->sub_classes.begin();
238 i != _class->sub_classes.end(); ++i) {
240 create_register_class_code(_class);
245 WrapperCreator::create_register_constants_code(Namespace* ns)
247 for(std::vector<Field*>::iterator i = ns->fields.begin();
248 i != ns->fields.end(); ++i) {
250 create_register_constant_code(field);
255 WrapperCreator::create_register_constant_code(Field* field)
257 if(!field->has_const_value)
259 out << ind << "sq_pushstring(v, \"" << field->name << "\", -1);\n";
260 if(field->type->atomic_type == &BasicType::INT) {
261 out << ind << "sq_pushinteger(v, " << field->const_int_value << ");\n";
262 } else if(field->type->atomic_type == &BasicType::FLOAT) {
263 out << ind << "sq_pushfloat(v, " << field->const_float_value << ");\n";
264 } else if(field->type->atomic_type == StringType::instance()) {
265 out << ind << "sq_pushstring(v, \""
266 << field->const_string_value << "\", -1);\n";
268 throw std::runtime_error("Constant is not int, float or string");
270 create_register_slot_code("constant", field->name);
275 WrapperCreator::create_register_slot_code(const std::string& what,
276 const std::string& name)
278 out << ind << "if(SQ_FAILED(sq_createslot(v, -3))) {\n";
279 out << ind << ind << "throw SquirrelError(v, \""
280 << "Couldn't register " << what << " '" << name << "'\");\n";
285 WrapperCreator::create_function_wrapper(Class* _class, Function* function)
287 if(function->type == Function::DESTRUCTOR)
290 std::string ns_prefix;
291 if(selected_namespace != "")
292 ns_prefix = selected_namespace + "::";
293 if(function->type == Function::CONSTRUCTOR)
294 function->name = "constructor";
296 out << "static SQInteger ";
298 out << _class->name << "_";
300 out << function->name << "_wrapper(HSQUIRRELVM vm)\n"
303 if(_class == 0 && function->parameters.empty()
304 && function->return_type.is_void()
305 && function->type != Function::CONSTRUCTOR) {
306 out << ind << "(void) vm;\n";
309 // retrieve pointer to class instance
310 if(_class != 0 && function->type != Function::CONSTRUCTOR) {
311 out << ind << "SQUserPointer data;\n";
312 out << ind << "if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0)) || !data) {\n";
313 out << ind << ind << "sq_throwerror(vm, _SC(\"'" << function->name << "' called without instance\"));\n";
314 out << ind << ind << "return SQ_ERROR;\n";
316 out << ind << ns_prefix << _class->name << "* _this = reinterpret_cast<" << ns_prefix << _class->name << "*> (data);\n";
320 if(function->custom) {
321 if(function->type != Function::FUNCTION)
322 throw std::runtime_error(
323 "custom not allow constructor+destructor yet");
324 if(function->return_type.atomic_type != SQIntegerType::instance())
325 throw std::runtime_error("custom function '" + function->name + "' has to return SQInteger");
326 if(function->parameters.size() != 1)
327 throw std::runtime_error(
328 "custom function '" + function->name + "' must have 1 HSQUIRRELVM parameter");
330 out << ind << "return ";
335 out << function->name << "(vm);\n";
341 // declare and retrieve arguments
344 for(std::vector<Parameter>::iterator p = function->parameters.begin();
345 p != function->parameters.end(); ++p) {
346 if(i == 0 && p->type.atomic_type == HSQUIRRELVMType::instance()) {
347 out << ind << "HSQUIRRELVM arg0 = vm;\n";
351 snprintf(argname, sizeof(argname), "arg%d", i);
352 prepare_argument(p->type, i + arg_offset, argname);
359 out << ind << "try {\n";
361 if(!function->return_type.is_void()) {
362 function->return_type.write_c_type(out);
363 out << " return_value = ";
366 if(function->type == Function::CONSTRUCTOR) {
367 out << ns_prefix << _class->name << "* _this = new " << ns_prefix;
374 if(function->type == Function::CONSTRUCTOR) {
375 out << _class->name << "(";
377 out << function->name << "(";
379 for(size_t i = 0; i < function->parameters.size(); ++i) {
382 const Parameter param = function->parameters[i];
383 if(param.type.ref == 0 && param.type.pointer == 0) {
384 if(param.type.atomic_type == &BasicType::INT)
385 out << "static_cast<int> (arg" << i << ")";
386 else if(param.type.atomic_type == &BasicType::FLOAT)
387 out << "static_cast<float> (arg" << i << ")";
388 else if(param.type.atomic_type == &BasicType::BOOL)
389 out << "arg" << i << " == SQTrue";
397 if(function->type == Function::CONSTRUCTOR) {
398 out << ind << "if(SQ_FAILED(sq_setinstanceup(vm, 1, _this))) {\n";
399 out << ind << ind << "sq_throwerror(vm, _SC(\"Couldn't setup instance of '" << _class->name << "' class\"));\n";
400 out << ind << ind << "return SQ_ERROR;\n";
402 out << ind << "sq_setreleasehook(vm, 1, "
403 << _class->name << "_release_hook);\n";
406 // push return value back on stack and return
407 if(function->suspend) {
408 if(!function->return_type.is_void()) {
409 std::stringstream msg;
410 msg << "Function '" << function->name << "' declared as suspend"
411 << " but has a return value.";
412 throw std::runtime_error(msg.str());
414 out << ind << ind << "return sq_suspendvm(vm);\n";
415 } else if(function->return_type.is_void()) {
416 out << ind << ind << "return 0;\n";
418 push_to_stack(function->return_type, "return_value");
419 out << ind << ind << "return 1;\n";
423 out << ind << "} catch(std::exception& e) {\n";
424 out << ind << ind << "sq_throwerror(vm, e.what());\n";
425 out << ind << ind << "return SQ_ERROR;\n";
426 out << ind << "} catch(...) {\n";
427 out << ind << ind << "sq_throwerror(vm, _SC(\"Unexpected exception while executing function '" << function->name << "'\"));\n";
428 out << ind << ind << "return SQ_ERROR;\n";
437 WrapperCreator::prepare_argument(const Type& type, size_t index,
438 const std::string& var)
440 if(type.ref > 0 && type.atomic_type != StringType::instance())
441 throw std::runtime_error("References not handled yet");
443 throw std::runtime_error("Pointers not handled yet");
444 if(type.atomic_type == &BasicType::INT) {
445 out << ind << "SQInteger " << var << ";\n";
446 out << ind << "if(SQ_FAILED(sq_getinteger(vm, " << index << ", &" << var << "))) {\n";
447 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not an integer\"));\n";
448 out << ind << ind << "return SQ_ERROR;\n";
450 } else if(type.atomic_type == &BasicType::FLOAT) {
451 out << ind << "SQFloat " << var << ";\n";
452 out << ind << "if(SQ_FAILED(sq_getfloat(vm, " << index << ", &" << var << "))) {\n";
453 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a float\"));\n";
454 out << ind << ind << "return SQ_ERROR;\n";
456 } else if(type.atomic_type == &BasicType::BOOL) {
457 out << ind << "SQBool " << var << ";\n";
458 out << ind << "if(SQ_FAILED(sq_getbool(vm, " << index << ", &" << var << "))) {\n";
459 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a bool\"));\n";
460 out << ind << ind << "return SQ_ERROR;\n";
462 } else if(type.atomic_type == StringType::instance()) {
463 out << ind << "const SQChar* " << var << ";\n";
464 out << ind << "if(SQ_FAILED(sq_getstring(vm, " << index << ", &" << var << "))) {\n";
465 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a string\"));\n";
466 out << ind << ind << "return SQ_ERROR;\n";
469 std::ostringstream msg;
470 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
471 throw std::runtime_error(msg.str());
476 WrapperCreator::push_to_stack(const Type& type, const std::string& var)
478 if(type.ref > 0 && type.atomic_type != StringType::instance())
479 throw std::runtime_error("References not handled yet");
481 throw std::runtime_error("Pointers not handled yet");
483 if(type.atomic_type == &BasicType::INT) {
484 out << "sq_pushinteger(vm, " << var << ");\n";
485 } else if(type.atomic_type == &BasicType::FLOAT) {
486 out << "sq_pushfloat(vm, " << var << ");\n";
487 } else if(type.atomic_type == &BasicType::BOOL) {
488 out << "sq_pushbool(vm, " << var << ");\n";
489 } else if(type.atomic_type == StringType::instance()) {
490 out << "sq_pushstring(vm, " << var << ".c_str(), "
491 << var << ".size());\n";
493 std::ostringstream msg;
494 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
495 throw std::runtime_error(msg.str());
500 WrapperCreator::create_class_wrapper(Class* _class)
502 create_class_release_hook(_class);
503 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
504 i != _class->members.end(); ++i) {
505 ClassMember* member = *i;
506 if(member->visibility != ClassMember::PUBLIC)
508 Function* function = dynamic_cast<Function*> (member);
511 // don't wrap destructors
512 if(function->type == Function::DESTRUCTOR)
514 create_function_wrapper(_class, function);
519 WrapperCreator::create_squirrel_instance(Class* _class)
521 out << "void create_squirrel_instance(HSQUIRRELVM v, "
522 << ns_prefix << _class->name
523 << "* object, bool setup_releasehook)\n"
525 << ind << "using namespace Wrapper;\n"
527 << ind << "sq_pushroottable(v);\n"
528 << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n"
529 << ind << "if(SQ_FAILED(sq_get(v, -2))) {\n"
530 << ind << ind << "std::ostringstream msg;\n"
531 << ind << ind << "msg << \"Couldn't resolved squirrel type '"
532 << _class->name << "'\";\n"
533 << ind << ind << "throw SquirrelError(v, msg.str());\n"
536 << ind << "if(SQ_FAILED(sq_createinstance(v, -1)) || "
537 << "SQ_FAILED(sq_setinstanceup(v, -1, object))) {\n"
538 << ind << ind << "std::ostringstream msg;\n"
539 << ind << ind << "msg << \"Couldn't setup squirrel instance for "
540 << "object of type '" << _class->name << "'\";\n"
541 << ind << ind << "throw SquirrelError(v, msg.str());\n"
543 << ind << "sq_remove(v, -2); // remove object name\n"
545 << ind << "if(setup_releasehook) {\n"
546 << ind << ind << "sq_setreleasehook(v, -1, "
547 << _class->name << "_release_hook);\n"
550 << ind << "sq_remove(v, -2); // remove root table\n"
556 WrapperCreator::create_class_release_hook(Class* _class)
558 out << "static SQInteger " << _class->name << "_release_hook(SQUserPointer ptr, SQInteger )\n"
560 << ind << ns_prefix << _class->name
561 << "* _this = reinterpret_cast<" << ns_prefix << _class->name
563 << ind << "delete _this;\n"
564 << ind << "return 0;\n"