*include stdio.h in a couple files for MinGW GCC 4.4.0
[supertux.git] / tools / miniswig / create_wrapper.cpp
index 64318fd..d5912e2 100644 (file)
@@ -1,11 +1,13 @@
 #include <config.h>
 
 #include "tree.hpp"
+#include "create_wrapper.hpp"
+#include "globals.hpp"
+
+#include <stdio.h>
 #include <iostream>
 #include <sstream>
 #include <stdexcept>
-#include "create_wrapper.hpp"
-#include "globals.hpp"
 
 void
 WrapperCreator::create_wrapper(Namespace* ns)
@@ -28,7 +30,6 @@ WrapperCreator::create_wrapper(Namespace* ns)
         << "#define __" << modulename << "_WRAPPER_H__\n"
         << "\n"
         << "#include <squirrel.h>\n"
-        << "#include \"wrapper.interface.hpp\"\n"
         << "\n"
         << "namespace Scripting\n"
         << "{\n"
@@ -44,6 +45,7 @@ WrapperCreator::create_wrapper(Namespace* ns)
         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";
@@ -51,8 +53,7 @@ WrapperCreator::create_wrapper(Namespace* ns)
     hppout <<"\n"
            << "}\n"
            << "\n"
-           << "#endif\n"
-           << "\n";
+           << "#endif\n";
 
     // cpp header
     out << "/**\n"
@@ -89,7 +90,6 @@ WrapperCreator::create_wrapper(Namespace* ns)
     }
 
     out << "} // end of namespace Wrapper\n";
-    out << "\n";
 
     for(std::vector<AtomicType*>::iterator i = ns->types.begin();
             i != ns->types.end(); ++i) {
@@ -110,8 +110,7 @@ WrapperCreator::create_wrapper(Namespace* ns)
 
     out << "}\n"
         << "\n"
-        << "} // end of namespace Scripting\n"
-        << "\n";
+        << "} // end of namespace Scripting\n";
 }
 
 void
@@ -124,6 +123,43 @@ WrapperCreator::create_register_function_code(Function* function, Class* _class)
     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<Parameter>::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";
 }
@@ -275,7 +311,7 @@ WrapperCreator::create_function_wrapper(Class* _class, Function* function)
     // retrieve pointer to class instance
     if(_class != 0 && function->type != Function::CONSTRUCTOR) {
         out << ind << "SQUserPointer data;\n";
-        out << ind << "if(SQ_FAILED(sq_getinstanceup(vm, 1, &data, 0))) {\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";
@@ -284,14 +320,14 @@ WrapperCreator::create_function_wrapper(Class* _class, Function* function)
 
     // 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)
@@ -321,7 +357,7 @@ WrapperCreator::create_function_wrapper(Class* _class, Function* function)
     }
 
     // call function
-    out << ind << "\n";
+    out << "\n";
     out << ind << "try {\n";
     out << ind << ind;
     if(!function->return_type.is_void()) {
@@ -368,7 +404,7 @@ WrapperCreator::create_function_wrapper(Class* _class, Function* function)
         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()) {
@@ -385,7 +421,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";
@@ -393,7 +429,7 @@ 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";