ecere: Further tweaks for Emscripten
[sdk] / ecere / src / com / instance.ec
index 65a15d3..db2b5d2 100644 (file)
@@ -1,3 +1,5 @@
+#define _Noreturn
+
 namespace com;
 
 // #define DISABLE_MEMMGR
@@ -20,9 +22,15 @@ import "dataTypes"
 
 #undef __BLOCKS__
 
+#if !defined(__EMSCRIPTEN__)
 #if !defined(ECERE_BOOTSTRAP)
 import "Mutex"
 #endif
+#endif
+
+#if defined(__EMSCRIPTEN__)
+#define GetCurrentThreadID()  0
+#endif
 
 // #define MEMINFO
 /*
@@ -43,7 +51,9 @@ import "Mutex"
 #endif
 
 #ifdef MEMINFO
+#if !defined(__EMSCRIPTEN__)
 import "Thread"
+#endif
 static define MAX_MEMORY_LOC = 40;
 static define MAX_STACK_FRAMES = 1000;
 
@@ -122,6 +132,7 @@ dllexport int isblank(int c);
 #endif
 bool Instance_LocateModule(const char * name, const char * fileName);
 void Instance_COM_Initialize(int argc, char ** argv, char ** parsedCommand, int * argcPtr, const char *** argvPtr);
+void System_SetArgs(int argc, char ** argv, int * argcPtr, const char *** argvPtr);
 void * Instance_Module_Load(const char * libLocation, const char * name, void ** Load, void ** Unload);
 void Instance_Module_Free(void * library);
 #if defined(_DEBUG)
@@ -189,7 +200,9 @@ public dllexport void MemoryGuard_PushLoc(const char * loc)
 {
 #ifdef MEMINFO
    MemStack stack;
+#if !defined(__EMSCRIPTEN__)
    memMutex.Wait();
+#endif
    stack = (MemStack)memStacks.Find(GetCurrentThreadID());
    if(!stack)
    {
@@ -199,22 +212,28 @@ public dllexport void MemoryGuard_PushLoc(const char * loc)
    }
    if(stack.pos < MAX_STACK_FRAMES)
       stack.frames[stack.pos++] = loc;
+#if !defined(__EMSCRIPTEN__)
    memMutex.Release();
 #endif
+#endif
 }
 
 public dllexport void MemoryGuard_PopLoc()
 {
 #ifdef MEMINFO
    MemStack stack;
+#if !defined(__EMSCRIPTEN__)
    memMutex.Wait();
+#endif
    stack = (MemStack)memStacks.Find(GetCurrentThreadID());
    if(stack && stack.pos > 0)
    {
       stack.pos--;
    }
+#if !defined(__EMSCRIPTEN__)
    memMutex.Release();
 #endif
+#endif
 }
 
 #ifdef ECERE_STATIC
@@ -355,7 +374,7 @@ public:
    ClassTemplateArgument * templateArgs;
    Class templateClass;
    OldList templatized;
-   int numParams;
+   int numParams;       // TOTAL number of params including all base classes; use templateParams.count for this level
    bool isInstanceClass;
    bool byValueSystemClass;
 
@@ -713,9 +732,11 @@ static uint TOTAL_MEM = 0;
 static uint OUTSIDE_MEM = 0;
 #endif
 
+#if !defined(__EMSCRIPTEN__)
 #if !defined(ECERE_BOOTSTRAP)
 static Mutex memMutex { };
 #endif
+#endif
 
 private class MemBlock : struct
 {
@@ -1289,11 +1310,20 @@ static void _myfree(void * pointer)
          printf("WARNING! pool is -1\n");
       else   */
       if(pool)
+      {
+#ifdef _DEBUG
+         memset(pointer, 0xec, block.size);
+#endif
          pool->Remove(block);
+      }
       else
       {
          TOTAL_MEM -= sizeof(class MemBlock) + block.size;
          OUTSIDE_MEM -= sizeof(class MemBlock) + block.size;
+
+#ifdef _DEBUG
+         memset(block, 0xec, sizeof(class MemBlock) + block.size);
+#endif
          free(block);
       }
    }
