5 #include "create_wrapper.hpp"
9 WrapperCreator::create_wrapper(Namespace* ns)
11 std::string fromfile = original_file != "" ? original_file : inputfile;
16 << " * WARNING: This file is automatically generated from:\n"
17 << " * '" << fromfile << "'\n"
18 << " * DO NOT CHANGE\n"
20 << "#ifndef __" << modulename << "_WRAPPER_H__\n"
21 << "#define __" << modulename << "_WRAPPER_H__\n"
23 << "#include <squirrel.h>\n"
25 << "void register_" << modulename << "_wrapper(HSQUIRRELVM v);\n"
32 << " * WARNING: This file is automatically generated from:\n"
33 << " * '" << fromfile << "'\n"
34 << " * DO NOT CHANGE\n"
36 << "#include <config.h>\n"
39 << "#include <assert.h>\n"
40 << "#include <string>\n"
41 << "#include <squirrel.h>\n"
42 << "#include \"wrapper_util.hpp\"\n"
43 << "#include \"wrapper.interface.hpp\"\n"
45 if(selected_namespace != "") {
46 out << "using namespace " << selected_namespace << ";\n";
50 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
51 i != ns->types.end(); ++i) {
52 AtomicType* type = *i;
53 Class* _class = dynamic_cast<Class*> (type);
55 create_class_wrapper(_class);
57 for(std::vector<Function*>::iterator i = ns->functions.begin();
58 i != ns->functions.end(); ++i) {
59 create_function_wrapper(0, *i);
62 out << "void register_" << modulename << "_wrapper(HSQUIRRELVM v)\n";
64 out << ind << "sq_pushroottable(v);\n";
66 create_register_constants_code(ns);
67 create_register_functions_code(ns);
68 create_register_classes_code(ns);
70 out << ind << "sq_pop(v, 1);\n";
75 WrapperCreator::create_register_function_code(Function* function, Class* _class)
77 if(function->type == Function::DESTRUCTOR)
80 out << ind << "sq_pushstring(v, \"" << function->name << "\", -1);\n";
81 out << ind << "sq_newclosure(v, &"
82 << (_class != 0 ? _class->name + "_" : "") << function->name
84 create_register_slot_code("function", function->name);
89 WrapperCreator::create_register_functions_code(Namespace* ns)
91 for(std::vector<Function*>::iterator i = ns->functions.begin();
92 i != ns->functions.end(); ++i) {
93 Function* function = *i;
94 create_register_function_code(function, 0);
99 WrapperCreator::create_register_classes_code(Namespace* ns)
101 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
102 i != ns->types.end(); ++i) {
103 AtomicType* type = *i;
104 Class* _class = dynamic_cast<Class*> (type);
107 if(_class->super_classes.size() > 0)
110 create_register_class_code(_class);
115 WrapperCreator::create_register_class_code(Class* _class)
117 out << ind << "// Register class " << _class->name << "\n";
118 out << ind << "sq_pushstring(v, \""
119 << _class->name << "\", -1);\n";
121 if(_class->super_classes.size() > 0) {
122 if(_class->super_classes.size() > 1) {
123 std::stringstream msg;
124 msg << "Multiple inheritance not supported (at class '"
125 << _class->name << "')";
126 throw std::runtime_error(msg.str());
129 out << ind << "sq_pushstring(v, \""
130 << _class->super_classes[0]->name << "\", -1);\n";
131 out << ind << "sq_get(v, -3);\n";
133 out << ind << "if(sq_newclass(v, "
134 << (_class->super_classes.size() > 0 ? "SQTrue" : "SQFalse")
136 out << ind << ind << "std::stringstream msg;\n";
137 out << ind << ind << "msg << \"Couldn't create new class '"
138 << _class->name << "'\";\n";
139 out << ind << ind << "throw SquirrelError(v, msg.str());\n";
142 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
143 i != _class->members.end(); ++i) {
144 ClassMember* member = *i;
145 if(member->visibility != ClassMember::PUBLIC)
147 Function* function = dynamic_cast<Function*> (member);
149 create_register_function_code(function, _class);
151 Field* field = dynamic_cast<Field*> (member);
153 create_register_constant_code(field);
157 create_register_slot_code("class", _class->name);
160 for(std::vector<Class*>::iterator i = _class->sub_classes.begin();
161 i != _class->sub_classes.end(); ++i) {
163 create_register_class_code(_class);
168 WrapperCreator::create_register_constants_code(Namespace* ns)
170 for(std::vector<Field*>::iterator i = ns->fields.begin();
171 i != ns->fields.end(); ++i) {
173 create_register_constant_code(field);
178 WrapperCreator::create_register_constant_code(Field* field)
180 if(!field->has_const_value)
182 out << ind << "sq_pushstring(v, \"" << field->name << "\", -1);\n";
183 if(field->type->atomic_type == &BasicType::INT) {
184 out << ind << "sq_pushinteger(v, " << field->const_int_value << ")\n";
185 } else if(field->type->atomic_type == &BasicType::FLOAT) {
186 out << ind << "sq_pushfloat(v, " << field->const_float_value << ")\n";
187 } else if(field->type->atomic_type == StringType::instance()) {
188 out << ind << "sq_pushstring(v, \""
189 << field->const_string_value << "\", -1);\n";
191 throw std::runtime_error("Constant is not int, float or string");
193 create_register_slot_code("constant", field->name);
198 WrapperCreator::create_register_slot_code(const std::string& what,
199 const std::string& name)
201 out << ind << "if(sq_createslot(v, -3) < 0) {\n";
202 out << ind << ind << "std::stringstream msg;\n";
203 out << ind << ind << "msg << \"Couldn't register " << what << "'"
205 out << ind << ind << "throw SquirrelError(v, msg.str());\n";
210 WrapperCreator::create_function_wrapper(Class* _class, Function* function)
212 if(function->type == Function::DESTRUCTOR)
215 std::string ns_prefix;
216 if(selected_namespace != "")
217 ns_prefix = selected_namespace + "::";
218 if(function->type == Function::CONSTRUCTOR)
219 function->name = "constructor";
221 out << "static int ";
223 out << _class->name << "_";
225 out << function->name << "_wrapper(HSQUIRRELVM v)\n"
228 if(_class == 0 && function->parameters.empty()
229 && function->return_type.is_void()
230 && function->type != Function::CONSTRUCTOR) {
231 out << ind << "(void) v;\n";
234 // eventually retrieve pointer to class instance
235 if(_class != 0 && function->type != Function::CONSTRUCTOR) {
236 out << ind << ns_prefix << _class->name << "* _this;\n";
237 out << ind << "sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);\n";
240 // declare and retrieve arguments
243 for(std::vector<Parameter>::iterator p = function->parameters.begin();
244 p != function->parameters.end(); ++p) {
245 if(i == 0 && p->type.atomic_type == HSQUIRRELVMType::instance()) {
246 out << ind << "HSQUIRRELVM arg0 = v;\n";
250 snprintf(argname, sizeof(argname), "arg%d", i);
251 prepare_argument(p->type, i + arg_offset, argname);
259 if(!function->return_type.is_void()) {
260 function->return_type.write_c_type(out);
261 out << " return_value = ";
264 if(function->type == Function::CONSTRUCTOR) {
265 out << ns_prefix << _class->name << "* _this = new " << ns_prefix;
272 if(function->type == Function::CONSTRUCTOR) {
273 out << _class->name << "(";
275 out << function->name << "(";
277 for(size_t i = 0; i < function->parameters.size(); ++i) {
283 if(function->type == Function::CONSTRUCTOR) {
284 out << ind << "sq_setinstanceup(v, 1, _this);\n";
285 out << ind << "sq_setreleasehook(v, 1, "
286 << _class->name << "_release_hook);\n";
289 // push return value back on stack and return
290 if(function->return_type.is_void()) {
291 if(function->docu_comment.find("@SUSPEND@") != std::string::npos) {
292 out << ind << "return sq_suspendvm(v);\n";
294 out << ind << "return 0;\n";
297 push_to_stack(function->return_type, "return_value");
298 out << ind << "return 1;\n";
305 WrapperCreator::prepare_argument(const Type& type, size_t index,
306 const std::string& var)
308 if(type.ref > 0 && type.atomic_type != StringType::instance())
309 throw std::runtime_error("References not handled yet");
311 throw std::runtime_error("Pointers not handled yet");
312 if(type.atomic_type == &BasicType::INT) {
313 out << ind << "int " << var << ";\n";
314 out << ind << "sq_getinteger(v, " << index << ", &" << var << ");\n";
315 } else if(type.atomic_type == &BasicType::FLOAT) {
316 out << ind << "float " << var << ";\n";
317 out << ind << "sq_getfloat(v, " << index << ", &" << var << ");\n";
318 } else if(type.atomic_type == &BasicType::BOOL) {
319 out << ind << "SQBool " << var << ";\n";
320 out << ind << "sq_getbool(v, " << index << ", &" << var << ");\n";
321 } else if(type.atomic_type == StringType::instance()) {
322 out << ind << "const char* " << var << ";\n";
323 out << ind << "sq_getstring(v, " << index << ", &" << var << ");\n";
325 std::ostringstream msg;
326 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
327 throw std::runtime_error(msg.str());
332 WrapperCreator::push_to_stack(const Type& type, const std::string& var)
334 if(type.ref > 0 && type.atomic_type != StringType::instance())
335 throw std::runtime_error("References not handled yet");
337 throw std::runtime_error("Pointers not handled yet");
339 if(type.atomic_type == &BasicType::INT) {
340 out << "sq_pushinteger(v, " << var << ");\n";
341 } else if(type.atomic_type == &BasicType::FLOAT) {
342 out << "sq_pushfloat(v, " << var << ");\n";
343 } else if(type.atomic_type == &BasicType::BOOL) {
344 out << "sq_pushbool(v, " << var << ");\n";
345 } else if(type.atomic_type == StringType::instance()) {
346 out << "sq_pushstring(v, " << var << ".c_str(), "
347 << var << ".size());\n";
349 std::ostringstream msg;
350 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
351 throw std::runtime_error(msg.str());
356 WrapperCreator::create_class_wrapper(Class* _class)
358 bool release_hook_created = false;
359 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
360 i != _class->members.end(); ++i) {
361 ClassMember* member = *i;
362 if(member->visibility != ClassMember::PUBLIC)
364 Function* function = dynamic_cast<Function*> (member);
367 if(function->type == Function::CONSTRUCTOR
368 && !release_hook_created) {
369 create_class_release_hook(_class);
370 release_hook_created = true;
372 // don't wrap destructors
373 if(function->type == Function::DESTRUCTOR)
375 create_function_wrapper(_class, function);
380 WrapperCreator::create_class_release_hook(Class* _class)
382 out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n"
384 << ind << _class->name
385 << "* _this = reinterpret_cast<" << _class->name << "*> (ptr);\n"
386 << ind << "delete _this;\n"
387 << ind << "return 0;\n"