(mono_runtime_delegate_invoke): impl.
[mono.git] / mono / metadata / icall.c
index 85071d332bf9f625f58f5492b97eba36ba0999f5..4fbb9c192522eb167538a6a3f2e2b8f61e8bae17 100644 (file)
@@ -37,6 +37,7 @@
 #include <mono/metadata/debug-symfile.h>
 #include <mono/metadata/string-icalls.h>
 #include <mono/io-layer/io-layer.h>
+#include <mono/utils/strtod.h>
 
 #if defined (PLATFORM_WIN32)
 #include <windows.h>
@@ -52,6 +53,24 @@ mono_double_ToStringImpl (double value)
        return mono_string_new (mono_domain_get (), retVal);
 }
 
+/*
+ * We expect a pointer to a char, not a string
+ */
+static double
+mono_double_ParseImpl (char *ptr)
+{
+       return bsd_strtod (ptr, NULL);
+}
+
+static MonoString *
+mono_float_ToStringImpl (float value)
+{
+       /* FIXME: Handle formats, etc. */
+       const gchar *retVal;
+       retVal = g_strdup_printf ("%f", value);
+       return mono_string_new (mono_domain_get (), retVal);
+}
+
 static MonoObject *
 ves_icall_System_Array_GetValueImpl (MonoObject *this, guint32 pos)
 {
@@ -546,10 +565,27 @@ ves_icall_System_Object_MemberwiseClone (MonoObject *this)
        return mono_object_clone (this);
 }
 
