X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=tools%2Fminiswig%2Fcreate_wrapper.cpp;h=826a9aba88138a079fb8d92ce8a2f43aaa28283e;hb=2b318b2284a78ae34a6752a807c4eb7b5632299c;hp=53e5b280c7511d5df8dd874ffee645ba60e686a4;hpb=23ff4a42c108309fda259e6da2f1502ad50c3890;p=supertux.git diff --git a/tools/miniswig/create_wrapper.cpp b/tools/miniswig/create_wrapper.cpp index 53e5b280c..826a9aba8 100644 --- a/tools/miniswig/create_wrapper.cpp +++ b/tools/miniswig/create_wrapper.cpp @@ -1,11 +1,13 @@ #include #include "tree.hpp" +#include "create_wrapper.hpp" +#include "globals.hpp" + #include #include #include -#include "create_wrapper.hpp" -#include "globals.hpp" +#include void WrapperCreator::create_wrapper(Namespace* ns) @@ -24,14 +26,10 @@ WrapperCreator::create_wrapper(Namespace* ns) << " * '" << fromfile << "'\n" << " * DO NOT CHANGE\n" << " */\n" - << "#ifndef __" << modulename << "_WRAPPER_H__\n" - << "#define __" << modulename << "_WRAPPER_H__\n" + << "#ifndef HEADER_SUPERTUX_SCRIPTING_WRAPPER_HPP\n" //TODO avoid hardcoding + << "#define HEADER_SUPERTUX_SCRIPTING_WRAPPER_HPP\n" << "\n" - << "#include \n" - << "#include \"wrapper.interface.hpp\"\n" - << "\n" - << "namespace Scripting\n" - << "{\n" + << "namespace Scripting {\n" << "\n"; hppout << "void register_" << modulename << "_wrapper(HSQUIRRELVM v);\n" @@ -40,10 +38,11 @@ WrapperCreator::create_wrapper(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; + hppout << "class " << _class->name << ";\n"; hppout << "void create_squirrel_instance(HSQUIRRELVM v, " << ns_prefix << _class->name << "* object, bool setup_releasehook = false);\n"; @@ -52,28 +51,23 @@ WrapperCreator::create_wrapper(Namespace* ns) << "}\n" << "\n" << "#endif\n" - << "\n"; - + << "\n" + << "/* EOF */\n"; + // cpp header out << "/**\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 \n" - << "#include \"squirrel_error.hpp\"\n" - << "#include \"wrapper.interface.hpp\"\n" << "\n" - << "namespace Scripting\n" - << "{\n" - << "namespace Wrapper\n" - << "{\n" + << "#include \"scripting/squirrel_error.hpp\"\n" + << "#include \"scripting/wrapper.interface.hpp\"\n" + << "\n" + << "namespace Scripting {\n" + << "namespace Wrapper {\n" << "\n"; for(std::vector::iterator i = ns->types.begin(); @@ -89,16 +83,15 @@ WrapperCreator::create_wrapper(Namespace* ns) } out << "} // end of namespace Wrapper\n"; - out << "\n"; for(std::vector::iterator i = ns->types.begin(); - i != ns->types.end(); ++i) { + i != ns->types.end(); ++i) { AtomicType* type = *i; Class* _class = dynamic_cast (type); if(_class != 0) create_squirrel_instance(_class); } - + out << "void register_" << modulename << "_wrapper(HSQUIRRELVM v)\n" << "{\n" << ind << "using namespace Wrapper;\n" @@ -111,7 +104,8 @@ WrapperCreator::create_wrapper(Namespace* ns) out << "}\n" << "\n" << "} // end of namespace Scripting\n" - << "\n"; + << "\n" + << "/* EOF */\n"; } void @@ -119,13 +113,50 @@ 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 + out << ind << "sq_newclosure(v, &" + << (_class != 0 ? _class->name + "_" : "") << function->name << "_wrapper, 0);\n"; + + if(function->custom) { + out << ind << "sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, " << function->parameter_spec << ");\n"; + } else { + out << ind << "sq_setparamscheck(v, SQ_MATCHTYPEMASKSTRING, \""; + + out << "x|t"; + + if(!function->parameters.empty()) + { + std::vector::iterator p = function->parameters.begin(); + + // Skip the first parameter since its a HSQUIRRELVM that is + // handled internally + if (function->suspend) { + ++p; + } else if (p->type.atomic_type == HSQUIRRELVMType::instance()) { + ++p; + } + + for(; p != function->parameters.end(); ++p) { + if(p->type.atomic_type == &BasicType::INT) { + out << "i"; + } else if(p->type.atomic_type == &BasicType::FLOAT) { + out << "n"; + } else if(p->type.atomic_type == &BasicType::BOOL) { + out << "b"; + } else if(p->type.atomic_type == StringType::instance()) { + out << "s"; + } else { + out << "."; + } + } + } + out << "\");\n"; + } + create_register_slot_code("function", function->name); - out << "\n"; + out << "\n"; } void @@ -144,7 +175,7 @@ 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) @@ -158,9 +189,9 @@ void WrapperCreator::create_register_class_code(Class* _class) { out << ind << "// Register class " << _class->name << "\n"; - out << ind << "sq_pushstring(v, \"" - << _class->name << "\", -1);\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; @@ -168,7 +199,7 @@ WrapperCreator::create_register_class_code(Class* _class) << _class->name << "')"; throw std::runtime_error(msg.str()); } - + out << ind << "sq_pushstring(v, \"" << _class->super_classes[0]->name << "\", -1);\n"; out << ind << "sq_get(v, -3);\n"; @@ -177,7 +208,7 @@ WrapperCreator::create_register_class_code(Class* _class) << (_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 '" + out << ind << ind << "msg << \"Couldn't create new class '" << _class->name << "'\";\n"; out << ind << ind << "throw SquirrelError(v, msg.str());\n"; out << ind << "}\n"; @@ -199,7 +230,7 @@ WrapperCreator::create_register_class_code(Class* _class) 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; @@ -266,31 +297,32 @@ WrapperCreator::create_function_wrapper(Class* _class, Function* function) out << function->name << "_wrapper(HSQUIRRELVM vm)\n" << "{\n"; // avoid warning... - if(_class == 0 && function->parameters.empty() + if(_class == 0 && function->parameters.empty() && function->return_type.is_void() && function->type != Function::CONSTRUCTOR) { out << ind << "(void) vm;\n"; } - + // retrieve pointer to class instance if(_class != 0 && function->type != Function::CONSTRUCTOR) { - out << ind << ns_prefix << _class->name << "* _this;\n"; - out << ind << "if(SQ_FAILED(sq_getinstanceup(vm, 1, reinterpret_cast (&_this), 0))) {\n"; + out << ind << "SQUserPointer data;\n"; + out << ind << "if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0)) || !data) {\n"; out << ind << ind << "sq_throwerror(vm, _SC(\"'" << function->name << "' called without instance\"));\n"; out << ind << ind << "return SQ_ERROR;\n"; out << ind << "}\n"; + out << ind << ns_prefix << _class->name << "* _this = reinterpret_cast<" << ns_prefix << _class->name << "*> (data);\n"; } // custom function? if(function->custom) { - if(function->type != Function::FUNCTION) + if(function->type != Function::FUNCTION) throw std::runtime_error( "custom not allow constructor+destructor yet"); if(function->return_type.atomic_type != SQIntegerType::instance()) - throw std::runtime_error("custom function has to return SQInteger"); + throw std::runtime_error("custom function '" + function->name + "' has to return SQInteger"); if(function->parameters.size() != 1) throw std::runtime_error( - "custom function must have 1 HSQUIRRELVM parameter"); + "custom function '" + function->name + "' must have 1 HSQUIRRELVM parameter"); out << ind << "return "; if(_class != 0) @@ -302,7 +334,7 @@ WrapperCreator::create_function_wrapper(Class* _class, Function* function) out << "\n"; return; } - + // declare and retrieve arguments int i = 0; int arg_offset = 2; @@ -318,9 +350,9 @@ WrapperCreator::create_function_wrapper(Class* _class, Function* function) } ++i; } - + // call function - out << ind << "\n"; + out << "\n"; out << ind << "try {\n"; out << ind << ind; if(!function->return_type.is_void()) { @@ -364,10 +396,10 @@ WrapperCreator::create_function_wrapper(Class* _class, Function* function) out << ind << ind << "sq_throwerror(vm, _SC(\"Couldn't setup instance of '" << _class->name << "' class\"));\n"; out << ind << ind << "return SQ_ERROR;\n"; out << ind << "}\n"; - out << ind << "sq_setreleasehook(vm, 1, " + out << ind << "sq_setreleasehook(vm, 1, " << _class->name << "_release_hook);\n"; } - out << ind << "\n"; + out << "\n"; // push return value back on stack and return if(function->suspend) { if(!function->return_type.is_void()) { @@ -384,7 +416,7 @@ WrapperCreator::create_function_wrapper(Class* _class, Function* function) out << ind << ind << "return 1;\n"; } - out << ind << "\n"; + out << "\n"; out << ind << "} catch(std::exception& e) {\n"; out << ind << ind << "sq_throwerror(vm, e.what());\n"; out << ind << ind << "return SQ_ERROR;\n"; @@ -392,8 +424,8 @@ WrapperCreator::create_function_wrapper(Class* _class, Function* function) out << ind << ind << "sq_throwerror(vm, _SC(\"Unexpected exception while executing function '" << function->name << "'\"));\n"; out << ind << ind << "return SQ_ERROR;\n"; out << ind << "}\n"; - out << ind << "\n"; - + out << "\n"; + out << "}\n"; out << "\n"; } @@ -452,7 +484,7 @@ WrapperCreator::push_to_stack(const Type& type, const std::string& var) } else if(type.atomic_type == &BasicType::BOOL) { out << "sq_pushbool(vm, " << var << ");\n"; } else if(type.atomic_type == StringType::instance()) { - out << "sq_pushstring(vm, " << var << ".c_str(), " + out << "sq_pushstring(vm, " << var << ".c_str(), " << var << ".size());\n"; } else { std::ostringstream msg; @@ -484,7 +516,7 @@ void WrapperCreator::create_squirrel_instance(Class* _class) { out << "void create_squirrel_instance(HSQUIRRELVM v, " - << ns_prefix << _class->name + << ns_prefix << _class->name << "* object, bool setup_releasehook)\n" << "{\n" << ind << "using namespace Wrapper;\n" @@ -520,14 +552,13 @@ WrapperCreator::create_squirrel_instance(Class* _class) void WrapperCreator::create_class_release_hook(Class* _class) { - out << "static SQInteger " << _class->name << "_release_hook(SQUserPointer ptr, int )\n" + out << "static SQInteger " << _class->name << "_release_hook(SQUserPointer ptr, SQInteger )\n" << "{\n" - << ind << ns_prefix << _class->name - << "* _this = reinterpret_cast<" << ns_prefix << _class->name + << ind << ns_prefix << _class->name + << "* _this = reinterpret_cast<" << ns_prefix << _class->name << "*> (ptr);\n" << ind << "delete _this;\n" << ind << "return 0;\n" << "}\n" << "\n"; } -