Mon Mar 25 13:04:56 CET 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / icall.c
index ceaec5ee34a0629933f1d818c4fcaf764810cd8d..60c6e6fd169335a47535871b06d34985b9dc6734 100644 (file)
@@ -28,6 +28,8 @@
 #include <mono/metadata/unicode.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/rand.h>
+#include <mono/metadata/sysmath.h>
+#include <mono/metadata/debug-symfile.h>
 #include <mono/io-layer/io-layer.h>
 #include "decimal.h"
 
@@ -355,17 +357,11 @@ ves_icall_System_Array_SetValue (MonoObject *this, MonoObject *value,
                                 MonoObject *idxs)
 {
        MonoArray *ao, *io, *vo;
-       MonoClass *ac, *ic, *vc;
+       MonoClass *ac, *ic;
        gint32 i, pos, *ind;
 
        MONO_CHECK_ARG_NULL (idxs);
 
-       vo = (MonoArray *)value;
-       if (vo)
-               vc = (MonoClass *)vo->obj.vtable->klass;
-       else
-               vc = NULL;
-
        io = (MonoArray *)idxs;
        ic = (MonoClass *)io->obj.vtable->klass;
        
@@ -509,6 +505,37 @@ ves_icall_System_Object_MemberwiseClone (MonoObject *this)
        return mono_object_clone (this);
 }
 
+static gint32
+ves_icall_System_Object_GetHashCode (MonoObject *this)
+{
+       return *((gint32 *)this - 1);
+}
+
+/*
+ * A hash function for value types. I have no idea if this is a good hash 
+ * function (its similar to g_str_hash).
+ */
+static gint32
+ves_icall_System_ValueType_GetHashCode (MonoObject *this)
+{
+       gint32 i, size;
+       const char *p;
+       guint h;
+
+       MONO_CHECK_ARG_NULL (this);
+
+       size = this->vtable->klass->instance_size - sizeof (MonoObject);
+
+       p = (char *)this + sizeof (MonoObject);
+
+       for (i = 0; i < size; i++) {
+               h = (h << 5) - h + *p;
+               p++;
+       }
+
+       return h;
+}
+
 static MonoReflectionType *
 ves_icall_System_Object_GetType (MonoObject *obj)
 {
@@ -567,7 +594,7 @@ static MonoReflectionType*
 ves_icall_type_from_name (MonoString *name)
 {
        MonoDomain *domain = mono_domain_get (); 
-       MonoClass *klass;
+       MonoType *type;
        MonoImage *image;
        MonoTypeNameParse info;
        gchar *str;
@@ -576,6 +603,7 @@ ves_icall_type_from_name (MonoString *name)
        /*g_print ("requested type %s\n", str);*/
        if (!mono_reflection_parse_type (str, &info)) {
                g_free (str);
+               g_list_free (info.modifiers);
                return NULL;
        }
 
@@ -584,40 +612,19 @@ ves_icall_type_from_name (MonoString *name)
                /* do we need to load if it's not already loaded? */
                if (!image) {
                        g_free (str);
+                       g_list_free (info.modifiers);
                        return NULL;
                }
        } else
                image = mono_defaults.corlib;
-       if (info.nest_name) {
-               klass = mono_class_from_name (image, info.nest_name_space, info.nest_name);
-               if (klass) {
-                       GList *nested;
-                       mono_class_init (klass);
-                       nested = klass->nested_classes;
-                       while (nested) {
-                               klass = nested->data;
-                               if (strcmp (klass->name, info.nest_name) == 0 &&
-                                               strcmp (klass->name_space, info.nest_name_space) == 0)
-                                       break;
-                               klass = NULL;
-                       }
-               }
-       } else {
-               klass = mono_class_from_name (image, info.name_space, info.name);
-       }
+
+       type = mono_reflection_get_type (image, &info, FALSE);
        g_free (str);
-       if (!klass)
+       g_list_free (info.modifiers);
+       if (!type)
                return NULL;
-       mono_class_init (klass);
-       if (info.rank) {
-               klass = mono_array_class_get (&klass->byval_arg, info.rank);
-               mono_class_init (klass);
-       }
-       
-       if (info.isbyref || info.ispointer) /* hack */
-               return mono_type_get_object (domain, &klass->this_arg);
-       else
-               return mono_type_get_object (domain, &klass->byval_arg);
+       /*g_print ("got it\n");*/
+       return mono_type_get_object (domain, type);
 }
 
 static MonoReflectionType*