@@ -1430,7 +1460,7 @@ static void * _malloc(unsigned int size)
    void * pointer;
 
 
-#if !defined(ECERE_BOOTSTRAP)
+#if !defined(ECERE_BOOTSTRAP) && !defined(__EMSCRIPTEN__)
    memMutex.Wait();
 #endif
 
@@ -1457,7 +1487,9 @@ static void * _malloc(unsigned int size)
                printf("      %s\n", stack.frames[c]);
 
          memoryErrorsCount++;
+#if !defined(__EMSCRIPTEN__)
          memMutex.Release();
+#endif
          return null;
       }
 
@@ -1472,7 +1504,7 @@ static void * _malloc(unsigned int size)
    }
 #endif
 
-#if !defined(ECERE_BOOTSTRAP)
+#if !defined(ECERE_BOOTSTRAP) && !defined(__EMSCRIPTEN__)
    memMutex.Release();
 #endif
 
@@ -1495,7 +1527,7 @@ static void * _calloc(int n, unsigned int size)
 #else
    void * pointer;
 
-#if !defined(ECERE_BOOTSTRAP)
+#if !defined(ECERE_BOOTSTRAP) && !defined(__EMSCRIPTEN__)
    memMutex.Wait();
 #endif
 
@@ -1520,7 +1552,9 @@ static void * _calloc(int n, unsigned int size)
             if(stack.frames[c])
                printf("      %s\n", stack.frames[c]);
          memoryErrorsCount++;
+#if !defined(__EMSCRIPTEN__)
          memMutex.Release();
+#endif
          return null;
       }
 
@@ -1537,7 +1571,7 @@ static void * _calloc(int n, unsigned int size)
    }
 #endif
 
-#if !defined(ECERE_BOOTSTRAP)
+#if !defined(ECERE_BOOTSTRAP) && !defined(__EMSCRIPTEN__)
    memMutex.Release();
 #endif
 
@@ -1561,7 +1595,7 @@ static void * _realloc(void * pointer, unsigned int size)
 #else
    if(!size) { _free(pointer); return null; }
 
-#if !defined(ECERE_BOOTSTRAP)
+#if !defined(ECERE_BOOTSTRAP) && !defined(__EMSCRIPTEN__)
    memMutex.Wait();
 #endif
 
@@ -1603,7 +1637,9 @@ static void * _realloc(void * pointer, unsigned int size)
          if(stack.frames[c])
             printf("      %s\n", stack.frames[c]);
       memoryErrorsCount++;
+#if !defined(__EMSCRIPTEN__)
       memMutex.Release();
+#endif
       return null;
    }
    memset(pointer, 0xAB, REDZONE);
@@ -1651,7 +1687,7 @@ static void * _realloc(void * pointer, unsigned int size)
    pointer = realloc(pointer, size);
 #endif
 
-#if !defined(ECERE_BOOTSTRAP)
+#if !defined(ECERE_BOOTSTRAP) && !defined(__EMSCRIPTEN__)
    memMutex.Release();
 #endif
    return pointer ? ((byte *)pointer + REDZONE) : null;
@@ -1672,7 +1708,7 @@ static void * _crealloc(void * pointer, unsigned int size)
 #else
    if(!size) { _free(pointer); return null; }
 
-#if !defined(ECERE_BOOTSTRAP)
+#if !defined(ECERE_BOOTSTRAP) && !defined(__EMSCRIPTEN__)
    memMutex.Wait();
 #endif
 
@@ -1714,7 +1750,9 @@ static void * _crealloc(void * pointer, unsigned int size)
          if(stack.frames[c])
             printf("      %s\n", stack.frames[c]);
       memoryErrorsCount++;
+#if !defined(__EMSCRIPTEN__)
       memMutex.Release();
+#endif
       return null;
    }
    memset(pointer, 0xAB, REDZONE);
@@ -1762,7 +1800,7 @@ static void * _crealloc(void * pointer, unsigned int size)
    pointer = crealloc(pointer, size);
 #endif
 
-#if !defined(ECERE_BOOTSTRAP)
+#if !defined(ECERE_BOOTSTRAP) && !defined(__EMSCRIPTEN__)
    memMutex.Release();
 #endif
    return pointer ? ((byte *)pointer + REDZONE) : null;