+#if HAVE_BOEHM_GC
+#define MONO_OBJECT_ALIGNMENT_SHIFT    3
+#else
+#define MONO_OBJECT_ALIGNMENT_SHIFT    2
+#endif
+
+/*
+ * Return hashcode based on object address. This function will need to be
+ * smarter in the presence of a moving garbage collector, which will cache
+ * the address hash before relocating the object.
+ *
+ * Wang's address-based hash function:
+ *   http://www.concentric.net/~Ttwang/tech/addrhash.htm
+ */
 static gint32
 ves_icall_System_Object_GetHashCode (MonoObject *this)
 {
-       return *((gint32 *)this - 1);
+       register guint32 key;
+       key = (GPOINTER_TO_UINT (this) >> MONO_OBJECT_ALIGNMENT_SHIFT) * 2654435761u;
+
+       return key & 0x7fffffff;
 }
 
 /*
@@ -915,7 +951,7 @@ ves_icall_get_type_info (MonoType *type, MonoTypeInfo *info)
 static MonoObject *
 ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this, MonoArray *params) 
 {
-       return mono_runtime_invoke_array (method->method, this, params);
+       return mono_runtime_invoke_array (method->method, this, params, NULL);
 }
 
 static MonoObject *
@@ -1010,7 +1046,7 @@ ves_icall_InternalExecute (MonoReflectionMethod *method, MonoObject *this, MonoA
        if (!strcmp (method->method->name, ".ctor"))
                g_assert_not_reached ();
 
-       result = mono_runtime_invoke_array (method->method, this, params);
+       result = mono_runtime_invoke_array (method->method, this, params, NULL);
 
        *outArgs = out_args;
 
@@ -1648,6 +1684,18 @@ ves_icall_System_MonoType_getFullName (MonoReflectionType *object)
        return res;
 }
 
+static void
+ves_icall_System_Reflection_Assembly_FillName (MonoReflectionAssembly *assembly, MonoReflectionAssemblyName *aname)
+{
+       MonoAssemblyName *name = &assembly->assembly->aname;
+
+       if (strcmp (name->name, "corlib") == 0)
+               aname->name = mono_string_new (mono_object_domain (assembly), "mscorlib");
+       else
+               aname->name = mono_string_new (mono_object_domain (assembly), name->name);
+       aname->major = name->major;
+}
+
 static MonoArray*
 ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly, MonoBoolean exportedOnly)
 {
@@ -1658,9 +1706,10 @@ ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly,
        int i, count;
        guint32 attrs, visibility;
 
+       /* we start the count from 1 because we skip the special type <Module> */
        if (exportedOnly) {
                count = 0;
-               for (i = 0; i < tdef->rows; ++i) {
+               for (i = 1; i < tdef->rows; ++i) {
                        attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
                        visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
                        if (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)
@@ -1671,7 +1720,7 @@ ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly,
        }
        res = mono_array_new (domain, mono_defaults.monotype_class, count);
        count = 0;
-       for (i = 0; i < tdef->rows; ++i) {
+       for (i = 1; i < tdef->rows; ++i) {
                attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
                visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
                if (!exportedOnly || (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)) {
@@ -1757,7 +1806,7 @@ ves_icall_System_DateTime_GetNow (void)
        
        GetLocalTime (&st);
        SystemTimeToFileTime (&st, &ft);
-       return (gint64)504911232000000000L + (((gint64)ft.dwHighDateTime)<<32) | ft.dwLowDateTime;
+       return (gint64)504911232000000000L + ((((gint64)ft.dwHighDateTime)<<32) | ft.dwLowDateTime);
 #else
        /* FIXME: put this in io-layer and call it GetLocalTime */
        struct timeval tv;
@@ -1804,8 +1853,16 @@ ves_icall_System_CurrentTimeZone_GetTimeZoneData (guint32 year, MonoArray **data
        start.tm_year = year-1900;
 
        t = mktime (&start);
-       gmtoff = start.tm_gmtoff;
-
+#if defined (HAVE_TIMEZONE)
+#define gmt_offset(x) (-1 * (((timezone / 60 / 60) - daylight) * 100))
+#elif defined (HAVE_TM_GMTOFF)
+#define gmt_offset(x) x.tm_gmtoff
+#else
+#error Neither HAVE_TIMEZONE nor HAVE_TM_GMTOFF defined. Rerun autoheader, autoconf, etc.
+#endif
+       
+       gmtoff = gmt_offset (start);
+       
        MONO_CHECK_ARG_NULL (data);
        MONO_CHECK_ARG_NULL (names);
 
@@ -1819,7 +1876,8 @@ ves_icall_System_CurrentTimeZone_GetTimeZoneData (guint32 year, MonoArray **data
                tt = *localtime (&t);
 
                /* Daylight saving starts or ends here. */
-               if (tt.tm_gmtoff != gmtoff) {
+               if (gmt_offset (tt) != gmtoff) {
+                       char tzone[10];
                        struct tm tt1;
                        time_t t1;
 
@@ -1828,33 +1886,35 @@ ves_icall_System_CurrentTimeZone_GetTimeZoneData (guint32 year, MonoArray **data
                        do {
                                t1 -= 3600;
                                tt1 = *localtime (&t1);
-                       } while (tt1.tm_gmtoff != gmtoff);
+                       } while (gmt_offset (tt1) != gmtoff);
 
                        /* Try to find the exact minute when daylight saving starts/ends. */
                        do {
                                t1 += 60;
                                tt1 = *localtime (&t1);
-                       } while (tt1.tm_gmtoff == gmtoff);
-
+                       } while (gmt_offset (tt1) == gmtoff);
+                       
+                       strftime (tzone, 10, "%Z", &tt);
+                       
                        /* Write data, if we're already in daylight saving, we're done. */
                        if (is_daylight) {
-                               mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tt.tm_zone));
+                               mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tzone));
                                mono_array_set ((*data), gint64, 1, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
                                return 1;
                        } else {
-                               mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tt.tm_zone));
+                               mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tzone));
                                mono_array_set ((*data), gint64, 0, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
                                is_daylight = 1;
                        }
 
                        /* This is only set once when we enter daylight saving. */
                        mono_array_set ((*data), gint64, 2, (gint64)gmtoff * 10000000L);
-                       mono_array_set ((*data), gint64, 3, (gint64)(tt.tm_gmtoff - gmtoff) * 10000000L);
+                       mono_array_set ((*data), gint64, 3, (gint64)(gmt_offset (tt) - gmtoff) * 10000000L);
 
-                       gmtoff = tt.tm_gmtoff;
+                       gmtoff = gmt_offset (tt);
                }
 
-               gmtoff = tt.tm_gmtoff;
+               gmtoff = gmt_offset (tt);
        }
        return 1;
 #else
