5 #include "create_wrapper.h"
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 \"wrapper_util.h\"\n"
25 << "extern WrappedFunction " << modulename << "_global_functions[];\n"
26 << "extern WrappedClass " << modulename << "_classes[];\n"
33 << " * WARNING: This file is automatically generated from:\n"
34 << " * '" << fromfile << "'\n"
35 << " * DO NOT CHANGE\n"
37 << "#include <config.h>\n"
40 << "#include <assert.h>\n"
41 << "#include <string>\n"
42 << "#include <squirrel.h>\n"
43 << "#include \"wrapper_util.h\"\n"
44 << "#include \"wrapper.interface.h\"\n"
46 if(selected_namespace != "") {
47 out << "using namespace " << selected_namespace << ";\n";
51 for(std::vector<AtomicType*>::iterator i = ns->types.begin();
52 i != ns->types.end(); ++i) {
53 AtomicType* type = *i;
54 Class* _class = dynamic_cast<Class*> (type);
56 create_class_wrapper(_class);
58 for(std::vector<Function*>::iterator i = ns->functions.begin();
59 i != ns->functions.end(); ++i) {
60 create_function_wrapper(0, *i);
63 // create function list...
64 out << "WrappedFunction " << modulename << "_global_functions[] = {\n";
65 for(std::vector<Function*>::iterator i = ns->functions.begin();
66 i != ns->functions.end(); ++i) {
67 Function* function = *i;
68 out << ind << "{ \"" << function->name << "\", &"
69 << function->name << "_wrapper },\n";
71 out << ind << "{ 0, 0 }\n"
75 // create class list...
76 std::ostringstream classlist;
77 classlist << "WrappedClass " << modulename << "_classes[] = {\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);
86 classlist << ind << "{ \"" << _class->name << "\", "
87 << modulename << "_" << _class->name
90 out << "static WrappedFunction " << modulename << "_"
91 << _class->name << "_methods[] = {\n";
92 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
93 i != _class->members.end(); ++i) {
94 ClassMember* member = *i;
95 if(member->visibility != ClassMember::PUBLIC)
97 Function* function = dynamic_cast<Function*> (member);
98 if(!function || function->type == Function::DESTRUCTOR)
101 out << ind << "{ \"" << function->name << "\", &"
102 << _class->name << "_" << function->name << "_wrapper },\n";
107 classlist << ind << "{ 0, 0 }\n";
109 out << classlist.str();
114 WrapperCreator::create_function_wrapper(Class* _class, Function* function)
116 if(function->type == Function::DESTRUCTOR)
119 std::string ns_prefix;
120 if(selected_namespace != "")
121 ns_prefix = selected_namespace + "::";
122 if(function->type == Function::CONSTRUCTOR)
123 function->name = "constructor";
125 out << "static int ";
127 out << _class->name << "_";
129 out << function->name << "_wrapper(HSQUIRRELVM v)\n"
132 if(_class == 0 && function->parameters.empty()
133 && function->return_type.is_void()
134 && function->type != Function::CONSTRUCTOR) {
135 out << ind << "(void) v;\n";
138 // eventually retrieve pointer to class instance
139 if(_class != 0 && function->type != Function::CONSTRUCTOR) {
140 out << ind << ns_prefix << _class->name << "* _this;\n";
141 out << ind << "sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);\n";
144 // declare and retrieve arguments
147 for(std::vector<Parameter>::iterator p = function->parameters.begin();
148 p != function->parameters.end(); ++p) {
149 if(i == 0 && p->type.atomic_type == HSQUIRRELVMType::instance()) {
150 out << ind << "HSQUIRRELVM arg0 = v;\n";
154 snprintf(argname, sizeof(argname), "arg%d", i);
155 prepare_argument(p->type, i + arg_offset, argname);
163 if(!function->return_type.is_void()) {
164 function->return_type.write_c_type(out);
165 out << " return_value = ";
168 if(function->type == Function::CONSTRUCTOR) {
169 out << ns_prefix << _class->name << "* _this = new " << ns_prefix;
176 if(function->type == Function::CONSTRUCTOR) {
177 out << _class->name << "(";
179 out << function->name << "(";
181 for(size_t i = 0; i < function->parameters.size(); ++i) {
187 if(function->type == Function::CONSTRUCTOR) {
188 out << ind << "sq_setinstanceup(v, 1, _this);\n";
189 out << ind << "sq_setreleasehook(v, 1, "
190 << _class->name << "_release_hook);\n";
193 // push return value back on stack and return
194 if(function->return_type.is_void()) {
195 out << ind << "return 0;\n";
197 push_to_stack(function->return_type, "return_value");
198 out << ind << "return 1;\n";
205 WrapperCreator::prepare_argument(const Type& type, size_t index,
206 const std::string& var)
208 if(type.ref > 0 && type.atomic_type != StringType::instance())
209 throw std::runtime_error("References not handled yet");
211 throw std::runtime_error("Pointers not handled yet");
212 if(type.atomic_type == &BasicType::INT) {
213 out << ind << "int " << var << ";\n";
214 out << ind << "sq_getinteger(v, " << index << ", &" << var << ");\n";
215 } else if(type.atomic_type == &BasicType::FLOAT) {
216 out << ind << "float " << var << ";\n";
217 out << ind << "sq_getfloat(v, " << index << ", &" << var << ");\n";
218 } else if(type.atomic_type == &BasicType::BOOL) {
219 out << ind << "SQBool " << var << ";\n";
220 out << ind << "sq_getbool(v, " << index << ", &" << var << ");\n";
221 } else if(type.atomic_type == StringType::instance()) {
222 out << ind << "const char* " << var << ";\n";
223 out << ind << "sq_getstring(v, " << index << ", &" << var << ");\n";
225 std::ostringstream msg;
226 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
227 throw std::runtime_error(msg.str());
232 WrapperCreator::push_to_stack(const Type& type, const std::string& var)
234 if(type.ref > 0 && type.atomic_type != StringType::instance())
235 throw std::runtime_error("References not handled yet");
237 throw std::runtime_error("Pointers not handled yet");
239 if(type.atomic_type == &BasicType::INT) {
240 out << "sq_pushinteger(v, " << var << ");\n";
241 } else if(type.atomic_type == &BasicType::FLOAT) {
242 out << "sq_pushfloat(v, " << var << ");\n";
243 } else if(type.atomic_type == &BasicType::BOOL) {
244 out << "sq_pushbool(v, " << var << ");\n";
245 } else if(type.atomic_type == StringType::instance()) {
246 out << "sq_pushstring(v, " << var << ".c_str(), "
247 << var << ".size());\n";
249 std::ostringstream msg;
250 msg << "Type '" << type.atomic_type->name << "' not supported yet.";
251 throw std::runtime_error(msg.str());
256 WrapperCreator::create_class_wrapper(Class* _class)
258 bool release_hook_created = false;
259 for(std::vector<ClassMember*>::iterator i = _class->members.begin();
260 i != _class->members.end(); ++i) {
261 ClassMember* member = *i;
262 if(member->visibility != ClassMember::PUBLIC)
264 Function* function = dynamic_cast<Function*> (member);
267 if(function->type == Function::CONSTRUCTOR
268 && !release_hook_created) {
269 create_class_release_hook(_class);
270 release_hook_created = true;
272 // don't wrap destructors
273 if(function->type == Function::DESTRUCTOR)
275 create_function_wrapper(_class, function);
280 WrapperCreator::create_class_release_hook(Class* _class)
282 out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n"
284 << ind << _class->name
285 << "* _this = reinterpret_cast<" << _class->name << "*> (ptr);\n"
286 << ind << "delete _this;\n"
287 << ind << "return 0;\n"