@@ -1776,7 +1814,7 @@ static void _free(void * pointer)
 #if defined(DISABLE_MEMMGR) && !defined(MEMINFO)
       free(pointer);
 #else
-#if !defined(ECERE_BOOTSTRAP)
+#if !defined(ECERE_BOOTSTRAP) && !defined(__EMSCRIPTEN__)
       if(memMutex != pointer) memMutex.Wait();
 #endif
 
@@ -1886,7 +1924,7 @@ static void _free(void * pointer)
       free(pointer);
 #endif
 
-#if !defined(ECERE_BOOTSTRAP)
+#if !defined(ECERE_BOOTSTRAP) && !defined(__EMSCRIPTEN__)
       if(memMutex != pointer) memMutex.Release();
 #endif
 
@@ -2776,7 +2814,8 @@ public dllexport Class eSystem_RegisterClass(ClassType type, const char * name,
                !strcmp(name, "LineStyle") ||
                !strcmp(name, "FillStyle") ||
                !strcmp(name, "FontObject") ||
-               !strcmp(name, "SymbolStyle"))
+               !strcmp(name, "FontObject") ||
+               !strcmp(name, "ecere::sys::Thread"))
             {
                _class.offset = force32Bits ? 24 : 12;
             }
@@ -2936,6 +2975,7 @@ static void FreeTemplateArg(Class template, ClassTemplateParameter param, int id
    {
       case type:
          delete (void *)template.templateArgs[id].dataTypeString;
+         template.templateArgs[id].dataTypeClass = null;
          break;
       case identifier:
          delete (void *)template.templateArgs[id].memberString;
@@ -2970,6 +3010,7 @@ static void FreeTemplateArgs(Class template)
                {
                   case type:
                      delete (void *)template.templateArgs[id].dataTypeString;
+                     template.templateArgs[id].dataTypeClass = null;
                      break;
                   case identifier:
                      delete (void *)template.templateArgs[id].memberString;
@@ -2995,12 +3036,9 @@ static void FreeTemplate(Class template)
       if(link)
          template.nameSpace->classes.Delete((BTNode)link);
    }
-   FreeTemplateArgs(template);
 
-   delete (void *)template.fullName;
-   delete (void *)template.name;
-   delete (void *)template.templateArgs;
-   delete (void *)template.dataTypeString;
+   FreeTemplatesDerivatives(template);
+   FreeTemplateArgs(template);
 
    while((deriv = template.derivatives.first))
    {
@@ -3008,6 +3046,11 @@ static void FreeTemplate(Class template)
       template.derivatives.Delete(deriv);
    }
 
+   delete (void *)template.fullName;
+   delete (void *)template.name;
+   delete template.templateArgs;
+   delete (void *)template.dataTypeString;
+
    if(template.module)
       template.module.classes.Delete(template);
    else
@@ -3024,8 +3067,6 @@ static void FreeTemplates(Class _class)
    }
 
    FreeTemplateArgs(_class);
-   //if(_class.templateArgs)
-      //printf("Deleting  Template args for %s\n", _class.name);
    delete _class.templateArgs;
    delete (void *)_class.dataTypeString;
 
@@ -3066,9 +3107,6 @@ public dllexport void eClass_Unregister(Class _class)
 
    FreeTemplates(_class);
 
-   FreeTemplateArgs(_class);
-   delete _class.templateArgs;
-
    while((template = _class.templatized.first))
    {
       FreeTemplate(template.data);
@@ -3290,25 +3328,20 @@ public int64 _strtoi64(const char * string, const char ** endString, int base)
       else if(ch >= 'A' && ch <= 'Z')
          ch -= ('A'- 10);
       else
-      {
-         if(endString)
-            *endString = string + c;
          // Invalid character
          break;
-      }
       if(ch < base)
       {
          value *= base;
          value += ch;
       }
       else
-      {
-         if(endString)
-            *endString = string + c;
          // Invalid character
          break;
-      }
    }
+   if(endString)
+      *endString = string + c;
+
    return sign*value;
 }
 
