X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=tools%2Fminiswig%2Fcreate_wrapper.cpp;h=1fba6e7bc1ab3555f1b35954e97bbd96ca652eac;hb=8f82a8e619346be81a3c1bf943baf68953f1f4e7;hp=a806de41104f7ea577e12dc693e776f7cedf8f18;hpb=0b585f0dc8e185bcf1a7e281108281f5b2b96706;p=supertux.git diff --git a/tools/miniswig/create_wrapper.cpp b/tools/miniswig/create_wrapper.cpp index a806de411..1fba6e7bc 100644 --- a/tools/miniswig/create_wrapper.cpp +++ b/tools/miniswig/create_wrapper.cpp @@ -1,115 +1,250 @@ -#include "tree.h" +#include "tree.hpp" #include #include #include -#include "create_wrapper.h" -#include "globals.h" +#include "create_wrapper.hpp" +#include "globals.hpp" void -WrapperCreator::create_wrapper(CompilationUnit* unit) +WrapperCreator::create_wrapper(Namespace* ns) { + std::string fromfile = original_file != "" ? original_file : inputfile; + + if(selected_namespace != "") { + ns_prefix = selected_namespace; + ns_prefix += "::"; + } + // hpp file hppout << "/**\n" - << " * WARNING: This file is automatically generated from '" - << inputfile << "' - do not change\n" + << " * WARNING: This file is automatically generated from:\n" + << " * '" << fromfile << "'\n" + << " * DO NOT CHANGE\n" << " */\n" - << "#ifndef __" << modulename << "_WRAPPER_HPP__\n" - << "#define __" << modulename << "_WRAPPER_HPP__\n" + << "#ifndef __" << modulename << "_WRAPPER_H__\n" + << "#define __" << modulename << "_WRAPPER_H__\n" << "\n" - << "#include \"WrapperUtil.hpp\"\n" - << "\n" - << "extern WrappedFunction " << modulename << "_global_functions[];\n" - << "extern WrappedClass " << modulename << "_classes[];\n" + << "#include \n" + << "#include \"wrapper.interface.hpp\"\n" << "\n" - << "#endif\n" + << "namespace SquirrelWrapper\n" + << "{\n" << "\n"; + + hppout << "void register_" << modulename << "_wrapper(HSQUIRRELVM v);\n" + << "\n"; + + for(std::vector::iterator i = ns->types.begin(); + i != ns->types.end(); ++i) { + AtomicType* type = *i; + Class* _class = dynamic_cast (type); + if(_class == 0) + continue; + + hppout << "void create_squirrel_instance(HSQUIRRELVM v, " + << ns_prefix << _class->name + << "* object, bool setup_releasehook = false);\n"; + } + hppout <<"\n" + << "}\n" + << "\n" + << "#endif\n" + << "\n"; // cpp header out << "/**\n" - << " * WARNING: This file is automatically generated from '" - << inputfile << "' - do not change\n" + << " * WARNING: This file is automatically generated from:\n" + << " * '" << fromfile << "'\n" + << " * DO NOT CHANGE\n" << " */\n" + << "#include \n" << "\n" << "#include \n" + << "#include \n" << "#include \n" << "#include \n" - << "#include \"WrapperUtil.hpp\"\n" - << "#include \"" << inputfile << "\"\n" + << "#include \"wrapper_util.hpp\"\n" + << "#include \"wrapper.interface.hpp\"\n" + << "\n" + << "namespace SquirrelWrapper\n" + << "{\n" << "\n"; - - for(std::vector::iterator i = unit->types.begin(); - i != unit->types.end(); ++i) { + + for(std::vector::iterator i = ns->types.begin(); + i != ns->types.end(); ++i) { AtomicType* type = *i; Class* _class = dynamic_cast (type); if(_class != 0) create_class_wrapper(_class); } - for(std::vector::iterator i = unit->functions.begin(); - i != unit->functions.end(); ++i) { + for(std::vector::iterator i = ns->functions.begin(); + i != ns->functions.end(); ++i) { create_function_wrapper(0, *i); } - // create function list... - out << "WrappedFunction " << modulename << "_global_functions[] = {\n"; - for(std::vector::iterator i = unit->functions.begin(); - i != unit->functions.end(); ++i) { - Function* function = *i; - out << ind << "{ \"" << function->name << "\", &" - << function->name << "_wrapper },\n"; - } - out << ind << "{ 0, 0 }\n" - << "};\n" + out << "void register_" << modulename << "_wrapper(HSQUIRRELVM v)\n" + << "{\n" + << ind << "sq_pushroottable(v);\n"; + + create_register_constants_code(ns); + create_register_functions_code(ns); + create_register_classes_code(ns); + + out << ind << "sq_pop(v, 1);\n" + << "}\n" + << "\n" + << "}\n" << "\n"; +} + +void +WrapperCreator::create_register_function_code(Function* function, Class* _class) +{ + if(function->type == Function::DESTRUCTOR) + return; + + out << ind << "sq_pushstring(v, \"" << function->name << "\", -1);\n"; + out << ind << "sq_newclosure(v, &" + << (_class != 0 ? _class->name + "_" : "") << function->name + << "_wrapper, 0);\n"; + create_register_slot_code("function", function->name); + out << "\n"; +} - // create class list... - std::ostringstream classlist; - classlist << "WrappedClass " << modulename << "_classes[] = {\n"; +void +WrapperCreator::create_register_functions_code(Namespace* ns) +{ + for(std::vector::iterator i = ns->functions.begin(); + i != ns->functions.end(); ++i) { + Function* function = *i; + create_register_function_code(function, 0); + } +} - for(std::vector::iterator i = unit->types.begin(); - i != unit->types.end(); ++i) { +void +WrapperCreator::create_register_classes_code(Namespace* ns) +{ + for(std::vector::iterator i = ns->types.begin(); + i != ns->types.end(); ++i) { AtomicType* type = *i; - Class* _class = dynamic_cast (type); + Class* _class = dynamic_cast (type); if(_class == 0) continue; + if(_class->super_classes.size() > 0) + continue; + + create_register_class_code(_class); + } +} + +void +WrapperCreator::create_register_class_code(Class* _class) +{ + out << ind << "// Register class " << _class->name << "\n"; + out << ind << "sq_pushstring(v, \"" + << _class->name << "\", -1);\n"; + + if(_class->super_classes.size() > 0) { + if(_class->super_classes.size() > 1) { + std::ostringstream msg; + msg << "Multiple inheritance not supported (at class '" + << _class->name << "')"; + throw std::runtime_error(msg.str()); + } - classlist << ind << "{ \"" << _class->name << "\", " - << modulename << "_" << _class->name - << "_methods },\n"; - - out << "static WrappedFunction " << modulename << "_" - << _class->name << "_methods[] = {\n"; - out << ind << "{ \"constructor\", &" - << _class->name << "_" << "construct_wrapper },\n"; - for(std::vector::iterator i = _class->members.begin(); - i != _class->members.end(); ++i) { - ClassMember* member = *i; - if(member->visibility != ClassMember::PUBLIC) - continue; - Function* function = dynamic_cast (member); - if(!function || function->type != Function::FUNCTION) - continue; + out << ind << "sq_pushstring(v, \"" + << _class->super_classes[0]->name << "\", -1);\n"; + out << ind << "sq_get(v, -3);\n"; + } + out << ind << "if(sq_newclass(v, " + << (_class->super_classes.size() > 0 ? "SQTrue" : "SQFalse") + << ") < 0) {\n"; + out << ind << ind << "std::ostringstream msg;\n"; + out << ind << ind << "msg << \"Couldn't create new class '" + << _class->name << "'\";\n"; + out << ind << ind << "throw SquirrelError(v, msg.str());\n"; + out << ind << "}\n"; - out << ind << "{ \"" << function->name << "\", &" - << _class->name << "_" << function->name << "_wrapper },\n"; + for(std::vector::iterator i = _class->members.begin(); + i != _class->members.end(); ++i) { + ClassMember* member = *i; + if(member->visibility != ClassMember::PUBLIC) + continue; + Function* function = dynamic_cast (member); + if(function) { + create_register_function_code(function, _class); + } + Field* field = dynamic_cast (member); + if(field) { + create_register_constant_code(field); } - classlist << ind << "{ 0, 0 }\n"; - out << "};\n" - << "\n"; } - classlist << "};\n"; - out << classlist.str(); + + create_register_slot_code("class", _class->name); + out << "\n"; + + for(std::vector::iterator i = _class->sub_classes.begin(); + i != _class->sub_classes.end(); ++i) { + Class* _class = *i; + create_register_class_code(_class); + } +} + +void +WrapperCreator::create_register_constants_code(Namespace* ns) +{ + for(std::vector::iterator i = ns->fields.begin(); + i != ns->fields.end(); ++i) { + Field* field = *i; + create_register_constant_code(field); + } +} + +void +WrapperCreator::create_register_constant_code(Field* field) +{ + if(!field->has_const_value) + return; + out << ind << "sq_pushstring(v, \"" << field->name << "\", -1);\n"; + if(field->type->atomic_type == &BasicType::INT) { + out << ind << "sq_pushinteger(v, " << field->const_int_value << ");\n"; + } else if(field->type->atomic_type == &BasicType::FLOAT) { + out << ind << "sq_pushfloat(v, " << field->const_float_value << ");\n"; + } else if(field->type->atomic_type == StringType::instance()) { + out << ind << "sq_pushstring(v, \"" + << field->const_string_value << "\", -1);\n"; + } else { + throw std::runtime_error("Constant is not int, float or string"); + } + create_register_slot_code("constant", field->name); out << "\n"; } void +WrapperCreator::create_register_slot_code(const std::string& what, + const std::string& name) +{ + out << ind << "if(sq_createslot(v, -3) < 0) {\n"; + out << ind << ind << "std::ostringstream msg;\n"; + out << ind << ind << "msg << \"Couldn't register " << what << "'" + << name << "'\";\n"; + out << ind << ind << "throw SquirrelError(v, msg.str());\n"; + out << ind << "}\n"; +} + +void WrapperCreator::create_function_wrapper(Class* _class, Function* function) { - if(function->type == Function::CONSTRUCTOR) - throw std::runtime_error("Constructors not supported yet"); if(function->type == Function::DESTRUCTOR) - throw std::runtime_error("Destructors not supported yet"); - + assert(false); + + std::string ns_prefix; + if(selected_namespace != "") + ns_prefix = selected_namespace + "::"; + if(function->type == Function::CONSTRUCTOR) + function->name = "constructor"; + out << "static int "; if(_class != 0) { out << _class->name << "_"; @@ -118,27 +253,33 @@ WrapperCreator::create_function_wrapper(Class* _class, Function* function) << "{\n"; // avoid warning... if(_class == 0 && function->parameters.empty() - && function->return_type.is_void()) { + && function->return_type.is_void() + && function->type != Function::CONSTRUCTOR) { out << ind << "(void) v;\n"; } - // eventually retrieve pointer to class - if(_class != 0) { - out << ind << _class->name << "* _this;\n"; + // eventually retrieve pointer to class instance + if(_class != 0 && function->type != Function::CONSTRUCTOR) { + out << ind << ns_prefix << _class->name << "* _this;\n"; out << ind << "sq_getinstanceup(v, 1, (SQUserPointer*) &_this, 0);\n"; - out << ind << "assert(_this != 0);\n"; } // declare and retrieve arguments - size_t i = 0; + int i = 0; + int arg_offset = 2; for(std::vector::iterator p = function->parameters.begin(); p != function->parameters.end(); ++p) { - char argname[64]; - snprintf(argname, sizeof(argname), "arg%u", i); - prepare_argument(p->type, i + 2, argname); - + if(i == 0 && p->type.atomic_type == HSQUIRRELVMType::instance()) { + out << ind << "HSQUIRRELVM arg0 = v;\n"; + arg_offset--; + } else { + char argname[64]; + snprintf(argname, sizeof(argname), "arg%d", i); + prepare_argument(p->type, i + arg_offset, argname); + } ++i; } + // call function out << ind << "\n"; out << ind; @@ -147,18 +288,41 @@ WrapperCreator::create_function_wrapper(Class* _class, Function* function) out << " return_value = "; } if(_class != 0) { - out << "_this->"; + if(function->type == Function::CONSTRUCTOR) { + out << ns_prefix << _class->name << "* _this = new " << ns_prefix; + } else { + out << "_this->"; + } + } else { + out << ns_prefix; + } + if(function->type == Function::CONSTRUCTOR) { + out << _class->name << "("; + } else { + out << function->name << "("; } - out << function->name << "("; for(size_t i = 0; i < function->parameters.size(); ++i) { if(i != 0) out << ", "; out << "arg" << i; } out << ");\n"; + if(function->type == Function::CONSTRUCTOR) { + out << ind << "sq_setinstanceup(v, 1, _this);\n"; + out << ind << "sq_setreleasehook(v, 1, " + << _class->name << "_release_hook);\n"; + } out << ind << "\n"; // push return value back on stack and return - if(function->return_type.is_void()) { + if(function->suspend) { + if(!function->return_type.is_void()) { + std::stringstream msg; + msg << "Function '" << function->name << "' declared as suspend" + << " but has a return value."; + throw std::runtime_error(msg.str()); + } + out << ind << "return sq_suspendvm(v);\n"; + } else if(function->return_type.is_void()) { out << ind << "return 0;\n"; } else { push_to_stack(function->return_type, "return_value"); @@ -222,8 +386,8 @@ WrapperCreator::push_to_stack(const Type& type, const std::string& var) void WrapperCreator::create_class_wrapper(Class* _class) { - create_class_destruct_function(_class); - create_class_construct_function(_class); + create_class_release_hook(_class); + create_squirrel_instance(_class); for(std::vector::iterator i = _class->members.begin(); i != _class->members.end(); ++i) { ClassMember* member = *i; @@ -232,37 +396,53 @@ WrapperCreator::create_class_wrapper(Class* _class) Function* function = dynamic_cast (member); if(!function) continue; - // don't wrap constructors and destructors (for now...) - if(function->type != Function::FUNCTION) + // don't wrap destructors + if(function->type == Function::DESTRUCTOR) continue; create_function_wrapper(_class, function); } } void -WrapperCreator::create_class_construct_function(Class* _class) +WrapperCreator::create_squirrel_instance(Class* _class) { - out << "static int " << _class->name << "_construct_wrapper(HSQUIRRELVM v)\n"; - out << "{\n"; - out << ind << _class->name << "* _this = new " - << _class->name << "();\n"; - out << ind << "sq_setinstanceup(v, 1, _this);\n"; - out << ind << "sq_setreleasehook(v, 1, " - << _class->name << "_release_wrapper);\n"; - out << "\n"; - out << ind << "return 0;\n"; - out << "}\n"; - out << "\n"; + out << "void create_squirrel_instance(HSQUIRRELVM v, " + << ns_prefix << _class->name + << "* object, bool setup_releasehook)\n" + << "{\n" + << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n" + << ind << "if(sq_get(v, -2) < 0) {\n" + << ind << ind << "std::ostringstream msg;\n" + << ind << ind << "msg << \"Couldn't resolved squirrel type '" + << _class->name << "'\";\n" + << ind << ind << "throw SquirrelError(v, msg.str());\n" + << ind << "}\n" + << "\n" + << ind << "if(sq_createinstance(v, -1) < 0 || " + << "sq_setinstanceup(v, -1, object) < 0) {\n" + << ind << ind << "std::ostringstream msg;\n" + << ind << ind << "msg << \"Couldn't setup squirrel instance for " + << "object of type '" << _class->name << "'\";\n" + << ind << ind << "throw SquirrelError(v, msg.str());\n" + << ind << "}\n" + << ind << "sq_remove(v, -2);\n" + << "\n" + << ind << "if(setup_releasehook) {\n" + << ind << ind << "sq_setreleasehook(v, -1, " + << _class->name << "_release_hook);\n" + << ind << "}\n" + << "}\n"; } void -WrapperCreator::create_class_destruct_function(Class* _class) +WrapperCreator::create_class_release_hook(Class* _class) { - out << "static int " << _class->name << "_release_wrapper(SQUserPointer ptr, int )\n" + out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n" << "{\n" - << ind << _class->name - << "* _this = reinterpret_cast<" << _class->name << "*> (ptr);\n" - << ind << "_this->~" << _class->name << "();\n" + << ind << ns_prefix << _class->name + << "* _this = reinterpret_cast<" << ns_prefix << _class->name + << "*> (ptr);\n" + << ind << "delete _this;\n" << ind << "return 0;\n" << "}\n" << "\n";