@@ -816,7 +823,7 @@ ves_icall_get_type_info (MonoType *type, MonoTypeInfo *info)
        info->attrs = class->flags;
        info->rank = class->rank;
        info->assembly = NULL; /* FIXME */
-       if (class->enumtype)
+       if (class->enumtype && class->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
                info->etype = mono_type_get_object (domain, class->enum_basetype);
        else if (class->element_class)
                info->etype = mono_type_get_object (domain, &class->element_class->byval_arg);
@@ -829,14 +836,50 @@ ves_icall_get_type_info (MonoType *type, MonoTypeInfo *info)
 }
 
 static MonoObject*
-ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this, MonoArray *params) {
-       //MonoMethodSignature *sig = method->method->signature;
+ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this, MonoArray *params) 
+{
+       MonoMethodSignature *sig = method->method->signature;
+       gpointer *pa;
+       int i;
 
-       /*
-        * Do we need to copy the values so that the called method can't change them?
-        */
+       pa = alloca (sizeof (gpointer) * params->bounds->length);
 
-       return NULL;
+       for (i = 0; i < params->bounds->length; i++) {
+               if (sig->params [i]->byref) {
+                       /* fixme: don't know how to handle this */
+                       g_assert_not_reached ();
+               }
+
+               switch (sig->params [i]->type) {
+               case MONO_TYPE_U1:
+               case MONO_TYPE_I1:
+               case MONO_TYPE_BOOLEAN:
+               case MONO_TYPE_U2:
+               case MONO_TYPE_I2:
+               case MONO_TYPE_CHAR:
+               case MONO_TYPE_U:
+               case MONO_TYPE_I:
+               case MONO_TYPE_U4:
+               case MONO_TYPE_I4:
+               case MONO_TYPE_U8:
+               case MONO_TYPE_I8:
+               case MONO_TYPE_VALUETYPE:
+                       pa [i] = (char *)(((gpointer *)params->vector)[i]) + sizeof (MonoObject);
+                       break;
+               case MONO_TYPE_STRING:
+                       pa [i] = (char *)(((gpointer *)params->vector)[i]);
+                       break;
+               default:
+                       g_error ("type 0x%x not handled in invoke", sig->params [i]->type);
+               }
+       }
+
+       if (!strcmp (method->method->name, ".ctor")) {
+               this = mono_object_new (mono_domain_get (), method->method->klass);
+               mono_runtime_invoke (method->method, this, pa);
+               return this;
+       } else
+               return mono_runtime_invoke (method->method, this, pa);
 }
 
 static MonoObject *
@@ -1102,7 +1145,7 @@ handle_parent:
                        mono_defaults.corlib, "System.Reflection", "FieldInfo");
        res = mono_array_new (domain, System_Reflection_FieldInfo, len);
        i = 0;
-       tmp = l;
+       tmp = g_slist_reverse (l);
        for (; tmp; tmp = tmp->next, ++i)
                mono_array_set (res, gpointer, i, tmp->data);
        g_slist_free (l);
@@ -1370,78 +1413,34 @@ static guint32 ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Erro
 }
 
 static MonoReflectionType*