@@ -2066,10 +2126,33 @@ ves_icall_System_Environment_GetEnvironmentVariableNames (void)
        return names;
 }
 
-static MonoString *
-ves_icall_System_Environment_GetCommandLine (void)
+/*
+ * Returns the number of milliseconds elapsed since the system started.
+ */
+static gint32
+ves_icall_System_Environment_get_TickCount (void)
+{
+#if defined (PLATFORM_WIN32)
+       return GetTickCount();
+#else
+       struct timeval tv;
+       struct timezone tz;
+       gint32 res;
+
+       res = (gint32) gettimeofday (&tv, &tz);
+
+       if (res != -1)
+               res = (gint32) ((tv.tv_sec & 0xFFFFF) * 1000 + (tv.tv_usec / 1000));
+       return res;
+#endif
+}
+
+
+static void
+ves_icall_System_Environment_Exit (int result)
 {
-       return NULL;    /* FIXME */
+       /* we may need to do some cleanup here... */
+       exit (result);
 }
 
 static void
@@ -2188,6 +2271,12 @@ static gconstpointer icall_map [] = {
         * System.Double
         */
        "System.Double::ToStringImpl", mono_double_ToStringImpl,
+       "System.Double::ParseImpl",    mono_double_ParseImpl,
+
+       /*
+        * System.Single
+        */
+       "System.Single::ToStringImpl", mono_float_ToStringImpl,
 
        /*
         * System.Decimal
@@ -2274,6 +2363,7 @@ static gconstpointer icall_map [] = {
         * System.Threading
         */
        "System.Threading.Thread::Thread_internal", ves_icall_System_Threading_Thread_Thread_internal,
+       "System.Threading.Thread::Thread_free_internal", ves_icall_System_Threading_Thread_Thread_free_internal,
        "System.Threading.Thread::Start_internal", ves_icall_System_Threading_Thread_Start_internal,
        "System.Threading.Thread::Sleep_internal", ves_icall_System_Threading_Thread_Sleep_internal,
        "System.Threading.Thread::CurrentThread_internal", ves_icall_System_Threading_Thread_CurrentThread_internal,
@@ -2305,8 +2395,10 @@ static gconstpointer icall_map [] = {
        "System.Runtime.InteropServices.Marshal::PtrToStringAuto", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAuto,
        "System.Runtime.InteropServices.Marshal::GetLastWin32Error", ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error,
 
+       "System.Reflection.Assembly::LoadFrom", ves_icall_System_Reflection_Assembly_LoadFrom,
        "System.Reflection.Assembly::GetType", ves_icall_System_Reflection_Assembly_GetType,
        "System.Reflection.Assembly::GetTypes", ves_icall_System_Reflection_Assembly_GetTypes,
+       "System.Reflection.Assembly::FillName", ves_icall_System_Reflection_Assembly_FillName,
        "System.Reflection.Assembly::get_code_base", ves_icall_System_Reflection_Assembly_get_code_base,
 
        /*
@@ -2466,7 +2558,9 @@ static gconstpointer icall_map [] = {
        "System.Environment::get_NewLine", ves_icall_System_Environment_get_NewLine,
        "System.Environment::GetEnvironmentVariable", ves_icall_System_Environment_GetEnvironmentVariable,
        "System.Environment::GetEnvironmentVariableNames", ves_icall_System_Environment_GetEnvironmentVariableNames,
-       "System.Environment::GetCommandLine", ves_icall_System_Environment_GetCommandLine,
+       "System.Environment::GetCommandLineArgs", mono_runtime_get_main_args,
+       "System.Environment::get_TickCount", ves_icall_System_Environment_get_TickCount,
+       "System.Environment::Exit", ves_icall_System_Environment_Exit,
 
        /*
         * Mono.CSharp.Debugger