bindings: Fixed support for C++ instantiating objects from eC
[sdk] / compiler / ecs / ecs.ec
index a583d36..225f0d6 100644 (file)
@@ -322,7 +322,6 @@ static void WriteMain(const char * fileName)
             Class regClass = eSystem_FindClass(privateModule, _class.name);
 
             FullClassNameCat(className, _class.name, true);
-            //MangleClassName(className);
 
             if(_class.itself)
                f.Printf("Class __ecereClass_%s;\n", className);
@@ -373,8 +372,6 @@ static void WriteMain(const char * fileName)
                   char propName[1024];
                   propName[0] = 0;
                   FullClassNameCat(propName, prop.name, true);
-                  // strcpy(propName, prop.name);
-                  //MangleClassName(propName);
 
                   if((!strcmp(_class.name, "float") || !strcmp(_class.name, "double") || module.name) && module.importType != staticImport)
                   {
@@ -463,17 +460,18 @@ static void WriteMain(const char * fileName)
       {
          f.Puts("   int exitCode;\n");
          f.Puts("   Module module;\n");
+         f.Puts("   bool setThingsUp = !__thisModule;\n");
       }
 
       //if(nonInst || thisAppClass)    // We use it all the time to get "Application" for the exit code now...
          f.Puts("   __attribute__((unused)) Class _class;\n");
 
       if(anyMethod)
-         f.Puts("   Method method;\n");
+         f.Puts("   __attribute__((unused)) Method method;\n");
       if(anyProp)
-         f.Puts("   Property _property;\n");
+         f.Puts("   __attribute__((unused)) Property _property;\n");
       if(anyFunction)
-         f.Puts("   GlobalFunction function;\n");
+         f.Puts("   __attribute__((unused)) GlobalFunction function;\n");
 
       f.Puts("\n");
 
@@ -492,10 +490,15 @@ static void WriteMain(const char * fileName)
             f.Puts("      __thisModule = module;\n");
          f.Puts("   }\n\n");
       }
-      else if(targetPlatform == win32 && !isConsole)
-         f.Puts("   __thisModule = __currentModule = module = __ecere_COM_Initialize(1, 0, null);\n\n");
       else
-         f.Puts("   __thisModule = __currentModule = module = __ecere_COM_Initialize(1, _argc, (void *)_argv);\n\n");
+      {
+         f.Puts("   if(setThingsUp)\n");
+         if(targetPlatform == win32 && !isConsole)
+            f.Puts("      __thisModule = __ecere_COM_Initialize(1, 0, null);\n\n");
+         else
+            f.Puts("      __thisModule = __ecere_COM_Initialize(1, _argc, (void *)_argv);\n\n");
+         f.Puts("   __currentModule = module = __thisModule;\n");
+      }
 
       // First load all modules
       if(_imports.count)
@@ -559,7 +562,6 @@ static void WriteMain(const char * fileName)
                   char classID[1024];
                   char className[1024] = "";
                   FullClassNameCat(className, _class.name, true);
-                  //MangleClassName(className);
 
                   if(_class.itself)
                      sprintf(classID, "__ecereClass_%s", className);
@@ -593,8 +595,6 @@ static void WriteMain(const char * fileName)
                      char propName[1024];
                      propName[0] = 0;
                      FullClassNameCat(propName, prop.name, true);
-                     // strcpy(propName, prop.name);
-                     //MangleClassName(propName);
 
                      f.Printf("   __ecereProp_%s_%s = _property = eClass_FindProperty(%s, \"%s\", module);\n",
                         className, propName, classID, prop.name);
@@ -657,7 +657,7 @@ static void WriteMain(const char * fileName)
       if(!isDynamicLibrary && thisAppClass)
       {
          f.Printf("   _class = eSystem_FindClass(__currentModule, \"%s\");\n", thisAppClass.name);
-         f.Printf("   eInstance_Evolve((Instance *)&__currentModule, _class);\n");
+         f.Printf("   if(setThingsUp) eInstance_Evolve((Instance *)&__currentModule, _class);\n");
          f.Printf("   __thisModule = __currentModule;\n");
       }
 
