Fri May 10 16:31:49 CEST 2002 Paolo Molaro <lupus@ximian.com>
[mono.git] / mono / metadata / icall.c
1 /*
2  * icall.c:
3  *
4  * Authors:
5  *   Dietmar Maurer (dietmar@ximian.com)
6  *   Paolo Molaro (lupus@ximian.com)
7  *       Patrik Torstensson (patrik.torstensson@labs2.com)
8  *
9  * (C) 2001 Ximian, Inc.
10  */
11
12 #include <config.h>
13 #include <glib.h>
14 #include <stdarg.h>
15 #include <string.h>
16 #include <sys/time.h>
17 #include <unistd.h>
18 #if defined (PLATFORM_WIN32)
19 #include <stdlib.h>
20 #endif
21
22 #include <mono/metadata/object.h>
23 #include <mono/metadata/threads.h>
24 #include <mono/metadata/reflection.h>
25 #include <mono/metadata/assembly.h>
26 #include <mono/metadata/tabledefs.h>
27 #include <mono/metadata/exception.h>
28 #include <mono/metadata/file-io.h>
29 #include <mono/metadata/socket-io.h>
30 #include <mono/metadata/mono-endian.h>
31 #include <mono/metadata/tokentype.h>
32 #include <mono/metadata/unicode.h>
33 #include <mono/metadata/appdomain.h>
34 #include <mono/metadata/gc.h>
35 #include <mono/metadata/rand.h>
36 #include <mono/metadata/sysmath.h>
37 #include <mono/metadata/debug-symfile.h>
38 #include <mono/metadata/string-icalls.h>
39 #include <mono/io-layer/io-layer.h>
40
41 #if defined (PLATFORM_WIN32)
42 #include <windows.h>
43 #endif
44 #include "decimal.h"
45
46 static MonoString *
47 mono_double_ToStringImpl (double value)
48 {
49         /* FIXME: Handle formats, etc. */
50         const gchar *retVal;
51         retVal = g_strdup_printf ("%f", value);
52         return mono_string_new (mono_domain_get (), retVal);
53 }
54
55 static MonoString *
56 mono_float_ToStringImpl (float value)
57 {
58         /* FIXME: Handle formats, etc. */
59         const gchar *retVal;
60         retVal = g_strdup_printf ("%f", value);
61         return mono_string_new (mono_domain_get (), retVal);
62 }
63
64 static MonoObject *
65 ves_icall_System_Array_GetValueImpl (MonoObject *this, guint32 pos)
66 {
67         MonoClass *ac;
68         MonoArray *ao;
69         gint32 esize;
70         gpointer *ea;
71
72         ao = (MonoArray *)this;
73         ac = (MonoClass *)ao->obj.vtable->klass;
74
75         esize = mono_array_element_size (ac);
76         ea = (gpointer*)((char*)ao->vector + (pos * esize));
77
78         if (ac->element_class->valuetype)
79                 return mono_value_box (this->vtable->domain, ac->element_class, ea);
80         else
81                 return *ea;
82 }
83
84 static MonoObject *
85 ves_icall_System_Array_GetValue (MonoObject *this, MonoObject *idxs)
86 {
87         MonoClass *ac, *ic;
88         MonoArray *ao, *io;
89         gint32 i, pos, *ind;
90
91         MONO_CHECK_ARG_NULL (idxs);
92
93         io = (MonoArray *)idxs;
94         ic = (MonoClass *)io->obj.vtable->klass;
95         
96         ao = (MonoArray *)this;
97         ac = (MonoClass *)ao->obj.vtable->klass;
98
99         g_assert (ic->rank == 1);
100         if (io->bounds != NULL || io->max_length !=  ac->rank)
101                 mono_raise_exception (mono_get_exception_argument (NULL, NULL));
102
103         ind = (guint32 *)io->vector;
104
105         if (ao->bounds == NULL) {
106                 if (*ind < 0 || *ind >= ao->max_length)
107                         mono_raise_exception (mono_get_exception_index_out_of_range ());
108
109                 return ves_icall_System_Array_GetValueImpl (this, *ind);
110         }
111         
112         for (i = 0; i < ac->rank; i++)
113                 if ((ind [i] < ao->bounds [i].lower_bound) ||
114                     (ind [i] >= ao->bounds [i].length + ao->bounds [i].lower_bound))
115                         mono_raise_exception (mono_get_exception_index_out_of_range ());
116
117         pos = ind [0] - ao->bounds [0].lower_bound;
118         for (i = 1; i < ac->rank; i++)
119                 pos = pos*ao->bounds [i].length + ind [i] - 
120                         ao->bounds [i].lower_bound;
121
122         return ves_icall_System_Array_GetValueImpl (this, pos);
123 }
124
125 static void
126 ves_icall_System_Array_SetValueImpl (MonoArray *this, MonoObject *value, guint32 pos)
127 {
128         MonoClass *ac, *vc, *ec;
129         gint32 esize, vsize;
130         gpointer *ea, *va;
131
132         guint64 u64;
133         gint64 i64;
134         gdouble r64;
135
136         if (value)
137                 vc = value->vtable->klass;
138         else
139                 vc = NULL;
140
141         ac = this->obj.vtable->klass;
142         ec = ac->element_class;
143
144         esize = mono_array_element_size (ac);
145         ea = (gpointer*)((char*)this->vector + (pos * esize));
146         va = (gpointer*)((char*)value + sizeof (MonoObject));
147
148         if (!value) {
149                 memset (ea, 0,  esize);
150                 return;
151         }
152
153 #define NO_WIDENING_CONVERSION G_STMT_START{\
154         mono_raise_exception (mono_get_exception_argument ( \
155                 "value", "not a widening conversion")); \
156 }G_STMT_END
157
158 #define CHECK_WIDENING_CONVERSION(extra) G_STMT_START{\
159         if (esize < vsize + ## extra) \
160                 mono_raise_exception (mono_get_exception_argument ( \
161                         "value", "not a widening conversion")); \
162 }G_STMT_END
163
164 #define INVALID_CAST G_STMT_START{\
165         mono_raise_exception (mono_get_exception_invalid_cast ()); \
166 }G_STMT_END
167
168         /* Check element (destination) type. */
169         switch (ec->byval_arg.type) {
170         case MONO_TYPE_STRING:
171                 switch (vc->byval_arg.type) {
172                 case MONO_TYPE_STRING:
173                         break;
174                 default:
175                         INVALID_CAST;
176                 }
177                 break;
178         case MONO_TYPE_BOOLEAN:
179                 switch (vc->byval_arg.type) {
180                 case MONO_TYPE_BOOLEAN:
181                         break;
182                 case MONO_TYPE_CHAR:
183                 case MONO_TYPE_U1:
184                 case MONO_TYPE_U2:
185                 case MONO_TYPE_U4:
186                 case MONO_TYPE_U8:
187                 case MONO_TYPE_I1:
188                 case MONO_TYPE_I2:
189                 case MONO_TYPE_I4:
190                 case MONO_TYPE_I8:
191                 case MONO_TYPE_R4:
192                 case MONO_TYPE_R8:
193                         NO_WIDENING_CONVERSION;
194                 default:
195                         INVALID_CAST;
196                 }
197                 break;
198         }
199
200         if (!ec->valuetype) {
201                 *ea = (gpointer)value;
202                 return;
203         }
204
205         if (mono_object_isinst (value, ec)) {
206                 memcpy (ea, (char *)value + sizeof (MonoObject), esize);
207                 return;
208         }
209
210         if (!vc->valuetype)
211                 INVALID_CAST;
212
213         vsize = mono_class_instance_size (vc) - sizeof (MonoObject);
214
215 #if 0
216         g_message (G_STRLOC ": %d (%d) <= %d (%d)",
217                    ec->byval_arg.type, esize,
218                    vc->byval_arg.type, vsize);
219 #endif
220
221 #define ASSIGN_UNSIGNED(etype) G_STMT_START{\
222         switch (vc->byval_arg.type) { \
223         case MONO_TYPE_U1: \
224         case MONO_TYPE_U2: \
225         case MONO_TYPE_U4: \
226         case MONO_TYPE_U8: \
227         case MONO_TYPE_CHAR: \
228                 CHECK_WIDENING_CONVERSION(0); \
229                 *(## etype *) ea = (## etype) u64; \
230                 return; \
231         /* You can't assign a signed value to an unsigned array. */ \
232         case MONO_TYPE_I1: \
233         case MONO_TYPE_I2: \
234         case MONO_TYPE_I4: \
235         case MONO_TYPE_I8: \
236         /* You can't assign a floating point number to an integer array. */ \
237         case MONO_TYPE_R4: \
238         case MONO_TYPE_R8: \
239                 NO_WIDENING_CONVERSION; \
240         } \
241 }G_STMT_END
242
243 #define ASSIGN_SIGNED(etype) G_STMT_START{\
244         switch (vc->byval_arg.type) { \
245         case MONO_TYPE_I1: \
246         case MONO_TYPE_I2: \
247         case MONO_TYPE_I4: \
248         case MONO_TYPE_I8: \
249                 CHECK_WIDENING_CONVERSION(0); \
250                 *(## etype *) ea = (## etype) i64; \
251                 return; \
252         /* You can assign an unsigned value to a signed array if the array's */ \
253         /* element size is larger than the value size. */ \
254         case MONO_TYPE_U1: \
255         case MONO_TYPE_U2: \
256         case MONO_TYPE_U4: \
257         case MONO_TYPE_U8: \
258         case MONO_TYPE_CHAR: \
259                 CHECK_WIDENING_CONVERSION(1); \
260                 *(## etype *) ea = (## etype) u64; \
261                 return; \
262         /* You can't assign a floating point number to an integer array. */ \
263         case MONO_TYPE_R4: \
264         case MONO_TYPE_R8: \
265                 NO_WIDENING_CONVERSION; \
266         } \
267 }G_STMT_END
268
269 #define ASSIGN_REAL(etype) G_STMT_START{\
270         switch (vc->byval_arg.type) { \
271         case MONO_TYPE_R4: \
272         case MONO_TYPE_R8: \
273                 CHECK_WIDENING_CONVERSION(0); \
274                 *(## etype *) ea = (## etype) r64; \
275                 return; \
276         /* All integer values fit into a floating point array, so we don't */ \
277         /* need to CHECK_WIDENING_CONVERSION here. */ \
278         case MONO_TYPE_I1: \
279         case MONO_TYPE_I2: \
280         case MONO_TYPE_I4: \
281         case MONO_TYPE_I8: \
282                 *(## etype *) ea = (## etype) i64; \
283                 return; \
284         case MONO_TYPE_U1: \
285         case MONO_TYPE_U2: \
286         case MONO_TYPE_U4: \
287         case MONO_TYPE_U8: \
288         case MONO_TYPE_CHAR: \
289                 *(## etype *) ea = (## etype) u64; \
290                 return; \
291         } \
292 }G_STMT_END
293
294         switch (vc->byval_arg.type) {
295         case MONO_TYPE_U1:
296                 u64 = *(guint8 *) va;
297                 break;
298         case MONO_TYPE_U2:
299                 u64 = *(guint16 *) va;
300                 break;
301         case MONO_TYPE_U4:
302                 u64 = *(guint32 *) va;
303                 break;
304         case MONO_TYPE_U8:
305                 u64 = *(guint64 *) va;
306                 break;
307         case MONO_TYPE_I1:
308                 i64 = *(gint8 *) va;
309                 break;
310         case MONO_TYPE_I2:
311                 i64 = *(gint16 *) va;
312                 break;
313         case MONO_TYPE_I4:
314                 i64 = *(gint32 *) va;
315                 break;
316         case MONO_TYPE_I8:
317                 i64 = *(gint64 *) va;
318                 break;
319         case MONO_TYPE_R4:
320                 r64 = *(gfloat *) va;
321                 break;
322         case MONO_TYPE_R8:
323                 r64 = *(gdouble *) va;
324                 break;
325         case MONO_TYPE_CHAR:
326                 u64 = *(guint16 *) va;
327                 break;
328         case MONO_TYPE_BOOLEAN:
329                 /* Boolean is only compatible with itself. */
330                 switch (ec->byval_arg.type) {
331                 case MONO_TYPE_CHAR:
332                 case MONO_TYPE_U1:
333                 case MONO_TYPE_U2:
334                 case MONO_TYPE_U4:
335                 case MONO_TYPE_U8:
336                 case MONO_TYPE_I1:
337                 case MONO_TYPE_I2:
338                 case MONO_TYPE_I4:
339                 case MONO_TYPE_I8:
340                 case MONO_TYPE_R4:
341                 case MONO_TYPE_R8:
342                         NO_WIDENING_CONVERSION;
343                 default:
344                         INVALID_CAST;
345                 }
346                 break;
347         }
348
349         /* If we can't do a direct copy, let's try a widening conversion. */
350         switch (ec->byval_arg.type) {
351         case MONO_TYPE_CHAR:
352                 ASSIGN_UNSIGNED (guint16);
353         case MONO_TYPE_U1:
354                 ASSIGN_UNSIGNED (guint8);
355         case MONO_TYPE_U2:
356                 ASSIGN_UNSIGNED (guint16);
357         case MONO_TYPE_U4:
358                 ASSIGN_UNSIGNED (guint32);
359         case MONO_TYPE_U8:
360                 ASSIGN_UNSIGNED (guint64);
361         case MONO_TYPE_I1:
362                 ASSIGN_SIGNED (gint8);
363         case MONO_TYPE_I2:
364                 ASSIGN_SIGNED (gint16);
365         case MONO_TYPE_I4:
366                 ASSIGN_SIGNED (gint32);
367         case MONO_TYPE_I8:
368                 ASSIGN_SIGNED (gint64);
369         case MONO_TYPE_R4:
370                 ASSIGN_REAL (gfloat);
371         case MONO_TYPE_R8:
372                 ASSIGN_REAL (gdouble);
373         }
374
375         INVALID_CAST;
376         /* Not reached, INVALID_CAST does not return. Just to avoid a compiler warning ... */
377         return;
378
379 #undef INVALID_CAST
380 #undef NO_WIDENING_CONVERSION
381 #undef CHECK_WIDENING_CONVERSION
382 #undef ASSIGN_UNSIGNED
383 #undef ASSIGN_SIGNED
384 #undef ASSIGN_REAL
385 }
386
387 static void 
388 ves_icall_System_Array_SetValue (MonoArray *this, MonoObject *value,
389                                  MonoArray *idxs)
390 {
391         MonoClass *ac, *ic;
392         gint32 i, pos, *ind;
393
394         MONO_CHECK_ARG_NULL (idxs);
395
396         ic = idxs->obj.vtable->klass;
397         ac = this->obj.vtable->klass;
398
399         g_assert (ic->rank == 1);
400         if (idxs->bounds != NULL || idxs->max_length != ac->rank)
401                 mono_raise_exception (mono_get_exception_argument (NULL, NULL));
402
403         ind = (guint32 *)idxs->vector;
404
405         if (this->bounds == NULL) {
406                 if (*ind < 0 || *ind >= this->max_length)
407                         mono_raise_exception (mono_get_exception_index_out_of_range ());
408
409                 ves_icall_System_Array_SetValueImpl (this, value, *ind);
410                 return;
411         }
412         
413         for (i = 0; i < ac->rank; i++)
414                 if ((ind [i] < this->bounds [i].lower_bound) ||
415                     (ind [i] >= this->bounds [i].length + this->bounds [i].lower_bound))
416                         mono_raise_exception (mono_get_exception_index_out_of_range ());
417
418         pos = ind [0] - this->bounds [0].lower_bound;
419         for (i = 1; i < ac->rank; i++)
420                 pos = pos * this->bounds [i].length + ind [i] - 
421                         this->bounds [i].lower_bound;
422
423         ves_icall_System_Array_SetValueImpl (this, value, pos);
424 }
425
426 static MonoArray *
427 ves_icall_System_Array_CreateInstanceImpl (MonoReflectionType *type, MonoArray *lengths, MonoArray *bounds)
428 {
429         MonoClass *aklass;
430         MonoArray *array;
431         gint32 *sizes, i;
432
433         MONO_CHECK_ARG_NULL (type);
434         MONO_CHECK_ARG_NULL (lengths);
435
436         MONO_CHECK_ARG (lengths, mono_array_length (lengths) > 0);
437         if (bounds)
438                 MONO_CHECK_ARG (bounds, mono_array_length (lengths) == mono_array_length (bounds));
439
440         for (i = 0; i < mono_array_length (lengths); i++)
441                 if (mono_array_get (lengths, gint32, i) < 0)
442                         mono_raise_exception (mono_get_exception_argument_out_of_range (NULL));
443
444         aklass = mono_array_class_get (type->type, mono_array_length (lengths));
445
446         sizes = alloca (aklass->rank * sizeof(guint32) * 2);
447         for (i = 0; i < aklass->rank; ++i) {
448                 sizes [i] = mono_array_get (lengths, gint32, i);
449                 if (bounds)
450                         sizes [i + aklass->rank] = mono_array_get (bounds, gint32, i);
451                 else
452                         sizes [i + aklass->rank] = 0;
453         }
454
455         array = mono_array_new_full (mono_domain_get (), aklass, sizes, sizes + aklass->rank);
456
457         return array;
458 }
459
460 static gint32 
461 ves_icall_System_Array_GetRank (MonoObject *this)
462 {
463         return this->vtable->klass->rank;
464 }
465
466 static gint32
467 ves_icall_System_Array_GetLength (MonoArray *this, gint32 dimension)
468 {
469         gint32 rank = ((MonoObject *)this)->vtable->klass->rank;
470         if ((dimension < 0) || (dimension >= rank))
471                 mono_raise_exception (mono_get_exception_index_out_of_range ());
472         
473         if (this->bounds == NULL)
474                 return this->max_length;
475         
476         return this->bounds [dimension].length;
477 }
478
479 static gint32
480 ves_icall_System_Array_GetLowerBound (MonoArray *this, gint32 dimension)
481 {
482         gint32 rank = ((MonoObject *)this)->vtable->klass->rank;
483         if ((dimension < 0) || (dimension >= rank))
484                 mono_raise_exception (mono_get_exception_index_out_of_range ());
485         
486         if (this->bounds == NULL)
487                 return 0;
488         
489         return this->bounds [dimension].lower_bound;
490 }
491
492 static void
493 ves_icall_System_Array_FastCopy (MonoArray *source, int source_idx, MonoArray* dest, int dest_idx, int length)
494 {
495         int element_size = mono_array_element_size (source->obj.vtable->klass);
496         void * dest_addr = mono_array_addr_with_size (dest, element_size, dest_idx);
497         void * source_addr = mono_array_addr_with_size (source, element_size, source_idx);
498
499         g_assert (dest_idx + length <= mono_array_length (dest));
500         g_assert (source_idx + length <= mono_array_length (source));
501         memmove (dest_addr, source_addr, element_size * length);
502 }
503
504 static void
505 ves_icall_InitializeArray (MonoArray *array, MonoClassField *field_handle)
506 {
507         MonoClass *klass = array->obj.vtable->klass;
508         guint32 size = mono_array_element_size (klass);
509         int i;
510
511         if (array->bounds == NULL)
512                 size *= array->max_length;
513         else
514                 for (i = 0; i < klass->rank; ++i) 
515                         size *= array->bounds [i].length;
516
517         memcpy (mono_array_addr (array, char, 0), field_handle->data, size);
518
519 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
520 #define SWAP(n) {\
521         gint i; \
522         guint ## n tmp; \
523         guint ## n *data = (guint ## n *) mono_array_addr (array, char, 0); \
524 \
525         for (i = 0; i < size; i += n/8, data++) { \
526                 tmp = read ## n (data); \
527                 *data = tmp; \
528         } \
529 }
530
531         printf ("Initialize array with elements of %s type\n", klass->element_class->name);
532
533         switch (klass->element_class->byval_arg.type) {
534         case MONO_TYPE_CHAR:
535         case MONO_TYPE_I2:
536         case MONO_TYPE_U2:
537                 SWAP (16);
538                 break;
539         case MONO_TYPE_I4:
540         case MONO_TYPE_U4:
541                 SWAP (32);
542                 break;
543         case MONO_TYPE_I8:
544         case MONO_TYPE_U8:
545                 SWAP (64);
546                 break;
547         }
548                  
549 #endif
550 }
551
552 static MonoObject *
553 ves_icall_System_Object_MemberwiseClone (MonoObject *this)
554 {
555         return mono_object_clone (this);
556 }
557
558 static gint32
559 ves_icall_System_Object_GetHashCode (MonoObject *this)
560 {
561         return *((gint32 *)this - 1);
562 }
563
564 /*
565  * A hash function for value types. I have no idea if this is a good hash 
566  * function (its similar to g_str_hash).
567  */
568 static gint32
569 ves_icall_System_ValueType_GetHashCode (MonoObject *this)
570 {
571         gint32 i, size;
572         const char *p;
573         guint h;
574
575         MONO_CHECK_ARG_NULL (this);
576
577         size = this->vtable->klass->instance_size - sizeof (MonoObject);
578
579         p = (const char *)this + sizeof (MonoObject);
580
581         for (i = 0; i < size; i++) {
582                 h = (h << 5) - h + *p;
583                 p++;
584         }
585
586         return h;
587 }
588
589 static MonoBoolean
590 ves_icall_System_ValueType_Equals (MonoObject *this, MonoObject *that)
591 {
592         gint32 size;
593         const char *p, *s;
594
595         MONO_CHECK_ARG_NULL (that);
596
597         if (this->vtable != that->vtable)
598                 return FALSE;
599
600         size = this->vtable->klass->instance_size - sizeof (MonoObject);
601
602         p = (const char *)this + sizeof (MonoObject);
603         s = (const char *)that + sizeof (MonoObject);
604
605         return memcmp (p, s, size)? FALSE: TRUE;
606 }
607
608 static MonoReflectionType *
609 ves_icall_System_Object_GetType (MonoObject *obj)
610 {
611         return mono_type_get_object (mono_domain_get (), &obj->vtable->klass->byval_arg);
612 }
613
614 static void
615 mono_type_type_from_obj (MonoReflectionType *mtype, MonoObject *obj)
616 {
617         mtype->type = &obj->vtable->klass->byval_arg;
618         g_assert (mtype->type->type);
619 }
620
621 static gint32
622 ves_icall_AssemblyBuilder_getToken (MonoReflectionAssemblyBuilder *assb, MonoObject *obj)
623 {
624         return mono_image_create_token (assb->dynamic_assembly, obj);
625 }
626
627 static gint32
628 ves_icall_AssemblyBuilder_getPEHeader (MonoReflectionAssemblyBuilder *assb, MonoArray *buf, gint32 *data_size)
629 {
630         int count, hsize;
631         MonoDynamicAssembly *ass = assb->dynamic_assembly;
632
633         hsize = mono_image_get_header (assb, (char*)buf->vector, mono_array_length (buf));
634         if (hsize < 0)
635                 return hsize;
636         count = ass->code.index + ass->meta_size;
637         count += 512 - 1;
638         count &= ~(512 - 1);
639         *data_size = count;
640
641         return hsize;
642 }
643
644 static gint32
645 ves_icall_AssemblyBuilder_getDataChunk (MonoReflectionAssemblyBuilder *assb, MonoArray *buf)
646 {
647         int count, real_data;
648         MonoDynamicAssembly *ass = assb->dynamic_assembly;
649         char *p = mono_array_addr (buf, char, 0);
650         
651         count = real_data = ass->code.index + ass->meta_size;
652         count += 512 - 1;
653         count &= ~(512 - 1);
654         if (count > mono_array_length (buf))
655                 return -1;
656         count -= real_data;
657         
658         memcpy (p, ass->code.data, ass->code.index);
659         p += ass->code.index;
660         memcpy (p, ass->assembly.image->raw_metadata, ass->meta_size);
661         p += ass->meta_size;
662         /* null-pad section */
663         memset (p, 0, count);
664
665         return real_data + count;
666 }
667
668 static MonoReflectionType*
669 ves_icall_type_from_name (MonoString *name)
670 {
671         MonoDomain *domain = mono_domain_get (); 
672         MonoType *type;
673         MonoImage *image;
674         MonoTypeNameParse info;
675         gchar *str;
676         
677         str = mono_string_to_utf8 (name);
678         /*g_print ("requested type %s\n", str);*/
679         if (!mono_reflection_parse_type (str, &info)) {
680                 g_free (str);
681                 g_list_free (info.modifiers);
682                 return NULL;
683         }
684
685         if (info.assembly) {
686                 image = mono_image_loaded (info.assembly);
687                 /* do we need to load if it's not already loaded? */
688                 if (!image) {
689                         g_free (str);
690                         g_list_free (info.modifiers);
691                         return NULL;
692                 }
693         } else
694                 image = mono_defaults.corlib;
695
696         type = mono_reflection_get_type (image, &info, FALSE);
697         g_free (str);
698         g_list_free (info.modifiers);
699         if (!type)
700                 return NULL;
701         /*g_print ("got it\n");*/
702         return mono_type_get_object (domain, type);
703 }
704
705 static MonoReflectionType*
706 ves_icall_type_from_handle (MonoType *handle)
707 {
708         MonoDomain *domain = mono_domain_get (); 
709         MonoClass *klass = mono_class_from_mono_type (handle);
710
711         mono_class_init (klass);
712         return mono_type_get_object (domain, handle);
713 }
714
715 static guint32
716 ves_icall_type_Equals (MonoReflectionType *type, MonoReflectionType *c)
717 {
718         if (type->type && c->type)
719                 return mono_metadata_type_equal (type->type, c->type);
720         g_print ("type equals\n");
721         return 0;
722 }
723
724 static guint32
725 ves_icall_type_is_subtype_of (MonoReflectionType *type, MonoReflectionType *c, MonoBoolean check_interfaces)
726 {
727         MonoDomain *domain; 
728         MonoClass *klass;
729         MonoClass *klassc;
730
731         g_assert (type != NULL);
732         
733         domain = ((MonoObject *)type)->vtable->domain;
734
735         if (!c) /* FIXME: dont know what do do here */
736                 return 0;
737
738         klass = mono_class_from_mono_type (type->type);
739         klassc = mono_class_from_mono_type (c->type);
740
741         /* cut&paste from mono_object_isinst (): keep in sync */
742         if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && !(klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
743                 MonoVTable *klass_vt = mono_class_vtable (domain, klass);
744                 if ((klassc->interface_id <= klass->max_interface_id) &&
745                     klass_vt->interface_offsets [klassc->interface_id])
746                         return 1;
747         } else if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && (klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
748                 int i;
749
750                 for (i = 0; i < klass->interface_count; i ++) {
751                         MonoClass *ic =  klass->interfaces [i];
752                         if (ic == klassc)
753                                 return 1;
754                 }
755         } else {
756                 /*
757                  * klass->baseval is 0 for interfaces 
758                  */
759                 if (klass->baseval && ((klass->baseval - klassc->baseval) <= klassc->diffval))
760                         return 1;
761         }
762         return 0;
763 }
764
765 static guint32
766 ves_icall_get_attributes (MonoReflectionType *type)
767 {
768         MonoClass *klass = mono_class_from_mono_type (type->type);
769
770         return klass->flags;
771 }
772
773 static void
774 ves_icall_get_method_info (MonoMethod *method, MonoMethodInfo *info)
775 {
776         MonoDomain *domain = mono_domain_get (); 
777
778         info->parent = mono_type_get_object (domain, &method->klass->byval_arg);
779         info->ret = mono_type_get_object (domain, method->signature->ret);
780         info->attrs = method->flags;
781         info->implattrs = method->iflags;
782 }
783
784 static MonoArray*
785 ves_icall_get_parameter_info (MonoMethod *method)
786 {
787         MonoDomain *domain = mono_domain_get (); 
788         MonoArray *res;
789         static MonoClass *System_Reflection_ParameterInfo;
790         MonoReflectionParameter** args;
791         int i;
792
793         args = mono_param_get_objects (domain, method);
794         if (!System_Reflection_ParameterInfo)
795                 System_Reflection_ParameterInfo = mono_class_from_name (
796                         mono_defaults.corlib, "System.Reflection", "ParameterInfo");
797         res = mono_array_new (domain, System_Reflection_ParameterInfo, method->signature->param_count);
798         for (i = 0; i < method->signature->param_count; ++i) {
799                 mono_array_set (res, gpointer, i, args [i]);
800         }
801         return res;
802 }
803
804 static void
805 ves_icall_get_field_info (MonoReflectionField *field, MonoFieldInfo *info)
806 {
807         MonoDomain *domain = mono_domain_get (); 
808
809         info->parent = mono_type_get_object (domain, &field->klass->byval_arg);
810         info->type = mono_type_get_object (domain, field->field->type);
811         info->name = mono_string_new (domain, field->field->name);
812         info->attrs = field->field->type->attrs;
813 }
814
815 static MonoObject *
816 ves_icall_MonoField_GetValue (MonoReflectionField *field, MonoObject *obj) {
817         MonoObject *res;
818         MonoClass *klass;
819         MonoType *ftype = field->field->type;
820         int type = ftype->type;
821         char *p, *r;
822         guint32 align;
823
824         mono_class_init (field->klass);
825         if (ftype->attrs & FIELD_ATTRIBUTE_STATIC) {
826                 MonoVTable *vtable;
827                 vtable = mono_class_vtable (mono_domain_get (), field->klass);
828                 p = (char*)(vtable->data) + field->field->offset;
829         } else {
830                 p = (char*)obj + field->field->offset;
831         }
832
833         switch (type) {
834         case MONO_TYPE_OBJECT:
835         case MONO_TYPE_STRING:
836         case MONO_TYPE_SZARRAY:
837         case MONO_TYPE_ARRAY:
838                 return *(MonoObject**)p;
839         }
840         klass = mono_class_from_mono_type (ftype);
841         res = mono_object_new (mono_domain_get (), klass);
842         r = (char*)res + sizeof (MonoObject);
843         memcpy (r, p, mono_class_value_size (klass, &align));
844
845         return res;
846 }
847
848 static void
849 ves_icall_get_property_info (MonoReflectionProperty *property, MonoPropertyInfo *info)
850 {
851         MonoDomain *domain = mono_domain_get (); 
852
853         info->parent = mono_type_get_object (domain, &property->klass->byval_arg);
854         info->name = mono_string_new (domain, property->property->name);
855         info->attrs = property->property->attrs;
856         info->get = property->property->get ? mono_method_get_object (domain, property->property->get): NULL;
857         info->set = property->property->set ? mono_method_get_object (domain, property->property->set): NULL;
858         /* 
859          * There may be other methods defined for properties, though, it seems they are not exposed 
860          * in the reflection API 
861          */
862 }
863
864 static MonoArray*
865 ves_icall_Type_GetInterfaces (MonoReflectionType* type)
866 {
867         MonoDomain *domain = mono_domain_get (); 
868         MonoArray *intf;
869         int ninterf, i;
870         MonoClass *class = mono_class_from_mono_type (type->type);
871         MonoClass *parent;
872
873         ninterf = 0;
874         for (parent = class; parent; parent = parent->parent) {
875                 ninterf += parent->interface_count;
876         }
877         intf = mono_array_new (domain, mono_defaults.monotype_class, ninterf);
878         ninterf = 0;
879         for (parent = class; parent; parent = parent->parent) {
880                 for (i = 0; i < parent->interface_count; ++i) {
881                         mono_array_set (intf, gpointer, ninterf, mono_type_get_object (domain, &parent->interfaces [i]->byval_arg));
882                         ++ninterf;
883                 }
884         }
885         return intf;
886 }
887
888 static MonoReflectionType*
889 ves_icall_MonoType_GetElementType (MonoReflectionType *type)
890 {
891         MonoClass *class = mono_class_from_mono_type (type->type);
892         if (class->enumtype && class->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
893                 return mono_type_get_object (mono_object_domain (type), class->enum_basetype);
894         else if (class->element_class)
895                 return mono_type_get_object (mono_object_domain (type), &class->element_class->byval_arg);
896         else
897                 return NULL;
898 }
899
900 static void
901 ves_icall_get_type_info (MonoType *type, MonoTypeInfo *info)
902 {
903         MonoDomain *domain = mono_domain_get (); 
904         MonoClass *class = mono_class_from_mono_type (type);
905
906         info->parent = class->parent ? mono_type_get_object (domain, &class->parent->byval_arg): NULL;
907         info->name = mono_string_new (domain, class->name);
908         info->name_space = mono_string_new (domain, class->name_space);
909         info->attrs = class->flags;
910         info->rank = class->rank;
911         info->assembly = mono_assembly_get_object (domain, class->image->assembly);
912         if (class->enumtype && class->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
913                 info->etype = mono_type_get_object (domain, class->enum_basetype);
914         else if (class->element_class)
915                 info->etype = mono_type_get_object (domain, &class->element_class->byval_arg);
916         else
917                 info->etype = NULL;
918
919         info->isbyref = type->byref;
920         info->ispointer = type->type == MONO_TYPE_PTR;
921         info->isprimitive = (type->type >= MONO_TYPE_BOOLEAN) && (type->type <= MONO_TYPE_R8);
922 }
923
924 static MonoObject *
925 ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this, MonoArray *params) 
926 {
927         return mono_runtime_invoke_array (method->method, this, params);
928 }
929
930 static MonoObject *
931 ves_icall_InternalExecute (MonoReflectionMethod *method, MonoObject *this, MonoArray *params, MonoArray **outArgs) 
932 {
933         MonoDomain *domain = mono_domain_get (); 
934         MonoMethod *m = method->method;
935         MonoMethodSignature *sig = m->signature;
936         MonoArray *out_args;
937         MonoObject *result;
938         int i, j, outarg_count = 0;
939
940         if (m->klass == mono_defaults.object_class) {
941
942                 if (!strcmp (m->name, "FieldGetter")) {
943                         MonoClass *k = this->vtable->klass;
944                         MonoString *name = mono_array_get (params, MonoString *, 1);
945                         char *str;
946
947                         str = mono_string_to_utf8 (name);
948                 
949                         for (i = 0; i < k->field.count; i++) {
950                                 if (!strcmp (k->fields [i].name, str)) {
951                                         MonoClass *field_klass =  mono_class_from_mono_type (k->fields [i].type);
952                                         if (field_klass->valuetype)
953                                                 result = mono_value_box (domain, field_klass,
954                                                                          (char *)this + k->fields [i].offset);
955                                         else 
956                                                 result = *((gpointer *)((char *)this + k->fields [i].offset));
957                                 
958                                         g_assert (result);
959                                         out_args = mono_array_new (domain, mono_defaults.object_class, 1);
960                                         *outArgs = out_args;
961                                         mono_array_set (out_args, gpointer, 0, result);
962                                         g_free (str);
963                                         return NULL;
964                                 }
965                         }
966
967                         g_free (str);
968                         g_assert_not_reached ();
969
970                 } else if (!strcmp (m->name, "FieldSetter")) {
971                         MonoClass *k = this->vtable->klass;
972                         MonoString *name = mono_array_get (params, MonoString *, 1);
973                         int size, align;
974                         char *str;
975
976                         str = mono_string_to_utf8 (name);
977                 
978                         for (i = 0; i < k->field.count; i++) {
979                                 if (!strcmp (k->fields [i].name, str)) {
980                                         MonoClass *field_klass =  mono_class_from_mono_type (k->fields [i].type);
981                                         MonoObject *val = mono_array_get (params, gpointer, 2);
982
983                                         if (field_klass->valuetype) {
984                                                 size = mono_type_size (k->fields [i].type, &align);
985                                                 memcpy ((char *)this + k->fields [i].offset, 
986                                                         ((char *)val) + sizeof (MonoObject), size);
987                                         } else 
988                                                 *((gpointer *)this + k->fields [i].offset) = val;
989                                 
990                                         g_assert (result);
991                                         g_free (str);
992                                         return NULL;
993                                 }
994                         }
995
996                         g_free (str);
997                         g_assert_not_reached ();
998
999                 }
1000         }
1001
1002         for (i = 0; i < mono_array_length (params); i++) {
1003                 if (sig->params [i]->byref) 
1004                         outarg_count++;
1005         }
1006
1007         out_args = mono_array_new (domain, mono_defaults.object_class, outarg_count);
1008         
1009         for (i = 0, j = 0; i < mono_array_length (params); i++) {
1010                 if (sig->params [i]->byref) {
1011                         gpointer arg;
1012                         arg = mono_array_get (params, gpointer, i);
1013                         mono_array_set (out_args, gpointer, j, arg);
1014                         j++;
1015                 }
1016         }
1017
1018         /* fixme: handle constructors? */
1019         if (!strcmp (method->method->name, ".ctor"))
1020                 g_assert_not_reached ();
1021
1022         result = mono_runtime_invoke_array (method->method, this, params);
1023
1024         *outArgs = out_args;
1025
1026         return result;
1027 }
1028
1029 static MonoObject *
1030 ves_icall_System_Enum_ToObject (MonoReflectionType *type, MonoObject *obj)
1031 {
1032         MonoDomain *domain = mono_domain_get (); 
1033         MonoClass *enumc, *objc;
1034         gint32 s1, s2;
1035         MonoObject *res;
1036         
1037         MONO_CHECK_ARG_NULL (type);
1038         MONO_CHECK_ARG_NULL (obj);
1039
1040         enumc = mono_class_from_mono_type (type->type);
1041         objc = obj->vtable->klass;
1042
1043         MONO_CHECK_ARG (obj, enumc->enumtype == TRUE);
1044         MONO_CHECK_ARG (obj, (objc->enumtype) || (objc->byval_arg.type >= MONO_TYPE_I1 &&
1045                                                   objc->byval_arg.type <= MONO_TYPE_U8));
1046         
1047         s1 = mono_class_value_size (enumc, NULL);
1048         s2 = mono_class_value_size (objc, NULL);
1049
1050         res = mono_object_new (domain, enumc);
1051
1052 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1053         memcpy ((char *)res + sizeof (MonoObject), (char *)obj + sizeof (MonoObject), MIN (s1, s2));
1054 #else
1055         memcpy ((char *)res + sizeof (MonoObject) + (s1 > s2 ? s1 - s2 : 0),
1056                 (char *)obj + sizeof (MonoObject) + (s2 > s1 ? s2 - s1 : 0),
1057                 MIN (s1, s2));
1058 #endif
1059         return res;
1060 }
1061
1062 static MonoObject *
1063 ves_icall_System_Enum_get_value (MonoObject *this)
1064 {
1065         MonoDomain *domain = mono_domain_get (); 
1066         MonoObject *res;
1067         MonoClass *enumc;
1068         gpointer dst;
1069         gpointer src;
1070         int size;
1071
1072         if (!this)
1073                 return NULL;
1074
1075         g_assert (this->vtable->klass->enumtype);
1076         
1077         enumc = mono_class_from_mono_type (this->vtable->klass->enum_basetype);
1078         res = mono_object_new (domain, enumc);
1079         dst = (char *)res + sizeof (MonoObject);
1080         src = (char *)this + sizeof (MonoObject);
1081         size = mono_class_value_size (enumc, NULL);
1082
1083         memcpy (dst, src, size);
1084
1085         return res;
1086 }
1087
1088 static void
1089 ves_icall_get_enum_info (MonoReflectionType *type, MonoEnumInfo *info)
1090 {
1091         MonoDomain *domain = mono_domain_get (); 
1092         MonoClass *enumc = mono_class_from_mono_type (type->type);
1093         guint i, j, nvalues, crow;
1094         MonoClassField *field;
1095         
1096         info->utype = mono_type_get_object (domain, enumc->enum_basetype);
1097         nvalues = enumc->field.count - 1;
1098         info->names = mono_array_new (domain, mono_defaults.string_class, nvalues);
1099         info->values = mono_array_new (domain, enumc, nvalues);
1100         
1101         for (i = 0, j = 0; i < enumc->field.count; ++i) {
1102                 field = &enumc->fields [i];
1103                 if (strcmp ("value__", field->name) == 0)
1104                         continue;
1105                 mono_array_set (info->names, gpointer, j, mono_string_new (domain, field->name));
1106                 if (!field->data) {
1107                         crow = mono_metadata_get_constant_index (enumc->image, MONO_TOKEN_FIELD_DEF | (i+enumc->field.first+1));
1108                         crow = mono_metadata_decode_row_col (&enumc->image->tables [MONO_TABLE_CONSTANT], crow-1, MONO_CONSTANT_VALUE);
1109                         /* 1 is the length of the blob */
1110                         field->data = 1 + mono_metadata_blob_heap (enumc->image, crow);
1111                 }
1112                 switch (enumc->enum_basetype->type) {
1113                 case MONO_TYPE_U1:
1114                 case MONO_TYPE_I1:
1115                         mono_array_set (info->values, gchar, j, *field->data);
1116                         break;
1117                 case MONO_TYPE_CHAR:
1118                 case MONO_TYPE_U2:
1119                 case MONO_TYPE_I2:
1120                         mono_array_set (info->values, gint16, j, read16 (field->data));
1121                         break;
1122                 case MONO_TYPE_U4:
1123                 case MONO_TYPE_I4:
1124                         mono_array_set (info->values, gint32, j, read32 (field->data));
1125                         break;
1126                 case MONO_TYPE_U8:
1127                 case MONO_TYPE_I8:
1128                         mono_array_set (info->values, gint64, j, read64 (field->data));
1129                         break;
1130                 default:
1131                         g_error ("Implement type 0x%02x in get_enum_info", enumc->enum_basetype->type);
1132                 }
1133                 ++j;
1134         }
1135 }
1136
1137 static MonoMethod*
1138 search_method (MonoReflectionType *type, const char *name, guint32 flags, MonoArray *args)
1139 {
1140         MonoClass *klass, *start_class;
1141         MonoMethod *m;
1142         MonoReflectionType *paramt;
1143         int i, j;
1144
1145         start_class = klass = mono_class_from_mono_type (type->type);
1146         while (klass) {
1147                 for (i = 0; i < klass->method.count; ++i) {
1148                         m = klass->methods [i];
1149                         if (!((m->flags & flags) == flags))
1150                                 continue;
1151                         if (strcmp(m->name, name))
1152                                 continue;
1153                         if (m->signature->param_count != mono_array_length (args))
1154                                 continue;
1155                         for (j = 0; j < m->signature->param_count; ++j) {
1156                                 paramt = mono_array_get (args, MonoReflectionType*, j);
1157                                 if (!mono_metadata_type_equal (paramt->type, m->signature->params [j]))
1158                                         break;
1159                         }
1160                         if (j == m->signature->param_count)
1161                                 return m;
1162                 }
1163                 klass = klass->parent;
1164         }
1165         g_print ("Method %s.%s::%s (%d) not found\n", start_class->name_space, start_class->name, name, mono_array_length (args));
1166         return NULL;
1167 }
1168
1169 static MonoReflectionMethod*
1170 ves_icall_get_constructor (MonoReflectionType *type, MonoArray *args)
1171 {
1172         MonoDomain *domain = mono_domain_get (); 
1173         MonoMethod *m;
1174
1175         m = search_method (type, ".ctor", METHOD_ATTRIBUTE_RT_SPECIAL_NAME, args);
1176         if (m)
1177                 return mono_method_get_object (domain, m);
1178         return NULL;
1179 }
1180
1181 static MonoReflectionMethod*
1182 ves_icall_get_method (MonoReflectionType *type, MonoString *name, MonoArray *args)
1183 {
1184         MonoDomain *domain = mono_domain_get (); 
1185         MonoMethod *m;
1186         char *n = mono_string_to_utf8 (name);
1187
1188         m = search_method (type, n, 0, args);
1189         g_free (n);
1190         if (m)
1191                 return mono_method_get_object (domain, m);
1192         return NULL;
1193 }
1194
1195 static MonoProperty*
1196 search_property (MonoClass *klass, char* name, MonoArray *args) {
1197         int i;
1198         MonoProperty *p;
1199
1200         /* FIXME: handle args */
1201         for (i = 0; i < klass->property.count; ++i) {
1202                 p = &klass->properties [i];
1203                 if (strcmp (p->name, name) == 0)
1204                         return p;
1205         }
1206         return NULL;
1207 }
1208
1209 static MonoReflectionProperty*
1210 ves_icall_get_property (MonoReflectionType *type, MonoString *name, MonoArray *args)
1211 {
1212         MonoDomain *domain = mono_domain_get (); 
1213         MonoProperty *p;
1214         MonoClass *class = mono_class_from_mono_type (type->type);
1215         char *n = mono_string_to_utf8 (name);
1216
1217         p = search_property (class, n, args);
1218         g_free (n);
1219         if (p)
1220                 return mono_property_get_object (domain, class, p);
1221         return NULL;
1222 }
1223
1224 enum {
1225         BFLAGS_IgnoreCase = 1,
1226         BFLAGS_DeclaredOnly = 2,
1227         BFLAGS_Instance = 4,
1228         BFLAGS_Static = 8,
1229         BFLAGS_Public = 0x10,
1230         BFLAGS_NonPublic = 0x20,
1231         BFLAGS_InvokeMethod = 0x100,
1232         BFLAGS_CreateInstance = 0x200,
1233         BFLAGS_GetField = 0x400,
1234         BFLAGS_SetField = 0x800,
1235         BFLAGS_GetProperty = 0x1000,
1236         BFLAGS_SetProperty = 0x2000,
1237         BFLAGS_ExactBinding = 0x10000,
1238         BFLAGS_SuppressChangeType = 0x20000,
1239         BFLAGS_OptionalParamBinding = 0x40000
1240 };
1241
1242 static MonoArray*
1243 ves_icall_Type_GetFields (MonoReflectionType *type, guint32 bflags)
1244 {
1245         MonoDomain *domain; 
1246         GSList *l = NULL, *tmp;
1247         static MonoClass *System_Reflection_FieldInfo;
1248         MonoClass *startklass, *klass;
1249         MonoArray *res;
1250         MonoObject *member;
1251         int i, len, match;
1252         MonoClassField *field;
1253
1254         domain = ((MonoObject *)type)->vtable->domain;
1255         klass = startklass = mono_class_from_mono_type (type->type);
1256
1257 handle_parent:  
1258         for (i = 0; i < klass->field.count; ++i) {
1259                 match = 0;
1260                 field = &klass->fields [i];
1261                 if ((field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == FIELD_ATTRIBUTE_PUBLIC) {
1262                         if (bflags & BFLAGS_Public)
1263                                 match++;
1264                 } else {
1265                         if (bflags & BFLAGS_NonPublic)
1266                                 match++;
1267                 }
1268                 if (!match)
1269                         continue;
1270                 match = 0;
1271                 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1272                         if (bflags & BFLAGS_Static)
1273                                 match++;
1274                 } else {
1275                         if (bflags & BFLAGS_Instance)
1276                                 match++;
1277                 }
1278
1279                 if (!match)
1280                         continue;
1281                 member = (MonoObject*)mono_field_get_object (domain, klass, field);
1282                 l = g_slist_prepend (l, member);
1283         }
1284         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1285                 goto handle_parent;
1286         len = g_slist_length (l);
1287         if (!System_Reflection_FieldInfo)
1288                 System_Reflection_FieldInfo = mono_class_from_name (
1289                         mono_defaults.corlib, "System.Reflection", "FieldInfo");
1290         res = mono_array_new (domain, System_Reflection_FieldInfo, len);
1291         i = 0;
1292         tmp = g_slist_reverse (l);
1293         for (; tmp; tmp = tmp->next, ++i)
1294                 mono_array_set (res, gpointer, i, tmp->data);
1295         g_slist_free (l);
1296         return res;
1297 }
1298
1299 static MonoArray*
1300 ves_icall_Type_GetMethods (MonoReflectionType *type, guint32 bflags)
1301 {
1302         MonoDomain *domain; 
1303         GSList *l = NULL, *tmp;
1304         static MonoClass *System_Reflection_MethodInfo;
1305         MonoClass *startklass, *klass;
1306         MonoArray *res;
1307         MonoMethod *method;
1308         MonoObject *member;
1309         int i, len, match;
1310                 
1311         domain = ((MonoObject *)type)->vtable->domain;
1312         klass = startklass = mono_class_from_mono_type (type->type);
1313
1314 handle_parent:
1315         for (i = 0; i < klass->method.count; ++i) {
1316                 match = 0;
1317                 method = klass->methods [i];
1318                 if (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0)
1319                         continue;
1320                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1321                         if (bflags & BFLAGS_Public)
1322                                 match++;
1323                 } else {
1324                         if (bflags & BFLAGS_NonPublic)
1325                                 match++;
1326                 }
1327                 if (!match)
1328                         continue;
1329                 match = 0;
1330                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1331                         if (bflags & BFLAGS_Static)
1332                                 match++;
1333                 } else {
1334                         if (bflags & BFLAGS_Instance)
1335                                 match++;
1336                 }
1337
1338                 if (!match)
1339                         continue;
1340                 match = 0;
1341                 member = (MonoObject*)mono_method_get_object (domain, method);
1342                         
1343                 l = g_slist_prepend (l, member);
1344         }
1345         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1346                 goto handle_parent;
1347         len = g_slist_length (l);
1348         if (!System_Reflection_MethodInfo)
1349                 System_Reflection_MethodInfo = mono_class_from_name (
1350                         mono_defaults.corlib, "System.Reflection", "MethodInfo");
1351         res = mono_array_new (domain, System_Reflection_MethodInfo, len);
1352         i = 0;
1353         tmp = l;
1354         for (; tmp; tmp = tmp->next, ++i)
1355                 mono_array_set (res, gpointer, i, tmp->data);
1356         g_slist_free (l);
1357
1358         return res;
1359 }
1360
1361 static MonoArray*
1362 ves_icall_Type_GetConstructors (MonoReflectionType *type, guint32 bflags)
1363 {
1364         MonoDomain *domain; 
1365         GSList *l = NULL, *tmp;
1366         static MonoClass *System_Reflection_ConstructorInfo;
1367         MonoClass *startklass, *klass;
1368         MonoArray *res;
1369         MonoMethod *method;
1370         MonoObject *member;
1371         int i, len, match;
1372
1373         domain = ((MonoObject *)type)->vtable->domain;
1374         klass = startklass = mono_class_from_mono_type (type->type);
1375
1376 handle_parent:  
1377         for (i = 0; i < klass->method.count; ++i) {
1378                 match = 0;
1379                 method = klass->methods [i];
1380                 if (strcmp (method->name, ".ctor") && strcmp (method->name, ".cctor"))
1381                         continue;
1382                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1383                         if (bflags & BFLAGS_Public)
1384                                 match++;
1385                 } else {
1386                         if (bflags & BFLAGS_NonPublic)
1387                                 match++;
1388                 }
1389                 if (!match)
1390                         continue;
1391                 match = 0;
1392                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1393                         if (bflags & BFLAGS_Static)
1394                                 match++;
1395                 } else {
1396                         if (bflags & BFLAGS_Instance)
1397                                 match++;
1398                 }
1399
1400                 if (!match)
1401                         continue;
1402                 member = (MonoObject*)mono_method_get_object (domain, method);
1403                         
1404                 l = g_slist_prepend (l, member);
1405         }
1406         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1407                 goto handle_parent;
1408         len = g_slist_length (l);
1409         if (!System_Reflection_ConstructorInfo)
1410                 System_Reflection_ConstructorInfo = mono_class_from_name (
1411                         mono_defaults.corlib, "System.Reflection", "ConstructorInfo");
1412         res = mono_array_new (domain, System_Reflection_ConstructorInfo, len);
1413         i = 0;
1414         tmp = l;
1415         for (; tmp; tmp = tmp->next, ++i)
1416                 mono_array_set (res, gpointer, i, tmp->data);
1417         g_slist_free (l);
1418         return res;
1419 }
1420
1421 static MonoArray*
1422 ves_icall_Type_GetProperties (MonoReflectionType *type, guint32 bflags)
1423 {
1424         MonoDomain *domain; 
1425         GSList *l = NULL, *tmp;
1426         static MonoClass *System_Reflection_PropertyInfo;
1427         MonoClass *startklass, *klass;
1428         MonoArray *res;
1429         MonoMethod *method;
1430         MonoProperty *prop;
1431         int i, len, match;
1432
1433         domain = ((MonoObject *)type)->vtable->domain;
1434         klass = startklass = mono_class_from_mono_type (type->type);
1435
1436 handle_parent:
1437         for (i = 0; i < klass->property.count; ++i) {
1438                 prop = &klass->properties [i];
1439                 match = 0;
1440                 method = prop->get;
1441                 if (!method)
1442                         method = prop->set;
1443                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1444                         if (bflags & BFLAGS_Public)
1445                                 match++;
1446                 } else {
1447                         if (bflags & BFLAGS_NonPublic)
1448                                 match++;
1449                 }
1450                 if (!match)
1451                         continue;
1452                 match = 0;
1453                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1454                         if (bflags & BFLAGS_Static)
1455                                 match++;
1456                 } else {
1457                         if (bflags & BFLAGS_Instance)
1458                                 match++;
1459                 }
1460
1461                 if (!match)
1462                         continue;
1463                 match = 0;
1464                 l = g_slist_prepend (l, mono_property_get_object (domain, klass, prop));
1465         }
1466         if (!l && (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent)))
1467                 goto handle_parent;
1468         len = g_slist_length (l);
1469         if (!System_Reflection_PropertyInfo)
1470                 System_Reflection_PropertyInfo = mono_class_from_name (
1471                         mono_defaults.corlib, "System.Reflection", "PropertyInfo");
1472         res = mono_array_new (domain, System_Reflection_PropertyInfo, len);
1473         i = 0;
1474         tmp = l;
1475         for (; tmp; tmp = tmp->next, ++i)
1476                 mono_array_set (res, gpointer, i, tmp->data);
1477         g_slist_free (l);
1478         return res;
1479 }
1480
1481 static MonoArray*
1482 ves_icall_Type_GetEvents (MonoReflectionType *type, guint32 bflags)
1483 {
1484         MonoDomain *domain; 
1485         GSList *l = NULL, *tmp;
1486         static MonoClass *System_Reflection_EventInfo;
1487         MonoClass *startklass, *klass;
1488         MonoArray *res;
1489         MonoMethod *method;
1490         MonoEvent *event;
1491         int i, len, match;
1492
1493         domain = ((MonoObject *)type)->vtable->domain;
1494         klass = startklass = mono_class_from_mono_type (type->type);
1495
1496 handle_parent:  
1497         for (i = 0; i < klass->event.count; ++i) {
1498                 event = &klass->events [i];
1499                 match = 0;
1500                 method = event->add;
1501                 if (!method)
1502                         method = event->remove;
1503                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1504                         if (bflags & BFLAGS_Public)
1505                                 match++;
1506                 } else {
1507                         if (bflags & BFLAGS_NonPublic)
1508                                 match++;
1509                 }
1510                 if (!match)
1511                         continue;
1512                 match = 0;
1513                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1514                         if (bflags & BFLAGS_Static)
1515                                 match++;
1516                 } else {
1517                         if (bflags & BFLAGS_Instance)
1518                                 match++;
1519                 }
1520
1521                 if (!match)
1522                         continue;
1523                 match = 0;
1524                 l = g_slist_prepend (l, mono_event_get_object (domain, klass, event));
1525         }
1526         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1527                 goto handle_parent;
1528         len = g_slist_length (l);
1529         if (!System_Reflection_EventInfo)
1530                 System_Reflection_EventInfo = mono_class_from_name (
1531                         mono_defaults.corlib, "System.Reflection", "EventInfo");
1532         res = mono_array_new (domain, System_Reflection_EventInfo, len);
1533         i = 0;
1534         tmp = l;
1535         for (; tmp; tmp = tmp->next, ++i)
1536                 mono_array_set (res, gpointer, i, tmp->data);
1537         g_slist_free (l);
1538         return res;
1539 }
1540
1541 static MonoArray*
1542 ves_icall_Type_GetNestedTypes (MonoReflectionType *type, guint32 bflags)
1543 {
1544         MonoDomain *domain; 
1545         GSList *l = NULL, *tmp;
1546         GList *tmpn;
1547         MonoClass *startklass, *klass;
1548         MonoArray *res;
1549         MonoObject *member;
1550         int i, len, match;
1551         MonoClass *nested;
1552
1553         domain = ((MonoObject *)type)->vtable->domain;
1554         klass = startklass = mono_class_from_mono_type (type->type);
1555
1556         for (tmpn = klass->nested_classes; tmpn; tmpn = tmpn->next) {
1557                 match = 0;
1558                 nested = tmpn->data;
1559                 if ((nested->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_NESTED_PUBLIC) {
1560                         if (bflags & BFLAGS_Public)
1561                                 match++;
1562                 } else {
1563                         if (bflags & BFLAGS_NonPublic)
1564                                 match++;
1565                 }
1566                 if (!match)
1567                         continue;
1568                 member = (MonoObject*)mono_type_get_object (domain, &nested->byval_arg);
1569                 l = g_slist_prepend (l, member);
1570         }
1571         len = g_slist_length (l);
1572         res = mono_array_new (domain, mono_defaults.monotype_class, len);
1573         i = 0;
1574         tmp = g_slist_reverse (l);
1575         for (; tmp; tmp = tmp->next, ++i)
1576                 mono_array_set (res, gpointer, i, tmp->data);
1577         g_slist_free (l);
1578         return res;
1579 }
1580
1581 static gpointer
1582 ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr (gpointer ptr)
1583 {
1584         return (gpointer)(*(int *)ptr);
1585 }
1586
1587 static MonoString*
1588 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAuto (gpointer ptr)
1589 {
1590         MonoDomain *domain = mono_domain_get (); 
1591
1592         return mono_string_new (domain, (char *)ptr);
1593 }
1594
1595 static guint32 ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error(void)
1596 {
1597         return(GetLastError());
1598 }
1599
1600 static MonoReflectionType*
1601 ves_icall_System_Reflection_Assembly_GetType (MonoReflectionAssembly *assembly, MonoString *name, MonoBoolean throwOnError, MonoBoolean ignoreCase)
1602 {
1603         MonoDomain *domain = mono_domain_get (); 
1604         gchar *str;
1605         MonoType *type;
1606         MonoTypeNameParse info;
1607
1608         str = mono_string_to_utf8 (name);
1609         /*g_print ("requested type %s in %s\n", str, assembly->assembly->name);*/
1610         if (!mono_reflection_parse_type (str, &info)) {
1611                 g_free (str);
1612                 g_list_free (info.modifiers);
1613                 if (throwOnError) /* uhm: this is a parse error, though... */
1614                         mono_raise_exception (mono_get_exception_type_load ());
1615                 /*g_print ("failed parse\n");*/
1616                 return NULL;
1617         }
1618
1619         type = mono_reflection_get_type (assembly->assembly->image, &info, ignoreCase);
1620         g_free (str);
1621         g_list_free (info.modifiers);
1622         if (!type) {
1623                 if (throwOnError)
1624                         mono_raise_exception (mono_get_exception_type_load ());
1625                 /* g_print ("failed find\n"); */
1626                 return NULL;
1627         }
1628         /* g_print ("got it\n"); */
1629         return mono_type_get_object (domain, type);
1630
1631 }
1632
1633 static MonoString *
1634 ves_icall_System_Reflection_Assembly_get_code_base (MonoReflectionAssembly *assembly)
1635 {
1636         MonoDomain *domain = mono_domain_get (); 
1637         MonoString *res;
1638         char *name = g_strconcat (
1639                 "file://", assembly->assembly->image->name, NULL);
1640         
1641         res = mono_string_new (domain, name);
1642         g_free (name);
1643         return res;
1644 }
1645
1646 static MonoString *
1647 ves_icall_System_MonoType_getFullName (MonoReflectionType *object)
1648 {
1649         MonoDomain *domain = mono_domain_get (); 
1650         MonoString *res;
1651         gchar *name;
1652
1653         name = mono_type_get_name (object->type);
1654         res = mono_string_new (domain, name);
1655         g_free (name);
1656
1657         return res;
1658 }
1659
1660 static void
1661 ves_icall_System_Reflection_Assembly_FillName (MonoReflectionAssembly *assembly, MonoReflectionAssemblyName *aname)
1662 {
1663         MonoAssemblyName *name = &assembly->assembly->aname;
1664
1665         if (strcmp (name->name, "corlib") == 0)
1666                 aname->name = mono_string_new (mono_object_domain (assembly), "mscorlib");
1667         else
1668                 aname->name = mono_string_new (mono_object_domain (assembly), name->name);
1669         aname->major = name->major;
1670 }
1671
1672 static MonoArray*
1673 ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly, MonoBoolean exportedOnly)
1674 {
1675         MonoDomain *domain = mono_domain_get (); 
1676         MonoArray *res;
1677         MonoClass *klass;
1678         MonoTableInfo *tdef = &assembly->assembly->image->tables [MONO_TABLE_TYPEDEF];
1679         int i, count;
1680         guint32 attrs, visibility;
1681
1682         if (exportedOnly) {
1683                 count = 0;
1684                 for (i = 0; i < tdef->rows; ++i) {
1685                         attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
1686                         visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
1687                         if (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)
1688                                 count++;
1689                 }
1690         } else {
1691                 count = tdef->rows;
1692         }
1693         res = mono_array_new (domain, mono_defaults.monotype_class, count);
1694         count = 0;
1695         for (i = 0; i < tdef->rows; ++i) {
1696                 attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
1697                 visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
1698                 if (!exportedOnly || (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)) {
1699                         klass = mono_class_get (assembly->assembly->image, (i + 1) | MONO_TOKEN_TYPE_DEF);
1700                         mono_array_set (res, gpointer, count, mono_type_get_object (domain, &klass->byval_arg));
1701                         count++;
1702                 }
1703         }
1704         
1705         return res;
1706 }
1707
1708 static MonoReflectionType*
1709 ves_icall_ModuleBuilder_create_modified_type (MonoReflectionTypeBuilder *tb, MonoString *smodifiers)
1710 {
1711         MonoClass *klass;
1712         int isbyref = 0, rank;
1713         char *str = mono_string_to_utf8 (smodifiers);
1714         char *p;
1715
1716         klass = mono_class_from_mono_type (tb->type.type);
1717         p = str;
1718         /* logic taken from mono_reflection_parse_type(): keep in sync */
1719         while (*p) {
1720                 switch (*p) {
1721                 case '&':
1722                         if (isbyref) { /* only one level allowed by the spec */
1723                                 g_free (str);
1724                                 return NULL;
1725                         }
1726                         isbyref = 1;
1727                         p++;
1728                         g_free (str);
1729                         return mono_type_get_object (mono_domain_get (), &klass->this_arg);
1730                         break;
1731                 case '*':
1732                         klass = mono_ptr_class_get (&klass->byval_arg);
1733                         mono_class_init (klass);
1734                         p++;
1735                         break;
1736                 case '[':
1737                         rank = 1;
1738                         p++;
1739                         while (*p) {
1740                                 if (*p == ']')
1741                                         break;
1742                                 if (*p == ',')
1743                                         rank++;
1744                                 else if (*p != '*') { /* '*' means unknown lower bound */
1745                                         g_free (str);
1746                                         return NULL;
1747                                 }
1748                                 ++p;
1749                         }
1750                         if (*p != ']') {
1751                                 g_free (str);
1752                                 return NULL;
1753                         }
1754                         p++;
1755                         klass = mono_array_class_get (&klass->byval_arg, rank);
1756                         mono_class_init (klass);
1757                         break;
1758                 default:
1759                         break;
1760                 }
1761         }
1762         g_free (str);
1763         return mono_type_get_object (mono_domain_get (), &klass->byval_arg);
1764 }
1765
1766 /*
1767  * Magic number to convert a time which is relative to
1768  * Jan 1, 1970 into a value which is relative to Jan 1, 0001.
1769  */
1770 #define EPOCH_ADJUST    ((gint64)62135596800L)
1771
1772 static gint64
1773 ves_icall_System_DateTime_GetNow (void)
1774 {
1775 #ifdef PLATFORM_WIN32
1776         SYSTEMTIME st;
1777         FILETIME ft;
1778         
1779         GetLocalTime (&st);
1780         SystemTimeToFileTime (&st, &ft);
1781         return (gint64)504911232000000000L + (((gint64)ft.dwHighDateTime)<<32) | ft.dwLowDateTime;
1782 #else
1783         /* FIXME: put this in io-layer and call it GetLocalTime */
1784         struct timeval tv;
1785         gint64 res;
1786
1787         if (gettimeofday (&tv, NULL) == 0) {
1788                 res = (((gint64)tv.tv_sec + EPOCH_ADJUST)* 1000000 + tv.tv_usec)*10;
1789                 return res;
1790         }
1791         /* fixme: raise exception */
1792         return 0;
1793 #endif
1794 }
1795
1796 /*
1797  * This is heavily based on zdump.c from glibc 2.2.
1798  *
1799  *  * data[0]:  start of daylight saving time (in DateTime ticks).
1800  *  * data[1]:  end of daylight saving time (in DateTime ticks).
1801  *  * data[2]:  utcoffset (in TimeSpan ticks).
1802  *  * data[3]:  additional offset when daylight saving (in TimeSpan ticks).
1803  *  * name[0]:  name of this timezone when not daylight saving.
1804  *  * name[1]:  name of this timezone when daylight saving.
1805  *
1806  *  FIXME: This only works with "standard" Unix dates (years between 1900 and 2100) while
1807  *         the class library allows years between 1 and 9999.
1808  *
1809  *  Returns true on success and zero on failure.
1810  */
1811 static guint32
1812 ves_icall_System_CurrentTimeZone_GetTimeZoneData (guint32 year, MonoArray **data, MonoArray **names)
1813 {
1814 #ifndef PLATFORM_WIN32
1815         MonoDomain *domain = mono_domain_get ();
1816         struct tm start, tt;
1817         time_t t;
1818
1819         long int gmtoff;
1820         int is_daylight = 0, day;
1821
1822         memset (&start, 0, sizeof (start));
1823
1824         start.tm_mday = 1;
1825         start.tm_year = year-1900;
1826
1827         t = mktime (&start);
1828 #if defined (HAVE_TIMEZONE)
1829 #define gmt_offset(x) (-1 * (((timezone / 60 / 60) - daylight) * 100))
1830 #elif defined (HAVE_TM_GMTOFF)
1831 #define gmt_offset(x) x.tm_gmtoff
1832 #else
1833 #error Neither HAVE_TIMEZONE nor HAVE_TM_GMTOFF defined. Rerun autoheader, autoconf, etc.
1834 #endif
1835         
1836         gmtoff = gmt_offset (start);
1837         
1838         MONO_CHECK_ARG_NULL (data);
1839         MONO_CHECK_ARG_NULL (names);
1840
1841         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
1842         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
1843
1844         /* For each day of the year, calculate the tm_gmtoff. */
1845         for (day = 0; day < 365; day++) {
1846
1847                 t += 3600*24;
1848                 tt = *localtime (&t);
1849
1850                 /* Daylight saving starts or ends here. */
1851                 if (gmt_offset (tt) != gmtoff) {
1852                         char tzone[10];
1853                         struct tm tt1;
1854                         time_t t1;
1855
1856                         /* Try to find the exact hour when daylight saving starts/ends. */
1857                         t1 = t;
1858                         do {
1859                                 t1 -= 3600;
1860                                 tt1 = *localtime (&t1);
1861                         } while (gmt_offset (tt1) != gmtoff);
1862
1863                         /* Try to find the exact minute when daylight saving starts/ends. */
1864                         do {
1865                                 t1 += 60;
1866                                 tt1 = *localtime (&t1);
1867                         } while (gmt_offset (tt1) == gmtoff);
1868                         
1869                         strftime (tzone, 10, "%Z", &tt);
1870                         
1871                         /* Write data, if we're already in daylight saving, we're done. */
1872                         if (is_daylight) {
1873                                 mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tzone));
1874                                 mono_array_set ((*data), gint64, 1, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
1875                                 return 1;
1876                         } else {
1877                                 mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tzone));
1878                                 mono_array_set ((*data), gint64, 0, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
1879                                 is_daylight = 1;
1880                         }
1881
1882                         /* This is only set once when we enter daylight saving. */
1883                         mono_array_set ((*data), gint64, 2, (gint64)gmtoff * 10000000L);
1884                         mono_array_set ((*data), gint64, 3, (gint64)(gmt_offset (tt) - gmtoff) * 10000000L);
1885
1886                         gmtoff = gmt_offset (tt);
1887                 }
1888
1889                 gmtoff = gmt_offset (tt);
1890         }
1891         return 1;
1892 #else
1893         MonoDomain *domain = mono_domain_get ();
1894         TIME_ZONE_INFORMATION tz_info;
1895         FILETIME ft;
1896         int i;
1897
1898         GetTimeZoneInformation (&tz_info);
1899
1900         MONO_CHECK_ARG_NULL (data);
1901         MONO_CHECK_ARG_NULL (names);
1902
1903         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
1904         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
1905
1906         for (i = 0; i < 32; ++i)
1907                 if (!tz_info.DaylightName [i])
1908                         break;
1909         mono_array_set ((*names), gpointer, 1, mono_string_new_utf16 (domain, tz_info.DaylightName, i));
1910         for (i = 0; i < 32; ++i)
1911                 if (!tz_info.StandardName [i])
1912                         break;
1913         mono_array_set ((*names), gpointer, 0, mono_string_new_utf16 (domain, tz_info.StandardName, i));
1914
1915         SystemTimeToFileTime (&tz_info.StandardDate, &ft);
1916         mono_array_set ((*data), gint64, 1, ((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime);
1917         SystemTimeToFileTime (&tz_info.DaylightDate, &ft);
1918         mono_array_set ((*data), gint64, 0, ((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime);
1919         mono_array_set ((*data), gint64, 3, tz_info.Bias + tz_info.StandardBias);
1920         mono_array_set ((*data), gint64, 2, tz_info.Bias + tz_info.DaylightBias);
1921
1922         return 1;
1923 #endif
1924 }
1925
1926 static gpointer
1927 ves_icall_System_Object_obj_address (MonoObject *this) {
1928         return this;
1929 }
1930
1931 /* System.Buffer */
1932
1933 static gint32 
1934 ves_icall_System_Buffer_ByteLengthInternal (MonoArray *array) {
1935         MonoClass *klass;
1936         MonoTypeEnum etype;
1937         int length, esize;
1938         int i;
1939
1940         klass = array->obj.vtable->klass;
1941         etype = klass->element_class->byval_arg.type;
1942         if (etype < MONO_TYPE_BOOLEAN || etype > MONO_TYPE_R8)
1943                 return -1;
1944
1945         if (array->bounds == NULL)
1946                 length = array->max_length;
1947         else {
1948                 length = 0;
1949                 for (i = 0; i < klass->rank; ++ i)
1950                         length += array->bounds [i].length;
1951         }
1952
1953         esize = mono_array_element_size (klass);
1954         return length * esize;
1955 }
1956
1957 static gint8 
1958 ves_icall_System_Buffer_GetByteInternal (MonoArray *array, gint32 idx) {
1959         return mono_array_get (array, gint8, idx);
1960 }
1961
1962 static void 
1963 ves_icall_System_Buffer_SetByteInternal (MonoArray *array, gint32 idx, gint8 value) {
1964         mono_array_set (array, gint8, idx, value);
1965 }
1966
1967 static void 
1968 ves_icall_System_Buffer_BlockCopyInternal (MonoArray *src, gint32 src_offset, MonoArray *dest, gint32 dest_offset, gint32 count) {
1969         char *src_buf, *dest_buf;
1970
1971         src_buf = (gint8 *)src->vector + src_offset;
1972         dest_buf = (gint8 *)dest->vector + dest_offset;
1973
1974         memcpy (dest_buf, src_buf, count);
1975 }
1976
1977 static MonoObject *
1978 ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObject *this)
1979 {
1980         MonoDomain *domain = mono_domain_get (); 
1981         MonoObject *res;
1982         MonoRealProxy *rp = ((MonoRealProxy *)this);
1983         MonoType *type;
1984         MonoClass *klass;
1985
1986         res = mono_object_new (domain, mono_defaults.transparent_proxy_class);
1987         
1988         ((MonoTransparentProxy *)res)->rp = rp;
1989         type = ((MonoReflectionType *)rp->class_to_proxy)->type;
1990         klass = mono_class_from_mono_type (type);
1991
1992         ((MonoTransparentProxy *)res)->klass = klass;
1993
1994         res->vtable = mono_class_proxy_vtable (domain, klass);
1995
1996         return res;
1997 }
1998
1999 /* System.Environment */
2000
2001 static MonoString *
2002 ves_icall_System_Environment_get_MachineName (void)
2003 {
2004 #if defined (PLATFORM_WIN32)
2005         gunichar2 *buf;
2006         guint32 len;
2007         MonoString *result;
2008
2009         len = MAX_COMPUTERNAME_LENGTH + 1;
2010         buf = g_new (gunichar2, len);
2011
2012         result = NULL;
2013         if (GetComputerName (buf, &len))
2014                 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
2015
2016         g_free (buf);
2017         return result;
2018 #else
2019         gchar *buf;
2020         int len;
2021         MonoString *result;
2022
2023         len = 256;
2024         buf = g_new (gchar, len);
2025
2026         result = NULL;
2027         if (gethostname (buf, len) != 0)
2028                 result = mono_string_new (mono_domain_get (), buf);
2029         
2030         g_free (buf);
2031         return result;
2032 #endif
2033 }
2034
2035 static MonoString *
2036 ves_icall_System_Environment_get_NewLine (void)
2037 {
2038 #if defined (PLATFORM_WIN32)
2039         return mono_string_new (mono_domain_get (), "\r\n");
2040 #else
2041         return mono_string_new (mono_domain_get (), "\n");
2042 #endif
2043 }
2044
2045 static MonoString *
2046 ves_icall_System_Environment_GetEnvironmentVariable (MonoString *name)
2047 {
2048         const gchar *value;
2049         gchar *utf8_name;
2050
2051         if (name == NULL)
2052                 return NULL;
2053
2054         utf8_name = mono_string_to_utf8 (name); /* FIXME: this should be ascii */
2055         value = g_getenv (utf8_name);
2056         g_free (utf8_name);
2057
2058         if (value == 0)
2059                 return NULL;
2060         
2061         return mono_string_new (mono_domain_get (), value);
2062 }
2063
2064 /*
2065  * There is no standard way to get at environ.
2066  */
2067 extern char **environ;
2068
2069 static MonoArray *
2070 ves_icall_System_Environment_GetEnvironmentVariableNames (void)
2071 {
2072         MonoArray *names;
2073         MonoDomain *domain;
2074         MonoString *str;
2075         gchar **e, **parts;
2076         int n;
2077
2078         n = 0;
2079         for (e = environ; *e != 0; ++ e)
2080                 ++ n;
2081
2082         domain = mono_domain_get ();
2083         names = mono_array_new (domain, mono_defaults.string_class, n);
2084
2085         n = 0;
2086         for (e = environ; *e != 0; ++ e) {
2087                 parts = g_strsplit (*e, "=", 2);
2088                 if (*parts != 0) {
2089                         str = mono_string_new (domain, *parts);
2090                         mono_array_set (names, MonoString *, n, str);
2091                 }
2092
2093                 g_strfreev (parts);
2094
2095                 ++ n;
2096         }
2097
2098         return names;
2099 }
2100
2101 static void
2102 ves_icall_System_Environment_Exit (int result)
2103 {
2104         /* we may need to do some cleanup here... */
2105         exit (result);
2106 }
2107
2108 static void
2109 ves_icall_MonoMethodMessage_InitMessage (MonoMethodMessage *this, 
2110                                          MonoReflectionMethod *method,
2111                                          MonoArray *out_args)
2112 {
2113         MonoDomain *domain = mono_domain_get ();
2114         
2115         mono_message_init (domain, this, method, out_args);
2116 }
2117
2118 static MonoBoolean
2119 ves_icall_IsTransparentProxy (MonoObject *proxy)
2120 {
2121         if (!proxy)
2122                 return 0;
2123
2124         if (proxy->vtable->klass == mono_defaults.transparent_proxy_class)
2125                 return 1;
2126
2127         return 0;
2128 }
2129
2130
2131 /* icall map */
2132
2133 static gconstpointer icall_map [] = {
2134         /*
2135          * System.Array
2136          */
2137         "System.Array::GetValue",         ves_icall_System_Array_GetValue,
2138         "System.Array::SetValue",         ves_icall_System_Array_SetValue,
2139         "System.Array::GetValueImpl",     ves_icall_System_Array_GetValueImpl,
2140         "System.Array::SetValueImpl",     ves_icall_System_Array_SetValueImpl,
2141         "System.Array::GetRank",          ves_icall_System_Array_GetRank,
2142         "System.Array::GetLength",        ves_icall_System_Array_GetLength,
2143         "System.Array::GetLowerBound",    ves_icall_System_Array_GetLowerBound,
2144         "System.Array::CreateInstanceImpl",   ves_icall_System_Array_CreateInstanceImpl,
2145         "System.Array::FastCopy",         ves_icall_System_Array_FastCopy,
2146         "System.Array::Clone",            mono_array_clone,
2147
2148         /*
2149          * System.Object
2150          */
2151         "System.Object::MemberwiseClone", ves_icall_System_Object_MemberwiseClone,
2152         "System.Object::GetType", ves_icall_System_Object_GetType,
2153         "System.Object::GetHashCode", ves_icall_System_Object_GetHashCode,
2154         "System.Object::obj_address", ves_icall_System_Object_obj_address,
2155
2156         /*
2157          * System.ValueType
2158          */
2159         "System.ValueType::GetHashCode", ves_icall_System_ValueType_GetHashCode,
2160         "System.ValueType::Equals", ves_icall_System_ValueType_Equals,
2161
2162         /*
2163          * System.String
2164          */
2165         
2166         "System.String::.ctor(char*)", ves_icall_System_String_ctor_charp,
2167         "System.String::.ctor(char*,uint,uint)", ves_icall_System_String_ctor_charp_int_int,
2168         "System.String::.ctor(sbyte*)", ves_icall_System_String_ctor_sbytep,
2169         "System.String::.ctor(sbyte*,uint,uint)", ves_icall_System_String_ctor_sbytep_int_int,
2170         "System.String::.ctor(sbyte*,uint,uint,System.Text.Encoding)", ves_icall_System_String_ctor_encoding,
2171         "System.String::.ctor(char[])", ves_icall_System_String_ctor_chara,
2172         "System.String::.ctor(char[],uint,uint)", ves_icall_System_String_ctor_chara_int_int,
2173         "System.String::.ctor(char,uint)", ves_icall_System_String_ctor_char_int,
2174         "System.String::InternalEquals", ves_icall_System_String_InternalEquals,
2175         "System.String::InternalJoin", ves_icall_System_String_InternalJoin,
2176         "System.String::InternalInsert", ves_icall_System_String_InternalInsert,
2177         "System.String::InternalReplace(char,char)", ves_icall_System_String_InternalReplace_Char,
2178         "System.String::InternalReplace(string,string)", ves_icall_System_String_InternalReplace_Str,
2179         "System.String::InternalRemove", ves_icall_System_String_InternalRemove,
2180         "System.String::InternalCopyTo", ves_icall_System_String_InternalCopyTo,
2181         "System.String::InternalSplit", ves_icall_System_String_InternalSplit,
2182         "System.String::InternalTrim", ves_icall_System_String_InternalTrim,
2183         "System.String::InternalIndexOf(char,uint,uint)", ves_icall_System_String_InternalIndexOf_Char,
2184         "System.String::InternalIndexOf(string,uint,uint)", ves_icall_System_String_InternalIndexOf_Str,
2185         "System.String::InternalIndexOfAny", ves_icall_System_String_InternalIndexOfAny,
2186         "System.String::InternalLastIndexOf(char,uint,uint)", ves_icall_System_String_InternalLastIndexOf_Char,
2187         "System.String::InternalLastIndexOf(string,uint,uint)", ves_icall_System_String_InternalLastIndexOf_Str,
2188         "System.String::InternalLastIndexOfAny", ves_icall_System_String_InternalLastIndexOfAny,
2189         "System.String::InternalPad", ves_icall_System_String_InternalPad,
2190         "System.String::InternalToLower", ves_icall_System_String_InternalToLower,
2191         "System.String::InternalToUpper", ves_icall_System_String_InternalToUpper,
2192         "System.String::InternalAllocateStr", ves_icall_System_String_InternalAllocateStr,
2193         "System.String::InternalStrcpy(string,uint,string)", ves_icall_System_String_InternalStrcpy_Str,
2194         "System.String::InternalStrcpy(string,uint,string,uint,uint)", ves_icall_System_String_InternalStrcpy_StrN,
2195         "System.String::InternalIntern", ves_icall_System_String_InternalIntern,
2196         "System.String::InternalIsInterned", ves_icall_System_String_InternalIsInterned,
2197         "System.String::InternalCompare(string,uint,string,uint,uint,bool)", ves_icall_System_String_InternalCompareStr_N,
2198         "System.String::GetHashCode", ves_icall_System_String_GetHashCode,
2199         "System.String::get_Chars", ves_icall_System_String_get_Chars,
2200
2201         /*
2202          * System.AppDomain
2203          */
2204         "System.AppDomain::createDomain", ves_icall_System_AppDomain_createDomain,
2205         "System.AppDomain::getCurDomain", ves_icall_System_AppDomain_getCurDomain,
2206         "System.AppDomain::GetData", ves_icall_System_AppDomain_GetData,
2207         "System.AppDomain::SetData", ves_icall_System_AppDomain_SetData,
2208         "System.AppDomain::getSetup", ves_icall_System_AppDomain_getSetup,
2209         "System.AppDomain::getFriendlyName", ves_icall_System_AppDomain_getFriendlyName,
2210         "System.AppDomain::GetAssemblies", ves_icall_System_AppDomain_GetAssemblies,
2211         "System.AppDomain::LoadAssembly", ves_icall_System_AppDomain_LoadAssembly,
2212         "System.AppDomain::Unload", ves_icall_System_AppDomain_Unload,
2213         "System.AppDomain::ExecuteAssembly", ves_icall_System_AppDomain_ExecuteAssembly,
2214
2215         /*
2216          * System.AppDomainSetup
2217          */
2218         "System.AppDomainSetup::InitAppDomainSetup", ves_icall_System_AppDomainSetup_InitAppDomainSetup,
2219
2220         /*
2221          * System.Double
2222          */
2223         "System.Double::ToStringImpl", mono_double_ToStringImpl,
2224
2225         /*
2226          * System.Single
2227          */
2228         "System.Single::ToStringImpl", mono_float_ToStringImpl,
2229
2230         /*
2231          * System.Decimal
2232          */
2233         "System.Decimal::decimal2UInt64", mono_decimal2UInt64,
2234         "System.Decimal::decimal2Int64", mono_decimal2Int64,
2235         "System.Decimal::double2decimal", mono_double2decimal, /* FIXME: wrong signature. */
2236         "System.Decimal::decimalIncr", mono_decimalIncr,
2237         "System.Decimal::decimalSetExponent", mono_decimalSetExponent,
2238         "System.Decimal::decimal2double", mono_decimal2double,
2239         "System.Decimal::decimalFloorAndTrunc", mono_decimalFloorAndTrunc,
2240         "System.Decimal::decimalRound", mono_decimalRound,
2241         "System.Decimal::decimalMult", mono_decimalMult,
2242         "System.Decimal::decimalDiv", mono_decimalDiv,
2243         "System.Decimal::decimalIntDiv", mono_decimalIntDiv,
2244         "System.Decimal::decimalCompare", mono_decimalCompare,
2245         "System.Decimal::string2decimal", mono_string2decimal,
2246         "System.Decimal::decimal2string", mono_decimal2string,
2247
2248         /*
2249          * ModuleBuilder
2250          */
2251         "System.Reflection.Emit.ModuleBuilder::create_modified_type", ves_icall_ModuleBuilder_create_modified_type,
2252         
2253         /*
2254          * AssemblyBuilder
2255          */
2256         "System.Reflection.Emit.AssemblyBuilder::getDataChunk", ves_icall_AssemblyBuilder_getDataChunk,
2257         "System.Reflection.Emit.AssemblyBuilder::getPEHeader", ves_icall_AssemblyBuilder_getPEHeader,
2258         "System.Reflection.Emit.AssemblyBuilder::getUSIndex", mono_image_insert_string,
2259         "System.Reflection.Emit.AssemblyBuilder::getToken", ves_icall_AssemblyBuilder_getToken,
2260         "System.Reflection.Emit.AssemblyBuilder::basic_init", mono_image_basic_init,
2261
2262         /*
2263          * Reflection stuff.
2264          */
2265         "System.Reflection.MonoMethodInfo::get_method_info", ves_icall_get_method_info,
2266         "System.Reflection.MonoMethodInfo::get_parameter_info", ves_icall_get_parameter_info,
2267         "System.Reflection.MonoFieldInfo::get_field_info", ves_icall_get_field_info,
2268         "System.Reflection.MonoPropertyInfo::get_property_info", ves_icall_get_property_info,
2269         "System.Reflection.MonoMethod::InternalInvoke", ves_icall_InternalInvoke,
2270         "System.Reflection.MonoCMethod::InternalInvoke", ves_icall_InternalInvoke,
2271         "System.MonoCustomAttrs::GetCustomAttributes", mono_reflection_get_custom_attrs,
2272         "System.Reflection.Emit.CustomAttributeBuilder::GetBlob", mono_reflection_get_custom_attrs_blob,
2273         "System.Reflection.MonoField::GetValue", ves_icall_MonoField_GetValue,
2274         "System.Reflection.Emit.SignatureHelper::get_signature_local", mono_reflection_sighelper_get_signature_local,
2275         "System.Reflection.Emit.SignatureHelper::get_signature_field", mono_reflection_sighelper_get_signature_field,
2276
2277         
2278         /* System.Enum */
2279
2280         "System.MonoEnumInfo::get_enum_info", ves_icall_get_enum_info,
2281         "System.Enum::get_value", ves_icall_System_Enum_get_value,
2282         "System.Enum::ToObject", ves_icall_System_Enum_ToObject,
2283
2284         /*
2285          * TypeBuilder
2286          */
2287         "System.Reflection.Emit.TypeBuilder::setup_internal_class", mono_reflection_setup_internal_class,
2288
2289         
2290         /*
2291          * MethodBuilder
2292          */
2293         
2294         /*
2295          * System.Type
2296          */
2297         "System.Type::internal_from_name", ves_icall_type_from_name,
2298         "System.Type::internal_from_handle", ves_icall_type_from_handle,
2299         "System.Type::get_constructor", ves_icall_get_constructor,
2300         "System.Type::get_property", ves_icall_get_property,
2301         "System.Type::get_method", ves_icall_get_method,
2302         "System.MonoType::get_attributes", ves_icall_get_attributes,
2303         "System.Type::type_is_subtype_of", ves_icall_type_is_subtype_of,
2304         "System.Type::Equals", ves_icall_type_Equals,
2305
2306         /*
2307          * System.Runtime.CompilerServices.RuntimeHelpers
2308          */
2309         "System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray", ves_icall_InitializeArray,
2310         
2311         /*
2312          * System.Threading
2313          */
2314         "System.Threading.Thread::Thread_internal", ves_icall_System_Threading_Thread_Thread_internal,
2315         "System.Threading.Thread::Thread_free_internal", ves_icall_System_Threading_Thread_Thread_free_internal,
2316         "System.Threading.Thread::Start_internal", ves_icall_System_Threading_Thread_Start_internal,
2317         "System.Threading.Thread::Sleep_internal", ves_icall_System_Threading_Thread_Sleep_internal,
2318         "System.Threading.Thread::CurrentThread_internal", ves_icall_System_Threading_Thread_CurrentThread_internal,
2319         "System.Threading.Thread::CurrentThreadDomain_internal", ves_icall_System_Threading_Thread_CurrentThreadDomain_internal,
2320         "System.Threading.Thread::Join_internal", ves_icall_System_Threading_Thread_Join_internal,
2321         "System.Threading.Thread::SlotHash_lookup", ves_icall_System_Threading_Thread_SlotHash_lookup,
2322         "System.Threading.Thread::SlotHash_store", ves_icall_System_Threading_Thread_SlotHash_store,
2323         "System.Threading.Monitor::Monitor_exit", ves_icall_System_Threading_Monitor_Monitor_exit,
2324         "System.Threading.Monitor::Monitor_test_owner", ves_icall_System_Threading_Monitor_Monitor_test_owner,
2325         "System.Threading.Monitor::Monitor_test_synchronised", ves_icall_System_Threading_Monitor_Monitor_test_synchronised,
2326         "System.Threading.Monitor::Monitor_pulse", ves_icall_System_Threading_Monitor_Monitor_pulse,
2327         "System.Threading.Monitor::Monitor_pulse_all", ves_icall_System_Threading_Monitor_Monitor_pulse_all,
2328         "System.Threading.Monitor::Monitor_try_enter", ves_icall_System_Threading_Monitor_Monitor_try_enter,
2329         "System.Threading.Monitor::Monitor_wait", ves_icall_System_Threading_Monitor_Monitor_wait,
2330         "System.Threading.Mutex::CreateMutex_internal", ves_icall_System_Threading_Mutex_CreateMutex_internal,
2331         "System.Threading.Mutex::ReleaseMutex_internal", ves_icall_System_Threading_Mutex_ReleaseMutex_internal,
2332         "System.Threading.NativeEventCalls::CreateEvent_internal", ves_icall_System_Threading_Events_CreateEvent_internal,
2333         "System.Threading.NativeEventCalls::SetEvent_internal",    ves_icall_System_Threading_Events_SetEvent_internal,
2334         "System.Threading.NativeEventCalls::ResetEvent_internal",  ves_icall_System_Threading_Events_ResetEvent_internal,
2335
2336         /*
2337          * System.Threading.WaitHandle
2338          */
2339         "System.Threading.WaitHandle::WaitAll_internal", ves_icall_System_Threading_WaitHandle_WaitAll_internal,
2340         "System.Threading.WaitHandle::WaitAny_internal", ves_icall_System_Threading_WaitHandle_WaitAny_internal,
2341         "System.Threading.WaitHandle::WaitOne_internal", ves_icall_System_Threading_WaitHandle_WaitOne_internal,
2342
2343         "System.Runtime.InteropServices.Marshal::ReadIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr,
2344         "System.Runtime.InteropServices.Marshal::PtrToStringAuto", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAuto,
2345         "System.Runtime.InteropServices.Marshal::GetLastWin32Error", ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error,
2346
2347         "System.Reflection.Assembly::LoadFrom", ves_icall_System_Reflection_Assembly_LoadFrom,
2348         "System.Reflection.Assembly::GetType", ves_icall_System_Reflection_Assembly_GetType,
2349         "System.Reflection.Assembly::GetTypes", ves_icall_System_Reflection_Assembly_GetTypes,
2350         "System.Reflection.Assembly::FillName", ves_icall_System_Reflection_Assembly_FillName,
2351         "System.Reflection.Assembly::get_code_base", ves_icall_System_Reflection_Assembly_get_code_base,
2352
2353         /*
2354          * System.MonoType.
2355          */
2356         "System.MonoType::getFullName", ves_icall_System_MonoType_getFullName,
2357         "System.MonoType::type_from_obj", mono_type_type_from_obj,
2358         "System.MonoType::GetElementType", ves_icall_MonoType_GetElementType,
2359         "System.MonoType::get_type_info", ves_icall_get_type_info,
2360         "System.MonoType::GetFields", ves_icall_Type_GetFields,
2361         "System.MonoType::GetMethods", ves_icall_Type_GetMethods,
2362         "System.MonoType::GetConstructors", ves_icall_Type_GetConstructors,
2363         "System.MonoType::GetProperties", ves_icall_Type_GetProperties,
2364         "System.MonoType::GetEvents", ves_icall_Type_GetEvents,
2365         "System.MonoType::GetInterfaces", ves_icall_Type_GetInterfaces,
2366         "System.MonoType::GetNestedTypes", ves_icall_Type_GetNestedTypes,
2367
2368         /*
2369          * System.Net.Sockets I/O Services
2370          */
2371         "System.Net.Sockets.Socket::Socket_internal", ves_icall_System_Net_Sockets_Socket_Socket_internal,
2372         "System.Net.Sockets.Socket::Close_internal", ves_icall_System_Net_Sockets_Socket_Close_internal,
2373         "System.Net.Sockets.SocketException::WSAGetLastError_internal", ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal,
2374         "System.Net.Sockets.Socket::Available_internal", ves_icall_System_Net_Sockets_Socket_Available_internal,
2375         "System.Net.Sockets.Socket::Blocking_internal", ves_icall_System_Net_Sockets_Socket_Blocking_internal,
2376         "System.Net.Sockets.Socket::Accept_internal", ves_icall_System_Net_Sockets_Socket_Accept_internal,
2377         "System.Net.Sockets.Socket::Listen_internal", ves_icall_System_Net_Sockets_Socket_Listen_internal,
2378         "System.Net.Sockets.Socket::LocalEndPoint_internal", ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal,
2379         "System.Net.Sockets.Socket::RemoteEndPoint_internal", ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal,
2380         "System.Net.Sockets.Socket::Bind_internal", ves_icall_System_Net_Sockets_Socket_Bind_internal,
2381         "System.Net.Sockets.Socket::Connect_internal", ves_icall_System_Net_Sockets_Socket_Connect_internal,
2382         "System.Net.Sockets.Socket::Receive_internal", ves_icall_System_Net_Sockets_Socket_Receive_internal,
2383         "System.Net.Sockets.Socket::RecvFrom_internal", ves_icall_System_Net_Sockets_Socket_RecvFrom_internal,
2384         "System.Net.Sockets.Socket::Send_internal", ves_icall_System_Net_Sockets_Socket_Send_internal,
2385         "System.Net.Sockets.Socket::SendTo_internal", ves_icall_System_Net_Sockets_Socket_SendTo_internal,
2386         "System.Net.Sockets.Socket::Select_internal", ves_icall_System_Net_Sockets_Socket_Select_internal,
2387         "System.Net.Sockets.Socket::Shutdown_internal", ves_icall_System_Net_Sockets_Socket_Shutdown_internal,
2388         "System.Net.Sockets.Socket::GetSocketOption_obj_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal,
2389         "System.Net.Sockets.Socket::GetSocketOption_arr_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal,
2390         "System.Net.Sockets.Socket::SetSocketOption_internal", ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal,
2391         "System.Net.Dns::GetHostByName_internal", ves_icall_System_Net_Dns_GetHostByName_internal,
2392         "System.Net.Dns::GetHostByAddr_internal", ves_icall_System_Net_Dns_GetHostByAddr_internal,
2393
2394         /*
2395          * System.Char
2396          */
2397         "System.Char::GetNumericValue", ves_icall_System_Char_GetNumericValue,
2398         "System.Char::GetUnicodeCategory", ves_icall_System_Char_GetUnicodeCategory,
2399         "System.Char::IsControl", ves_icall_System_Char_IsControl,
2400         "System.Char::IsDigit", ves_icall_System_Char_IsDigit,
2401         "System.Char::IsLetter", ves_icall_System_Char_IsLetter,
2402         "System.Char::IsLower", ves_icall_System_Char_IsLower,
2403         "System.Char::IsUpper", ves_icall_System_Char_IsUpper,
2404         "System.Char::IsNumber", ves_icall_System_Char_IsNumber,
2405         "System.Char::IsPunctuation", ves_icall_System_Char_IsPunctuation,
2406         "System.Char::IsSeparator", ves_icall_System_Char_IsSeparator,
2407         "System.Char::IsSurrogate", ves_icall_System_Char_IsSurrogate,
2408         "System.Char::IsSymbol", ves_icall_System_Char_IsSymbol,
2409         "System.Char::IsWhiteSpace", ves_icall_System_Char_IsWhiteSpace,
2410         "System.Char::ToLower", ves_icall_System_Char_ToLower,
2411         "System.Char::ToUpper", ves_icall_System_Char_ToUpper,
2412
2413         "System.Text.Encoding::IConvNewEncoder", ves_icall_iconv_new_encoder,
2414         "System.Text.Encoding::IConvNewDecoder", ves_icall_iconv_new_decoder,
2415         "System.Text.Encoding::IConvReset", ves_icall_iconv_reset,
2416         "System.Text.Encoding::IConvGetByteCount", ves_icall_iconv_get_byte_count,
2417         "System.Text.Encoding::IConvGetBytes", ves_icall_iconv_get_bytes,
2418         "System.Text.Encoding::IConvGetCharCount", ves_icall_iconv_get_char_count,
2419         "System.Text.Encoding::IConvGetChars", ves_icall_iconv_get_chars,
2420
2421         "System.DateTime::GetNow", ves_icall_System_DateTime_GetNow,
2422         "System.CurrentTimeZone::GetTimeZoneData", ves_icall_System_CurrentTimeZone_GetTimeZoneData,
2423
2424         /*
2425          * System.GC
2426          */
2427         "System.GC::InternalCollect", ves_icall_System_GC_InternalCollect,
2428         "System.GC::GetTotalMemory", ves_icall_System_GC_GetTotalMemory,
2429         "System.GC::KeepAlive", ves_icall_System_GC_KeepAlive,
2430         "System.GC::ReRegisterForFinalize", ves_icall_System_GC_ReRegisterForFinalize,
2431         "System.GC::SuppressFinalize", ves_icall_System_GC_SuppressFinalize,
2432         "System.GC::WaitForPendingFinalizers", ves_icall_System_GC_WaitForPendingFinalizers,
2433
2434         /*
2435          * System.Security.Cryptography calls
2436          */
2437
2438          "System.Security.Cryptography.RNGCryptoServiceProvider::GetBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_GetBytes,
2439          "System.Security.Cryptography.RNGCryptoServiceProvider::GetNonZeroBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_GetNonZeroBytes,
2440         
2441         /*
2442          * System.Buffer
2443          */
2444         "System.Buffer::ByteLengthInternal", ves_icall_System_Buffer_ByteLengthInternal,
2445         "System.Buffer::GetByteInternal", ves_icall_System_Buffer_GetByteInternal,
2446         "System.Buffer::SetByteInternal", ves_icall_System_Buffer_SetByteInternal,
2447         "System.Buffer::BlockCopyInternal", ves_icall_System_Buffer_BlockCopyInternal,
2448
2449         /*
2450          * System.IO.MonoIO
2451          */
2452         "System.IO.MonoIO::GetLastError", ves_icall_System_IO_MonoIO_GetLastError,
2453         "System.IO.MonoIO::CreateDirectory", ves_icall_System_IO_MonoIO_CreateDirectory,
2454         "System.IO.MonoIO::RemoveDirectory", ves_icall_System_IO_MonoIO_RemoveDirectory,
2455         "System.IO.MonoIO::FindFirstFile", ves_icall_System_IO_MonoIO_FindFirstFile,
2456         "System.IO.MonoIO::FindNextFile", ves_icall_System_IO_MonoIO_FindNextFile,
2457         "System.IO.MonoIO::FindClose", ves_icall_System_IO_MonoIO_FindClose,
2458         "System.IO.MonoIO::GetCurrentDirectory", ves_icall_System_IO_MonoIO_GetCurrentDirectory,
2459         "System.IO.MonoIO::SetCurrentDirectory", ves_icall_System_IO_MonoIO_SetCurrentDirectory,
2460         "System.IO.MonoIO::MoveFile", ves_icall_System_IO_MonoIO_MoveFile,
2461         "System.IO.MonoIO::CopyFile", ves_icall_System_IO_MonoIO_CopyFile,
2462         "System.IO.MonoIO::DeleteFile", ves_icall_System_IO_MonoIO_DeleteFile,
2463         "System.IO.MonoIO::GetFileAttributes", ves_icall_System_IO_MonoIO_GetFileAttributes,
2464         "System.IO.MonoIO::SetFileAttributes", ves_icall_System_IO_MonoIO_SetFileAttributes,
2465         "System.IO.MonoIO::GetFileStat", ves_icall_System_IO_MonoIO_GetFileStat,
2466         "System.IO.MonoIO::Open", ves_icall_System_IO_MonoIO_Open,
2467         "System.IO.MonoIO::Close", ves_icall_System_IO_MonoIO_Close,
2468         "System.IO.MonoIO::Read", ves_icall_System_IO_MonoIO_Read,
2469         "System.IO.MonoIO::Write", ves_icall_System_IO_MonoIO_Write,
2470         "System.IO.MonoIO::Seek", ves_icall_System_IO_MonoIO_Seek,
2471         "System.IO.MonoIO::GetLength", ves_icall_System_IO_MonoIO_GetLength,
2472         "System.IO.MonoIO::SetLength", ves_icall_System_IO_MonoIO_SetLength,
2473         "System.IO.MonoIO::SetFileTime", ves_icall_System_IO_MonoIO_SetFileTime,
2474         "System.IO.MonoIO::Flush", ves_icall_System_IO_MonoIO_Flush,
2475         "System.IO.MonoIO::get_ConsoleOutput", ves_icall_System_IO_MonoIO_get_ConsoleOutput,
2476         "System.IO.MonoIO::get_ConsoleInput", ves_icall_System_IO_MonoIO_get_ConsoleInput,
2477         "System.IO.MonoIO::get_ConsoleError", ves_icall_System_IO_MonoIO_get_ConsoleError,
2478         "System.IO.MonoIO::get_VolumeSeparatorChar", ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar,
2479         "System.IO.MonoIO::get_DirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar,
2480         "System.IO.MonoIO::get_AltDirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar,
2481         "System.IO.MonoIO::get_PathSeparator", ves_icall_System_IO_MonoIO_get_PathSeparator,
2482         "System.IO.MonoIO::get_InvalidPathChars", ves_icall_System_IO_MonoIO_get_InvalidPathChars,
2483
2484         /*
2485          * System.Math
2486          */
2487         "System.Math::Sin", ves_icall_System_Math_Sin,
2488     "System.Math::Cos", ves_icall_System_Math_Cos,
2489     "System.Math::Tan", ves_icall_System_Math_Tan,
2490     "System.Math::Sinh", ves_icall_System_Math_Sinh,
2491     "System.Math::Cosh", ves_icall_System_Math_Cosh,
2492     "System.Math::Tanh", ves_icall_System_Math_Tanh,
2493     "System.Math::Acos", ves_icall_System_Math_Acos,
2494     "System.Math::Asin", ves_icall_System_Math_Asin,
2495     "System.Math::Atan", ves_icall_System_Math_Atan,
2496     "System.Math::Atan2", ves_icall_System_Math_Atan2,
2497     "System.Math::Exp", ves_icall_System_Math_Exp,
2498     "System.Math::Log", ves_icall_System_Math_Log,
2499     "System.Math::Log10", ves_icall_System_Math_Log10,
2500     "System.Math::Pow", ves_icall_System_Math_Pow,
2501     "System.Math::Sqrt", ves_icall_System_Math_Sqrt,
2502
2503         /*
2504          * System.Environment
2505          */
2506         "System.Environment::get_MachineName", ves_icall_System_Environment_get_MachineName,
2507         "System.Environment::get_NewLine", ves_icall_System_Environment_get_NewLine,
2508         "System.Environment::GetEnvironmentVariable", ves_icall_System_Environment_GetEnvironmentVariable,
2509         "System.Environment::GetEnvironmentVariableNames", ves_icall_System_Environment_GetEnvironmentVariableNames,
2510         "System.Environment::GetCommandLineArgs", mono_runtime_get_main_args,
2511         "System.Environment::Exit", ves_icall_System_Environment_Exit,
2512
2513         /*
2514          * Mono.CSharp.Debugger
2515          */
2516         "Mono.CSharp.Debugger.MonoSymbolWriter::get_local_type_from_sig", 
2517         ves_icall_Debugger_MonoSymbolWriter_get_local_type_from_sig,
2518         "Mono.CSharp.Debugger.MonoSymbolWriter::get_method", 
2519         ves_icall_Debugger_MonoSymbolWriter_method_from_token,
2520         "Mono.CSharp.Debugger.DwarfFileWriter::get_type_token", 
2521         ves_icall_Debugger_DwarfFileWriter_get_type_token,
2522
2523
2524         /*
2525          * System.Runtime.Remoting
2526          */     
2527         "System.Runtime.Remoting.RemotingServices::InternalExecute",
2528         ves_icall_InternalExecute,
2529         "System.Runtime.Remoting.RemotingServices::IsTransparentProxy",
2530         ves_icall_IsTransparentProxy,
2531
2532         /*
2533          * System.Runtime.Remoting.Messaging
2534          */     
2535         "System.Runtime.Remoting.Messaging.MonoMethodMessage::InitMessage",
2536         ves_icall_MonoMethodMessage_InitMessage,
2537         
2538         /*
2539          * System.Runtime.Remoting.Proxies
2540          */     
2541         "System.Runtime.Remoting.Proxies.RealProxy::GetTransparentProxy", 
2542         ves_icall_Remoting_RealProxy_GetTransparentProxy,
2543
2544         /*
2545          * System.Threading.Interlocked
2546          */
2547         "System.Threading.Interlocked::Increment(uint&)", ves_icall_System_Threading_Interlocked_Increment_Int,
2548         "System.Threading.Interlocked::Increment(long&)", ves_icall_System_Threading_Interlocked_Increment_Long,
2549         "System.Threading.Interlocked::Decrement(uint&)", ves_icall_System_Threading_Interlocked_Decrement_Int,
2550         "System.Threading.Interlocked::Decrement(long&)", ves_icall_System_Threading_Interlocked_Decrement_Long,
2551         "System.Threading.Interlocked::CompareExchange(uint&,uint,uint)", ves_icall_System_Threading_Interlocked_CompareExchange_Int,
2552         "System.Threading.Interlocked::CompareExchange(object&,object,object)", ves_icall_System_Threading_Interlocked_CompareExchange_Object,
2553         "System.Threading.Interlocked::CompareExchange(single&,single,single)", ves_icall_System_Threading_Interlocked_CompareExchange_Single,
2554         "System.Threading.Interlocked::Exchange(uint&,uint)", ves_icall_System_Threading_Interlocked_Exchange_Int,
2555         "System.Threading.Interlocked::Exchange(object&,object)", ves_icall_System_Threading_Interlocked_Exchange_Object,
2556         "System.Threading.Interlocked::Exchange(single&,single)", ves_icall_System_Threading_Interlocked_Exchange_Single,
2557
2558         /*
2559          * add other internal calls here
2560          */
2561         NULL, NULL
2562 };
2563
2564 void
2565 mono_init_icall (void)
2566 {
2567         const char *name;
2568         int i = 0;
2569
2570         while ((name = icall_map [i])) {
2571                 mono_add_internal_call (name, icall_map [i+1]);
2572                 i += 2;
2573         }
2574        
2575 }
2576
2577