@@ -3345,25 +3378,19 @@ public uint64 _strtoui64(const char * string, const char ** endString, int base)
       else if(ch >= 'A' && ch <= 'Z')
          ch -= ('A' - 10);
       else
-      {
-         if(endString)
-            *endString = string + c;
          // Invalid character
          break;
-      }
       if(ch < base)
       {
          value *= base;
          value += ch;
       }
       else
-      {
-         if(endString)
-            *endString = string + c;
          // Invalid character
          break;
-      }
    }
+   if(endString)
+      *endString = string + c;
    return sign*value;
 }
 
@@ -3449,7 +3476,7 @@ static void CopyTemplateArg(ClassTemplateParameter param, ClassTemplateArgument
    switch(param.type)
    {
       case type:
-         arg.dataTypeString = CopyString(arg.dataTypeString);
+            arg.dataTypeString = CopyString(arg.dataTypeString);
          break;
       case expression:
 
@@ -3814,6 +3841,7 @@ static void ComputeClassParameters(Class templatedClass, const char * templatePa
                         {
                            int id = p;
                            Class sClass;
+                           // NOTE: This struct 'arg' here is only to build up templateString
                            ClassTemplateArgument arg;
                            for(sClass = expClass.base; sClass; sClass = sClass.base) id += sClass.templateParams.count;
                            arg = expClass.templateArgs[id];
@@ -3825,8 +3853,7 @@ static void ComputeClassParameters(Class templatedClass, const char * templatePa
                               {
                                  if(cParam.type == type && arg.dataTypeString && !strcmp(cParam.name, arg.dataTypeString))
                                  {
-                                    arg.dataTypeString = templatedClass.templateArgs[p].dataTypeString;
-                                    arg.dataTypeClass = templatedClass.templateArgs[p].dataTypeClass;
+                                    arg = templatedClass.templateArgs[p];
                                     break;
                                  }
                               }
@@ -3913,6 +3940,7 @@ static void ComputeClassParameters(Class templatedClass, const char * templatePa
                         {
                            FreeTemplateArg(templatedClass, param, c);
 
+                           // TRICKY: This copies from equivalent parameters
                            arg->dataTypeString = templatedClass.templateArgs[p].dataTypeString;
                            arg->dataTypeClass = templatedClass.templateArgs[p].dataTypeClass;
                            CopyTemplateArg(cParam, arg);
@@ -4600,7 +4628,9 @@ public dllexport void * eInstance_New(Class _class)
 #ifdef MEMINFO
 
 #undef malloc
+#if !defined(__EMSCRIPTEN__)
    memMutex.Wait();
+#endif
       //allocateClass = _class;
       allocateClass = malloc(strlen(_class.name)+1);
       allocateInternal = _class.module == __thisModule;
@@ -4626,11 +4656,17 @@ public dllexport void * eInstance_New(Class _class)
                size *= 3;
          }
          instance = _calloc(1, size);
+         if(!instance && size)
+            printf("Failed to allocate memory instantiating %s object!\n", _class.name);
+         else if(!size)
+            printf("Warning: 0 size instantiating %s object!\n", _class.name);
       }
 #ifdef MEMINFO
       allocateClass = null;
+#if !defined(__EMSCRIPTEN__)
    memMutex.Release();
 #endif
+#endif
 
 #if !defined(MEMINFO) && defined(MEMTRACKING)
       {
@@ -4639,13 +4675,13 @@ public dllexport void * eInstance_New(Class _class)
       }
 #endif
 
-      if(_class.type == normalClass)
+      if(instance && _class.type == normalClass)
       {
          instance._class = _class;
          // Copy the virtual table initially
          instance._vTbl = _class._vTbl;
       }
-      if(!ConstructInstance(instance, _class, null))
+      if(instance && !ConstructInstance(instance, _class, null))
       {
          _free(instance);
          instance = null;
@@ -5933,7 +5969,7 @@ static int64 GetEnumSize(Class _class)
 #define strnicmp strncasecmp
 #endif
 
-#if defined(ECERE_BOOTSTRAP) || (defined(__GNUC__) && !defined(__DJGPP__) && !defined(__WIN32__))
+#if defined(ECERE_BOOTSTRAP) || (defined(__GNUC__) && !defined(__DJGPP__) && !defined(__WIN32__) && !defined(__EMSCRIPTEN__))
 #undef strlwr
 #undef strupr
 default dllexport char * strlwr(char *string)
@@ -6525,7 +6561,7 @@ static void LoadCOM(Module module)
 
    // --- Stdio ---
    eSystem_RegisterFunction("sprintf", "int sprintf(char *, const char *, ...)", sprintf, module, baseSystemAccess);
-   eSystem_RegisterFunction("snprintf", "int sprintf(char *, uintsize, const char *, ...)", snprintf, module, baseSystemAccess);
+   eSystem_RegisterFunction("snprintf", "int snprintf(char *, uintsize, const char *, ...)", snprintf, module, baseSystemAccess);
    eSystem_RegisterFunction("printf", "int printf(const char *, ...)", printf, module, baseSystemAccess);
    eSystem_RegisterFunction("vsprintf", "int vsprintf(char*, const char*, __builtin_va_list)", vsprintf, module, baseSystemAccess);
    eSystem_RegisterFunction("vsnprintf", "int vsnprintf(char*, uintsize, const char*, __builtin_va_list)", vsnprintf, module, baseSystemAccess);
@@ -6592,6 +6628,11 @@ public dllexport Application __ecere_COM_Initialize(bool guiApp, int argc, char
    return app;
 }
 
+public dllexport void eSystem_SetArgs(Application app, int argc, char * argv[])
+{
+   System_SetArgs(argc, argv, &app.argc, &app.argv);
+}
+
 public dllexport ClassTemplateParameter eClass_AddTemplateParameter(Class _class, const char * name, TemplateParameterType type, const void * info, ClassTemplateArgument defaultArg)
 {
    if(_class && name)
@@ -6952,7 +6993,33 @@ public int ISO8859_1toUTF8(const char * source, char * dest, int max)
       unichar ch = ((byte *)source)[c];
       switch(ch)
       {
-         case 150: ch = (unichar)0x2012; break;
+         case 128: ch = (unichar)0x20AC; break;
+         case 130: ch = (unichar)0x201A; break;
+         case 131: ch = (unichar)0x0192; break;
+         case 132: ch = (unichar)0x201E; break;
+         case 133: ch = (unichar)0x2026; break;
+         case 134: ch = (unichar)0x2020; break;
+         case 135: ch = (unichar)0x2021; break;
+         case 136: ch = (unichar)0x02C6; break;
+         case 137: ch = (unichar)0x2030; break;
+         case 138: ch = (unichar)0x0160; break;
+         case 139: ch = (unichar)0x2039; break;
+         case 140: ch = (unichar)0x0152; break;
+         case 142: ch = (unichar)0x017D; break;
+         case 145: ch = (unichar)0x2018; break;
+         case 146: ch = (unichar)0x2019; break;
+         case 147: ch = (unichar)0x201C; break;
+         case 148: ch = (unichar)0x201D; break;
+         case 149: ch = (unichar)0x2022; break;
+         case 150: ch = (unichar)0x2013; break;
+         case 151: ch = (unichar)0x2014; break;
+         case 152: ch = (unichar)0x02DC; break;
+         case 153: ch = (unichar)0x2122; break;
+         case 154: ch = (unichar)0x0161; break;
+         case 155: ch = (unichar)0x203A; break;
+         case 156: ch = (unichar)0x0153; break;
+         case 158: ch = (unichar)0x017E; break;
+         case 159: ch = (unichar)0x0178; break;
       }
       if(ch < 0x80)
       {
@@ -7324,7 +7391,9 @@ public void queryMemInfo(char * string)
 
    blocksByClass.Free();
    sizeByClass.Free();
+#if !defined(__EMSCRIPTEN__)
    memMutex.Wait();
+#endif
    for(p = 0; pools && p < NUM_POOLS; p++)
    {
       BlockPool * pool = &pools[p];
@@ -7336,7 +7405,9 @@ public void queryMemInfo(char * string)
          sizeByClass[c] += block.size;
       }
    }
+#if !defined(__EMSCRIPTEN__)
    memMutex.Release();
+#endif
 
    //for(c : blocksByClass)
    {