-ves_icall_System_Reflection_Assembly_GetType (MonoReflectionAssembly *assembly, MonoString *type, MonoBoolean throwOnError, MonoBoolean ignoreCase)
+ves_icall_System_Reflection_Assembly_GetType (MonoReflectionAssembly *assembly, MonoString *name, MonoBoolean throwOnError, MonoBoolean ignoreCase)
 {
        MonoDomain *domain = mono_domain_get (); 
-       /* FIXME : use ignoreCase */
-       gchar *name, *namespace, *str;
-       char *byref, *isarray, *ispointer, *isa2;
-       guint rank, recarray = 0;
-       MonoClass *klass;
+       gchar *str;
+       MonoType *type;
+       MonoTypeNameParse info;
 
-       str = namespace = mono_string_to_utf8 (type);
+       str = mono_string_to_utf8 (name);
        /*g_print ("requested type %s in %s\n", str, assembly->assembly->name);*/
-
-       name = strrchr (str, '.');
-       byref = strrchr (str, '&');
-       ispointer = strrchr (str, '*');
-       if (byref)
-               *byref = 0;
-       if (ispointer)
-               *ispointer = 0;
-       isarray = strrchr (str, '[');
-       if (isarray) {
-               rank = 1;
-               *isarray = 0;
-               while (*isarray) {
-                       if (*isarray == ',')
-                               rank++;
-                       if (*isarray == ']')
-                               break;
-                       ++isarray;
-               }
-       }
-
-       isa2 = strrchr (str, '[');
-       if (isa2 && *isa2 == '[') {
-               /* g_print ("recarray for: type name search: %s.%s\n", namespace, name); */
-               recarray = 1;
-               *isa2 = 0;
-       }
-
-       if (name) {
-               *name = 0;
-               ++name;
-       } else {
-               namespace = "";
-               name = str;
+       if (!mono_reflection_parse_type (str, &info)) {
+               g_free (str);
+               g_list_free (info.modifiers);
+               if (throwOnError) /* uhm: this is a parse error, though... */
+                       mono_raise_exception (mono_get_exception_type_load ());
+               return NULL;
        }
 
-       /*g_print ("type name search: %s.%s\n", namespace, name);*/
-       klass = mono_class_from_name (assembly->assembly->image, namespace, name);
+       type = mono_reflection_get_type (assembly->assembly->image, &info, ignoreCase);
        g_free (str);
-       if (!klass) {
+       g_list_free (info.modifiers);
+       if (!type) {
                if (throwOnError)
                        mono_raise_exception (mono_get_exception_type_load ());
                return NULL;
        }
-       if (!klass->inited)
-               mono_class_init (klass);
-
-       if (isarray) {
-               klass = mono_array_class_get (&klass->byval_arg, rank);
-               mono_class_init (klass);
-               if (recarray) {
-                       klass = mono_array_class_get (klass, rank);
-                       mono_class_init (klass);
-               }
-               /*g_print ("got array class %s [%d] (0x%x)\n", klass->element_class->name, klass->rank, klass->this_arg.type);*/
-       }
+       /*g_print ("got it\n");*/
+       return mono_type_get_object (domain, type);
 
-       if (byref || ispointer)
-               return mono_type_get_object (domain, &klass->this_arg);
-       else
-               return mono_type_get_object (domain, &klass->byval_arg);
 }
 
 static MonoString *
@@ -1507,16 +1506,54 @@ ves_icall_System_MonoType_assQualifiedName (MonoReflectionType *object)
 }
 
 static MonoReflectionType*
-ves_icall_ModuleBuilder_create_modified_type (MonoReflectionType *tb, gint32 arrayrank, MonoBoolean isbyref)
+ves_icall_ModuleBuilder_create_modified_type (MonoReflectionTypeBuilder *tb, MonoString *smodifiers)
 {
        MonoClass *klass;
-
-       if (arrayrank)
-               klass = mono_array_class_get (tb->type, arrayrank);
-       else
-               klass = mono_class_from_mono_type (tb->type);
-
-       return mono_type_get_object (mono_domain_get (), isbyref? &klass->this_arg: &klass->byval_arg);
+       int isbyref = 0, rank;
+       char *str = mono_string_to_utf8 (smodifiers);
+       char *p;
+
+       klass = mono_class_from_mono_type (tb->type.type);
+       p = str;
+       /* logic taken from mono_reflection_parse_type(): keep in sync */
+       while (*p) {
+               switch (*p) {
+               case '&':
+                       if (isbyref) /* only one level allowed by the spec */
+                               return NULL;
+                       isbyref = 1;
+                       p++;
+                       return mono_type_get_object (mono_domain_get (), &klass->this_arg);
+                       break;
+               case '*':
+                       klass = mono_ptr_class_get (&klass->byval_arg);
+                       mono_class_init (klass);
+                       p++;
+                       break;
+               case '[':
+                       rank = 1;
+                       p++;
+                       while (*p) {
+                               if (*p == ']')
+                                       break;
+                               if (*p == ',')
+                                       rank++;
+                               else if (*p != '*') /* '*' means unknown lower bound */
+                                       return NULL;
+                               ++p;
+                       }
+                       if (*p != ']')
+                               return NULL;
+                       p++;
+                       klass = mono_array_class_get (&klass->byval_arg, rank);
+                       mono_class_init (klass);
+                       break;
+               default:
+                       break;
+               }
+       }
+       g_free (str);
+       return mono_type_get_object (mono_domain_get (), &klass->byval_arg);
 }
 
 static MonoString *
@@ -1638,17 +1675,59 @@ ves_icall_System_CurrentTimeZone_GetTimeZoneData (guint32 year, MonoArray **data
 
                gmtoff = tt.tm_gmtoff;
        }
-
-       return 0;
 #endif
+       return 0;
 }
 
