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";
59 << " * WARNING: This file is automatically generated from:\n"
60 << " * '" << fromfile << "'\n"
61 << " * DO NOT CHANGE\n"
63 << "#include <config.h>\n"
66 << "#include <assert.h>\n"
67 << "#include <string>\n"
68 << "#include <sstream>\n"
69 << "#include <squirrel.h>\n"
70 << "#include \"squirrel_error.hpp\"\n"
71 << "#include \"wrapper.interface.hpp\"\n"
73 << "namespace Scripting\n"
75 << "namespace Wrapper\n"
79 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
80 i != ns->types.end(); ++i) {
81 AtomicType* type = *i;
82 Class* _class = dynamic_cast<Class*> (type);
84 create_class_wrapper(_class);
86 for(std::vector<Function*>::iterator i = ns->functions.begin();
87 i != ns->functions.end(); ++i) {
88 create_function_wrapper(0, *i);
91 out << "} // end of namespace Wrapper\n";
93 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
94 i != ns->types.end(); ++i) {
95 AtomicType* type = *i;
96 Class* _class = dynamic_cast<Class*> (type);
98 create_squirrel_instance(_class);
101 out << "void register_" << modulename << "_wrapper(HSQUIRRELVM v)\n"
103 << ind << "using namespace Wrapper;\n"
106 create_register_constants_code(ns);
107 create_register_functions_code(ns);
108 create_register_classes_code(ns);
112 << "} // 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 SQInteger ";
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 << "SQUserPointer data;\n";
276 out << ind << "if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) {\n";
277 out << ind << ind << "sq_throwerror(vm, _SC(\"'" << function->name << "' called without instance\"));\n";
278 out << ind << ind << "return SQ_ERROR;\n";
280 out << ind << ns_prefix << _class->name << "* _this = reinterpret_cast<" << ns_prefix << _class->name << "*> (data);\n";
284 if(function->custom) {
285 if(function->type != Function::FUNCTION)
286 throw std::runtime_error(
287 "custom not allow constructor+destructor yet");
288 if(function->return_type.atomic_type != SQIntegerType::instance())
289 throw std::runtime_error("custom function has to return SQInteger");
290 if(function->parameters.size() != 1)
291 throw std::runtime_error(
292 "custom function must have 1 HSQUIRRELVM parameter");
294 out << ind << "return ";
299 out << function->name << "(vm);\n";
305 // declare and retrieve arguments
308 for(std::vector<Parameter>::iterator p = function->parameters.begin();
309 p != function->parameters.end(); ++p) {
310 if(i == 0 && p->type.atomic_type == HSQUIRRELVMType::instance()) {
311 out << ind << "HSQUIRRELVM arg0 = vm;\n";
315 snprintf(argname, sizeof(argname), "arg%d", i);
316 prepare_argument(p->type, i + arg_offset, argname);
323 out << ind << "try {\n";
325 if(!function->return_type.is_void()) {
326 function->return_type.write_c_type(out);
327 out << " return_value = ";
330 if(function->type == Function::CONSTRUCTOR) {
331 out << ns_prefix << _class->name << "* _this = new " << ns_prefix;
338 if(function->type == Function::CONSTRUCTOR) {
339 out << _class->name << "(";
341 out << function->name << "(";
343 for(size_t i = 0; i < function->parameters.size(); ++i) {
346 const Parameter param = function->parameters[i];
347 if(param.type.ref == 0 && param.type.pointer == 0) {
348 if(param.type.atomic_type == &BasicType::INT)
349 out << "static_cast<int> (arg" << i << ")";
350 else if(param.type.atomic_type == &BasicType::FLOAT)
351 out << "static_cast<float> (arg" << i << ")";
352 else if(param.type.atomic_type == &BasicType::BOOL)
353 out << "arg" << i << " == SQTrue";
361 if(function->type == Function::CONSTRUCTOR) {
362 out << ind << "if(SQ_FAILED(sq_setinstanceup(vm, 1, _this))) {\n";
363 out << ind << ind << "sq_throwerror(vm, _SC(\"Couldn't setup instance of '" << _class->name << "' class\"));\n";
364 out << ind << ind << "return SQ_ERROR;\n";
366 out << ind << "sq_setreleasehook(vm, 1, "
367 << _class->name << "_release_hook);\n";
370 // push return value back on stack and return
371 if(function->suspend) {
372 if(!function->return_type.is_void()) {
373 std::stringstream msg;
374 msg << "Function '" << function->name << "' declared as suspend"
375 << " but has a return value.";
376 throw std::runtime_error(msg.str());
378 out << ind << ind << "return sq_suspendvm(vm);\n";
379 } else if(function->return_type.is_void()) {
380 out << ind << ind << "return 0;\n";
382 push_to_stack(function->return_type, "return_value");
383 out << ind << ind << "return 1;\n";
387 out << ind << "} catch(std::exception& e) {\n";
388 out << ind << ind << "sq_throwerror(vm, e.what());\n";
389 out << ind << ind << "return SQ_ERROR;\n";
390 out << ind << "} catch(...) {\n";
391 out << ind << ind << "sq_throwerror(vm, _SC(\"Unexpected exception while executing function '" << function->name << "'\"));\n";
392 out << ind << ind << "return SQ_ERROR;\n";
401 WrapperCreator::prepare_argument(const Type& type, size_t index,
402 const std::string& var)
404 if(type.ref > 0 && type.atomic_type != StringType::instance())
405 throw std::runtime_error("References not handled yet");
407 throw std::runtime_error("Pointers not handled yet");
408 if(type.atomic_type == &BasicType::INT) {
409 out << ind << "SQInteger " << var << ";\n";
410 out << ind << "if(SQ_FAILED(sq_getinteger(vm, " << index << ", &" << var << "))) {\n";
411 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not an integer\"));\n";
412 out << ind << ind << "return SQ_ERROR;\n";
414 } else if(type.atomic_type == &BasicType::FLOAT) {
415 out << ind << "SQFloat " << var << ";\n";
416 out << ind << "if(SQ_FAILED(sq_getfloat(vm, " << index << ", &" << var << "))) {\n";
417 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a float\"));\n";
418 out << ind << ind << "return SQ_ERROR;\n";
420 } else if(type.atomic_type == &BasicType::BOOL) {
421 out << ind << "SQBool " << var << ";\n";
422 out << ind << "if(SQ_FAILED(sq_getbool(vm, " << index << ", &" << var << "))) {\n";
423 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a bool\"));\n";
424 out << ind << ind << "return SQ_ERROR;\n";
426 } else if(type.atomic_type == StringType::instance()) {
427 out << ind << "const SQChar* " << var << ";\n";
428 out << ind << "if(SQ_FAILED(sq_getstring(vm, " << index << ", &" << var << "))) {\n";
429 out << ind << ind << "sq_throwerror(vm, _SC(\"Argument " << (index-1) << " not a string\"));\n";
430 out << ind << ind << "return SQ_ERROR;\n";
433 std::ostringstream msg;
434 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
435 throw std::runtime_error(msg.str());
440 WrapperCreator::push_to_stack(const Type& type, const std::string& var)
442 if(type.ref > 0 && type.atomic_type != StringType::instance())
443 throw std::runtime_error("References not handled yet");
445 throw std::runtime_error("Pointers not handled yet");
447 if(type.atomic_type == &BasicType::INT) {
448 out << "sq_pushinteger(vm, " << var << ");\n";
449 } else if(type.atomic_type == &BasicType::FLOAT) {
450 out << "sq_pushfloat(vm, " << var << ");\n";
451 } else if(type.atomic_type == &BasicType::BOOL) {
452 out << "sq_pushbool(vm, " << var << ");\n";
453 } else if(type.atomic_type == StringType::instance()) {
454 out << "sq_pushstring(vm, " << var << ".c_str(), "
455 << var << ".size());\n";
457 std::ostringstream msg;
458 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
459 throw std::runtime_error(msg.str());
464 WrapperCreator::create_class_wrapper(Class* _class)
466 create_class_release_hook(_class);
467 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
468 i != _class->members.end(); ++i) {
469 ClassMember* member = *i;
470 if(member->visibility != ClassMember::PUBLIC)
472 Function* function = dynamic_cast<Function*> (member);
475 // don't wrap destructors
476 if(function->type == Function::DESTRUCTOR)
478 create_function_wrapper(_class, function);
483 WrapperCreator::create_squirrel_instance(Class* _class)
485 out << "void create_squirrel_instance(HSQUIRRELVM v, "
486 << ns_prefix << _class->name
487 << "* object, bool setup_releasehook)\n"
489 << ind << "using namespace Wrapper;\n"
491 << ind << "sq_pushroottable(v);\n"
492 << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n"
493 << ind << "if(SQ_FAILED(sq_get(v, -2))) {\n"
494 << ind << ind << "std::ostringstream msg;\n"
495 << ind << ind << "msg << \"Couldn't resolved squirrel type '"
496 << _class->name << "'\";\n"
497 << ind << ind << "throw SquirrelError(v, msg.str());\n"
500 << ind << "if(SQ_FAILED(sq_createinstance(v, -1)) || "
501 << "SQ_FAILED(sq_setinstanceup(v, -1, object))) {\n"
502 << ind << ind << "std::ostringstream msg;\n"
503 << ind << ind << "msg << \"Couldn't setup squirrel instance for "
504 << "object of type '" << _class->name << "'\";\n"
505 << ind << ind << "throw SquirrelError(v, msg.str());\n"
507 << ind << "sq_remove(v, -2); // remove object name\n"
509 << ind << "if(setup_releasehook) {\n"
510 << ind << ind << "sq_setreleasehook(v, -1, "
511 << _class->name << "_release_hook);\n"
514 << ind << "sq_remove(v, -2); // remove root table\n"
520 WrapperCreator::create_class_release_hook(Class* _class)
522 out << "static SQInteger " << _class->name << "_release_hook(SQUserPointer ptr, SQInteger )\n"
524 << ind << ns_prefix << _class->name
525 << "* _this = reinterpret_cast<" << ns_prefix << _class->name
527 << ind << "delete _this;\n"
528 << ind << "return 0;\n"