miniswig suspend is now an attribute appended to functions
[supertux.git] / tools / miniswig / create_wrapper.cpp
index 5186f66..1fba6e7 100644 (file)
@@ -10,6 +10,11 @@ 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"
@@ -21,35 +26,12 @@ WrapperCreator::create_wrapper(Namespace* ns)
         << "#define __" << modulename << "_WRAPPER_H__\n"
         << "\n"
         << "#include <squirrel.h>\n"
+        << "#include \"wrapper.interface.hpp\"\n"
+        << "\n"
+        << "namespace SquirrelWrapper\n"
+        << "{\n"
         << "\n";
-    if(selected_namespace != "") {
-      hppout << "namespace " << selected_namespace << "\n"
-             << "{\n";
-    }
-    for(std::vector<AtomicType*>::iterator i = ns->types.begin();
-            i != ns->types.end(); ++i) {
-        AtomicType* type = *i;
-        Class* _class = dynamic_cast<Class*> (type);                 
-        if(_class == 0)
-          continue;
 
-        hppout << "class " << _class->name << ";\n";
-    }
-
-    if(selected_namespace != "") {
-      hppout << "}\n";
-    }
-    
-    hppout << "\n"
-           << "namespace SquirrelWrapper\n"
-           << "{\n"
-           << "\n";
-
-    if(selected_namespace != "") {
-      hppout << "using namespace " << selected_namespace << ";\n"
-             << "\n";
-    }
-    
     hppout << "void register_" << modulename << "_wrapper(HSQUIRRELVM v);\n"
            << "\n";
 
@@ -61,7 +43,7 @@ WrapperCreator::create_wrapper(Namespace* ns)
             continue;
 
         hppout << "void create_squirrel_instance(HSQUIRRELVM v, "
-               << _class->name
+               << ns_prefix << _class->name
                << "* object, bool setup_releasehook = false);\n";
     }
     hppout <<"\n"
@@ -89,11 +71,6 @@ WrapperCreator::create_wrapper(Namespace* ns)
         << "{\n"
         << "\n";
 
-    if(selected_namespace != "") {
-        out << "using namespace " << selected_namespace << ";\n";
-        out << "\n";
-    }
-    
     for(std::vector<AtomicType*>::iterator i = ns->types.begin();
             i != ns->types.end(); ++i) {
         AtomicType* type = *i;
@@ -337,12 +314,16 @@ 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->docu_comment.find("@SUSPEND@") != std::string::npos) {
-          out << ind << "return sq_suspendvm(v);\n";
-        } else {
-          out << ind << "return 0;\n";
+    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");
         out << ind << "return 1;\n";
@@ -426,7 +407,7 @@ void
 WrapperCreator::create_squirrel_instance(Class* _class)
 {
     out << "void create_squirrel_instance(HSQUIRRELVM v, "
-        << _class->name 
+        << ns_prefix << _class->name 
         << "* object, bool setup_releasehook)\n"
         << "{\n"
         << ind << "sq_pushstring(v, \"" << _class->name << "\", -1);\n"
@@ -444,6 +425,7 @@ WrapperCreator::create_squirrel_instance(Class* _class)
         << "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, "
@@ -457,8 +439,9 @@ 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"