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";
268 if(function->custom) {
269 if(function->type != Function::FUNCTION)
270 throw std::runtime_error(
271 "custom not allow constructor+destructor yet");
272 if(function->return_type.atomic_type != &BasicType::INT)
273 throw std::runtime_error("custom function has to return int");
274 if(function->parameters.size() != 1)
275 throw std::runtime_error(
276 "custom function must have 1 HSQUIRRELVM parameter");
278 out << ind << "return ";
283 out << function->name << "(v);\n";
289 // declare and retrieve arguments
292 for(std::vector<Parameter>::iterator p = function->parameters.begin();
293 p != function->parameters.end(); ++p) {
294 if(i == 0 && p->type.atomic_type == HSQUIRRELVMType::instance()) {
295 out << ind << "HSQUIRRELVM arg0 = v;\n";
299 snprintf(argname, sizeof(argname), "arg%d", i);
300 prepare_argument(p->type, i + arg_offset, argname);
308 if(!function->return_type.is_void()) {
309 function->return_type.write_c_type(out);
310 out << " return_value = ";
313 if(function->type == Function::CONSTRUCTOR) {
314 out << ns_prefix << _class->name << "* _this = new " << ns_prefix;
321 if(function->type == Function::CONSTRUCTOR) {
322 out << _class->name << "(";
324 out << function->name << "(";
326 for(size_t i = 0; i < function->parameters.size(); ++i) {
332 if(function->type == Function::CONSTRUCTOR) {
333 out << ind << "sq_setinstanceup(v, 1, _this);\n";
334 out << ind << "sq_setreleasehook(v, 1, "
335 << _class->name << "_release_hook);\n";
338 // push return value back on stack and return
339 if(function->suspend) {
340 if(!function->return_type.is_void()) {
341 std::stringstream msg;
342 msg << "Function '" << function->name << "' declared as suspend"
343 << " but has a return value.";
344 throw std::runtime_error(msg.str());
346 out << ind << "return sq_suspendvm(v);\n";
347 } else if(function->return_type.is_void()) {
348 out << ind << "return 0;\n";
350 push_to_stack(function->return_type, "return_value");
351 out << ind << "return 1;\n";
358 WrapperCreator::prepare_argument(const Type& type, size_t index,
359 const std::string& var)
361 if(type.ref > 0 && type.atomic_type != StringType::instance())
362 throw std::runtime_error("References not handled yet");
364 throw std::runtime_error("Pointers not handled yet");
365 if(type.atomic_type == &BasicType::INT) {
366 out << ind << "int " << var << ";\n";
367 out << ind << "sq_getinteger(v, " << index << ", &" << var << ");\n";
368 } else if(type.atomic_type == &BasicType::FLOAT) {
369 out << ind << "float " << var << ";\n";
370 out << ind << "sq_getfloat(v, " << index << ", &" << var << ");\n";
371 } else if(type.atomic_type == &BasicType::BOOL) {
372 out << ind << "SQBool " << var << ";\n";
373 out << ind << "sq_getbool(v, " << index << ", &" << var << ");\n";
374 } else if(type.atomic_type == StringType::instance()) {
375 out << ind << "const char* " << var << ";\n";
376 out << ind << "sq_getstring(v, " << index << ", &" << var << ");\n";
378 std::ostringstream msg;
379 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
380 throw std::runtime_error(msg.str());
385 WrapperCreator::push_to_stack(const Type& type, const std::string& var)
387 if(type.ref > 0 && type.atomic_type != StringType::instance())
388 throw std::runtime_error("References not handled yet");
390 throw std::runtime_error("Pointers not handled yet");
392 if(type.atomic_type == &BasicType::INT) {
393 out << "sq_pushinteger(v, " << var << ");\n";
394 } else if(type.atomic_type == &BasicType::FLOAT) {
395 out << "sq_pushfloat(v, " << var << ");\n";
396 } else if(type.atomic_type == &BasicType::BOOL) {
397 out << "sq_pushbool(v, " << var << ");\n";
398 } else if(type.atomic_type == StringType::instance()) {
399 out << "sq_pushstring(v, " << var << ".c_str(), "
400 << var << ".size());\n";
402 std::ostringstream msg;
403 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
404 throw std::runtime_error(msg.str());
409 WrapperCreator::create_class_wrapper(Class* _class)
411 create_class_release_hook(_class);
412 create_squirrel_instance(_class);
413 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
414 i != _class->members.end(); ++i) {
415 ClassMember* member = *i;
416 if(member->visibility != ClassMember::PUBLIC)
418 Function* function = dynamic_cast<Function*> (member);
421 // don't wrap destructors
422 if(function->type == Function::DESTRUCTOR)
424 create_function_wrapper(_class, function);
429 WrapperCreator::create_squirrel_instance(Class* _class)
431 out << "void create_squirrel_instance(HSQUIRRELVM v, "
432 << ns_prefix << _class->name
433 << "* object, bool setup_releasehook)\n"
435 << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n"
436 << ind << "if(sq_get(v, -2) < 0) {\n"
437 << ind << ind << "std::ostringstream msg;\n"
438 << ind << ind << "msg << \"Couldn't resolved squirrel type '"
439 << _class->name << "'\";\n"
440 << ind << ind << "throw SquirrelError(v, msg.str());\n"
443 << ind << "if(sq_createinstance(v, -1) < 0 || "
444 << "sq_setinstanceup(v, -1, object) < 0) {\n"
445 << ind << ind << "std::ostringstream msg;\n"
446 << ind << ind << "msg << \"Couldn't setup squirrel instance for "
447 << "object of type '" << _class->name << "'\";\n"
448 << ind << ind << "throw SquirrelError(v, msg.str());\n"
450 << ind << "sq_remove(v, -2);\n"
452 << ind << "if(setup_releasehook) {\n"
453 << ind << ind << "sq_setreleasehook(v, -1, "
454 << _class->name << "_release_hook);\n"
461 WrapperCreator::create_class_release_hook(Class* _class)
463 out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n"
465 << ind << ns_prefix << _class->name
466 << "* _this = reinterpret_cast<" << ns_prefix << _class->name
468 << ind << "delete _this;\n"
469 << ind << "return 0;\n"