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"
104 << ind << "sq_pushroottable(v);\n";
106 create_register_constants_code(ns);
107 create_register_functions_code(ns);
108 create_register_classes_code(ns);
110 out << ind << "sq_pop(v, 1);\n"
113 << "} // end of namespace Scripting\n"
118 WrapperCreator::create_register_function_code(Function* function, Class* _class)
120 if(function->type == Function::DESTRUCTOR)
123 out << ind << "sq_pushstring(v, \"" << function->name << "\", -1);\n";
124 out << ind << "sq_newclosure(v, &"
125 << (_class != 0 ? _class->name + "_" : "") << function->name
126 << "_wrapper, 0);\n";
127 create_register_slot_code("function", function->name);
132 WrapperCreator::create_register_functions_code(Namespace* ns)
134 for(std::vector<Function*>::iterator i = ns->functions.begin();
135 i != ns->functions.end(); ++i) {
136 Function* function = *i;
137 create_register_function_code(function, 0);
142 WrapperCreator::create_register_classes_code(Namespace* ns)
144 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
145 i != ns->types.end(); ++i) {
146 AtomicType* type = *i;
147 Class* _class = dynamic_cast<Class*> (type);
150 if(_class->super_classes.size() > 0)
153 create_register_class_code(_class);
158 WrapperCreator::create_register_class_code(Class* _class)
160 out << ind << "// Register class " << _class->name << "\n";
161 out << ind << "sq_pushstring(v, \""
162 << _class->name << "\", -1);\n";
164 if(_class->super_classes.size() > 0) {
165 if(_class->super_classes.size() > 1) {
166 std::ostringstream msg;
167 msg << "Multiple inheritance not supported (at class '"
168 << _class->name << "')";
169 throw std::runtime_error(msg.str());
172 out << ind << "sq_pushstring(v, \""
173 << _class->super_classes[0]->name << "\", -1);\n";
174 out << ind << "sq_get(v, -3);\n";
176 out << ind << "if(sq_newclass(v, "
177 << (_class->super_classes.size() > 0 ? "SQTrue" : "SQFalse")
179 out << ind << ind << "std::ostringstream msg;\n";
180 out << ind << ind << "msg << \"Couldn't create new class '"
181 << _class->name << "'\";\n";
182 out << ind << ind << "throw SquirrelError(v, msg.str());\n";
185 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
186 i != _class->members.end(); ++i) {
187 ClassMember* member = *i;
188 if(member->visibility != ClassMember::PUBLIC)
190 Function* function = dynamic_cast<Function*> (member);
192 create_register_function_code(function, _class);
194 Field* field = dynamic_cast<Field*> (member);
196 create_register_constant_code(field);
200 create_register_slot_code("class", _class->name);
203 for(std::vector<Class*>::iterator i = _class->sub_classes.begin();
204 i != _class->sub_classes.end(); ++i) {
206 create_register_class_code(_class);
211 WrapperCreator::create_register_constants_code(Namespace* ns)
213 for(std::vector<Field*>::iterator i = ns->fields.begin();
214 i != ns->fields.end(); ++i) {
216 create_register_constant_code(field);
221 WrapperCreator::create_register_constant_code(Field* field)
223 if(!field->has_const_value)
225 out << ind << "sq_pushstring(v, \"" << field->name << "\", -1);\n";
226 if(field->type->atomic_type == &BasicType::INT) {
227 out << ind << "sq_pushinteger(v, " << field->const_int_value << ");\n";
228 } else if(field->type->atomic_type == &BasicType::FLOAT) {
229 out << ind << "sq_pushfloat(v, " << field->const_float_value << ");\n";
230 } else if(field->type->atomic_type == StringType::instance()) {
231 out << ind << "sq_pushstring(v, \""
232 << field->const_string_value << "\", -1);\n";
234 throw std::runtime_error("Constant is not int, float or string");
236 create_register_slot_code("constant", field->name);
241 WrapperCreator::create_register_slot_code(const std::string& what,
242 const std::string& name)
244 out << ind << "if(SQ_FAILED(sq_createslot(v, -3))) {\n";
245 out << ind << ind << "std::ostringstream msg;\n";
246 out << ind << ind << "msg << \"Couldn't register " << what << "'"
248 out << ind << ind << "throw SquirrelError(v, msg.str());\n";
253 WrapperCreator::create_function_wrapper(Class* _class, Function* function)
255 if(function->type == Function::DESTRUCTOR)
258 std::string ns_prefix;
259 if(selected_namespace != "")
260 ns_prefix = selected_namespace + "::";
261 if(function->type == Function::CONSTRUCTOR)
262 function->name = "constructor";
264 out << "static int ";
266 out << _class->name << "_";
268 out << function->name << "_wrapper(HSQUIRRELVM v)\n"
271 if(_class == 0 && function->parameters.empty()
272 && function->return_type.is_void()
273 && function->type != Function::CONSTRUCTOR) {
274 out << ind << "(void) v;\n";
277 // eventually retrieve pointer to class instance
278 if(_class != 0 && function->type != Function::CONSTRUCTOR) {
279 out << ind << ns_prefix << _class->name << "* _this;\n";
280 out << ind << "sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);\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 != &BasicType::INT)
289 throw std::runtime_error("custom function has to return int");
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 << "(v);\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 = v;\n";
315 snprintf(argname, sizeof(argname), "arg%d", i);
316 prepare_argument(p->type, i + arg_offset, argname);
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 << "sq_setinstanceup(v, 1, _this);\n";
350 out << ind << "sq_setreleasehook(v, 1, "
351 << _class->name << "_release_hook);\n";
354 // push return value back on stack and return
355 if(function->suspend) {
356 if(!function->return_type.is_void()) {
357 std::stringstream msg;
358 msg << "Function '" << function->name << "' declared as suspend"
359 << " but has a return value.";
360 throw std::runtime_error(msg.str());
362 out << ind << "return sq_suspendvm(v);\n";
363 } else if(function->return_type.is_void()) {
364 out << ind << "return 0;\n";
366 push_to_stack(function->return_type, "return_value");
367 out << ind << "return 1;\n";
374 WrapperCreator::prepare_argument(const Type& type, size_t index,
375 const std::string& var)
377 if(type.ref > 0 && type.atomic_type != StringType::instance())
378 throw std::runtime_error("References not handled yet");
380 throw std::runtime_error("Pointers not handled yet");
381 if(type.atomic_type == &BasicType::INT) {
382 out << ind << "int " << var << ";\n";
383 out << ind << "sq_getinteger(v, " << index << ", &" << var << ");\n";
384 } else if(type.atomic_type == &BasicType::FLOAT) {
385 out << ind << "float " << var << ";\n";
386 out << ind << "sq_getfloat(v, " << index << ", &" << var << ");\n";
387 } else if(type.atomic_type == &BasicType::BOOL) {
388 out << ind << "SQBool " << var << ";\n";
389 out << ind << "sq_getbool(v, " << index << ", &" << var << ");\n";
390 } else if(type.atomic_type == StringType::instance()) {
391 out << ind << "const char* " << var << ";\n";
392 out << ind << "sq_getstring(v, " << index << ", &" << var << ");\n";
394 std::ostringstream msg;
395 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
396 throw std::runtime_error(msg.str());
401 WrapperCreator::push_to_stack(const Type& type, const std::string& var)
403 if(type.ref > 0 && type.atomic_type != StringType::instance())
404 throw std::runtime_error("References not handled yet");
406 throw std::runtime_error("Pointers not handled yet");
408 if(type.atomic_type == &BasicType::INT) {
409 out << "sq_pushinteger(v, " << var << ");\n";
410 } else if(type.atomic_type == &BasicType::FLOAT) {
411 out << "sq_pushfloat(v, " << var << ");\n";
412 } else if(type.atomic_type == &BasicType::BOOL) {
413 out << "sq_pushbool(v, " << var << ");\n";
414 } else if(type.atomic_type == StringType::instance()) {
415 out << "sq_pushstring(v, " << var << ".c_str(), "
416 << var << ".size());\n";
418 std::ostringstream msg;
419 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
420 throw std::runtime_error(msg.str());
425 WrapperCreator::create_class_wrapper(Class* _class)
427 create_class_release_hook(_class);
428 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
429 i != _class->members.end(); ++i) {
430 ClassMember* member = *i;
431 if(member->visibility != ClassMember::PUBLIC)
433 Function* function = dynamic_cast<Function*> (member);
436 // don't wrap destructors
437 if(function->type == Function::DESTRUCTOR)
439 create_function_wrapper(_class, function);
444 WrapperCreator::create_squirrel_instance(Class* _class)
446 out << "void create_squirrel_instance(HSQUIRRELVM v, "
447 << ns_prefix << _class->name
448 << "* object, bool setup_releasehook)\n"
450 << ind << "using namespace Wrapper;\n"
452 << ind << "sq_pushroottable(v);\n"
453 << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n"
454 << ind << "if(SQ_FAILED(sq_get(v, -2))) {\n"
455 << ind << ind << "std::ostringstream msg;\n"
456 << ind << ind << "msg << \"Couldn't resolved squirrel type '"
457 << _class->name << "'\";\n"
458 << ind << ind << "throw SquirrelError(v, msg.str());\n"
461 << ind << "if(SQ_FAILED(sq_createinstance(v, -1)) || "
462 << "SQ_FAILED(sq_setinstanceup(v, -1, object))) {\n"
463 << ind << ind << "std::ostringstream msg;\n"
464 << ind << ind << "msg << \"Couldn't setup squirrel instance for "
465 << "object of type '" << _class->name << "'\";\n"
466 << ind << ind << "throw SquirrelError(v, msg.str());\n"
468 << ind << "sq_remove(v, -2); // remove object name\n"
470 << ind << "if(setup_releasehook) {\n"
471 << ind << ind << "sq_setreleasehook(v, -1, "
472 << _class->name << "_release_hook);\n"
475 << ind << "sq_remove(v, -2); // remove root table\n"
481 WrapperCreator::create_class_release_hook(Class* _class)
483 out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n"
485 << ind << ns_prefix << _class->name
486 << "* _this = reinterpret_cast<" << ns_prefix << _class->name
488 << ind << "delete _this;\n"
489 << ind << "return 0;\n"