@@ -722,7 +722,7 @@ static void WriteMain(const char * fileName)
          bool destroyI18n = false;
          if(::modules.count)
          {
-            for(defModule = ::modules.first; defModule; defModule = defModule.next)
+            for(defModule = ::modules.last; defModule; defModule = defModule.prev)
                if(defModule.globalInstance)
                {
                   char moduleName[1024];
@@ -853,7 +853,7 @@ static void BindDCOMClient()
          int vid;
          bool doVirtual;
 
-         DeclareClass(FindClass("ecere::net::DCOMClientObject"), "__ecereClass___ecereNameSpace__ecere__net__DCOMClientObject");
+         DeclareClass(null, FindClass("ecere::net::DCOMClientObject"), "__ecereClass___ecereNameSpace__ecere__net__DCOMClientObject");
          f.Printf("class %s : ecere::net::DCOMClientObject\n", _class.fullName);
          f.Printf("{\n");
 
@@ -902,8 +902,7 @@ static void BindDCOMClient()
                            }
                            strcpy(className, "__ecereClass_");
                            FullClassNameCat(className, classSym.string, true);
-                           //MangleClassName(className);
-                           DeclareClass(classSym, className);
+                           DeclareClass(null, classSym, className);
 
                            PrintType(method.dataType.returnType, type, true, true);
 
@@ -921,8 +920,8 @@ static void BindDCOMClient()
                            {
                               // Hardcode 1024 chars max string for now
                               f.Printf("            char %s[1024];\n", param.name);
-                              DeclareClass(FindClass("StaticString"), "__ecereClass_StaticString");
-                              DeclareClass(FindClass("String"), "__ecereClass_String");
+                              DeclareClass(null, FindClass("StaticString"), "__ecereClass_StaticString");
+                              DeclareClass(null, FindClass("String"), "__ecereClass_String");
                            }
                            else
                            {
@@ -944,8 +943,7 @@ static void BindDCOMClient()
 
                               strcpy(className, "__ecereClass_");
                               FullClassNameCat(className, classSym.string, true);
-                              //MangleClassName(className);
-                              DeclareClass(classSym, className);
+                              DeclareClass(null, classSym, className);
 
                               PrintType(param, type, true, true);
 
@@ -965,7 +963,7 @@ static void BindDCOMClient()
                            f.Printf("            __ecereBuffer.Unserialize(");
                            if(param.kind == classType && !strcmp(param._class.string, "String"))
                            {
-                              DeclareClass(FindClass("StaticString"), "__ecereClass_StaticString");
+                              DeclareClass(null, FindClass("StaticString"), "__ecereClass_StaticString");
                               f.Printf("(StaticString)");
                            }
                            f.Puts(param.name);
@@ -994,7 +992,7 @@ static void BindDCOMClient()
                            {
                               if(!strcmp(param._class.string, "String"))
                               {
-                                 DeclareClass(FindClass("StaticString"), "__ecereClass_StaticString");
+                                 DeclareClass(null, FindClass("StaticString"), "__ecereClass_StaticString");
                                  f.Printf("            __ecereBuffer.Serialize((StaticString)%s);\n", param.name);
                               }
                               else
@@ -1067,7 +1065,7 @@ static void BindDCOMClient()
                   FullClassNameCat(name, method._class.fullName, true);
                   strcat(name, "_");
                   strcat(name, method.name);
-                  DeclareMethod(method, name);
+                  DeclareMethod(null, method, name);
 
                   f.Printf("virtual ");
                }
@@ -1096,8 +1094,7 @@ static void BindDCOMClient()
 
                      strcpy(className, "__ecereClass_");
                      FullClassNameCat(className, classSym.string, true);
-                     //MangleClassName(className);
-                     DeclareClass(classSym, className);
+                     DeclareClass(null, classSym, className);
 
                      PrintType(method.dataType.returnType, type, true, true);
 
@@ -1114,7 +1111,8 @@ static void BindDCOMClient()
                         f.Printf(" = 0");
                      f.Printf(";\n\n");
                   }
-                  f.Printf("      incref this;\n");
+                  // f.Printf("      incref this;\n");
+                  f.Printf("      safeIncRef();\n");
                   for(param = method.dataType.params.first; param; param = param.next)
                   {
                      char type[1024] = "";
@@ -1132,18 +1130,17 @@ static void BindDCOMClient()
                      strcpy(className, "__ecereClass_");
                      FullClassNameCat(className, classSym.string, true);
 
-                     //MangleClassName(className);
-                     DeclareClass(classSym, className);
+                     DeclareClass(null, classSym, className);
 
                      if(param.kind == classType && !strcmp(param._class.string, "String"))
                      {
-                        DeclareClass(FindClass("StaticString"), "__ecereClass_StaticString");
+                        DeclareClass(null, FindClass("StaticString"), "__ecereClass_StaticString");
                         f.Printf("      __ecereBuffer.Serialize((StaticString)%s);\n", param.name);
                      }
                      else
                         f.Printf("      __ecereBuffer.Serialize(%s);\n", param.name);
                   }
-                  DeclareMethod(
+                  DeclareMethod(null,
                      eClass_FindMethod(
                         eSystem_FindClass(privateModule, "ecere::net::DCOMClientObject"), "CallMethod", privateModule),
                      "__ecereMethod___ecereNameSpace__ecere__net__DCOMClientObject_CallMethod");
@@ -1156,7 +1153,7 @@ static void BindDCOMClient()
                      {
                         if(!strcmp(param._class.string, "String"))
                         {
-                           DeclareClass(FindClass("StaticString"), "__ecereClass_StaticString");
+                           DeclareClass(null, FindClass("StaticString"), "__ecereClass_StaticString");
                            f.Printf("         __ecereBuffer.Unserialize((StaticString)%s);\n", param.name);
                         }
                         else
@@ -1167,7 +1164,7 @@ static void BindDCOMClient()
                   {
                      if(method.dataType.returnType.kind == classType && !strcmp(method.dataType.returnType._class.string, "String"))
                      {
-                        DeclareClass(FindClass("StaticString"), "__ecereClass_StaticString");
+                        DeclareClass(null, FindClass("StaticString"), "__ecereClass_StaticString");
                         f.Printf("         __ecereBuffer.Unserialize((StaticString)__ecereResult);\n");
                      }
                      else
@@ -1175,7 +1172,8 @@ static void BindDCOMClient()
                   }
                   f.Printf("      }\n");
                   f.Printf("      __ecereBuffer.Free();\n");
-                  f.Printf("      delete this;\n");
+                  //f.Printf("      delete this;\n");
+                  f.Printf("      safeDecRef();\n");
                   if(method.dataType.returnType.kind != voidType)
                   {
                      f.Printf("      return __ecereResult;\n");
@@ -1185,10 +1183,10 @@ static void BindDCOMClient()
                f.Printf("   }\n");
             }
             next = (Method)((BTNode)method).next;
-            while(next && ((next.type == virtualMethod) != doVirtual || (doVirtual && next.vid != vid)))
+            while((!next && doVirtual) || (next && ((next.type == virtualMethod) != doVirtual || (doVirtual && next.vid != vid))))
             {
                id++;
-               next = (Method)((BTNode)next).next;
+               next = next ? (Method)((BTNode)next).next : null;
                if(!next && doVirtual)
                {
                   if(vid == _class.vTblSize)
@@ -1227,7 +1225,7 @@ static void BindDCOMServer()
       if(!dcomSymbols) dcomSymbols = TempFile { };
       f = dcomSymbols;
 
-      DeclareClass(FindClass("ecere::net::DCOMServerObject"), "__ecereClass___ecereNameSpace__ecere__net__DCOMServerObject");
+      DeclareClass(null, FindClass("ecere::net::DCOMServerObject"), "__ecereClass___ecereNameSpace__ecere__net__DCOMServerObject");
 
       // SERVER BINDINGS
       for(_class = privateModule.classes.first; _class; _class = _class.next)
@@ -1290,8 +1288,7 @@ static void BindDCOMServer()
                         }
                         strcpy(className, "__ecereClass_");
                         FullClassNameCat(className, classSym.string, true);
-                        //MangleClassName(className);
-                        DeclareClass(classSym, className);
+                        DeclareClass(null, classSym, className);
 
                         PrintType(method.dataType.returnType, type, true, true);
 
@@ -1309,8 +1306,8 @@ static void BindDCOMServer()
                         {
                            // Hardcode 1024 chars max string for now
                            f.Printf("            char %s[1024];\n", param.name);
-                           DeclareClass(FindClass("StaticString"), "__ecereClass_StaticString");
-                           DeclareClass(FindClass("String"), "__ecereClass_String");
+                           DeclareClass(null, FindClass("StaticString"), "__ecereClass_StaticString");
+                           DeclareClass(null, FindClass("String"), "__ecereClass_String");
                         }
                         else
                         {
@@ -1332,8 +1329,7 @@ static void BindDCOMServer()
 
                            strcpy(className, "__ecereClass_");
                            FullClassNameCat(className, classSym.string, true);
-                           //MangleClassName(className);
-                           DeclareClass(classSym, className);
+                           DeclareClass(null, classSym, className);
 
                            PrintType(param, type, true, true);
 
@@ -1353,7 +1349,7 @@ static void BindDCOMServer()
                         f.Printf("            __ecereBuffer.Unserialize(");
                         if(param.kind == classType && !strcmp(param._class.string, "String"))
                         {
-                           DeclareClass(FindClass("StaticString"), "__ecereClass_StaticString");
+                           DeclareClass(null, FindClass("StaticString"), "__ecereClass_StaticString");
                            f.Printf("(StaticString)");
                         }
                         f.Puts(param.name);
@@ -1382,7 +1378,7 @@ static void BindDCOMServer()
                         {
                            if(!strcmp(param._class.string, "String"))
                            {
-                              DeclareClass(FindClass("StaticString"), "__ecereClass_StaticString");
+                              DeclareClass(null, FindClass("StaticString"), "__ecereClass_StaticString");
                               f.Printf("            __ecereBuffer.Serialize((StaticString)%s);\n", param.name);
                            }
                            else
@@ -1430,12 +1426,12 @@ static void BindDCOMServer()
                {
                   if(!mutexDeclared)
                   {
-                     DeclareClass(FindClass("ecere::sys::Mutex"), "__ecereClass___ecereNameSpace__ecere__sys__Mutex");
-                     DeclareMethod(
+                     DeclareClass(null, FindClass("ecere::sys::Mutex"), "__ecereClass___ecereNameSpace__ecere__sys__Mutex");
+                     DeclareMethod(null,
                         eClass_FindMethod(
                            eSystem_FindClass(privateModule, "ecere::sys::Mutex"), "Wait", privateModule),
                               "__ecereMethod___ecereNameSpace__ecere__sys__Mutex_Wait");
-                     DeclareMethod(
+                     DeclareMethod(null,
                         eClass_FindMethod(
                            eSystem_FindClass(privateModule, "ecere::sys::Mutex"), "Release", privateModule),
                               "__ecereMethod___ecereNameSpace__ecere__sys__Mutex_Release");
@@ -1474,8 +1470,7 @@ static void BindDCOMServer()
 
                            strcpy(className, "__ecereClass_");
                            FullClassNameCat(className, classSym.string, true);
-                           //MangleClassName(className);
-                           DeclareClass(classSym, className);
+                           DeclareClass(null, classSym, className);
 
                            PrintType(method.dataType.returnType, type, true, true);
 
@@ -1513,19 +1508,18 @@ static void BindDCOMServer()
                            }
                            strcpy(className, "__ecereClass_");
                            FullClassNameCat(className, classSym.string, true);
-                           //MangleClassName(className);
-                           DeclareClass(classSym, className);
+                           DeclareClass(null, classSym, className);
 
                            if(param.kind == classType && !strcmp(param._class.string, "String"))
                            {
-                              DeclareClass(FindClass("StaticString"), "__ecereClass_StaticString");
+                              DeclareClass(null, FindClass("StaticString"), "__ecereClass_StaticString");
                               f.Printf("      __ecereObject.argsBuffer.Serialize((StaticString)%s);\n", param.name);
                            }
                            else
                               f.Printf("      __ecereObject.argsBuffer.Serialize(%s);\n", param.name);
                         }
 
-                        DeclareMethod(
+                        DeclareMethod(null,
                            eClass_FindMethod(
                               eSystem_FindClass(privateModule, "ecere::net::DCOMServerObject"), "CallVirtualMethod", privateModule),
                            "__ecereMethod___ecereNameSpace__ecere__net__DCOMServerObject_CallVirtualMethod");
@@ -1554,7 +1548,7 @@ static void BindDCOMServer()
                            {
                               if(!strcmp(param._class.string, "String"))
                               {
-                                 DeclareClass(FindClass("StaticString"), "__ecereClass_StaticString");
+                                 DeclareClass(null, FindClass("StaticString"), "__ecereClass_StaticString");
                                  f.Printf("         __ecereObject.returnBuffer.Unserialize((StaticString)%s);\n", param.name);
                               }
                               else
@@ -1565,7 +1559,7 @@ static void BindDCOMServer()
                         {
                            if(method.dataType.returnType.kind == classType && !strcmp(method.dataType.returnType._class.string, "String"))
                            {
-                              DeclareClass(FindClass("StaticString"), "__ecereClass_StaticString");
+                              DeclareClass(null, FindClass("StaticString"), "__ecereClass_StaticString");
                               f.Printf("         __ecereObject.returnBuffer.Unserialize((StaticString)__ecereResult);\n");
                            }
                            else
@@ -1617,7 +1611,7 @@ class SymbolgenApp : Application
 
       outputPot = false;
       disabledPooling = false;
-      targetPlatform = GetRuntimePlatform();
+      targetPlatform = __runtimePlatform;
       targetBits = GetHostBits();
 
       /*
@@ -1728,7 +1722,7 @@ class SymbolgenApp : Application
 
       if(!valid)
       {
-         printf($"Syntax:\n   ecs [-t <target platform>] <input>[, <input>]* -o <output>\n");
+         printf("%s", $"Syntax:\n   ecs [-t <target platform>] <input>[, <input>]* -o <output>\n");
       }
       else
       {
@@ -1985,6 +1979,8 @@ class SymbolgenApp : Application
                   thisAppClass = FindAppClass(&privateModule.application.privateNameSpace, false);
                */
                thisAppClass = SearchAppClass_Module(privateModule);
+               if(!thisAppClass)
+                  thisAppClass = eSystem_FindClass(privateModule, "Application");
             }
             WriteMain(output);