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 SquirrelWrapper\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 <squirrel.h>\n"
67 << "#include \"wrapper_util.hpp\"\n"
68 << "#include \"wrapper.interface.hpp\"\n"
70 << "namespace SquirrelWrapper\n"
74 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
75 i != ns->types.end(); ++i) {
76 AtomicType* type = *i;
77 Class* _class = dynamic_cast<Class*> (type);
79 create_class_wrapper(_class);
81 for(std::vector<Function*>::iterator i = ns->functions.begin();
82 i != ns->functions.end(); ++i) {
83 create_function_wrapper(0, *i);
86 out << "void register_" << modulename << "_wrapper(HSQUIRRELVM v)\n"
88 << ind << "sq_pushroottable(v);\n";
90 create_register_constants_code(ns);
91 create_register_functions_code(ns);
92 create_register_classes_code(ns);
94 out << ind << "sq_pop(v, 1);\n"
102 WrapperCreator::create_register_function_code(Function* function, Class* _class)
104 if(function->type == Function::DESTRUCTOR)
107 out << ind << "sq_pushstring(v, \"" << function->name << "\", -1);\n";
108 out << ind << "sq_newclosure(v, &"
109 << (_class != 0 ? _class->name + "_" : "") << function->name
110 << "_wrapper, 0);\n";
111 create_register_slot_code("function", function->name);
116 WrapperCreator::create_register_functions_code(Namespace* ns)
118 for(std::vector<Function*>::iterator i = ns->functions.begin();
119 i != ns->functions.end(); ++i) {
120 Function* function = *i;
121 create_register_function_code(function, 0);
126 WrapperCreator::create_register_classes_code(Namespace* ns)
128 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
129 i != ns->types.end(); ++i) {
130 AtomicType* type = *i;
131 Class* _class = dynamic_cast<Class*> (type);
134 if(_class->super_classes.size() > 0)
137 create_register_class_code(_class);
142 WrapperCreator::create_register_class_code(Class* _class)
144 out << ind << "// Register class " << _class->name << "\n";
145 out << ind << "sq_pushstring(v, \""
146 << _class->name << "\", -1);\n";
148 if(_class->super_classes.size() > 0) {
149 if(_class->super_classes.size() > 1) {
150 std::ostringstream msg;
151 msg << "Multiple inheritance not supported (at class '"
152 << _class->name << "')";
153 throw std::runtime_error(msg.str());
156 out << ind << "sq_pushstring(v, \""
157 << _class->super_classes[0]->name << "\", -1);\n";
158 out << ind << "sq_get(v, -3);\n";
160 out << ind << "if(sq_newclass(v, "
161 << (_class->super_classes.size() > 0 ? "SQTrue" : "SQFalse")
163 out << ind << ind << "std::ostringstream msg;\n";
164 out << ind << ind << "msg << \"Couldn't create new class '"
165 << _class->name << "'\";\n";
166 out << ind << ind << "throw SquirrelError(v, msg.str());\n";
169 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
170 i != _class->members.end(); ++i) {
171 ClassMember* member = *i;
172 if(member->visibility != ClassMember::PUBLIC)
174 Function* function = dynamic_cast<Function*> (member);
176 create_register_function_code(function, _class);
178 Field* field = dynamic_cast<Field*> (member);
180 create_register_constant_code(field);
184 create_register_slot_code("class", _class->name);
187 for(std::vector<Class*>::iterator i = _class->sub_classes.begin();
188 i != _class->sub_classes.end(); ++i) {
190 create_register_class_code(_class);
195 WrapperCreator::create_register_constants_code(Namespace* ns)
197 for(std::vector<Field*>::iterator i = ns->fields.begin();
198 i != ns->fields.end(); ++i) {
200 create_register_constant_code(field);
205 WrapperCreator::create_register_constant_code(Field* field)
207 if(!field->has_const_value)
209 out << ind << "sq_pushstring(v, \"" << field->name << "\", -1);\n";
210 if(field->type->atomic_type == &BasicType::INT) {
211 out << ind << "sq_pushinteger(v, " << field->const_int_value << ");\n";
212 } else if(field->type->atomic_type == &BasicType::FLOAT) {
213 out << ind << "sq_pushfloat(v, " << field->const_float_value << ");\n";
214 } else if(field->type->atomic_type == StringType::instance()) {
215 out << ind << "sq_pushstring(v, \""
216 << field->const_string_value << "\", -1);\n";
218 throw std::runtime_error("Constant is not int, float or string");
220 create_register_slot_code("constant", field->name);
225 WrapperCreator::create_register_slot_code(const std::string& what,
226 const std::string& name)
228 out << ind << "if(sq_createslot(v, -3) < 0) {\n";
229 out << ind << ind << "std::ostringstream msg;\n";
230 out << ind << ind << "msg << \"Couldn't register " << what << "'"
232 out << ind << ind << "throw SquirrelError(v, msg.str());\n";
237 WrapperCreator::create_function_wrapper(Class* _class, Function* function)
239 if(function->type == Function::DESTRUCTOR)
242 std::string ns_prefix;
243 if(selected_namespace != "")
244 ns_prefix = selected_namespace + "::";
245 if(function->type == Function::CONSTRUCTOR)
246 function->name = "constructor";
248 out << "static int ";
250 out << _class->name << "_";
252 out << function->name << "_wrapper(HSQUIRRELVM v)\n"
255 if(_class == 0 && function->parameters.empty()
256 && function->return_type.is_void()
257 && function->type != Function::CONSTRUCTOR) {
258 out << ind << "(void) v;\n";
261 // eventually retrieve pointer to class instance
262 if(_class != 0 && function->type != Function::CONSTRUCTOR) {
263 out << ind << ns_prefix << _class->name << "* _this;\n";
264 out << ind << "sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);\n";
267 // declare and retrieve arguments
270 for(std::vector<Parameter>::iterator p = function->parameters.begin();
271 p != function->parameters.end(); ++p) {
272 if(i == 0 && p->type.atomic_type == HSQUIRRELVMType::instance()) {
273 out << ind << "HSQUIRRELVM arg0 = v;\n";
277 snprintf(argname, sizeof(argname), "arg%d", i);
278 prepare_argument(p->type, i + arg_offset, argname);
286 if(!function->return_type.is_void()) {
287 function->return_type.write_c_type(out);
288 out << " return_value = ";
291 if(function->type == Function::CONSTRUCTOR) {
292 out << ns_prefix << _class->name << "* _this = new " << ns_prefix;
299 if(function->type == Function::CONSTRUCTOR) {
300 out << _class->name << "(";
302 out << function->name << "(";
304 for(size_t i = 0; i < function->parameters.size(); ++i) {
310 if(function->type == Function::CONSTRUCTOR) {
311 out << ind << "sq_setinstanceup(v, 1, _this);\n";
312 out << ind << "sq_setreleasehook(v, 1, "
313 << _class->name << "_release_hook);\n";
316 // push return value back on stack and return
317 if(function->suspend) {
318 if(!function->return_type.is_void()) {
319 std::stringstream msg;
320 msg << "Function '" << function->name << "' declared as suspend"
321 << " but has a return value.";
322 throw std::runtime_error(msg.str());
324 out << ind << "return sq_suspendvm(v);\n";
325 } else if(function->return_type.is_void()) {
326 out << ind << "return 0;\n";
328 push_to_stack(function->return_type, "return_value");
329 out << ind << "return 1;\n";
336 WrapperCreator::prepare_argument(const Type& type, size_t index,
337 const std::string& var)
339 if(type.ref > 0 && type.atomic_type != StringType::instance())
340 throw std::runtime_error("References not handled yet");
342 throw std::runtime_error("Pointers not handled yet");
343 if(type.atomic_type == &BasicType::INT) {
344 out << ind << "int " << var << ";\n";
345 out << ind << "sq_getinteger(v, " << index << ", &" << var << ");\n";
346 } else if(type.atomic_type == &BasicType::FLOAT) {
347 out << ind << "float " << var << ";\n";
348 out << ind << "sq_getfloat(v, " << index << ", &" << var << ");\n";
349 } else if(type.atomic_type == &BasicType::BOOL) {
350 out << ind << "SQBool " << var << ";\n";
351 out << ind << "sq_getbool(v, " << index << ", &" << var << ");\n";
352 } else if(type.atomic_type == StringType::instance()) {
353 out << ind << "const char* " << var << ";\n";
354 out << ind << "sq_getstring(v, " << index << ", &" << var << ");\n";
356 std::ostringstream msg;
357 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
358 throw std::runtime_error(msg.str());
363 WrapperCreator::push_to_stack(const Type& type, const std::string& var)
365 if(type.ref > 0 && type.atomic_type != StringType::instance())
366 throw std::runtime_error("References not handled yet");
368 throw std::runtime_error("Pointers not handled yet");
370 if(type.atomic_type == &BasicType::INT) {
371 out << "sq_pushinteger(v, " << var << ");\n";
372 } else if(type.atomic_type == &BasicType::FLOAT) {
373 out << "sq_pushfloat(v, " << var << ");\n";
374 } else if(type.atomic_type == &BasicType::BOOL) {
375 out << "sq_pushbool(v, " << var << ");\n";
376 } else if(type.atomic_type == StringType::instance()) {
377 out << "sq_pushstring(v, " << var << ".c_str(), "
378 << var << ".size());\n";
380 std::ostringstream msg;
381 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
382 throw std::runtime_error(msg.str());
387 WrapperCreator::create_class_wrapper(Class* _class)
389 create_class_release_hook(_class);
390 create_squirrel_instance(_class);
391 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
392 i != _class->members.end(); ++i) {
393 ClassMember* member = *i;
394 if(member->visibility != ClassMember::PUBLIC)
396 Function* function = dynamic_cast<Function*> (member);
399 // don't wrap destructors
400 if(function->type == Function::DESTRUCTOR)
402 create_function_wrapper(_class, function);
407 WrapperCreator::create_squirrel_instance(Class* _class)
409 out << "void create_squirrel_instance(HSQUIRRELVM v, "
410 << ns_prefix << _class->name
411 << "* object, bool setup_releasehook)\n"
413 << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n"
414 << ind << "if(sq_get(v, -2) < 0) {\n"
415 << ind << ind << "std::ostringstream msg;\n"
416 << ind << ind << "msg << \"Couldn't resolved squirrel type '"
417 << _class->name << "'\";\n"
418 << ind << ind << "throw SquirrelError(v, msg.str());\n"
421 << ind << "if(sq_createinstance(v, -1) < 0 || "
422 << "sq_setinstanceup(v, -1, object) < 0) {\n"
423 << ind << ind << "std::ostringstream msg;\n"
424 << ind << ind << "msg << \"Couldn't setup squirrel instance for "
425 << "object of type '" << _class->name << "'\";\n"
426 << ind << ind << "throw SquirrelError(v, msg.str());\n"
428 << ind << "sq_remove(v, -2);\n"
430 << ind << "if(setup_releasehook) {\n"
431 << ind << ind << "sq_setreleasehook(v, -1, "
432 << _class->name << "_release_hook);\n"
438 WrapperCreator::create_class_release_hook(Class* _class)
440 out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n"
442 << ind << ns_prefix << _class->name
443 << "* _this = reinterpret_cast<" << ns_prefix << _class->name
445 << ind << "delete _this;\n"
446 << ind << "return 0;\n"