X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=tools%2Fminiswig%2Fcreate_wrapper.cpp;h=1fba6e7bc1ab3555f1b35954e97bbd96ca652eac;hb=8f82a8e619346be81a3c1bf943baf68953f1f4e7;hp=330792d2a232b1eefe96450e9813b3c165a07a90;hpb=5b7f9214cb929399f1a855ef5807018a9447d510;p=supertux.git diff --git a/tools/miniswig/create_wrapper.cpp b/tools/miniswig/create_wrapper.cpp index 330792d2a..1fba6e7bc 100644 --- a/tools/miniswig/create_wrapper.cpp +++ b/tools/miniswig/create_wrapper.cpp @@ -1,15 +1,20 @@ -#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(Namespace* ns) { std::string fromfile = original_file != "" ? original_file : inputfile; + if(selected_namespace != "") { + ns_prefix = selected_namespace; + ns_prefix += "::"; + } + // hpp file hppout << "/**\n" @@ -20,13 +25,32 @@ WrapperCreator::create_wrapper(Namespace* ns) << "#ifndef __" << modulename << "_WRAPPER_H__\n" << "#define __" << modulename << "_WRAPPER_H__\n" << "\n" - << "#include \"wrapper_util.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" @@ -42,12 +66,11 @@ WrapperCreator::create_wrapper(Namespace* ns) << "#include \n" << "#include \"wrapper_util.hpp\"\n" << "#include \"wrapper.interface.hpp\"\n" + << "\n" + << "namespace SquirrelWrapper\n" + << "{\n" << "\n"; - if(selected_namespace != "") { - out << "using namespace " << selected_namespace << ";\n"; - out << "\n"; - } - + for(std::vector::iterator i = ns->types.begin(); i != ns->types.end(); ++i) { AtomicType* type = *i; @@ -60,54 +83,154 @@ WrapperCreator::create_wrapper(Namespace* ns) create_function_wrapper(0, *i); } - // create function list... - out << "WrappedFunction " << modulename << "_global_functions[] = {\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"; +} + +void +WrapperCreator::create_register_functions_code(Namespace* ns) +{ for(std::vector::iterator i = ns->functions.begin(); i != ns->functions.end(); ++i) { Function* function = *i; - out << ind << "{ \"" << function->name << "\", &" - << function->name << "_wrapper },\n"; + create_register_function_code(function, 0); } - out << ind << "{ 0, 0 }\n" - << "};\n" - << "\n"; - - // create class list... - std::ostringstream classlist; - classlist << "WrappedClass " << modulename << "_classes[] = {\n"; +} +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"; - 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::DESTRUCTOR) - 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); } - out << "};\n" - << "\n"; } - classlist << ind << "{ 0, 0 }\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 @@ -148,7 +271,7 @@ WrapperCreator::create_function_wrapper(Class* _class, Function* function) p != function->parameters.end(); ++p) { if(i == 0 && p->type.atomic_type == HSQUIRRELVMType::instance()) { out << ind << "HSQUIRRELVM arg0 = v;\n"; - arg_offset++; + arg_offset--; } else { char argname[64]; snprintf(argname, sizeof(argname), "arg%d", i); @@ -191,7 +314,15 @@ WrapperCreator::create_function_wrapper(Class* _class, Function* function) } 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"); @@ -255,7 +386,8 @@ WrapperCreator::push_to_stack(const Type& type, const std::string& var) void WrapperCreator::create_class_wrapper(Class* _class) { - bool release_hook_created = false; + 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; @@ -264,11 +396,6 @@ WrapperCreator::create_class_wrapper(Class* _class) Function* function = dynamic_cast (member); if(!function) continue; - if(function->type == Function::CONSTRUCTOR - && !release_hook_created) { - create_class_release_hook(_class); - release_hook_created = true; - } // don't wrap destructors if(function->type == Function::DESTRUCTOR) continue; @@ -277,12 +404,44 @@ WrapperCreator::create_class_wrapper(Class* _class) } void +WrapperCreator::create_squirrel_instance(Class* _class) +{ + 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_release_hook(Class* _class) { out << "static int " << _class->name << "_release_hook(SQUserPointer ptr, int )\n" << "{\n" - << ind << _class->name - << "* _this = reinterpret_cast<" << _class->name << "*> (ptr);\n" + << ind << ns_prefix << _class->name + << "* _this = reinterpret_cast<" << ns_prefix << _class->name + << "*> (ptr);\n" << ind << "delete _this;\n" << ind << "return 0;\n" << "}\n"