-
 static gpointer
 ves_icall_System_Object_obj_address (MonoObject *this) {
        return this;
 }
 
+/* System.Buffer */
+
+static gint32 
+ves_icall_System_Buffer_ByteLengthInternal (MonoArray *array) {
+       MonoClass *klass;
+       MonoTypeEnum etype;
+       int length, esize;
+       int i;
+
+       klass = array->obj.vtable->klass;
+       etype = klass->element_class->byval_arg.type;
+       if (etype < MONO_TYPE_BOOLEAN || etype > MONO_TYPE_R8)
+               return -1;
+
+       length = 0;
+       for (i = 0; i < klass->rank; ++ i)
+               length += array->bounds [i].length;
+
+       esize = mono_array_element_size (klass);
+       return length * esize;
+}
+
+static gint8 
+ves_icall_System_Buffer_GetByteInternal (MonoArray *array, gint32 index) {
+       return mono_array_get (array, gint8, index);
+}
+
+static void 
+ves_icall_System_Buffer_SetByteInternal (MonoArray *array, gint32 index, gint8 value) {
+       mono_array_set (array, gint8, index, value);
+}
+
+static void 
+ves_icall_System_Buffer_BlockCopyInternal (MonoArray *src, gint32 src_offset, MonoArray *dest, gint32 dest_offset, gint32 count) {
+       char *src_buf, *dest_buf;
+
+       src_buf = (gint8 *)src->vector + src_offset;
+       dest_buf = (gint8 *)dest->vector + dest_offset;
+
+       memcpy (dest_buf, src_buf, count);
+}
+
+/* icall map */
+
 static gpointer icall_map [] = {
        /*
         * System.Array
@@ -1669,8 +1748,14 @@ static gpointer icall_map [] = {
         */
        "System.Object::MemberwiseClone", ves_icall_System_Object_MemberwiseClone,
        "System.Object::GetType", ves_icall_System_Object_GetType,
+       "System.Object::GetHashCode", ves_icall_System_Object_GetHashCode,
        "System.Object::obj_address", ves_icall_System_Object_obj_address,
 
+       /*
+        * System.ValueType
+        */
+       "System.ValueType::GetHashCode", ves_icall_System_ValueType_GetHashCode,
+
        /*
         * System.String
         */
@@ -1735,9 +1820,13 @@ static gpointer icall_map [] = {
        "System.Reflection.MonoFieldInfo::get_field_info", ves_icall_get_field_info,
        "System.Reflection.MonoPropertyInfo::get_property_info", ves_icall_get_property_info,
        "System.Reflection.MonoMethod::InternalInvoke", ves_icall_InternalInvoke,
+       "System.Reflection.MonoCMethod::InternalInvoke", ves_icall_InternalInvoke,
        "System.MonoCustomAttrs::GetCustomAttributes", mono_reflection_get_custom_attrs,
        "System.Reflection.Emit.CustomAttributeBuilder::GetBlob", mono_reflection_get_custom_attrs_blob,
        "System.Reflection.MonoField::GetValue", ves_icall_MonoField_GetValue,
+       "System.Reflection.Emit.SignatureHelper::get_signature_local", mono_reflection_sighelper_get_signature_local,
+       "System.Reflection.Emit.SignatureHelper::get_signature_field", mono_reflection_sighelper_get_signature_field,
+
        
        /* System.Enum */
 
@@ -1749,6 +1838,7 @@ static gpointer icall_map [] = {
         * TypeBuilder
         */
        "System.Reflection.Emit.TypeBuilder::setup_internal_class", mono_reflection_setup_internal_class,
+
        
        /*
         * MethodBuilder
@@ -1827,12 +1917,6 @@ static gpointer icall_map [] = {
         * System.PAL.OpSys I/O Services
         */
        "System.PAL.OpSys::GetStdHandle", ves_icall_System_PAL_OpSys_GetStdHandle,
-       "System.PAL.OpSys::ReadFile", ves_icall_System_PAL_OpSys_ReadFile,
-       "System.PAL.OpSys::WriteFile", ves_icall_System_PAL_OpSys_WriteFile,
-       "System.PAL.OpSys::SetLengthFile", ves_icall_System_PAL_OpSys_SetLengthFile,
-       "System.PAL.OpSys::OpenFile", ves_icall_System_PAL_OpSys_OpenFile,
-       "System.PAL.OpSys::CloseFile", ves_icall_System_PAL_OpSys_CloseFile,
-       "System.PAL.OpSys::SeekFile", ves_icall_System_PAL_OpSys_SeekFile,
        "System.PAL.OpSys::DeleteFile", ves_icall_System_PAL_OpSys_DeleteFile,
        "System.PAL.OpSys::ExistsFile", ves_icall_System_PAL_OpSys_ExistsFile,
        "System.PAL.OpSys::GetFileTime", ves_icall_System_PAL_OpSys_GetFileTime,
@@ -1900,6 +1984,54 @@ static gpointer icall_map [] = {
 
         "System.Security.Cryptography.RNGCryptoServiceProvider::GetBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_GetBytes,
         "System.Security.Cryptography.RNG_CryptoServiceProvider::GetNonZeroBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_GetNonZeroBytes,
+       
+       /*
+        * System.Buffer
+        */
+       "System.Buffer::ByteLengthInternal", ves_icall_System_Buffer_ByteLengthInternal,
+       "System.Buffer::GetByteInternal", ves_icall_System_Buffer_GetByteInternal,
+       "System.Buffer::SetByteInternal", ves_icall_System_Buffer_SetByteInternal,
+       "System.Buffer::BlockCopyInternal", ves_icall_System_Buffer_BlockCopyInternal,
+
+       /*
+        * System.IO.FileStream
+        */
+       "System.IO.FileStream::FileOpen", ves_icall_System_IO_FileStream_FileOpen,
+       "System.IO.FileStream::FileClose", ves_icall_System_IO_FileStream_FileClose,
+       "System.IO.FileStream::FileRead", ves_icall_System_IO_FileStream_FileRead,
+       "System.IO.FileStream::FileWrite", ves_icall_System_IO_FileStream_FileWrite,
+       "System.IO.FileStream::FileSeek", ves_icall_System_IO_FileStream_FileSeek,
+       "System.IO.FileStream::FileGetLength", ves_icall_System_IO_FileStream_FileGetLength,
+       "System.IO.FileStream::FileSetLength", ves_icall_System_IO_FileStream_FileSetLength,
+       "System.IO.FileStream::FileFlush", ves_icall_System_IO_FileStream_FileFlush,
+
+       /*
+        * System.Math
+        */
+       "System.Math::Sin", ves_icall_System_Math_Sin,
+        "System.Math::Cos", ves_icall_System_Math_Cos,
+        "System.Math::Tan", ves_icall_System_Math_Tan,
+        "System.Math::Sinh", ves_icall_System_Math_Sinh,
+        "System.Math::Cosh", ves_icall_System_Math_Cosh,
+        "System.Math::Tanh", ves_icall_System_Math_Tanh,
+        "System.Math::Acos", ves_icall_System_Math_Acos,
+        "System.Math::Asin", ves_icall_System_Math_Asin,
+        "System.Math::Atan", ves_icall_System_Math_Atan,
+        "System.Math::Atan2", ves_icall_System_Math_Atan2,
+        "System.Math::Exp", ves_icall_System_Math_Exp,
+        "System.Math::Log", ves_icall_System_Math_Log,
+        "System.Math::Log10", ves_icall_System_Math_Log10,
+        "System.Math::Pow", ves_icall_System_Math_Pow,
+        "System.Math::Sqrt", ves_icall_System_Math_Sqrt,
+
+       /*
+        * Mono.CSharp.Debugger
+        */
+       "Mono.CSharp.Debugger.MonoSymbolWriter::get_local_type_from_sig", ves_icall_Debugger_MonoSymbolWriter_get_local_type_from_sig,
+       "Mono.CSharp.Debugger.MonoSymbolWriter::get_method", ves_icall_Debugger_MonoSymbolWriter_method_from_token,
+       "Mono.CSharp.Debugger.DwarfFileWriter::get_type_token", ves_icall_Debugger_DwarfFileWriter_get_type_token,
+
+
 
        /*
         * add other internal calls here