2002-09-27 Dick Porter <dick@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/marshal.h>
35 #include <mono/metadata/gc.h>
36 #include <mono/metadata/rand.h>
37 #include <mono/metadata/sysmath.h>
38 #include <mono/metadata/string-icalls.h>
39 #include <mono/metadata/debug-mono-symfile.h>
40 #include <mono/metadata/process.h>
41 #include <mono/io-layer/io-layer.h>
42 #include <mono/utils/strtod.h>
43
44 #if defined (PLATFORM_WIN32)
45 #include <windows.h>
46 #endif
47 #include "decimal.h"
48
49 static MonoString *
50 mono_double_ToStringImpl (double value)
51 {
52         /* FIXME: Handle formats, etc. */
53         MonoString *s;
54         gchar *retVal;
55         retVal = g_strdup_printf ("%.15g", value);
56         s = mono_string_new (mono_domain_get (), retVal);
57         g_free (retVal);
58         return s;
59 }
60
61 /*
62  * We expect a pointer to a char, not a string
63  */
64 static double
65 mono_double_ParseImpl (char *ptr)
66 {
67         return bsd_strtod (ptr, NULL);
68 }
69
70 static MonoString *
71 mono_float_ToStringImpl (float value)
72 {
73         return mono_double_ToStringImpl (value);
74 }
75
76 static MonoObject *
77 ves_icall_System_Array_GetValueImpl (MonoObject *this, guint32 pos)
78 {
79         MonoClass *ac;
80         MonoArray *ao;
81         gint32 esize;
82         gpointer *ea;
83
84         ao = (MonoArray *)this;
85         ac = (MonoClass *)ao->obj.vtable->klass;
86
87         esize = mono_array_element_size (ac);
88         ea = (gpointer*)((char*)ao->vector + (pos * esize));
89
90         if (ac->element_class->valuetype)
91                 return mono_value_box (this->vtable->domain, ac->element_class, ea);
92         else
93                 return *ea;
94 }
95
96 static MonoObject *
97 ves_icall_System_Array_GetValue (MonoObject *this, MonoObject *idxs)
98 {
99         MonoClass *ac, *ic;
100         MonoArray *ao, *io;
101         gint32 i, pos, *ind;
102
103         MONO_CHECK_ARG_NULL (idxs);
104
105         io = (MonoArray *)idxs;
106         ic = (MonoClass *)io->obj.vtable->klass;
107         
108         ao = (MonoArray *)this;
109         ac = (MonoClass *)ao->obj.vtable->klass;
110
111         g_assert (ic->rank == 1);
112         if (io->bounds != NULL || io->max_length !=  ac->rank)
113                 mono_raise_exception (mono_get_exception_argument (NULL, NULL));
114
115         ind = (guint32 *)io->vector;
116
117         if (ao->bounds == NULL) {
118                 if (*ind < 0 || *ind >= ao->max_length)
119                         mono_raise_exception (mono_get_exception_index_out_of_range ());
120
121                 return ves_icall_System_Array_GetValueImpl (this, *ind);
122         }
123         
124         for (i = 0; i < ac->rank; i++)
125                 if ((ind [i] < ao->bounds [i].lower_bound) ||
126                     (ind [i] >= ao->bounds [i].length + ao->bounds [i].lower_bound))
127                         mono_raise_exception (mono_get_exception_index_out_of_range ());
128
129         pos = ind [0] - ao->bounds [0].lower_bound;
130         for (i = 1; i < ac->rank; i++)
131                 pos = pos*ao->bounds [i].length + ind [i] - 
132                         ao->bounds [i].lower_bound;
133
134         return ves_icall_System_Array_GetValueImpl (this, pos);
135 }
136
137 static void
138 ves_icall_System_Array_SetValueImpl (MonoArray *this, MonoObject *value, guint32 pos)
139 {
140         MonoClass *ac, *vc, *ec;
141         gint32 esize, vsize;
142         gpointer *ea, *va;
143
144         guint64 u64;
145         gint64 i64;
146         gdouble r64;
147
148         if (value)
149                 vc = value->vtable->klass;
150         else
151                 vc = NULL;
152
153         ac = this->obj.vtable->klass;
154         ec = ac->element_class;
155
156         esize = mono_array_element_size (ac);
157         ea = (gpointer*)((char*)this->vector + (pos * esize));
158         va = (gpointer*)((char*)value + sizeof (MonoObject));
159
160         if (!value) {
161                 memset (ea, 0,  esize);
162                 return;
163         }
164
165 #define NO_WIDENING_CONVERSION G_STMT_START{\
166         mono_raise_exception (mono_get_exception_argument ( \
167                 "value", "not a widening conversion")); \
168 }G_STMT_END
169
170 #define CHECK_WIDENING_CONVERSION(extra) G_STMT_START{\
171         if (esize < vsize + (extra)) \
172                 mono_raise_exception (mono_get_exception_argument ( \
173                         "value", "not a widening conversion")); \
174 }G_STMT_END
175
176 #define INVALID_CAST G_STMT_START{\
177         mono_raise_exception (mono_get_exception_invalid_cast ()); \
178 }G_STMT_END
179
180         /* Check element (destination) type. */
181         switch (ec->byval_arg.type) {
182         case MONO_TYPE_STRING:
183                 switch (vc->byval_arg.type) {
184                 case MONO_TYPE_STRING:
185                         break;
186                 default:
187                         INVALID_CAST;
188                 }
189                 break;
190         case MONO_TYPE_BOOLEAN:
191                 switch (vc->byval_arg.type) {
192                 case MONO_TYPE_BOOLEAN:
193                         break;
194                 case MONO_TYPE_CHAR:
195                 case MONO_TYPE_U1:
196                 case MONO_TYPE_U2:
197                 case MONO_TYPE_U4:
198                 case MONO_TYPE_U8:
199                 case MONO_TYPE_I1:
200                 case MONO_TYPE_I2:
201                 case MONO_TYPE_I4:
202                 case MONO_TYPE_I8:
203                 case MONO_TYPE_R4:
204                 case MONO_TYPE_R8:
205                         NO_WIDENING_CONVERSION;
206                 default:
207                         INVALID_CAST;
208                 }
209                 break;
210         }
211
212         if (!ec->valuetype) {
213                 *ea = (gpointer)value;
214                 return;
215         }
216
217         if (mono_object_isinst (value, ec)) {
218                 memcpy (ea, (char *)value + sizeof (MonoObject), esize);
219                 return;
220         }
221
222         if (!vc->valuetype)
223                 INVALID_CAST;
224
225         vsize = mono_class_instance_size (vc) - sizeof (MonoObject);
226
227 #if 0
228         g_message (G_STRLOC ": %d (%d) <= %d (%d)",
229                    ec->byval_arg.type, esize,
230                    vc->byval_arg.type, vsize);
231 #endif
232
233 #define ASSIGN_UNSIGNED(etype) G_STMT_START{\
234         switch (vc->byval_arg.type) { \
235         case MONO_TYPE_U1: \
236         case MONO_TYPE_U2: \
237         case MONO_TYPE_U4: \
238         case MONO_TYPE_U8: \
239         case MONO_TYPE_CHAR: \
240                 CHECK_WIDENING_CONVERSION(0); \
241                 *(etype *) ea = (etype) u64; \
242                 return; \
243         /* You can't assign a signed value to an unsigned array. */ \
244         case MONO_TYPE_I1: \
245         case MONO_TYPE_I2: \
246         case MONO_TYPE_I4: \
247         case MONO_TYPE_I8: \
248         /* You can't assign a floating point number to an integer array. */ \
249         case MONO_TYPE_R4: \
250         case MONO_TYPE_R8: \
251                 NO_WIDENING_CONVERSION; \
252         } \
253 }G_STMT_END
254
255 #define ASSIGN_SIGNED(etype) G_STMT_START{\
256         switch (vc->byval_arg.type) { \
257         case MONO_TYPE_I1: \
258         case MONO_TYPE_I2: \
259         case MONO_TYPE_I4: \
260         case MONO_TYPE_I8: \
261                 CHECK_WIDENING_CONVERSION(0); \
262                 *(etype *) ea = (etype) i64; \
263                 return; \
264         /* You can assign an unsigned value to a signed array if the array's */ \
265         /* element size is larger than the value size. */ \
266         case MONO_TYPE_U1: \
267         case MONO_TYPE_U2: \
268         case MONO_TYPE_U4: \
269         case MONO_TYPE_U8: \
270         case MONO_TYPE_CHAR: \
271                 CHECK_WIDENING_CONVERSION(1); \
272                 *(etype *) ea = (etype) u64; \
273                 return; \
274         /* You can't assign a floating point number to an integer array. */ \
275         case MONO_TYPE_R4: \
276         case MONO_TYPE_R8: \
277                 NO_WIDENING_CONVERSION; \
278         } \
279 }G_STMT_END
280
281 #define ASSIGN_REAL(etype) G_STMT_START{\
282         switch (vc->byval_arg.type) { \
283         case MONO_TYPE_R4: \
284         case MONO_TYPE_R8: \
285                 CHECK_WIDENING_CONVERSION(0); \
286                 *(etype *) ea = (etype) r64; \
287                 return; \
288         /* All integer values fit into a floating point array, so we don't */ \
289         /* need to CHECK_WIDENING_CONVERSION here. */ \
290         case MONO_TYPE_I1: \
291         case MONO_TYPE_I2: \
292         case MONO_TYPE_I4: \
293         case MONO_TYPE_I8: \
294                 *(etype *) ea = (etype) i64; \
295                 return; \
296         case MONO_TYPE_U1: \
297         case MONO_TYPE_U2: \
298         case MONO_TYPE_U4: \
299         case MONO_TYPE_U8: \
300         case MONO_TYPE_CHAR: \
301                 *(etype *) ea = (etype) u64; \
302                 return; \
303         } \
304 }G_STMT_END
305
306         switch (vc->byval_arg.type) {
307         case MONO_TYPE_U1:
308                 u64 = *(guint8 *) va;
309                 break;
310         case MONO_TYPE_U2:
311                 u64 = *(guint16 *) va;
312                 break;
313         case MONO_TYPE_U4:
314                 u64 = *(guint32 *) va;
315                 break;
316         case MONO_TYPE_U8:
317                 u64 = *(guint64 *) va;
318                 break;
319         case MONO_TYPE_I1:
320                 i64 = *(gint8 *) va;
321                 break;
322         case MONO_TYPE_I2:
323                 i64 = *(gint16 *) va;
324                 break;
325         case MONO_TYPE_I4:
326                 i64 = *(gint32 *) va;
327                 break;
328         case MONO_TYPE_I8:
329                 i64 = *(gint64 *) va;
330                 break;
331         case MONO_TYPE_R4:
332                 r64 = *(gfloat *) va;
333                 break;
334         case MONO_TYPE_R8:
335                 r64 = *(gdouble *) va;
336                 break;
337         case MONO_TYPE_CHAR:
338                 u64 = *(guint16 *) va;
339                 break;
340         case MONO_TYPE_BOOLEAN:
341                 /* Boolean is only compatible with itself. */
342                 switch (ec->byval_arg.type) {
343                 case MONO_TYPE_CHAR:
344                 case MONO_TYPE_U1:
345                 case MONO_TYPE_U2:
346                 case MONO_TYPE_U4:
347                 case MONO_TYPE_U8:
348                 case MONO_TYPE_I1:
349                 case MONO_TYPE_I2:
350                 case MONO_TYPE_I4:
351                 case MONO_TYPE_I8:
352                 case MONO_TYPE_R4:
353                 case MONO_TYPE_R8:
354                         NO_WIDENING_CONVERSION;
355                 default:
356                         INVALID_CAST;
357                 }
358                 break;
359         }
360
361         /* If we can't do a direct copy, let's try a widening conversion. */
362         switch (ec->byval_arg.type) {
363         case MONO_TYPE_CHAR:
364                 ASSIGN_UNSIGNED (guint16);
365         case MONO_TYPE_U1:
366                 ASSIGN_UNSIGNED (guint8);
367         case MONO_TYPE_U2:
368                 ASSIGN_UNSIGNED (guint16);
369         case MONO_TYPE_U4:
370                 ASSIGN_UNSIGNED (guint32);
371         case MONO_TYPE_U8:
372                 ASSIGN_UNSIGNED (guint64);
373         case MONO_TYPE_I1:
374                 ASSIGN_SIGNED (gint8);
375         case MONO_TYPE_I2:
376                 ASSIGN_SIGNED (gint16);
377         case MONO_TYPE_I4:
378                 ASSIGN_SIGNED (gint32);
379         case MONO_TYPE_I8:
380                 ASSIGN_SIGNED (gint64);
381         case MONO_TYPE_R4:
382                 ASSIGN_REAL (gfloat);
383         case MONO_TYPE_R8:
384                 ASSIGN_REAL (gdouble);
385         }
386
387         INVALID_CAST;
388         /* Not reached, INVALID_CAST does not return. Just to avoid a compiler warning ... */
389         return;
390
391 #undef INVALID_CAST
392 #undef NO_WIDENING_CONVERSION
393 #undef CHECK_WIDENING_CONVERSION
394 #undef ASSIGN_UNSIGNED
395 #undef ASSIGN_SIGNED
396 #undef ASSIGN_REAL
397 }
398
399 static void 
400 ves_icall_System_Array_SetValue (MonoArray *this, MonoObject *value,
401                                  MonoArray *idxs)
402 {
403         MonoClass *ac, *ic;
404         gint32 i, pos, *ind;
405
406         MONO_CHECK_ARG_NULL (idxs);
407
408         ic = idxs->obj.vtable->klass;
409         ac = this->obj.vtable->klass;
410
411         g_assert (ic->rank == 1);
412         if (idxs->bounds != NULL || idxs->max_length != ac->rank)
413                 mono_raise_exception (mono_get_exception_argument (NULL, NULL));
414
415         ind = (guint32 *)idxs->vector;
416
417         if (this->bounds == NULL) {
418                 if (*ind < 0 || *ind >= this->max_length)
419                         mono_raise_exception (mono_get_exception_index_out_of_range ());
420
421                 ves_icall_System_Array_SetValueImpl (this, value, *ind);
422                 return;
423         }
424         
425         for (i = 0; i < ac->rank; i++)
426                 if ((ind [i] < this->bounds [i].lower_bound) ||
427                     (ind [i] >= this->bounds [i].length + this->bounds [i].lower_bound))
428                         mono_raise_exception (mono_get_exception_index_out_of_range ());
429
430         pos = ind [0] - this->bounds [0].lower_bound;
431         for (i = 1; i < ac->rank; i++)
432                 pos = pos * this->bounds [i].length + ind [i] - 
433                         this->bounds [i].lower_bound;
434
435         ves_icall_System_Array_SetValueImpl (this, value, pos);
436 }
437
438 static MonoArray *
439 ves_icall_System_Array_CreateInstanceImpl (MonoReflectionType *type, MonoArray *lengths, MonoArray *bounds)
440 {
441         MonoClass *aklass;
442         MonoArray *array;
443         gint32 *sizes, i;
444
445         MONO_CHECK_ARG_NULL (type);
446         MONO_CHECK_ARG_NULL (lengths);
447
448         MONO_CHECK_ARG (lengths, mono_array_length (lengths) > 0);
449         if (bounds)
450                 MONO_CHECK_ARG (bounds, mono_array_length (lengths) == mono_array_length (bounds));
451
452         for (i = 0; i < mono_array_length (lengths); i++)
453                 if (mono_array_get (lengths, gint32, i) < 0)
454                         mono_raise_exception (mono_get_exception_argument_out_of_range (NULL));
455
456         aklass = mono_array_class_get (type->type, mono_array_length (lengths));
457
458         sizes = alloca (aklass->rank * sizeof(guint32) * 2);
459         for (i = 0; i < aklass->rank; ++i) {
460                 sizes [i] = mono_array_get (lengths, gint32, i);
461                 if (bounds)
462                         sizes [i + aklass->rank] = mono_array_get (bounds, gint32, i);
463                 else
464                         sizes [i + aklass->rank] = 0;
465         }
466
467         array = mono_array_new_full (mono_object_domain (type), aklass, sizes, sizes + aklass->rank);
468
469         return array;
470 }
471
472 static gint32 
473 ves_icall_System_Array_GetRank (MonoObject *this)
474 {
475         return this->vtable->klass->rank;
476 }
477
478 static gint32
479 ves_icall_System_Array_GetLength (MonoArray *this, gint32 dimension)
480 {
481         gint32 rank = ((MonoObject *)this)->vtable->klass->rank;
482         if ((dimension < 0) || (dimension >= rank))
483                 mono_raise_exception (mono_get_exception_index_out_of_range ());
484         
485         if (this->bounds == NULL)
486                 return this->max_length;
487         
488         return this->bounds [dimension].length;
489 }
490
491 static gint32
492 ves_icall_System_Array_GetLowerBound (MonoArray *this, gint32 dimension)
493 {
494         gint32 rank = ((MonoObject *)this)->vtable->klass->rank;
495         if ((dimension < 0) || (dimension >= rank))
496                 mono_raise_exception (mono_get_exception_index_out_of_range ());
497         
498         if (this->bounds == NULL)
499                 return 0;
500         
501         return this->bounds [dimension].lower_bound;
502 }
503
504 static void
505 ves_icall_System_Array_FastCopy (MonoArray *source, int source_idx, MonoArray* dest, int dest_idx, int length)
506 {
507         int element_size = mono_array_element_size (source->obj.vtable->klass);
508         void * dest_addr = mono_array_addr_with_size (dest, element_size, dest_idx);
509         void * source_addr = mono_array_addr_with_size (source, element_size, source_idx);
510
511         g_assert (dest_idx + length <= mono_array_length (dest));
512         g_assert (source_idx + length <= mono_array_length (source));
513         memmove (dest_addr, source_addr, element_size * length);
514 }
515
516 static void
517 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_InitializeArray (MonoArray *array, MonoClassField *field_handle)
518 {
519         MonoClass *klass = array->obj.vtable->klass;
520         guint32 size = mono_array_element_size (klass);
521         int i;
522
523         if (array->bounds == NULL)
524                 size *= array->max_length;
525         else
526                 for (i = 0; i < klass->rank; ++i) 
527                         size *= array->bounds [i].length;
528
529         memcpy (mono_array_addr (array, char, 0), field_handle->data, size);
530
531 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
532 #define SWAP(n) {\
533         gint i; \
534         guint ## n tmp; \
535         guint ## n *data = (guint ## n *) mono_array_addr (array, char, 0); \
536 \
537         for (i = 0; i < size; i += n/8, data++) { \
538                 tmp = read ## n (data); \
539                 *data = tmp; \
540         } \
541 }
542
543         /* printf ("Initialize array with elements of %s type\n", klass->element_class->name); */
544
545         switch (klass->element_class->byval_arg.type) {
546         case MONO_TYPE_CHAR:
547         case MONO_TYPE_I2:
548         case MONO_TYPE_U2:
549                 SWAP (16);
550                 break;
551         case MONO_TYPE_I4:
552         case MONO_TYPE_U4:
553                 SWAP (32);
554                 break;
555         case MONO_TYPE_I8:
556         case MONO_TYPE_U8:
557                 SWAP (64);
558                 break;
559         }
560                  
561 #endif
562 }
563
564 static gint
565 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetOffsetToStringData (void)
566 {
567         return offsetof (MonoString, chars);
568 }
569
570 static MonoObject *
571 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetObjectValue (MonoObject *obj)
572 {
573         if ((obj == NULL) || (! (obj->vtable->klass->valuetype)))
574                 return obj;
575         else
576                 return mono_object_clone (obj);
577 }
578
579 static void
580 ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor (MonoType *handle)
581 {
582         MonoClass *klass;
583
584         MONO_CHECK_ARG_NULL (handle);
585
586         klass = mono_class_from_mono_type (handle);
587         MONO_CHECK_ARG (handle, klass);
588
589         /* This will call the type constructor */
590         mono_class_vtable (mono_domain_get (), klass);
591 }
592
593 static MonoObject *
594 ves_icall_System_Object_MemberwiseClone (MonoObject *this)
595 {
596         return mono_object_clone (this);
597 }
598
599 #if HAVE_BOEHM_GC
600 #define MONO_OBJECT_ALIGNMENT_SHIFT     3
601 #else
602 #define MONO_OBJECT_ALIGNMENT_SHIFT     2
603 #endif
604
605 /*
606  * Return hashcode based on object address. This function will need to be
607  * smarter in the presence of a moving garbage collector, which will cache
608  * the address hash before relocating the object.
609  *
610  * Wang's address-based hash function:
611  *   http://www.concentric.net/~Ttwang/tech/addrhash.htm
612  */
613 static gint32
614 ves_icall_System_Object_GetHashCode (MonoObject *this)
615 {
616         register guint32 key;
617         key = (GPOINTER_TO_UINT (this) >> MONO_OBJECT_ALIGNMENT_SHIFT) * 2654435761u;
618
619         return key & 0x7fffffff;
620 }
621
622 /*
623  * A hash function for value types. I have no idea if this is a good hash 
624  * function (its similar to g_str_hash).
625  */
626 static gint32
627 ves_icall_System_ValueType_GetHashCode (MonoObject *this)
628 {
629         gint32 i, size;
630         const char *p;
631         guint h = 0;
632
633         MONO_CHECK_ARG_NULL (this);
634
635         size = this->vtable->klass->instance_size - sizeof (MonoObject);
636
637         p = (const char *)this + sizeof (MonoObject);
638
639         for (i = 0; i < size; i++) {
640                 h = (h << 5) - h + *p;
641                 p++;
642         }
643
644         return h;
645 }
646
647 static MonoBoolean
648 ves_icall_System_ValueType_Equals (MonoObject *this, MonoObject *that)
649 {
650         gint32 size;
651         const char *p, *s;
652
653         MONO_CHECK_ARG_NULL (that);
654
655         if (this->vtable != that->vtable)
656                 return FALSE;
657
658         size = this->vtable->klass->instance_size - sizeof (MonoObject);
659
660         p = (const char *)this + sizeof (MonoObject);
661         s = (const char *)that + sizeof (MonoObject);
662
663         return memcmp (p, s, size)? FALSE: TRUE;
664 }
665
666 static MonoReflectionType *
667 ves_icall_System_Object_GetType (MonoObject *obj)
668 {
669         return mono_type_get_object (mono_object_domain (obj), &obj->vtable->klass->byval_arg);
670 }
671
672 static void
673 mono_type_type_from_obj (MonoReflectionType *mtype, MonoObject *obj)
674 {
675         mtype->type = &obj->vtable->klass->byval_arg;
676         g_assert (mtype->type->type);
677 }
678
679 static gint32
680 ves_icall_AssemblyBuilder_getToken (MonoReflectionAssemblyBuilder *assb, MonoObject *obj)
681 {
682         return mono_image_create_token (assb->dynamic_assembly, obj);
683 }
684
685 static gint32
686 ves_icall_AssemblyBuilder_getDataChunk (MonoReflectionAssemblyBuilder *assb, MonoArray *buf, gint32 offset)
687 {
688         int count;
689         MonoDynamicAssembly *ass = assb->dynamic_assembly;
690         char *p = mono_array_addr (buf, char, 0);
691
692         mono_image_create_pefile (assb);
693
694         if (offset >= ass->pefile.index)
695                 return 0;
696         count = mono_array_length (buf);
697         count = MIN (count, ass->pefile.index - offset);
698         
699         memcpy (p, ass->pefile.data + offset, count);
700
701         return count;
702 }
703
704 static gboolean
705 get_get_type_caller (MonoMethod *m, gint32 no, gint32 ilo, gpointer data) {
706         MonoImage **dest = data;
707
708         /* skip icalls and Type::GetType () */
709         if (!m || m->wrapper_type || (m->iflags & METHOD_IMPL_ATTRIBUTE_INTERNAL_CALL) ||
710                         (strcmp (m->name, "GetType") == 0 && m->klass == mono_defaults.monotype_class->parent))
711                 return FALSE;
712         *dest = m->klass->image;
713         return TRUE;
714 }
715
716 static MonoReflectionType*
717 ves_icall_type_from_name (MonoString *name)
718 {
719         MonoImage *image = NULL;
720         MonoType *type;
721         gchar *str;
722
723         mono_stack_walk (get_get_type_caller, &image);
724         str = mono_string_to_utf8 (name);
725         /*g_print ("requested type %s\n", str);*/
726         type = mono_reflection_type_from_name (str, image);
727         g_free (str);
728         if (!type)
729                 return NULL;
730         /*g_print ("got it\n");*/
731         return mono_type_get_object (mono_object_domain (name), type);
732 }
733
734 static MonoReflectionType*
735 ves_icall_type_from_handle (MonoType *handle)
736 {
737         MonoDomain *domain = mono_domain_get (); 
738         MonoClass *klass = mono_class_from_mono_type (handle);
739
740         mono_class_init (klass);
741         return mono_type_get_object (domain, handle);
742 }
743
744 static guint32
745 ves_icall_type_Equals (MonoReflectionType *type, MonoReflectionType *c)
746 {
747         if (type->type && c->type)
748                 return mono_metadata_type_equal (type->type, c->type);
749         g_print ("type equals\n");
750         return 0;
751 }
752
753 /* System.TypeCode */
754 typedef enum {
755         TYPECODE_EMPTY,
756         TYPECODE_OBJECT,
757         TYPECODE_DBNULL,
758         TYPECODE_BOOLEAN,
759         TYPECODE_CHAR,
760         TYPECODE_SBYTE,
761         TYPECODE_BYTE,
762         TYPECODE_INT16,
763         TYPECODE_UINT16,
764         TYPECODE_INT32,
765         TYPECODE_UINT32,
766         TYPECODE_INT64,
767         TYPECODE_UINT64,
768         TYPECODE_SINGLE,
769         TYPECODE_DOUBLE,
770         TYPECODE_DECIMAL,
771         TYPECODE_DATETIME,
772         TYPECODE_STRING = 18
773 } TypeCode;
774
775 static guint32
776 ves_icall_type_GetTypeCode (MonoReflectionType *type)
777 {
778         int t = type->type->type;
779 handle_enum:
780         switch (t) {
781         case MONO_TYPE_VOID:
782                 return TYPECODE_OBJECT;
783         case MONO_TYPE_BOOLEAN:
784                 return TYPECODE_BOOLEAN;
785         case MONO_TYPE_U1:
786                 return TYPECODE_BYTE;
787         case MONO_TYPE_I1:
788                 return TYPECODE_SBYTE;
789         case MONO_TYPE_U2:
790                 return TYPECODE_UINT16;
791         case MONO_TYPE_I2:
792                 return TYPECODE_INT16;
793         case MONO_TYPE_CHAR:
794                 return TYPECODE_CHAR;
795         case MONO_TYPE_PTR:
796         case MONO_TYPE_U:
797         case MONO_TYPE_I:
798                 return TYPECODE_OBJECT;
799         case MONO_TYPE_U4:
800                 return TYPECODE_UINT32;
801         case MONO_TYPE_I4:
802                 return TYPECODE_INT32;
803         case MONO_TYPE_U8:
804                 return TYPECODE_UINT64;
805         case MONO_TYPE_I8:
806                 return TYPECODE_INT64;
807         case MONO_TYPE_R4:
808                 return TYPECODE_SINGLE;
809         case MONO_TYPE_R8:
810                 return TYPECODE_DOUBLE;
811         case MONO_TYPE_VALUETYPE:
812                 if (type->type->data.klass->enumtype) {
813                         t = type->type->data.klass->enum_basetype->type;
814                         goto handle_enum;
815                 } else {
816                         MonoClass *k =  type->type->data.klass;
817                         if (strcmp (k->name_space, "System") == 0) {
818                                 if (strcmp (k->name, "Decimal") == 0)
819                                         return TYPECODE_DECIMAL;
820                                 else if (strcmp (k->name, "DateTime") == 0)
821                                         return TYPECODE_DATETIME;
822                                 else if (strcmp (k->name, "DBNull") == 0)
823                                         return TYPECODE_DBNULL;
824                         }
825                 }
826                 /* handle datetime, dbnull.. */
827                 return TYPECODE_OBJECT;
828         case MONO_TYPE_STRING:
829                 return TYPECODE_STRING;
830         case MONO_TYPE_SZARRAY:
831         case MONO_TYPE_ARRAY:
832         case MONO_TYPE_OBJECT:
833                 return TYPECODE_OBJECT;
834         case MONO_TYPE_CLASS:
835                 return TYPECODE_OBJECT;
836         default:
837                 g_error ("type 0x%02x not handled in GetTypeCode()", t);
838         }
839         return 0;
840 }
841
842 static guint32
843 ves_icall_type_is_subtype_of (MonoReflectionType *type, MonoReflectionType *c, MonoBoolean check_interfaces)
844 {
845         MonoDomain *domain; 
846         MonoClass *klass;
847         MonoClass *klassc;
848
849         g_assert (type != NULL);
850         
851         domain = ((MonoObject *)type)->vtable->domain;
852
853         if (!c) /* FIXME: dont know what do do here */
854                 return 0;
855
856         klass = mono_class_from_mono_type (type->type);
857         klassc = mono_class_from_mono_type (c->type);
858
859         /* cut&paste from mono_object_isinst (): keep in sync */
860         if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && !(klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
861                 MonoVTable *klass_vt = mono_class_vtable (domain, klass);
862                 if ((klassc->interface_id <= klass->max_interface_id) &&
863                     klass_vt->interface_offsets [klassc->interface_id])
864                         return 1;
865         } else if (check_interfaces && (klassc->flags & TYPE_ATTRIBUTE_INTERFACE) && (klass->flags & TYPE_ATTRIBUTE_INTERFACE)) {
866                 int i;
867
868                 for (i = 0; i < klass->interface_count; i ++) {
869                         MonoClass *ic =  klass->interfaces [i];
870                         if (ic == klassc)
871                                 return 1;
872                 }
873         } else {
874                 /*
875                  * klass->baseval is 0 for interfaces 
876                  */
877                 if (klass->baseval && ((klass->baseval - klassc->baseval) <= klassc->diffval))
878                         return 1;
879         }
880         return 0;
881 }
882
883 static guint32
884 ves_icall_get_attributes (MonoReflectionType *type)
885 {
886         MonoClass *klass = mono_class_from_mono_type (type->type);
887
888         return klass->flags;
889 }
890
891 static void
892 ves_icall_get_method_info (MonoMethod *method, MonoMethodInfo *info)
893 {
894         MonoDomain *domain = mono_domain_get ();
895
896         info->parent = mono_type_get_object (domain, &method->klass->byval_arg);
897         info->ret = mono_type_get_object (domain, method->signature->ret);
898         info->attrs = method->flags;
899         info->implattrs = method->iflags;
900 }
901
902 static MonoArray*
903 ves_icall_get_parameter_info (MonoMethod *method)
904 {
905         MonoDomain *domain = mono_domain_get (); 
906         MonoArray *res;
907         static MonoClass *System_Reflection_ParameterInfo;
908         MonoReflectionParameter** args;
909         int i;
910
911         args = mono_param_get_objects (domain, method);
912         if (!System_Reflection_ParameterInfo)
913                 System_Reflection_ParameterInfo = mono_class_from_name (
914                         mono_defaults.corlib, "System.Reflection", "ParameterInfo");
915         res = mono_array_new (domain, System_Reflection_ParameterInfo, method->signature->param_count);
916         for (i = 0; i < method->signature->param_count; ++i) {
917                 mono_array_set (res, gpointer, i, args [i]);
918         }
919         return res;
920 }
921
922 static void
923 ves_icall_get_field_info (MonoReflectionField *field, MonoFieldInfo *info)
924 {
925         MonoDomain *domain = mono_object_domain (field); 
926
927         info->parent = mono_type_get_object (domain, &field->klass->byval_arg);
928         info->type = mono_type_get_object (domain, field->field->type);
929         info->name = mono_string_new (domain, field->field->name);
930         info->attrs = field->field->type->attrs;
931 }
932
933 static MonoObject *
934 ves_icall_MonoField_GetValueInternal (MonoReflectionField *field, MonoObject *obj)
935 {       
936         MonoObject *o;
937         MonoClassField *cf = field->field;
938         MonoClass *klass;
939         MonoVTable *vtable;
940         MonoDomain *domain = mono_object_domain (field); 
941         gchar *v;
942         gboolean is_static = FALSE;
943         gboolean is_ref = FALSE;
944
945         mono_class_init (field->klass);
946
947         switch (cf->type->type) {
948         case MONO_TYPE_STRING:
949         case MONO_TYPE_OBJECT:
950         case MONO_TYPE_CLASS:
951         case MONO_TYPE_ARRAY:
952         case MONO_TYPE_SZARRAY:
953                 is_ref = TRUE;
954                 break;
955         case MONO_TYPE_U1:
956         case MONO_TYPE_I1:
957         case MONO_TYPE_BOOLEAN:
958         case MONO_TYPE_U2:
959         case MONO_TYPE_I2:
960         case MONO_TYPE_CHAR:
961         case MONO_TYPE_U:
962         case MONO_TYPE_I:
963         case MONO_TYPE_U4:
964         case MONO_TYPE_I4:
965         case MONO_TYPE_R4:
966         case MONO_TYPE_U8:
967         case MONO_TYPE_I8:
968         case MONO_TYPE_R8:
969         case MONO_TYPE_VALUETYPE:
970                 is_ref = cf->type->byref;
971                 break;
972         default:
973                 g_error ("type 0x%x not handled in "
974                          "ves_icall_Monofield_GetValue", cf->type->type);
975                 return NULL;
976         }
977
978         if (cf->type->attrs & FIELD_ATTRIBUTE_STATIC) {
979                 is_static = TRUE;
980                 vtable = mono_class_vtable (domain, field->klass);
981         }
982         
983         if (is_ref) {
984                 if (is_static) {
985                         mono_field_static_get_value (vtable, cf, &o);
986                 } else {
987                         mono_field_get_value (obj, cf, &o);
988                 }
989                 return o;
990         }
991
992         /* boxed value type */
993         klass = mono_class_from_mono_type (cf->type);
994         o = mono_object_new (domain, klass);
995         v = ((gchar *) o) + sizeof (MonoObject);
996         if (is_static) {
997                 mono_field_static_get_value (vtable, cf, v);
998         } else {
999                 mono_field_get_value (obj, cf, v);
1000         }
1001
1002         return o;
1003 }
1004
1005 static void
1006 ves_icall_FieldInfo_SetValueInternal (MonoReflectionField *field, MonoObject *obj, MonoObject *value)
1007 {
1008         MonoClassField *cf = field->field;
1009         gchar *v;
1010
1011         v = (gchar *) value;
1012         if (!cf->type->byref) {
1013                 switch (cf->type->type) {
1014                 case MONO_TYPE_U1:
1015                 case MONO_TYPE_I1:
1016                 case MONO_TYPE_BOOLEAN:
1017                 case MONO_TYPE_U2:
1018                 case MONO_TYPE_I2:
1019                 case MONO_TYPE_CHAR:
1020                 case MONO_TYPE_U:
1021                 case MONO_TYPE_I:
1022                 case MONO_TYPE_U4:
1023                 case MONO_TYPE_I4:
1024                 case MONO_TYPE_R4:
1025                 case MONO_TYPE_U8:
1026                 case MONO_TYPE_I8:
1027                 case MONO_TYPE_R8:
1028                 case MONO_TYPE_VALUETYPE:
1029                         v += sizeof (MonoObject);
1030                         break;
1031                 case MONO_TYPE_STRING:
1032                 case MONO_TYPE_OBJECT:
1033                 case MONO_TYPE_CLASS:
1034                 case MONO_TYPE_ARRAY:
1035                 case MONO_TYPE_SZARRAY:
1036                         /* Do nothing */
1037                         break;
1038                 default:
1039                         g_error ("type 0x%x not handled in "
1040                                  "ves_icall_FieldInfo_SetValueInternal", cf->type->type);
1041                         return;
1042                 }
1043         }
1044
1045         if (cf->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1046                 MonoVTable *vtable = mono_class_vtable (mono_object_domain (field), field->klass);
1047                 mono_field_static_set_value (vtable, cf, v);
1048         } else {
1049                 mono_field_set_value (obj, cf, v);
1050         }
1051 }
1052
1053 static void
1054 ves_icall_get_property_info (MonoReflectionProperty *property, MonoPropertyInfo *info)
1055 {
1056         MonoDomain *domain = mono_object_domain (property); 
1057
1058         info->parent = mono_type_get_object (domain, &property->klass->byval_arg);
1059         info->name = mono_string_new (domain, property->property->name);
1060         info->attrs = property->property->attrs;
1061         info->get = property->property->get ? mono_method_get_object (domain, property->property->get, NULL): NULL;
1062         info->set = property->property->set ? mono_method_get_object (domain, property->property->set, NULL): NULL;
1063         /* 
1064          * There may be other methods defined for properties, though, it seems they are not exposed 
1065          * in the reflection API 
1066          */
1067 }
1068
1069 static void
1070 ves_icall_get_event_info (MonoReflectionEvent *event, MonoEventInfo *info)
1071 {
1072         MonoDomain *domain = mono_object_domain (event); 
1073
1074         info->parent = mono_type_get_object (domain, &event->klass->byval_arg);
1075         info->name = mono_string_new (domain, event->event->name);
1076         info->attrs = event->event->attrs;
1077         info->add_method = event->event->add ? mono_method_get_object (domain, event->event->add, NULL): NULL;
1078         info->remove_method = event->event->remove ? mono_method_get_object (domain, event->event->remove, NULL): NULL;
1079         info->raise_method = event->event->raise ? mono_method_get_object (domain, event->event->raise, NULL): NULL;
1080 }
1081
1082 static MonoArray*
1083 ves_icall_Type_GetInterfaces (MonoReflectionType* type)
1084 {
1085         MonoDomain *domain = mono_object_domain (type); 
1086         MonoArray *intf;
1087         int ninterf, i;
1088         MonoClass *class = mono_class_from_mono_type (type->type);
1089         MonoClass *parent;
1090
1091         ninterf = 0;
1092         for (parent = class; parent; parent = parent->parent) {
1093                 ninterf += parent->interface_count;
1094         }
1095         intf = mono_array_new (domain, mono_defaults.monotype_class, ninterf);
1096         ninterf = 0;
1097         for (parent = class; parent; parent = parent->parent) {
1098                 for (i = 0; i < parent->interface_count; ++i) {
1099                         mono_array_set (intf, gpointer, ninterf, mono_type_get_object (domain, &parent->interfaces [i]->byval_arg));
1100                         ++ninterf;
1101                 }
1102         }
1103         return intf;
1104 }
1105
1106 static MonoReflectionType*
1107 ves_icall_MonoType_GetElementType (MonoReflectionType *type)
1108 {
1109         MonoClass *class = mono_class_from_mono_type (type->type);
1110         if (class->enumtype && class->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
1111                 return mono_type_get_object (mono_object_domain (type), class->enum_basetype);
1112         else if (class->element_class)
1113                 return mono_type_get_object (mono_object_domain (type), &class->element_class->byval_arg);
1114         else
1115                 return NULL;
1116 }
1117
1118 static MonoReflectionType*
1119 ves_icall_get_type_parent (MonoReflectionType *type)
1120 {
1121         MonoClass *class = mono_class_from_mono_type (type->type);
1122         return class->parent ? mono_type_get_object (mono_object_domain (type), &class->parent->byval_arg): NULL;
1123 }
1124
1125 static MonoBoolean
1126 ves_icall_type_ispointer (MonoReflectionType *type)
1127 {
1128         return type->type->type == MONO_TYPE_PTR;
1129 }
1130
1131 static MonoBoolean
1132 ves_icall_type_isbyref (MonoReflectionType *type)
1133 {
1134         return type->type->byref;
1135 }
1136
1137 static MonoReflectionModule*
1138 ves_icall_MonoType_get_Module (MonoReflectionType *type)
1139 {
1140         MonoClass *class = mono_class_from_mono_type (type->type);
1141         return mono_module_get_object (mono_object_domain (type), class->image);
1142 }
1143
1144 static void
1145 ves_icall_get_type_info (MonoType *type, MonoTypeInfo *info)
1146 {
1147         MonoDomain *domain = mono_domain_get (); 
1148         MonoClass *class = mono_class_from_mono_type (type);
1149
1150         info->nested_in = class->nested_in ? mono_type_get_object (domain, &class->nested_in->byval_arg): NULL;
1151         info->name = mono_string_new (domain, class->name);
1152         info->name_space = mono_string_new (domain, class->name_space);
1153         info->rank = class->rank;
1154         info->assembly = mono_assembly_get_object (domain, class->image->assembly);
1155         if (class->enumtype && class->enum_basetype) /* types that are modifierd typebuilkders may not have enum_basetype set */
1156                 info->etype = mono_type_get_object (domain, class->enum_basetype);
1157         else if (class->element_class)
1158                 info->etype = mono_type_get_object (domain, &class->element_class->byval_arg);
1159         else
1160                 info->etype = NULL;
1161
1162         info->isprimitive = (type->type >= MONO_TYPE_BOOLEAN) && (type->type <= MONO_TYPE_R8);
1163 }
1164
1165 static MonoObject *
1166 ves_icall_InternalInvoke (MonoReflectionMethod *method, MonoObject *this, MonoArray *params) 
1167 {
1168         return mono_runtime_invoke_array (method->method, this, params, NULL);
1169 }
1170
1171 static MonoObject *
1172 ves_icall_InternalExecute (MonoReflectionMethod *method, MonoObject *this, MonoArray *params, MonoArray **outArgs) 
1173 {
1174         MonoDomain *domain = mono_object_domain (method); 
1175         MonoMethod *m = method->method;
1176         MonoMethodSignature *sig = m->signature;
1177         MonoArray *out_args;
1178         MonoObject *result;
1179         int i, j, outarg_count = 0;
1180
1181         if (m->klass == mono_defaults.object_class) {
1182
1183                 if (!strcmp (m->name, "FieldGetter")) {
1184                         MonoClass *k = this->vtable->klass;
1185                         MonoString *name = mono_array_get (params, MonoString *, 1);
1186                         char *str;
1187
1188                         str = mono_string_to_utf8 (name);
1189                 
1190                         for (i = 0; i < k->field.count; i++) {
1191                                 if (!strcmp (k->fields [i].name, str)) {
1192                                         MonoClass *field_klass =  mono_class_from_mono_type (k->fields [i].type);
1193                                         if (field_klass->valuetype)
1194                                                 result = mono_value_box (domain, field_klass,
1195                                                                          (char *)this + k->fields [i].offset);
1196                                         else 
1197                                                 result = *((gpointer *)((char *)this + k->fields [i].offset));
1198                                 
1199                                         g_assert (result);
1200                                         out_args = mono_array_new (domain, mono_defaults.object_class, 1);
1201                                         *outArgs = out_args;
1202                                         mono_array_set (out_args, gpointer, 0, result);
1203                                         g_free (str);
1204                                         return NULL;
1205                                 }
1206                         }
1207
1208                         g_free (str);
1209                         g_assert_not_reached ();
1210
1211                 } else if (!strcmp (m->name, "FieldSetter")) {
1212                         MonoClass *k = this->vtable->klass;
1213                         MonoString *name = mono_array_get (params, MonoString *, 1);
1214                         int size, align;
1215                         char *str;
1216
1217                         str = mono_string_to_utf8 (name);
1218                 
1219                         for (i = 0; i < k->field.count; i++) {
1220                                 if (!strcmp (k->fields [i].name, str)) {
1221                                         MonoClass *field_klass =  mono_class_from_mono_type (k->fields [i].type);
1222                                         MonoObject *val = mono_array_get (params, gpointer, 2);
1223
1224                                         if (field_klass->valuetype) {
1225                                                 size = mono_type_size (k->fields [i].type, &align);
1226                                                 memcpy ((char *)this + k->fields [i].offset, 
1227                                                         ((char *)val) + sizeof (MonoObject), size);
1228                                         } else 
1229                                                 *((gpointer *)this + k->fields [i].offset) = val;
1230                                 
1231                                         g_assert (result);
1232                                         g_free (str);
1233                                         return NULL;
1234                                 }
1235                         }
1236
1237                         g_free (str);
1238                         g_assert_not_reached ();
1239
1240                 }
1241         }
1242
1243         for (i = 0; i < mono_array_length (params); i++) {
1244                 if (sig->params [i]->byref) 
1245                         outarg_count++;
1246         }
1247
1248         out_args = mono_array_new (domain, mono_defaults.object_class, outarg_count);
1249         
1250         for (i = 0, j = 0; i < mono_array_length (params); i++) {
1251                 if (sig->params [i]->byref) {
1252                         gpointer arg;
1253                         arg = mono_array_get (params, gpointer, i);
1254                         mono_array_set (out_args, gpointer, j, arg);
1255                         j++;
1256                 }
1257         }
1258
1259         /* fixme: handle constructors? */
1260         if (!strcmp (method->method->name, ".ctor"))
1261                 g_assert_not_reached ();
1262
1263         result = mono_runtime_invoke_array (method->method, this, params, NULL);
1264
1265         *outArgs = out_args;
1266
1267         return result;
1268 }
1269
1270 static MonoObject *
1271 ves_icall_System_Enum_ToObject (MonoReflectionType *type, MonoObject *obj)
1272 {
1273         MonoDomain *domain; 
1274         MonoClass *enumc, *objc;
1275         gint32 s1, s2;
1276         MonoObject *res;
1277         
1278         MONO_CHECK_ARG_NULL (type);
1279         MONO_CHECK_ARG_NULL (obj);
1280
1281         domain = mono_object_domain (type); 
1282         enumc = mono_class_from_mono_type (type->type);
1283         objc = obj->vtable->klass;
1284
1285         MONO_CHECK_ARG (obj, enumc->enumtype == TRUE);
1286         MONO_CHECK_ARG (obj, (objc->enumtype) || (objc->byval_arg.type >= MONO_TYPE_I1 &&
1287                                                   objc->byval_arg.type <= MONO_TYPE_U8));
1288         
1289         s1 = mono_class_value_size (enumc, NULL);
1290         s2 = mono_class_value_size (objc, NULL);
1291
1292         res = mono_object_new (domain, enumc);
1293
1294 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
1295         memcpy ((char *)res + sizeof (MonoObject), (char *)obj + sizeof (MonoObject), MIN (s1, s2));
1296 #else
1297         memcpy ((char *)res + sizeof (MonoObject) + (s1 > s2 ? s1 - s2 : 0),
1298                 (char *)obj + sizeof (MonoObject) + (s2 > s1 ? s2 - s1 : 0),
1299                 MIN (s1, s2));
1300 #endif
1301         return res;
1302 }
1303
1304 static MonoObject *
1305 ves_icall_System_Enum_get_value (MonoObject *this)
1306 {
1307         MonoObject *res;
1308         MonoClass *enumc;
1309         gpointer dst;
1310         gpointer src;
1311         int size;
1312
1313         if (!this)
1314                 return NULL;
1315
1316         g_assert (this->vtable->klass->enumtype);
1317         
1318         enumc = mono_class_from_mono_type (this->vtable->klass->enum_basetype);
1319         res = mono_object_new (mono_object_domain (this), enumc);
1320         dst = (char *)res + sizeof (MonoObject);
1321         src = (char *)this + sizeof (MonoObject);
1322         size = mono_class_value_size (enumc, NULL);
1323
1324         memcpy (dst, src, size);
1325
1326         return res;
1327 }
1328
1329 static void
1330 ves_icall_get_enum_info (MonoReflectionType *type, MonoEnumInfo *info)
1331 {
1332         MonoDomain *domain = mono_object_domain (type); 
1333         MonoClass *enumc = mono_class_from_mono_type (type->type);
1334         guint i, j, nvalues, crow;
1335         MonoClassField *field;
1336         
1337         info->utype = mono_type_get_object (domain, enumc->enum_basetype);
1338         nvalues = enumc->field.count - 1;
1339         info->names = mono_array_new (domain, mono_defaults.string_class, nvalues);
1340         info->values = mono_array_new (domain, enumc, nvalues);
1341         
1342         for (i = 0, j = 0; i < enumc->field.count; ++i) {
1343                 field = &enumc->fields [i];
1344                 if (strcmp ("value__", field->name) == 0)
1345                         continue;
1346                 mono_array_set (info->names, gpointer, j, mono_string_new (domain, field->name));
1347                 if (!field->data) {
1348                         crow = mono_metadata_get_constant_index (enumc->image, MONO_TOKEN_FIELD_DEF | (i+enumc->field.first+1));
1349                         crow = mono_metadata_decode_row_col (&enumc->image->tables [MONO_TABLE_CONSTANT], crow-1, MONO_CONSTANT_VALUE);
1350                         /* 1 is the length of the blob */
1351                         field->data = 1 + mono_metadata_blob_heap (enumc->image, crow);
1352                 }
1353                 switch (enumc->enum_basetype->type) {
1354                 case MONO_TYPE_U1:
1355                 case MONO_TYPE_I1:
1356                         mono_array_set (info->values, gchar, j, *field->data);
1357                         break;
1358                 case MONO_TYPE_CHAR:
1359                 case MONO_TYPE_U2:
1360                 case MONO_TYPE_I2:
1361                         mono_array_set (info->values, gint16, j, read16 (field->data));
1362                         break;
1363                 case MONO_TYPE_U4:
1364                 case MONO_TYPE_I4:
1365                         mono_array_set (info->values, gint32, j, read32 (field->data));
1366                         break;
1367                 case MONO_TYPE_U8:
1368                 case MONO_TYPE_I8:
1369                         mono_array_set (info->values, gint64, j, read64 (field->data));
1370                         break;
1371                 default:
1372                         g_error ("Implement type 0x%02x in get_enum_info", enumc->enum_basetype->type);
1373                 }
1374                 ++j;
1375         }
1376 }
1377
1378 static MonoProperty*
1379 search_property (MonoClass *klass, char* name, MonoArray *args) {
1380         int i;
1381         MonoProperty *p;
1382
1383         /* FIXME: handle args */
1384         for (i = 0; i < klass->property.count; ++i) {
1385                 p = &klass->properties [i];
1386                 if (strcmp (p->name, name) == 0)
1387                         return p;
1388         }
1389         return NULL;
1390 }
1391
1392 static MonoReflectionProperty*
1393 ves_icall_get_property (MonoReflectionType *type, MonoString *name, MonoArray *args)
1394 {
1395         MonoDomain *domain = mono_object_domain (type); 
1396         MonoProperty *p;
1397         MonoClass *class = mono_class_from_mono_type (type->type);
1398         char *n = mono_string_to_utf8 (name);
1399
1400         p = search_property (class, n, args);
1401         g_free (n);
1402         if (p)
1403                 return mono_property_get_object (domain, class, p);
1404         return NULL;
1405 }
1406
1407 enum {
1408         BFLAGS_IgnoreCase = 1,
1409         BFLAGS_DeclaredOnly = 2,
1410         BFLAGS_Instance = 4,
1411         BFLAGS_Static = 8,
1412         BFLAGS_Public = 0x10,
1413         BFLAGS_NonPublic = 0x20,
1414         BFLAGS_InvokeMethod = 0x100,
1415         BFLAGS_CreateInstance = 0x200,
1416         BFLAGS_GetField = 0x400,
1417         BFLAGS_SetField = 0x800,
1418         BFLAGS_GetProperty = 0x1000,
1419         BFLAGS_SetProperty = 0x2000,
1420         BFLAGS_ExactBinding = 0x10000,
1421         BFLAGS_SuppressChangeType = 0x20000,
1422         BFLAGS_OptionalParamBinding = 0x40000
1423 };
1424
1425 static MonoFieldInfo *
1426 ves_icall_Type_GetField (MonoReflectionType *type, MonoString *name, guint32 bflags)
1427 {
1428         MonoDomain *domain; 
1429         MonoClass *startklass, *klass;
1430         int i, match;
1431         MonoClassField *field;
1432         char *utf8_name;
1433         domain = ((MonoObject *)type)->vtable->domain;
1434         klass = startklass = mono_class_from_mono_type (type->type);
1435
1436         if (!name)
1437                 return NULL;
1438
1439 handle_parent:  
1440         for (i = 0; i < klass->field.count; ++i) {
1441                 match = 0;
1442                 field = &klass->fields [i];
1443                 if ((field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == FIELD_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 (field->type->attrs & FIELD_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                 
1464                 utf8_name = mono_string_to_utf8 (name);
1465
1466                 if (strcmp (field->name, utf8_name)) {
1467                         g_free (utf8_name);
1468                         continue;
1469                 }
1470                 g_free (utf8_name);
1471                 
1472                 return (MonoFieldInfo *)mono_field_get_object (domain, klass, field);
1473         }
1474         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1475                 goto handle_parent;
1476
1477         return NULL;
1478 }
1479
1480 static MonoArray*
1481 ves_icall_Type_GetFields (MonoReflectionType *type, guint32 bflags)
1482 {
1483         MonoDomain *domain; 
1484         GSList *l = NULL, *tmp;
1485         MonoClass *startklass, *klass;
1486         MonoArray *res;
1487         MonoObject *member;
1488         int i, len, match;
1489         MonoClassField *field;
1490
1491         domain = ((MonoObject *)type)->vtable->domain;
1492         klass = startklass = mono_class_from_mono_type (type->type);
1493
1494 handle_parent:  
1495         for (i = 0; i < klass->field.count; ++i) {
1496                 match = 0;
1497                 field = &klass->fields [i];
1498                 if ((field->type->attrs & FIELD_ATTRIBUTE_FIELD_ACCESS_MASK) == FIELD_ATTRIBUTE_PUBLIC) {
1499                         if (bflags & BFLAGS_Public)
1500                                 match++;
1501                 } else {
1502                         if (bflags & BFLAGS_NonPublic)
1503                                 match++;
1504                 }
1505                 if (!match)
1506                         continue;
1507                 match = 0;
1508                 if (field->type->attrs & FIELD_ATTRIBUTE_STATIC) {
1509                         if (bflags & BFLAGS_Static)
1510                                 match++;
1511                 } else {
1512                         if (bflags & BFLAGS_Instance)
1513                                 match++;
1514                 }
1515
1516                 if (!match)
1517                         continue;
1518                 member = (MonoObject*)mono_field_get_object (domain, klass, field);
1519                 l = g_slist_prepend (l, member);
1520         }
1521         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1522                 goto handle_parent;
1523         len = g_slist_length (l);
1524         res = mono_array_new (domain, mono_defaults.field_info_class, len);
1525         i = 0;
1526         tmp = g_slist_reverse (l);
1527         for (; tmp; tmp = tmp->next, ++i)
1528                 mono_array_set (res, gpointer, i, tmp->data);
1529         g_slist_free (l);
1530         return res;
1531 }
1532
1533 static MonoArray*
1534 ves_icall_Type_GetMethods (MonoReflectionType *type, guint32 bflags)
1535 {
1536         MonoDomain *domain; 
1537         GSList *l = NULL, *tmp;
1538         static MonoClass *System_Reflection_MethodInfo;
1539         MonoClass *startklass, *klass;
1540         MonoArray *res;
1541         MonoMethod *method;
1542         MonoObject *member;
1543         int i, len, match;
1544         GHashTable *method_slots = g_hash_table_new (NULL, NULL);
1545                 
1546         domain = ((MonoObject *)type)->vtable->domain;
1547         klass = startklass = mono_class_from_mono_type (type->type);
1548         len = 0;
1549
1550 handle_parent:
1551         for (i = 0; i < klass->method.count; ++i) {
1552                 match = 0;
1553                 method = klass->methods [i];
1554                 if (strcmp (method->name, ".ctor") == 0 || strcmp (method->name, ".cctor") == 0)
1555                         continue;
1556                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1557                         if (bflags & BFLAGS_Public)
1558                                 match++;
1559                 } else {
1560                         if (bflags & BFLAGS_NonPublic)
1561                                 match++;
1562                 }
1563                 if (!match)
1564                         continue;
1565                 match = 0;
1566                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1567                         if (bflags & BFLAGS_Static)
1568                                 match++;
1569                 } else {
1570                         if (bflags & BFLAGS_Instance)
1571                                 match++;
1572                 }
1573
1574                 if (!match)
1575                         continue;
1576                 match = 0;
1577                 if (g_hash_table_lookup (method_slots, GUINT_TO_POINTER (method->slot)))
1578                         continue;
1579                 g_hash_table_insert (method_slots, GUINT_TO_POINTER (method->slot), method);
1580                 member = (MonoObject*)mono_method_get_object (domain, method, startklass);
1581                 
1582                 l = g_slist_prepend (l, member);
1583                 len++;
1584         }
1585         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1586                 goto handle_parent;
1587         if (!System_Reflection_MethodInfo)
1588                 System_Reflection_MethodInfo = mono_class_from_name (
1589                         mono_defaults.corlib, "System.Reflection", "MethodInfo");
1590         res = mono_array_new (domain, System_Reflection_MethodInfo, len);
1591         i = 0;
1592         tmp = l;
1593         for (; tmp; tmp = tmp->next, ++i)
1594                 mono_array_set (res, gpointer, i, tmp->data);
1595         g_slist_free (l);
1596         g_hash_table_destroy (method_slots);
1597         return res;
1598 }
1599
1600 static MonoArray*
1601 ves_icall_Type_GetConstructors (MonoReflectionType *type, guint32 bflags)
1602 {
1603         MonoDomain *domain; 
1604         GSList *l = NULL, *tmp;
1605         static MonoClass *System_Reflection_ConstructorInfo;
1606         MonoClass *startklass, *klass;
1607         MonoArray *res;
1608         MonoMethod *method;
1609         MonoObject *member;
1610         int i, len, match;
1611
1612         domain = ((MonoObject *)type)->vtable->domain;
1613         klass = startklass = mono_class_from_mono_type (type->type);
1614
1615 handle_parent:  
1616         for (i = 0; i < klass->method.count; ++i) {
1617                 match = 0;
1618                 method = klass->methods [i];
1619                 if (strcmp (method->name, ".ctor") && strcmp (method->name, ".cctor"))
1620                         continue;
1621                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1622                         if (bflags & BFLAGS_Public)
1623                                 match++;
1624                 } else {
1625                         if (bflags & BFLAGS_NonPublic)
1626                                 match++;
1627                 }
1628                 if (!match)
1629                         continue;
1630                 match = 0;
1631                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1632                         if (bflags & BFLAGS_Static)
1633                                 match++;
1634                 } else {
1635                         if (bflags & BFLAGS_Instance)
1636                                 match++;
1637                 }
1638
1639                 if (!match)
1640                         continue;
1641                 member = (MonoObject*)mono_method_get_object (domain, method, startklass);
1642                         
1643                 l = g_slist_prepend (l, member);
1644         }
1645         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1646                 goto handle_parent;
1647         len = g_slist_length (l);
1648         if (!System_Reflection_ConstructorInfo)
1649                 System_Reflection_ConstructorInfo = mono_class_from_name (
1650                         mono_defaults.corlib, "System.Reflection", "ConstructorInfo");
1651         res = mono_array_new (domain, System_Reflection_ConstructorInfo, len);
1652         i = 0;
1653         tmp = g_slist_reverse (l);
1654         for (; tmp; tmp = tmp->next, ++i)
1655                 mono_array_set (res, gpointer, i, tmp->data);
1656         g_slist_free (l);
1657         return res;
1658 }
1659
1660 static MonoArray*
1661 ves_icall_Type_GetProperties (MonoReflectionType *type, guint32 bflags)
1662 {
1663         MonoDomain *domain; 
1664         GSList *l = NULL, *tmp;
1665         static MonoClass *System_Reflection_PropertyInfo;
1666         MonoClass *startklass, *klass;
1667         MonoArray *res;
1668         MonoMethod *method;
1669         MonoProperty *prop;
1670         int i, match;
1671         int len = 0;
1672         GHashTable *method_slots = g_hash_table_new (NULL, NULL);
1673
1674         domain = ((MonoObject *)type)->vtable->domain;
1675         klass = startklass = mono_class_from_mono_type (type->type);
1676
1677 handle_parent:
1678         for (i = 0; i < klass->property.count; ++i) {
1679                 prop = &klass->properties [i];
1680                 match = 0;
1681                 method = prop->get;
1682                 if (!method)
1683                         method = prop->set;
1684                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1685                         if (bflags & BFLAGS_Public)
1686                                 match++;
1687                 } else {
1688                         if (bflags & BFLAGS_NonPublic)
1689                                 match++;
1690                 }
1691                 if (!match)
1692                         continue;
1693                 match = 0;
1694                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1695                         if (bflags & BFLAGS_Static)
1696                                 match++;
1697                 } else {
1698                         if (bflags & BFLAGS_Instance)
1699                                 match++;
1700                 }
1701
1702                 if (!match)
1703                         continue;
1704                 match = 0;
1705
1706                 if (g_hash_table_lookup (method_slots, GUINT_TO_POINTER (method->slot)))
1707                         continue;
1708                 g_hash_table_insert (method_slots, GUINT_TO_POINTER (method->slot), prop);
1709
1710                 l = g_slist_prepend (l, mono_property_get_object (domain, klass, prop));
1711                 len++;
1712         }
1713         if ((!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent)))
1714                 goto handle_parent;
1715         if (!System_Reflection_PropertyInfo)
1716                 System_Reflection_PropertyInfo = mono_class_from_name (
1717                         mono_defaults.corlib, "System.Reflection", "PropertyInfo");
1718         res = mono_array_new (domain, System_Reflection_PropertyInfo, len);
1719         i = 0;
1720         tmp = l;
1721         for (; tmp; tmp = tmp->next, ++i)
1722                 mono_array_set (res, gpointer, i, tmp->data);
1723         g_slist_free (l);
1724         g_hash_table_destroy (method_slots);
1725         return res;
1726 }
1727
1728 static MonoArray*
1729 ves_icall_Type_GetEvents (MonoReflectionType *type, guint32 bflags)
1730 {
1731         MonoDomain *domain; 
1732         GSList *l = NULL, *tmp;
1733         static MonoClass *System_Reflection_EventInfo;
1734         MonoClass *startklass, *klass;
1735         MonoArray *res;
1736         MonoMethod *method;
1737         MonoEvent *event;
1738         int i, len, match;
1739
1740         domain = ((MonoObject *)type)->vtable->domain;
1741         klass = startklass = mono_class_from_mono_type (type->type);
1742
1743 handle_parent:  
1744         for (i = 0; i < klass->event.count; ++i) {
1745                 event = &klass->events [i];
1746                 match = 0;
1747                 method = event->add;
1748                 if (!method)
1749                         method = event->remove;
1750                 if ((method->flags & METHOD_ATTRIBUTE_MEMBER_ACCESS_MASK) == METHOD_ATTRIBUTE_PUBLIC) {
1751                         if (bflags & BFLAGS_Public)
1752                                 match++;
1753                 } else {
1754                         if (bflags & BFLAGS_NonPublic)
1755                                 match++;
1756                 }
1757                 if (!match)
1758                         continue;
1759                 match = 0;
1760                 if (method->flags & METHOD_ATTRIBUTE_STATIC) {
1761                         if (bflags & BFLAGS_Static)
1762                                 match++;
1763                 } else {
1764                         if (bflags & BFLAGS_Instance)
1765                                 match++;
1766                 }
1767
1768                 if (!match)
1769                         continue;
1770                 match = 0;
1771                 l = g_slist_prepend (l, mono_event_get_object (domain, klass, event));
1772         }
1773         if (!(bflags & BFLAGS_DeclaredOnly) && (klass = klass->parent))
1774                 goto handle_parent;
1775         len = g_slist_length (l);
1776         if (!System_Reflection_EventInfo)
1777                 System_Reflection_EventInfo = mono_class_from_name (
1778                         mono_defaults.corlib, "System.Reflection", "EventInfo");
1779         res = mono_array_new (domain, System_Reflection_EventInfo, len);
1780         i = 0;
1781         tmp = l;
1782         for (; tmp; tmp = tmp->next, ++i)
1783                 mono_array_set (res, gpointer, i, tmp->data);
1784         g_slist_free (l);
1785         return res;
1786 }
1787
1788 static MonoArray*
1789 ves_icall_Type_GetNestedTypes (MonoReflectionType *type, guint32 bflags)
1790 {
1791         MonoDomain *domain; 
1792         GSList *l = NULL, *tmp;
1793         GList *tmpn;
1794         MonoClass *startklass, *klass;
1795         MonoArray *res;
1796         MonoObject *member;
1797         int i, len, match;
1798         MonoClass *nested;
1799
1800         domain = ((MonoObject *)type)->vtable->domain;
1801         klass = startklass = mono_class_from_mono_type (type->type);
1802
1803         for (tmpn = klass->nested_classes; tmpn; tmpn = tmpn->next) {
1804                 match = 0;
1805                 nested = tmpn->data;
1806                 if ((nested->flags & TYPE_ATTRIBUTE_VISIBILITY_MASK) == TYPE_ATTRIBUTE_NESTED_PUBLIC) {
1807                         if (bflags & BFLAGS_Public)
1808                                 match++;
1809                 } else {
1810                         if (bflags & BFLAGS_NonPublic)
1811                                 match++;
1812                 }
1813                 if (!match)
1814                         continue;
1815                 member = (MonoObject*)mono_type_get_object (domain, &nested->byval_arg);
1816                 l = g_slist_prepend (l, member);
1817         }
1818         len = g_slist_length (l);
1819         res = mono_array_new (domain, mono_defaults.monotype_class, len);
1820         i = 0;
1821         tmp = g_slist_reverse (l);
1822         for (; tmp; tmp = tmp->next, ++i)
1823                 mono_array_set (res, gpointer, i, tmp->data);
1824         g_slist_free (l);
1825         return res;
1826 }
1827
1828 static MonoReflectionType*
1829 ves_icall_System_Reflection_Assembly_GetType (MonoReflectionAssembly *assembly, MonoString *name, MonoBoolean throwOnError, MonoBoolean ignoreCase)
1830 {
1831         gchar *str;
1832         MonoType *type;
1833         MonoTypeNameParse info;
1834
1835         str = mono_string_to_utf8 (name);
1836         /*g_print ("requested type %s in %s\n", str, assembly->assembly->aname.name);*/
1837         if (!mono_reflection_parse_type (str, &info)) {
1838                 g_free (str);
1839                 g_list_free (info.modifiers);
1840                 g_list_free (info.nested);
1841                 if (throwOnError) /* uhm: this is a parse error, though... */
1842                         mono_raise_exception (mono_get_exception_type_load ());
1843                 /*g_print ("failed parse\n");*/
1844                 return NULL;
1845         }
1846
1847         type = mono_reflection_get_type (assembly->assembly->image, &info, ignoreCase);
1848         g_free (str);
1849         g_list_free (info.modifiers);
1850         g_list_free (info.nested);
1851         if (!type) {
1852                 if (throwOnError)
1853                         mono_raise_exception (mono_get_exception_type_load ());
1854                 /* g_print ("failed find\n"); */
1855                 return NULL;
1856         }
1857         /* g_print ("got it\n"); */
1858         return mono_type_get_object (mono_object_domain (assembly), type);
1859
1860 }
1861
1862 static MonoString *
1863 ves_icall_System_Reflection_Assembly_get_code_base (MonoReflectionAssembly *assembly)
1864 {
1865         MonoDomain *domain = mono_object_domain (assembly); 
1866         MonoString *res;
1867         char *name = g_strconcat (
1868                 "file://", assembly->assembly->image->name, NULL);
1869         
1870         res = mono_string_new (domain, name);
1871         g_free (name);
1872         return res;
1873 }
1874
1875 static MonoString *
1876 ves_icall_System_Reflection_Assembly_get_location (MonoReflectionAssembly *assembly)
1877 {
1878         MonoDomain *domain = mono_object_domain (assembly); 
1879         MonoString *res;
1880         char *name = g_strconcat (
1881                 assembly->assembly->basedir, G_DIR_SEPARATOR_S,
1882                 assembly->assembly->image->module_name, NULL);
1883
1884         res = mono_string_new (domain, name);
1885         g_free (name);
1886         return res;
1887 }
1888
1889 static MonoReflectionMethod*
1890 ves_icall_System_Reflection_Assembly_get_EntryPoint (MonoReflectionAssembly *assembly) {
1891         guint32 token = mono_image_get_entry_point (assembly->assembly->image);
1892         if (!token)
1893                 return NULL;
1894         return mono_method_get_object (mono_object_domain (assembly), mono_get_method (assembly->assembly->image, token, NULL), NULL);
1895 }
1896
1897 static MonoArray*
1898 ves_icall_System_Reflection_Assembly_GetManifestResourceNames (MonoReflectionAssembly *assembly) {
1899         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
1900         MonoArray *result = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, table->rows);
1901         int i;
1902         const char *val;
1903
1904         for (i = 0; i < table->rows; ++i) {
1905                 val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_MANIFEST_NAME));
1906                 mono_array_set (result, gpointer, i, mono_string_new (mono_object_domain (assembly), val));
1907         }
1908         return result;
1909 }
1910
1911 /* move this in some file in mono/util/ */
1912 static char *
1913 g_concat_dir_and_file (const char *dir, const char *file)
1914 {
1915         g_return_val_if_fail (dir != NULL, NULL);
1916         g_return_val_if_fail (file != NULL, NULL);
1917
1918         /*
1919          * If the directory name doesn't have a / on the end, we need
1920          * to add one so we get a proper path to the file
1921          */
1922         if (dir [strlen(dir) - 1] != G_DIR_SEPARATOR)
1923                 return g_strconcat (dir, G_DIR_SEPARATOR_S, file, NULL);
1924         else
1925                 return g_strconcat (dir, file, NULL);
1926 }
1927
1928 static MonoObject*
1929 ves_icall_System_Reflection_Assembly_GetManifestResourceInternal (MonoReflectionAssembly *assembly, MonoString *name) {
1930         char *n = mono_string_to_utf8 (name);
1931         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_MANIFESTRESOURCE];
1932         guint32 i;
1933         guint32 cols [MONO_MANIFEST_SIZE];
1934         const char *val;
1935         MonoObject *result;
1936
1937         for (i = 0; i < table->rows; ++i) {
1938                 mono_metadata_decode_row (table, i, cols, MONO_MANIFEST_SIZE);
1939                 val = mono_metadata_string_heap (assembly->assembly->image, cols [MONO_MANIFEST_NAME]);
1940                 if (strcmp (val, n) == 0)
1941                         break;
1942         }
1943         g_free (n);
1944         if (i == table->rows)
1945                 return NULL;
1946         /* FIXME */
1947         if (!cols [MONO_MANIFEST_IMPLEMENTATION]) {
1948                 guint32 size;
1949                 MonoArray *data;
1950                 val = mono_image_get_resource (assembly->assembly->image, cols [MONO_MANIFEST_OFFSET], &size);
1951                 if (!val)
1952                         return NULL;
1953                 data = mono_array_new (mono_object_domain (assembly), mono_defaults.byte_class, size);
1954                 memcpy (mono_array_addr (data, char, 0), val, size);
1955                 return (MonoObject*)data;
1956         }
1957         switch (cols [MONO_MANIFEST_IMPLEMENTATION] & IMPLEMENTATION_MASK) {
1958         case IMPLEMENTATION_FILE:
1959                 i = cols [MONO_MANIFEST_IMPLEMENTATION] >> IMPLEMENTATION_BITS;
1960                 table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
1961                 i = mono_metadata_decode_row_col (table, i - 1, MONO_FILE_NAME);
1962                 val = mono_metadata_string_heap (assembly->assembly->image, i);
1963                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
1964                 result = (MonoObject*)mono_string_new (mono_object_domain (assembly), n);
1965                 /* check hash if needed */
1966                 g_free (n);
1967                 return result;
1968         case IMPLEMENTATION_ASSEMBLYREF:
1969         case IMPLEMENTATION_EXP_TYPE:
1970                 /* FIXME */
1971                 break;
1972         }
1973         return NULL;
1974 }
1975
1976 static MonoObject*
1977 ves_icall_System_Reflection_Assembly_GetFilesInternal (MonoReflectionAssembly *assembly, MonoString *name) {
1978         MonoTableInfo *table = &assembly->assembly->image->tables [MONO_TABLE_FILE];
1979         MonoArray *result;
1980         int i;
1981         const char *val;
1982         char *n;
1983
1984         /* check hash if needed */
1985         if (name) {
1986                 n = mono_string_to_utf8 (name);
1987                 for (i = 0; i < table->rows; ++i) {
1988                         val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
1989                         if (strcmp (val, n) == 0) {
1990                                 MonoString *fn;
1991                                 g_free (n);
1992                                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
1993                                 fn = mono_string_new (mono_object_domain (assembly), n);
1994                                 g_free (n);
1995                                 return (MonoObject*)fn;
1996                         }
1997                 }
1998                 g_free (n);
1999                 return NULL;
2000         }
2001
2002         for (i = 0; i < table->rows; ++i) {
2003                 result = mono_array_new (mono_object_domain (assembly), mono_defaults.string_class, table->rows);
2004                 val = mono_metadata_string_heap (assembly->assembly->image, mono_metadata_decode_row_col (table, i, MONO_FILE_NAME));
2005                 n = g_concat_dir_and_file (assembly->assembly->basedir, val);
2006                 mono_array_set (result, gpointer, i, mono_string_new (mono_object_domain (assembly), n));
2007                 g_free (n);
2008         }
2009         return (MonoObject*)result;
2010 }
2011
2012 static MonoReflectionMethod*
2013 ves_icall_GetCurrentMethod (void) {
2014         MonoMethod *m = mono_method_get_last_managed ();
2015         return mono_method_get_object (mono_domain_get (), m, NULL);
2016 }
2017
2018 static MonoReflectionAssembly*
2019 ves_icall_System_Reflection_Assembly_GetExecutingAssembly (void)
2020 {
2021         MonoMethod *m = mono_method_get_last_managed ();
2022         return mono_assembly_get_object (mono_domain_get (), m->klass->image->assembly);
2023 }
2024
2025
2026 static gboolean
2027 get_caller (MonoMethod *m, gint32 no, gint32 ilo, gpointer data)
2028 {
2029         MonoMethod **dest = data;
2030         if (m == *dest) {
2031                 *dest = NULL;
2032                 return FALSE;
2033         }
2034         if (!(*dest)) {
2035                 *dest = m;
2036                 return TRUE;
2037         }
2038         return FALSE;
2039 }
2040
2041 static MonoReflectionAssembly*
2042 ves_icall_System_Reflection_Assembly_GetEntryAssembly (void)
2043 {
2044         MonoDomain* domain = mono_domain_get ();
2045         g_assert (domain->entry_assembly);
2046         return mono_assembly_get_object (domain, domain->entry_assembly);
2047 }
2048
2049
2050 static MonoReflectionAssembly*
2051 ves_icall_System_Reflection_Assembly_GetCallingAssembly (void)
2052 {
2053         MonoMethod *m = mono_method_get_last_managed ();
2054         MonoMethod *dest = m;
2055         mono_stack_walk (get_caller, &dest);
2056         if (!dest)
2057                 dest = m;
2058         return mono_assembly_get_object (mono_domain_get (), dest->klass->image->assembly);
2059 }
2060
2061 static MonoString *
2062 ves_icall_System_MonoType_getFullName (MonoReflectionType *object)
2063 {
2064         MonoDomain *domain = mono_object_domain (object); 
2065         MonoString *res;
2066         gchar *name;
2067
2068         name = mono_type_get_name (object->type);
2069         res = mono_string_new (domain, name);
2070         g_free (name);
2071
2072         return res;
2073 }
2074
2075 static void
2076 ves_icall_System_Reflection_Assembly_FillName (MonoReflectionAssembly *assembly, MonoReflectionAssemblyName *aname)
2077 {
2078         MonoAssemblyName *name = &assembly->assembly->aname;
2079
2080         if (strcmp (name->name, "corlib") == 0)
2081                 aname->name = mono_string_new (mono_object_domain (assembly), "mscorlib");
2082         else
2083                 aname->name = mono_string_new (mono_object_domain (assembly), name->name);
2084         aname->major = name->major;
2085 }
2086
2087 static MonoArray*
2088 ves_icall_System_Reflection_Assembly_GetTypes (MonoReflectionAssembly *assembly, MonoBoolean exportedOnly)
2089 {
2090         MonoDomain *domain = mono_object_domain (assembly); 
2091         MonoArray *res;
2092         MonoClass *klass;
2093         MonoTableInfo *tdef = &assembly->assembly->image->tables [MONO_TABLE_TYPEDEF];
2094         int i, count;
2095         guint32 attrs, visibility;
2096
2097         /* we start the count from 1 because we skip the special type <Module> */
2098         if (exportedOnly) {
2099                 count = 0;
2100                 for (i = 1; i < tdef->rows; ++i) {
2101                         attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
2102                         visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2103                         if (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)
2104                                 count++;
2105                 }
2106         } else {
2107                 count = tdef->rows - 1;
2108         }
2109         res = mono_array_new (domain, mono_defaults.monotype_class, count);
2110         count = 0;
2111         for (i = 1; i < tdef->rows; ++i) {
2112                 attrs = mono_metadata_decode_row_col (tdef, i, MONO_TYPEDEF_FLAGS);
2113                 visibility = attrs & TYPE_ATTRIBUTE_VISIBILITY_MASK;
2114                 if (!exportedOnly || (visibility == TYPE_ATTRIBUTE_PUBLIC || visibility == TYPE_ATTRIBUTE_NESTED_PUBLIC)) {
2115                         klass = mono_class_get (assembly->assembly->image, (i + 1) | MONO_TOKEN_TYPE_DEF);
2116                         mono_array_set (res, gpointer, count, mono_type_get_object (domain, &klass->byval_arg));
2117                         count++;
2118                 }
2119         }
2120         
2121         return res;
2122 }
2123
2124 static MonoReflectionType*
2125 ves_icall_ModuleBuilder_create_modified_type (MonoReflectionTypeBuilder *tb, MonoString *smodifiers)
2126 {
2127         MonoClass *klass;
2128         int isbyref = 0, rank;
2129         char *str = mono_string_to_utf8 (smodifiers);
2130         char *p;
2131
2132         klass = mono_class_from_mono_type (tb->type.type);
2133         p = str;
2134         /* logic taken from mono_reflection_parse_type(): keep in sync */
2135         while (*p) {
2136                 switch (*p) {
2137                 case '&':
2138                         if (isbyref) { /* only one level allowed by the spec */
2139                                 g_free (str);
2140                                 return NULL;
2141                         }
2142                         isbyref = 1;
2143                         p++;
2144                         g_free (str);
2145                         return mono_type_get_object (mono_object_domain (tb), &klass->this_arg);
2146                         break;
2147                 case '*':
2148                         klass = mono_ptr_class_get (&klass->byval_arg);
2149                         mono_class_init (klass);
2150                         p++;
2151                         break;
2152                 case '[':
2153                         rank = 1;
2154                         p++;
2155                         while (*p) {
2156                                 if (*p == ']')
2157                                         break;
2158                                 if (*p == ',')
2159                                         rank++;
2160                                 else if (*p != '*') { /* '*' means unknown lower bound */
2161                                         g_free (str);
2162                                         return NULL;
2163                                 }
2164                                 ++p;
2165                         }
2166                         if (*p != ']') {
2167                                 g_free (str);
2168                                 return NULL;
2169                         }
2170                         p++;
2171                         klass = mono_array_class_get (&klass->byval_arg, rank);
2172                         mono_class_init (klass);
2173                         break;
2174                 default:
2175                         break;
2176                 }
2177         }
2178         g_free (str);
2179         return mono_type_get_object (mono_object_domain (tb), &klass->byval_arg);
2180 }
2181
2182 static MonoObject *
2183 ves_icall_System_Delegate_CreateDelegate_internal (MonoReflectionType *type, MonoObject *target,
2184                                                    MonoReflectionMethod *info)
2185 {
2186         MonoClass *delegate_class = mono_class_from_mono_type (type->type);
2187         MonoObject *delegate;
2188         gpointer func;
2189
2190         mono_assert (delegate_class->parent == mono_defaults.multicastdelegate_class);
2191
2192         delegate = mono_object_new (mono_object_domain (type), delegate_class);
2193
2194         func = mono_compile_method (info->method);
2195
2196         mono_delegate_ctor (delegate, target, func);
2197
2198         return delegate;
2199 }
2200
2201 /*
2202  * Magic number to convert a time which is relative to
2203  * Jan 1, 1970 into a value which is relative to Jan 1, 0001.
2204  */
2205 #define EPOCH_ADJUST    ((gint64)62135596800L)
2206
2207 static gint64
2208 ves_icall_System_DateTime_GetNow (void)
2209 {
2210 #ifdef PLATFORM_WIN32
2211         SYSTEMTIME st;
2212         FILETIME ft;
2213         
2214         GetLocalTime (&st);
2215         SystemTimeToFileTime (&st, &ft);
2216         return (gint64)504911232000000000L + ((((gint64)ft.dwHighDateTime)<<32) | ft.dwLowDateTime);
2217 #else
2218         /* FIXME: put this in io-layer and call it GetLocalTime */
2219         struct timeval tv;
2220         gint64 res;
2221
2222         if (gettimeofday (&tv, NULL) == 0) {
2223                 res = (((gint64)tv.tv_sec + EPOCH_ADJUST)* 1000000 + tv.tv_usec)*10;
2224                 return res;
2225         }
2226         /* fixme: raise exception */
2227         return 0;
2228 #endif
2229 }
2230
2231 /*
2232  * This is heavily based on zdump.c from glibc 2.2.
2233  *
2234  *  * data[0]:  start of daylight saving time (in DateTime ticks).
2235  *  * data[1]:  end of daylight saving time (in DateTime ticks).
2236  *  * data[2]:  utcoffset (in TimeSpan ticks).
2237  *  * data[3]:  additional offset when daylight saving (in TimeSpan ticks).
2238  *  * name[0]:  name of this timezone when not daylight saving.
2239  *  * name[1]:  name of this timezone when daylight saving.
2240  *
2241  *  FIXME: This only works with "standard" Unix dates (years between 1900 and 2100) while
2242  *         the class library allows years between 1 and 9999.
2243  *
2244  *  Returns true on success and zero on failure.
2245  */
2246 static guint32
2247 ves_icall_System_CurrentTimeZone_GetTimeZoneData (guint32 year, MonoArray **data, MonoArray **names)
2248 {
2249 #ifndef PLATFORM_WIN32
2250         MonoDomain *domain = mono_domain_get ();
2251         struct tm start, tt;
2252         time_t t;
2253
2254         long int gmtoff;
2255         int is_daylight = 0, day;
2256
2257         memset (&start, 0, sizeof (start));
2258
2259         start.tm_mday = 1;
2260         start.tm_year = year-1900;
2261
2262         t = mktime (&start);
2263 #if defined (HAVE_TIMEZONE)
2264 #define gmt_offset(x) (-1 * (((timezone / 60 / 60) - daylight) * 100))
2265 #elif defined (HAVE_TM_GMTOFF)
2266 #define gmt_offset(x) x.tm_gmtoff
2267 #else
2268 #error Neither HAVE_TIMEZONE nor HAVE_TM_GMTOFF defined. Rerun autoheader, autoconf, etc.
2269 #endif
2270         
2271         gmtoff = gmt_offset (start);
2272         
2273         MONO_CHECK_ARG_NULL (data);
2274         MONO_CHECK_ARG_NULL (names);
2275
2276         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
2277         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
2278
2279         /* For each day of the year, calculate the tm_gmtoff. */
2280         for (day = 0; day < 365; day++) {
2281
2282                 t += 3600*24;
2283                 tt = *localtime (&t);
2284
2285                 /* Daylight saving starts or ends here. */
2286                 if (gmt_offset (tt) != gmtoff) {
2287                         char tzone[10];
2288                         struct tm tt1;
2289                         time_t t1;
2290
2291                         /* Try to find the exact hour when daylight saving starts/ends. */
2292                         t1 = t;
2293                         do {
2294                                 t1 -= 3600;
2295                                 tt1 = *localtime (&t1);
2296                         } while (gmt_offset (tt1) != gmtoff);
2297
2298                         /* Try to find the exact minute when daylight saving starts/ends. */
2299                         do {
2300                                 t1 += 60;
2301                                 tt1 = *localtime (&t1);
2302                         } while (gmt_offset (tt1) == gmtoff);
2303                         
2304                         strftime (tzone, 10, "%Z", &tt);
2305                         
2306                         /* Write data, if we're already in daylight saving, we're done. */
2307                         if (is_daylight) {
2308                                 mono_array_set ((*names), gpointer, 0, mono_string_new (domain, tzone));
2309                                 mono_array_set ((*data), gint64, 1, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
2310                                 return 1;
2311                         } else {
2312                                 mono_array_set ((*names), gpointer, 1, mono_string_new (domain, tzone));
2313                                 mono_array_set ((*data), gint64, 0, ((gint64)t1 + EPOCH_ADJUST) * 10000000L);
2314                                 is_daylight = 1;
2315                         }
2316
2317                         /* This is only set once when we enter daylight saving. */
2318                         mono_array_set ((*data), gint64, 2, (gint64)gmtoff * 10000000L);
2319                         mono_array_set ((*data), gint64, 3, (gint64)(gmt_offset (tt) - gmtoff) * 10000000L);
2320
2321                         gmtoff = gmt_offset (tt);
2322                 }
2323
2324                 gmtoff = gmt_offset (tt);
2325         }
2326         return 1;
2327 #else
2328         MonoDomain *domain = mono_domain_get ();
2329         TIME_ZONE_INFORMATION tz_info;
2330         FILETIME ft;
2331         int i;
2332
2333         GetTimeZoneInformation (&tz_info);
2334
2335         MONO_CHECK_ARG_NULL (data);
2336         MONO_CHECK_ARG_NULL (names);
2337
2338         (*data) = mono_array_new (domain, mono_defaults.int64_class, 4);
2339         (*names) = mono_array_new (domain, mono_defaults.string_class, 2);
2340
2341         for (i = 0; i < 32; ++i)
2342                 if (!tz_info.DaylightName [i])
2343                         break;
2344         mono_array_set ((*names), gpointer, 1, mono_string_new_utf16 (domain, tz_info.DaylightName, i));
2345         for (i = 0; i < 32; ++i)
2346                 if (!tz_info.StandardName [i])
2347                         break;
2348         mono_array_set ((*names), gpointer, 0, mono_string_new_utf16 (domain, tz_info.StandardName, i));
2349
2350         SystemTimeToFileTime (&tz_info.StandardDate, &ft);
2351         mono_array_set ((*data), gint64, 1, ((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime);
2352         SystemTimeToFileTime (&tz_info.DaylightDate, &ft);
2353         mono_array_set ((*data), gint64, 0, ((guint64)ft.dwHighDateTime<<32) | ft.dwLowDateTime);
2354         mono_array_set ((*data), gint64, 3, tz_info.Bias + tz_info.StandardBias);
2355         mono_array_set ((*data), gint64, 2, tz_info.Bias + tz_info.DaylightBias);
2356
2357         return 1;
2358 #endif
2359 }
2360
2361 static gpointer
2362 ves_icall_System_Object_obj_address (MonoObject *this) {
2363         return this;
2364 }
2365
2366 /* System.Buffer */
2367
2368 static gint32 
2369 ves_icall_System_Buffer_ByteLengthInternal (MonoArray *array) {
2370         MonoClass *klass;
2371         MonoTypeEnum etype;
2372         int length, esize;
2373         int i;
2374
2375         klass = array->obj.vtable->klass;
2376         etype = klass->element_class->byval_arg.type;
2377         if (etype < MONO_TYPE_BOOLEAN || etype > MONO_TYPE_R8)
2378                 return -1;
2379
2380         if (array->bounds == NULL)
2381                 length = array->max_length;
2382         else {
2383                 length = 0;
2384                 for (i = 0; i < klass->rank; ++ i)
2385                         length += array->bounds [i].length;
2386         }
2387
2388         esize = mono_array_element_size (klass);
2389         return length * esize;
2390 }
2391
2392 static gint8 
2393 ves_icall_System_Buffer_GetByteInternal (MonoArray *array, gint32 idx) {
2394         return mono_array_get (array, gint8, idx);
2395 }
2396
2397 static void 
2398 ves_icall_System_Buffer_SetByteInternal (MonoArray *array, gint32 idx, gint8 value) {
2399         mono_array_set (array, gint8, idx, value);
2400 }
2401
2402 static void 
2403 ves_icall_System_Buffer_BlockCopyInternal (MonoArray *src, gint32 src_offset, MonoArray *dest, gint32 dest_offset, gint32 count) {
2404         char *src_buf, *dest_buf;
2405
2406         src_buf = (gint8 *)src->vector + src_offset;
2407         dest_buf = (gint8 *)dest->vector + dest_offset;
2408
2409         memcpy (dest_buf, src_buf, count);
2410 }
2411
2412 static MonoObject *
2413 ves_icall_Remoting_RealProxy_GetTransparentProxy (MonoObject *this)
2414 {
2415         MonoDomain *domain = mono_object_domain (this); 
2416         MonoObject *res;
2417         MonoRealProxy *rp = ((MonoRealProxy *)this);
2418         MonoType *type;
2419         MonoClass *klass;
2420
2421         res = mono_object_new (domain, mono_defaults.transparent_proxy_class);
2422         
2423         ((MonoTransparentProxy *)res)->rp = rp;
2424         type = ((MonoReflectionType *)rp->class_to_proxy)->type;
2425         klass = mono_class_from_mono_type (type);
2426
2427         ((MonoTransparentProxy *)res)->klass = klass;
2428
2429         res->vtable = mono_class_proxy_vtable (domain, klass);
2430
2431         return res;
2432 }
2433
2434 /* System.Environment */
2435
2436 static MonoString *
2437 ves_icall_System_Environment_get_MachineName (void)
2438 {
2439 #if defined (PLATFORM_WIN32)
2440         gunichar2 *buf;
2441         guint32 len;
2442         MonoString *result;
2443
2444         len = MAX_COMPUTERNAME_LENGTH + 1;
2445         buf = g_new (gunichar2, len);
2446
2447         result = NULL;
2448         if (GetComputerName (buf, (PDWORD) &len))
2449                 result = mono_string_new_utf16 (mono_domain_get (), buf, len);
2450
2451         g_free (buf);
2452         return result;
2453 #else
2454         gchar *buf;
2455         int len;
2456         MonoString *result;
2457
2458         len = 256;
2459         buf = g_new (gchar, len);
2460
2461         result = NULL;
2462         if (gethostname (buf, len) != 0)
2463                 result = mono_string_new (mono_domain_get (), buf);
2464         
2465         g_free (buf);
2466         return result;
2467 #endif
2468 }
2469
2470 static int
2471 ves_icall_System_Environment_get_Platform (void)
2472 {
2473 #if defined (PLATFORM_WIN32)
2474         /* Win32NT */
2475         return 2;
2476 #else
2477         /* Unix */
2478         return 128;
2479 #endif
2480 }
2481
2482 static MonoString *
2483 ves_icall_System_Environment_get_NewLine (void)
2484 {
2485 #if defined (PLATFORM_WIN32)
2486         return mono_string_new (mono_domain_get (), "\r\n");
2487 #else
2488         return mono_string_new (mono_domain_get (), "\n");
2489 #endif
2490 }
2491
2492 static MonoString *
2493 ves_icall_System_Environment_GetEnvironmentVariable (MonoString *name)
2494 {
2495         const gchar *value;
2496         gchar *utf8_name;
2497
2498         if (name == NULL)
2499                 return NULL;
2500
2501         utf8_name = mono_string_to_utf8 (name); /* FIXME: this should be ascii */
2502         value = g_getenv (utf8_name);
2503         g_free (utf8_name);
2504
2505         if (value == 0)
2506                 return NULL;
2507         
2508         return mono_string_new (mono_domain_get (), value);
2509 }
2510
2511 /*
2512  * There is no standard way to get at environ.
2513  */
2514 extern char **environ;
2515
2516 static MonoArray *
2517 ves_icall_System_Environment_GetEnvironmentVariableNames (void)
2518 {
2519         MonoArray *names;
2520         MonoDomain *domain;
2521         MonoString *str;
2522         gchar **e, **parts;
2523         int n;
2524
2525         n = 0;
2526         for (e = environ; *e != 0; ++ e)
2527                 ++ n;
2528
2529         domain = mono_domain_get ();
2530         names = mono_array_new (domain, mono_defaults.string_class, n);
2531
2532         n = 0;
2533         for (e = environ; *e != 0; ++ e) {
2534                 parts = g_strsplit (*e, "=", 2);
2535                 if (*parts != 0) {
2536                         str = mono_string_new (domain, *parts);
2537                         mono_array_set (names, MonoString *, n, str);
2538                 }
2539
2540                 g_strfreev (parts);
2541
2542                 ++ n;
2543         }
2544
2545         return names;
2546 }
2547
2548 /*
2549  * Returns the number of milliseconds elapsed since the system started.
2550  */
2551 static gint32
2552 ves_icall_System_Environment_get_TickCount (void)
2553 {
2554 #if defined (PLATFORM_WIN32)
2555         return GetTickCount();
2556 #else
2557         struct timeval tv;
2558         struct timezone tz;
2559         gint32 res;
2560
2561         res = (gint32) gettimeofday (&tv, &tz);
2562
2563         if (res != -1)
2564                 res = (gint32) ((tv.tv_sec & 0xFFFFF) * 1000 + (tv.tv_usec / 1000));
2565         return res;
2566 #endif
2567 }
2568
2569
2570 static void
2571 ves_icall_System_Environment_Exit (int result)
2572 {
2573         /* we may need to do some cleanup here... */
2574         exit (result);
2575 }
2576
2577 static MonoString*
2578 ves_icall_System_Text_Encoding_InternalCodePage (void) {
2579         const char *cset;
2580         g_get_charset (&cset);
2581         /* g_print ("charset: %s\n", cset); */
2582         /* handle some common aliases */
2583         switch (*cset) {
2584         case 'A':
2585                 if (strcmp (cset, "ANSI_X3.4-1968") == 0)
2586                         cset = "us-ascii";
2587                 break;
2588         }
2589         return mono_string_new (mono_domain_get (), cset);
2590 }
2591
2592 static void
2593 ves_icall_MonoMethodMessage_InitMessage (MonoMethodMessage *this, 
2594                                          MonoReflectionMethod *method,
2595                                          MonoArray *out_args)
2596 {
2597         mono_message_init (mono_object_domain (this), this, method, out_args);
2598 }
2599
2600 static MonoBoolean
2601 ves_icall_IsTransparentProxy (MonoObject *proxy)
2602 {
2603         if (!proxy)
2604                 return 0;
2605
2606         if (proxy->vtable->klass == mono_defaults.transparent_proxy_class)
2607                 return 1;
2608
2609         return 0;
2610 }
2611
2612 static MonoObject *
2613 ves_icall_System_Runtime_Serialization_FormatterServices_GetUninitializedObject_Internal (MonoReflectionType *type)
2614 {
2615         MonoClass *klass;
2616         MonoObject *obj;
2617         MonoDomain *domain;
2618         
2619         domain = mono_object_domain (type);
2620         klass = mono_class_from_mono_type (type->type);
2621
2622         if (klass->rank >= 1) {
2623                 g_assert (klass->rank == 1);
2624                 obj = (MonoObject *) mono_array_new (domain, klass->element_class, 0);
2625         } else {
2626                 obj = mono_object_new (domain, klass);
2627         }
2628
2629         return obj;
2630 }
2631
2632 static MonoString *
2633 ves_icall_System_IO_get_temp_path (void)
2634 {
2635         return mono_string_new (mono_domain_get (), g_get_tmp_dir ());
2636 }
2637
2638 static gpointer
2639 ves_icall_RuntimeMethod_GetFunctionPointer (MonoMethod *method)
2640 {
2641         return mono_compile_method (method);
2642 }
2643
2644 /* icall map */
2645
2646 static gconstpointer icall_map [] = {
2647         /*
2648          * System.Array
2649          */
2650         "System.Array::GetValue",         ves_icall_System_Array_GetValue,
2651         "System.Array::SetValue",         ves_icall_System_Array_SetValue,
2652         "System.Array::GetValueImpl",     ves_icall_System_Array_GetValueImpl,
2653         "System.Array::SetValueImpl",     ves_icall_System_Array_SetValueImpl,
2654         "System.Array::GetRank",          ves_icall_System_Array_GetRank,
2655         "System.Array::GetLength",        ves_icall_System_Array_GetLength,
2656         "System.Array::GetLowerBound",    ves_icall_System_Array_GetLowerBound,
2657         "System.Array::CreateInstanceImpl",   ves_icall_System_Array_CreateInstanceImpl,
2658         "System.Array::FastCopy",         ves_icall_System_Array_FastCopy,
2659         "System.Array::Clone",            mono_array_clone,
2660
2661         /*
2662          * System.Object
2663          */
2664         "System.Object::MemberwiseClone", ves_icall_System_Object_MemberwiseClone,
2665         "System.Object::GetType", ves_icall_System_Object_GetType,
2666         "System.Object::GetHashCode", ves_icall_System_Object_GetHashCode,
2667         "System.Object::obj_address", ves_icall_System_Object_obj_address,
2668
2669         /*
2670          * System.ValueType
2671          */
2672         "System.ValueType::GetHashCode", ves_icall_System_ValueType_GetHashCode,
2673         "System.ValueType::Equals", ves_icall_System_ValueType_Equals,
2674
2675         /*
2676          * System.String
2677          */
2678         
2679         "System.String::.ctor(char*)", ves_icall_System_String_ctor_charp,
2680         "System.String::.ctor(char*,int,int)", ves_icall_System_String_ctor_charp_int_int,
2681         "System.String::.ctor(sbyte*)", ves_icall_System_String_ctor_sbytep,
2682         "System.String::.ctor(sbyte*,int,int)", ves_icall_System_String_ctor_sbytep_int_int,
2683         "System.String::.ctor(sbyte*,int,int,System.Text.Encoding)", ves_icall_System_String_ctor_encoding,
2684         "System.String::.ctor(char[])", ves_icall_System_String_ctor_chara,
2685         "System.String::.ctor(char[],int,int)", ves_icall_System_String_ctor_chara_int_int,
2686         "System.String::.ctor(char,int)", ves_icall_System_String_ctor_char_int,
2687         "System.String::InternalEquals", ves_icall_System_String_InternalEquals,
2688         "System.String::InternalJoin", ves_icall_System_String_InternalJoin,
2689         "System.String::InternalInsert", ves_icall_System_String_InternalInsert,
2690         "System.String::InternalReplace(char,char)", ves_icall_System_String_InternalReplace_Char,
2691         "System.String::InternalReplace(string,string)", ves_icall_System_String_InternalReplace_Str,
2692         "System.String::InternalRemove", ves_icall_System_String_InternalRemove,
2693         "System.String::InternalCopyTo", ves_icall_System_String_InternalCopyTo,
2694         "System.String::InternalSplit", ves_icall_System_String_InternalSplit,
2695         "System.String::InternalTrim", ves_icall_System_String_InternalTrim,
2696         "System.String::InternalIndexOf(char,int,int)", ves_icall_System_String_InternalIndexOf_Char,
2697         "System.String::InternalIndexOf(string,int,int)", ves_icall_System_String_InternalIndexOf_Str,
2698         "System.String::InternalIndexOfAny", ves_icall_System_String_InternalIndexOfAny,
2699         "System.String::InternalLastIndexOf(char,int,int)", ves_icall_System_String_InternalLastIndexOf_Char,
2700         "System.String::InternalLastIndexOf(string,int,int)", ves_icall_System_String_InternalLastIndexOf_Str,
2701         "System.String::InternalLastIndexOfAny", ves_icall_System_String_InternalLastIndexOfAny,
2702         "System.String::InternalPad", ves_icall_System_String_InternalPad,
2703         "System.String::InternalToLower", ves_icall_System_String_InternalToLower,
2704         "System.String::InternalToUpper", ves_icall_System_String_InternalToUpper,
2705         "System.String::InternalAllocateStr", ves_icall_System_String_InternalAllocateStr,
2706         "System.String::InternalStrcpy(string,int,string)", ves_icall_System_String_InternalStrcpy_Str,
2707         "System.String::InternalStrcpy(string,int,string,int,int)", ves_icall_System_String_InternalStrcpy_StrN,
2708         "System.String::InternalIntern", ves_icall_System_String_InternalIntern,
2709         "System.String::InternalIsInterned", ves_icall_System_String_InternalIsInterned,
2710         "System.String::InternalCompare(string,int,string,int,int,bool)", ves_icall_System_String_InternalCompareStr_N,
2711         "System.String::GetHashCode", ves_icall_System_String_GetHashCode,
2712         "System.String::get_Chars", ves_icall_System_String_get_Chars,
2713
2714         /*
2715          * System.AppDomain
2716          */
2717         "System.AppDomain::createDomain", ves_icall_System_AppDomain_createDomain,
2718         "System.AppDomain::getCurDomain", ves_icall_System_AppDomain_getCurDomain,
2719         "System.AppDomain::GetData", ves_icall_System_AppDomain_GetData,
2720         "System.AppDomain::SetData", ves_icall_System_AppDomain_SetData,
2721         "System.AppDomain::getSetup", ves_icall_System_AppDomain_getSetup,
2722         "System.AppDomain::getFriendlyName", ves_icall_System_AppDomain_getFriendlyName,
2723         "System.AppDomain::GetAssemblies", ves_icall_System_AppDomain_GetAssemblies,
2724         "System.AppDomain::LoadAssembly", ves_icall_System_AppDomain_LoadAssembly,
2725         "System.AppDomain::Unload", ves_icall_System_AppDomain_Unload,
2726         "System.AppDomain::ExecuteAssembly", ves_icall_System_AppDomain_ExecuteAssembly,
2727
2728         /*
2729          * System.AppDomainSetup
2730          */
2731         "System.AppDomainSetup::InitAppDomainSetup", ves_icall_System_AppDomainSetup_InitAppDomainSetup,
2732
2733         /*
2734          * System.Double
2735          */
2736         "System.Double::ToStringImpl", mono_double_ToStringImpl,
2737         "System.Double::ParseImpl",    mono_double_ParseImpl,
2738
2739         /*
2740          * System.Single
2741          */
2742         "System.Single::ToStringImpl", mono_float_ToStringImpl,
2743
2744         /*
2745          * System.Decimal
2746          */
2747         "System.Decimal::decimal2UInt64", mono_decimal2UInt64,
2748         "System.Decimal::decimal2Int64", mono_decimal2Int64,
2749         "System.Decimal::double2decimal", mono_double2decimal, /* FIXME: wrong signature. */
2750         "System.Decimal::decimalIncr", mono_decimalIncr,
2751         "System.Decimal::decimalSetExponent", mono_decimalSetExponent,
2752         "System.Decimal::decimal2double", mono_decimal2double,
2753         "System.Decimal::decimalFloorAndTrunc", mono_decimalFloorAndTrunc,
2754         "System.Decimal::decimalRound", mono_decimalRound,
2755         "System.Decimal::decimalMult", mono_decimalMult,
2756         "System.Decimal::decimalDiv", mono_decimalDiv,
2757         "System.Decimal::decimalIntDiv", mono_decimalIntDiv,
2758         "System.Decimal::decimalCompare", mono_decimalCompare,
2759         "System.Decimal::string2decimal", mono_string2decimal,
2760         "System.Decimal::decimal2string", mono_decimal2string,
2761
2762         /*
2763          * ModuleBuilder
2764          */
2765         "System.Reflection.Emit.ModuleBuilder::create_modified_type", ves_icall_ModuleBuilder_create_modified_type,
2766         
2767         /*
2768          * AssemblyBuilder
2769          */
2770         "System.Reflection.Emit.AssemblyBuilder::getDataChunk", ves_icall_AssemblyBuilder_getDataChunk,
2771         "System.Reflection.Emit.AssemblyBuilder::getUSIndex", mono_image_insert_string,
2772         "System.Reflection.Emit.AssemblyBuilder::getToken", ves_icall_AssemblyBuilder_getToken,
2773         "System.Reflection.Emit.AssemblyBuilder::basic_init", mono_image_basic_init,
2774
2775         /*
2776          * Reflection stuff.
2777          */
2778         "System.Reflection.MonoMethodInfo::get_method_info", ves_icall_get_method_info,
2779         "System.Reflection.MonoMethodInfo::get_parameter_info", ves_icall_get_parameter_info,
2780         "System.Reflection.MonoFieldInfo::get_field_info", ves_icall_get_field_info,
2781         "System.Reflection.MonoPropertyInfo::get_property_info", ves_icall_get_property_info,
2782         "System.Reflection.MonoEventInfo::get_event_info", ves_icall_get_event_info,
2783         "System.Reflection.MonoMethod::InternalInvoke", ves_icall_InternalInvoke,
2784         "System.Reflection.MonoCMethod::InternalInvoke", ves_icall_InternalInvoke,
2785         "System.Reflection.MethodBase::GetCurrentMethod", ves_icall_GetCurrentMethod,
2786         "System.MonoCustomAttrs::GetCustomAttributes", mono_reflection_get_custom_attrs,
2787         "System.Reflection.Emit.CustomAttributeBuilder::GetBlob", mono_reflection_get_custom_attrs_blob,
2788         "System.Reflection.MonoField::GetValueInternal", ves_icall_MonoField_GetValueInternal,
2789         "System.Reflection.MonoField::SetValueInternal", ves_icall_FieldInfo_SetValueInternal,
2790         "System.Reflection.Emit.SignatureHelper::get_signature_local", mono_reflection_sighelper_get_signature_local,
2791         "System.Reflection.Emit.SignatureHelper::get_signature_field", mono_reflection_sighelper_get_signature_field,
2792
2793         "System.RuntimeMethodHandle::GetFunctionPointer", ves_icall_RuntimeMethod_GetFunctionPointer,
2794         
2795         /* System.Enum */
2796
2797         "System.MonoEnumInfo::get_enum_info", ves_icall_get_enum_info,
2798         "System.Enum::get_value", ves_icall_System_Enum_get_value,
2799         "System.Enum::ToObject", ves_icall_System_Enum_ToObject,
2800
2801         /*
2802          * TypeBuilder
2803          */
2804         "System.Reflection.Emit.TypeBuilder::setup_internal_class", mono_reflection_setup_internal_class,
2805         "System.Reflection.Emit.TypeBuilder::create_internal_class", mono_reflection_create_internal_class,
2806         "System.Reflection.Emit.TypeBuilder::create_runtime_class", mono_reflection_create_runtime_class,
2807         
2808         /*
2809          * MethodBuilder
2810          */
2811         
2812         /*
2813          * System.Type
2814          */
2815         "System.Type::internal_from_name", ves_icall_type_from_name,
2816         "System.Type::internal_from_handle", ves_icall_type_from_handle,
2817         "System.Type::get_property", ves_icall_get_property,
2818         "System.MonoType::get_attributes", ves_icall_get_attributes,
2819         "System.Type::type_is_subtype_of", ves_icall_type_is_subtype_of,
2820         "System.Type::Equals", ves_icall_type_Equals,
2821         "System.Type::GetTypeCode", ves_icall_type_GetTypeCode,
2822
2823         /*
2824          * System.Runtime.CompilerServices.RuntimeHelpers
2825          */
2826         "System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_InitializeArray,
2827         "System.Runtime.CompilerServices.RuntimeHelpers::GetOffsetToStringData", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetOffsetToStringData,
2828         "System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_GetObjectValue,
2829         "System.Runtime.CompilerServices.RuntimeHelpers::RunClassConstructor", ves_icall_System_Runtime_CompilerServices_RuntimeHelpers_RunClassConstructor,
2830         
2831         /*
2832          * System.Threading
2833          */
2834         "System.Threading.Thread::Abort(object)", ves_icall_System_Threading_Thread_Abort,
2835         "System.Threading.Thread::ResetAbort", ves_icall_System_Threading_Thread_ResetAbort,
2836         "System.Threading.Thread::Thread_internal", ves_icall_System_Threading_Thread_Thread_internal,
2837         "System.Threading.Thread::Thread_free_internal", ves_icall_System_Threading_Thread_Thread_free_internal,
2838         "System.Threading.Thread::Start_internal", ves_icall_System_Threading_Thread_Start_internal,
2839         "System.Threading.Thread::Sleep_internal", ves_icall_System_Threading_Thread_Sleep_internal,
2840         "System.Threading.Thread::CurrentThread_internal", mono_thread_current,
2841         "System.Threading.Thread::CurrentThreadDomain_internal", ves_icall_System_Threading_Thread_CurrentThreadDomain_internal,
2842         "System.Threading.Thread::Join_internal", ves_icall_System_Threading_Thread_Join_internal,
2843         "System.Threading.Thread::SlotHash_lookup", ves_icall_System_Threading_Thread_SlotHash_lookup,
2844         "System.Threading.Thread::SlotHash_store", ves_icall_System_Threading_Thread_SlotHash_store,
2845         "System.Threading.Monitor::Monitor_exit", ves_icall_System_Threading_Monitor_Monitor_exit,
2846         "System.Threading.Monitor::Monitor_test_owner", ves_icall_System_Threading_Monitor_Monitor_test_owner,
2847         "System.Threading.Monitor::Monitor_test_synchronised", ves_icall_System_Threading_Monitor_Monitor_test_synchronised,
2848         "System.Threading.Monitor::Monitor_pulse", ves_icall_System_Threading_Monitor_Monitor_pulse,
2849         "System.Threading.Monitor::Monitor_pulse_all", ves_icall_System_Threading_Monitor_Monitor_pulse_all,
2850         "System.Threading.Monitor::Monitor_try_enter", ves_icall_System_Threading_Monitor_Monitor_try_enter,
2851         "System.Threading.Monitor::Monitor_wait", ves_icall_System_Threading_Monitor_Monitor_wait,
2852         "System.Threading.Mutex::CreateMutex_internal", ves_icall_System_Threading_Mutex_CreateMutex_internal,
2853         "System.Threading.Mutex::ReleaseMutex_internal", ves_icall_System_Threading_Mutex_ReleaseMutex_internal,
2854         "System.Threading.NativeEventCalls::CreateEvent_internal", ves_icall_System_Threading_Events_CreateEvent_internal,
2855         "System.Threading.NativeEventCalls::SetEvent_internal",    ves_icall_System_Threading_Events_SetEvent_internal,
2856         "System.Threading.NativeEventCalls::ResetEvent_internal",  ves_icall_System_Threading_Events_ResetEvent_internal,
2857
2858         /*
2859          * System.Threading.WaitHandle
2860          */
2861         "System.Threading.WaitHandle::WaitAll_internal", ves_icall_System_Threading_WaitHandle_WaitAll_internal,
2862         "System.Threading.WaitHandle::WaitAny_internal", ves_icall_System_Threading_WaitHandle_WaitAny_internal,
2863         "System.Threading.WaitHandle::WaitOne_internal", ves_icall_System_Threading_WaitHandle_WaitOne_internal,
2864
2865         /*
2866          * System.Runtime.InteropServices.Marshal
2867          */
2868         "System.Runtime.InteropServices.Marshal::ReadIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr,
2869         "System.Runtime.InteropServices.Marshal::ReadByte", ves_icall_System_Runtime_InteropServices_Marshal_ReadByte,
2870         "System.Runtime.InteropServices.Marshal::ReadInt16", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt16,
2871         "System.Runtime.InteropServices.Marshal::ReadInt32", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt32,
2872         "System.Runtime.InteropServices.Marshal::ReadInt64", ves_icall_System_Runtime_InteropServices_Marshal_ReadInt64,
2873         "System.Runtime.InteropServices.Marshal::WriteIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_WriteIntPtr,
2874         "System.Runtime.InteropServices.Marshal::WriteByte", ves_icall_System_Runtime_InteropServices_Marshal_WriteByte,
2875         "System.Runtime.InteropServices.Marshal::WriteInt16", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt16,
2876         "System.Runtime.InteropServices.Marshal::WriteInt32", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt32,
2877         "System.Runtime.InteropServices.Marshal::WriteInt64", ves_icall_System_Runtime_InteropServices_Marshal_WriteInt64,
2878
2879         "System.Runtime.InteropServices.Marshal::PtrToStringAnsi(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi,
2880         "System.Runtime.InteropServices.Marshal::PtrToStringAnsi(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len,
2881         "System.Runtime.InteropServices.Marshal::PtrToStringAuto(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi,
2882         "System.Runtime.InteropServices.Marshal::PtrToStringAuto(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAnsi_len,
2883         "System.Runtime.InteropServices.Marshal::PtrToStringUni(intptr)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni,
2884         "System.Runtime.InteropServices.Marshal::PtrToStringUni(intptr,int)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringUni_len,
2885         "System.Runtime.InteropServices.Marshal::PtrToStringBSTR", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringBSTR,
2886
2887         "System.Runtime.InteropServices.Marshal::GetLastWin32Error", ves_icall_System_Runtime_InteropServices_Marshal_GetLastWin32Error,
2888         "System.Runtime.InteropServices.Marshal::AllocHGlobal", mono_marshal_alloc,
2889         "System.Runtime.InteropServices.Marshal::FreeHGlobal", mono_marshal_free,
2890         "System.Runtime.InteropServices.Marshal::ReAllocHGlobal", mono_marshal_realloc,
2891         "System.Runtime.InteropServices.Marshal::copy_to_unmanaged", ves_icall_System_Runtime_InteropServices_Marshal_copy_to_unmanaged,
2892         "System.Runtime.InteropServices.Marshal::copy_from_unmanaged", ves_icall_System_Runtime_InteropServices_Marshal_copy_from_unmanaged,
2893         "System.Runtime.InteropServices.Marshal::SizeOf", ves_icall_System_Runtime_InteropServices_Marshal_SizeOf,
2894         "System.Runtime.InteropServices.Marshal::StructureToPtr", ves_icall_System_Runtime_InteropServices_Marshal_StructureToPtr,
2895         "System.Runtime.InteropServices.Marshal::PtrToStructure(intptr,object)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure,
2896         "System.Runtime.InteropServices.Marshal::PtrToStructure(intptr,System.Type)", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStructure_type,
2897         "System.Runtime.InteropServices.Marshal::OffsetOf", ves_icall_System_Runtime_InteropServices_Marshal_OffsetOf,
2898         "System.Runtime.InteropServices.Marshal::StringToHGlobalAnsi", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi,
2899         "System.Runtime.InteropServices.Marshal::StringToHGlobalAuto", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi,
2900         "System.Runtime.InteropServices.Marshal::StringToHGlobalUni", ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalUni,
2901         "System.Runtime.InteropServices.Marshal::DestroyStructure", ves_icall_System_Runtime_InteropServices_Marshal_DestroyStructure,
2902
2903
2904         "System.Reflection.Assembly::LoadFrom", ves_icall_System_Reflection_Assembly_LoadFrom,
2905         "System.Reflection.Assembly::GetType", ves_icall_System_Reflection_Assembly_GetType,
2906         "System.Reflection.Assembly::GetTypes", ves_icall_System_Reflection_Assembly_GetTypes,
2907         "System.Reflection.Assembly::FillName", ves_icall_System_Reflection_Assembly_FillName,
2908         "System.Reflection.Assembly::get_code_base", ves_icall_System_Reflection_Assembly_get_code_base,
2909         "System.Reflection.Assembly::get_location", ves_icall_System_Reflection_Assembly_get_location,
2910         "System.Reflection.Assembly::GetExecutingAssembly", ves_icall_System_Reflection_Assembly_GetExecutingAssembly,
2911         "System.Reflection.Assembly::GetEntryAssembly", ves_icall_System_Reflection_Assembly_GetEntryAssembly,
2912         "System.Reflection.Assembly::GetCallingAssembly", ves_icall_System_Reflection_Assembly_GetCallingAssembly,
2913         "System.Reflection.Assembly::get_EntryPoint", ves_icall_System_Reflection_Assembly_get_EntryPoint,
2914         "System.Reflection.Assembly::GetManifestResourceNames", ves_icall_System_Reflection_Assembly_GetManifestResourceNames,
2915         "System.Reflection.Assembly::GetManifestResourceInternal", ves_icall_System_Reflection_Assembly_GetManifestResourceInternal,
2916         "System.Reflection.Assembly::GetFilesInternal", ves_icall_System_Reflection_Assembly_GetFilesInternal,
2917
2918         /*
2919          * System.MonoType.
2920          */
2921         "System.MonoType::getFullName", ves_icall_System_MonoType_getFullName,
2922         "System.MonoType::type_from_obj", mono_type_type_from_obj,
2923         "System.MonoType::GetElementType", ves_icall_MonoType_GetElementType,
2924         "System.MonoType::get_type_info", ves_icall_get_type_info,
2925         "System.MonoType::get_BaseType", ves_icall_get_type_parent,
2926         "System.MonoType::get_Module", ves_icall_MonoType_get_Module,
2927         "System.MonoType::IsPointerImpl", ves_icall_type_ispointer,
2928         "System.MonoType::IsByRefImpl", ves_icall_type_isbyref,
2929         "System.MonoType::GetField", ves_icall_Type_GetField,
2930         "System.MonoType::GetFields", ves_icall_Type_GetFields,
2931         "System.MonoType::GetMethods", ves_icall_Type_GetMethods,
2932         "System.MonoType::GetConstructors", ves_icall_Type_GetConstructors,
2933         "System.MonoType::GetProperties", ves_icall_Type_GetProperties,
2934         "System.MonoType::GetEvents", ves_icall_Type_GetEvents,
2935         "System.MonoType::GetInterfaces", ves_icall_Type_GetInterfaces,
2936         "System.MonoType::GetNestedTypes", ves_icall_Type_GetNestedTypes,
2937
2938         /*
2939          * System.Net.Sockets I/O Services
2940          */
2941         "System.Net.Sockets.Socket::Socket_internal", ves_icall_System_Net_Sockets_Socket_Socket_internal,
2942         "System.Net.Sockets.Socket::Close_internal", ves_icall_System_Net_Sockets_Socket_Close_internal,
2943         "System.Net.Sockets.SocketException::WSAGetLastError_internal", ves_icall_System_Net_Sockets_SocketException_WSAGetLastError_internal,
2944         "System.Net.Sockets.Socket::Available_internal", ves_icall_System_Net_Sockets_Socket_Available_internal,
2945         "System.Net.Sockets.Socket::Blocking_internal", ves_icall_System_Net_Sockets_Socket_Blocking_internal,
2946         "System.Net.Sockets.Socket::Accept_internal", ves_icall_System_Net_Sockets_Socket_Accept_internal,
2947         "System.Net.Sockets.Socket::Listen_internal", ves_icall_System_Net_Sockets_Socket_Listen_internal,
2948         "System.Net.Sockets.Socket::LocalEndPoint_internal", ves_icall_System_Net_Sockets_Socket_LocalEndPoint_internal,
2949         "System.Net.Sockets.Socket::RemoteEndPoint_internal", ves_icall_System_Net_Sockets_Socket_RemoteEndPoint_internal,
2950         "System.Net.Sockets.Socket::Bind_internal", ves_icall_System_Net_Sockets_Socket_Bind_internal,
2951         "System.Net.Sockets.Socket::Connect_internal", ves_icall_System_Net_Sockets_Socket_Connect_internal,
2952         "System.Net.Sockets.Socket::Receive_internal", ves_icall_System_Net_Sockets_Socket_Receive_internal,
2953         "System.Net.Sockets.Socket::RecvFrom_internal", ves_icall_System_Net_Sockets_Socket_RecvFrom_internal,
2954         "System.Net.Sockets.Socket::Send_internal", ves_icall_System_Net_Sockets_Socket_Send_internal,
2955         "System.Net.Sockets.Socket::SendTo_internal", ves_icall_System_Net_Sockets_Socket_SendTo_internal,
2956         "System.Net.Sockets.Socket::Select_internal", ves_icall_System_Net_Sockets_Socket_Select_internal,
2957         "System.Net.Sockets.Socket::Shutdown_internal", ves_icall_System_Net_Sockets_Socket_Shutdown_internal,
2958         "System.Net.Sockets.Socket::GetSocketOption_obj_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_obj_internal,
2959         "System.Net.Sockets.Socket::GetSocketOption_arr_internal", ves_icall_System_Net_Sockets_Socket_GetSocketOption_arr_internal,
2960         "System.Net.Sockets.Socket::SetSocketOption_internal", ves_icall_System_Net_Sockets_Socket_SetSocketOption_internal,
2961         "System.Net.Dns::GetHostByName_internal", ves_icall_System_Net_Dns_GetHostByName_internal,
2962         "System.Net.Dns::GetHostByAddr_internal", ves_icall_System_Net_Dns_GetHostByAddr_internal,
2963
2964         /*
2965          * System.Char
2966          */
2967         "System.Char::GetNumericValue", ves_icall_System_Char_GetNumericValue,
2968         "System.Char::GetUnicodeCategory", ves_icall_System_Char_GetUnicodeCategory,
2969         "System.Char::IsControl", ves_icall_System_Char_IsControl,
2970         "System.Char::IsDigit", ves_icall_System_Char_IsDigit,
2971         "System.Char::IsLetter", ves_icall_System_Char_IsLetter,
2972         "System.Char::IsLower", ves_icall_System_Char_IsLower,
2973         "System.Char::IsUpper", ves_icall_System_Char_IsUpper,
2974         "System.Char::IsNumber", ves_icall_System_Char_IsNumber,
2975         "System.Char::IsPunctuation", ves_icall_System_Char_IsPunctuation,
2976         "System.Char::IsSeparator", ves_icall_System_Char_IsSeparator,
2977         "System.Char::IsSurrogate", ves_icall_System_Char_IsSurrogate,
2978         "System.Char::IsSymbol", ves_icall_System_Char_IsSymbol,
2979         "System.Char::IsWhiteSpace", ves_icall_System_Char_IsWhiteSpace,
2980         "System.Char::ToLower", ves_icall_System_Char_ToLower,
2981         "System.Char::ToUpper", ves_icall_System_Char_ToUpper,
2982
2983         /*
2984          * System.Text.Encoding
2985          */
2986         "System.Text.Encoding::InternalCodePage", ves_icall_System_Text_Encoding_InternalCodePage,
2987
2988         "System.DateTime::GetNow", ves_icall_System_DateTime_GetNow,
2989         "System.CurrentTimeZone::GetTimeZoneData", ves_icall_System_CurrentTimeZone_GetTimeZoneData,
2990
2991         /*
2992          * System.GC
2993          */
2994         "System.GC::InternalCollect", ves_icall_System_GC_InternalCollect,
2995         "System.GC::GetTotalMemory", ves_icall_System_GC_GetTotalMemory,
2996         "System.GC::KeepAlive", ves_icall_System_GC_KeepAlive,
2997         "System.GC::ReRegisterForFinalize", ves_icall_System_GC_ReRegisterForFinalize,
2998         "System.GC::SuppressFinalize", ves_icall_System_GC_SuppressFinalize,
2999         "System.GC::WaitForPendingFinalizers", ves_icall_System_GC_WaitForPendingFinalizers,
3000         "System.Runtime.InteropServices.GCHandle::GetTarget", ves_icall_System_GCHandle_GetTarget,
3001         "System.Runtime.InteropServices.GCHandle::GetTargetHandle", ves_icall_System_GCHandle_GetTargetHandle,
3002         "System.Runtime.InteropServices.GCHandle::FreeHandle", ves_icall_System_GCHandle_FreeHandle,
3003         "System.Runtime.InteropServices.GCHandle::GetAddrOfPinnedObject", ves_icall_System_GCHandle_GetAddrOfPinnedObject,
3004
3005         /*
3006          * System.Security.Cryptography calls
3007          */
3008
3009          "System.Security.Cryptography.RNGCryptoServiceProvider::GetBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_GetBytes,
3010          "System.Security.Cryptography.RNGCryptoServiceProvider::GetNonZeroBytes", ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_GetNonZeroBytes,
3011         
3012         /*
3013          * System.Buffer
3014          */
3015         "System.Buffer::ByteLengthInternal", ves_icall_System_Buffer_ByteLengthInternal,
3016         "System.Buffer::GetByteInternal", ves_icall_System_Buffer_GetByteInternal,
3017         "System.Buffer::SetByteInternal", ves_icall_System_Buffer_SetByteInternal,
3018         "System.Buffer::BlockCopyInternal", ves_icall_System_Buffer_BlockCopyInternal,
3019
3020         /*
3021          * System.IO.MonoIO
3022          */
3023         "System.IO.MonoIO::GetLastError", ves_icall_System_IO_MonoIO_GetLastError,
3024         "System.IO.MonoIO::CreateDirectory", ves_icall_System_IO_MonoIO_CreateDirectory,
3025         "System.IO.MonoIO::RemoveDirectory", ves_icall_System_IO_MonoIO_RemoveDirectory,
3026         "System.IO.MonoIO::FindFirstFile", ves_icall_System_IO_MonoIO_FindFirstFile,
3027         "System.IO.MonoIO::FindNextFile", ves_icall_System_IO_MonoIO_FindNextFile,
3028         "System.IO.MonoIO::FindClose", ves_icall_System_IO_MonoIO_FindClose,
3029         "System.IO.MonoIO::GetCurrentDirectory", ves_icall_System_IO_MonoIO_GetCurrentDirectory,
3030         "System.IO.MonoIO::SetCurrentDirectory", ves_icall_System_IO_MonoIO_SetCurrentDirectory,
3031         "System.IO.MonoIO::MoveFile", ves_icall_System_IO_MonoIO_MoveFile,
3032         "System.IO.MonoIO::CopyFile", ves_icall_System_IO_MonoIO_CopyFile,
3033         "System.IO.MonoIO::DeleteFile", ves_icall_System_IO_MonoIO_DeleteFile,
3034         "System.IO.MonoIO::GetFileAttributes", ves_icall_System_IO_MonoIO_GetFileAttributes,
3035         "System.IO.MonoIO::SetFileAttributes", ves_icall_System_IO_MonoIO_SetFileAttributes,
3036         "System.IO.MonoIO::GetFileStat", ves_icall_System_IO_MonoIO_GetFileStat,
3037         "System.IO.MonoIO::Open", ves_icall_System_IO_MonoIO_Open,
3038         "System.IO.MonoIO::Close", ves_icall_System_IO_MonoIO_Close,
3039         "System.IO.MonoIO::Read", ves_icall_System_IO_MonoIO_Read,
3040         "System.IO.MonoIO::Write", ves_icall_System_IO_MonoIO_Write,
3041         "System.IO.MonoIO::Seek", ves_icall_System_IO_MonoIO_Seek,
3042         "System.IO.MonoIO::GetLength", ves_icall_System_IO_MonoIO_GetLength,
3043         "System.IO.MonoIO::SetLength", ves_icall_System_IO_MonoIO_SetLength,
3044         "System.IO.MonoIO::SetFileTime", ves_icall_System_IO_MonoIO_SetFileTime,
3045         "System.IO.MonoIO::Flush", ves_icall_System_IO_MonoIO_Flush,
3046         "System.IO.MonoIO::get_ConsoleOutput", ves_icall_System_IO_MonoIO_get_ConsoleOutput,
3047         "System.IO.MonoIO::get_ConsoleInput", ves_icall_System_IO_MonoIO_get_ConsoleInput,
3048         "System.IO.MonoIO::get_ConsoleError", ves_icall_System_IO_MonoIO_get_ConsoleError,
3049         "System.IO.MonoIO::CreatePipe(intptr&,intptr&)", ves_icall_System_IO_MonoIO_CreatePipe,
3050         "System.IO.MonoIO::get_VolumeSeparatorChar", ves_icall_System_IO_MonoIO_get_VolumeSeparatorChar,
3051         "System.IO.MonoIO::get_DirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_DirectorySeparatorChar,
3052         "System.IO.MonoIO::get_AltDirectorySeparatorChar", ves_icall_System_IO_MonoIO_get_AltDirectorySeparatorChar,
3053         "System.IO.MonoIO::get_PathSeparator", ves_icall_System_IO_MonoIO_get_PathSeparator,
3054         "System.IO.MonoIO::get_InvalidPathChars", ves_icall_System_IO_MonoIO_get_InvalidPathChars,
3055
3056         /*
3057          * System.Math
3058          */
3059         "System.Math::Sin", ves_icall_System_Math_Sin,
3060     "System.Math::Cos", ves_icall_System_Math_Cos,
3061     "System.Math::Tan", ves_icall_System_Math_Tan,
3062     "System.Math::Sinh", ves_icall_System_Math_Sinh,
3063     "System.Math::Cosh", ves_icall_System_Math_Cosh,
3064     "System.Math::Tanh", ves_icall_System_Math_Tanh,
3065     "System.Math::Acos", ves_icall_System_Math_Acos,
3066     "System.Math::Asin", ves_icall_System_Math_Asin,
3067     "System.Math::Atan", ves_icall_System_Math_Atan,
3068     "System.Math::Atan2", ves_icall_System_Math_Atan2,
3069     "System.Math::Exp", ves_icall_System_Math_Exp,
3070     "System.Math::Log", ves_icall_System_Math_Log,
3071     "System.Math::Log10", ves_icall_System_Math_Log10,
3072     "System.Math::PowImpl", ves_icall_System_Math_Pow,
3073     "System.Math::Sqrt", ves_icall_System_Math_Sqrt,
3074
3075         /*
3076          * System.Environment
3077          */
3078         "System.Environment::get_MachineName", ves_icall_System_Environment_get_MachineName,
3079         "System.Environment::get_NewLine", ves_icall_System_Environment_get_NewLine,
3080         "System.Environment::GetEnvironmentVariable", ves_icall_System_Environment_GetEnvironmentVariable,
3081         "System.Environment::GetEnvironmentVariableNames", ves_icall_System_Environment_GetEnvironmentVariableNames,
3082         "System.Environment::GetCommandLineArgs", mono_runtime_get_main_args,
3083         "System.Environment::get_TickCount", ves_icall_System_Environment_get_TickCount,
3084         "System.Environment::Exit", ves_icall_System_Environment_Exit,
3085         "System.Environment::get_Platform", ves_icall_System_Environment_get_Platform,
3086
3087         /*
3088          * System.Runtime.Remoting
3089          */     
3090         "System.Runtime.Remoting.RemotingServices::InternalExecute",
3091         ves_icall_InternalExecute,
3092         "System.Runtime.Remoting.RemotingServices::IsTransparentProxy",
3093         ves_icall_IsTransparentProxy,
3094
3095         /*
3096          * System.Runtime.Remoting.Messaging
3097          */     
3098         "System.Runtime.Remoting.Messaging.MonoMethodMessage::InitMessage",
3099         ves_icall_MonoMethodMessage_InitMessage,
3100         
3101         /*
3102          * System.Runtime.Remoting.Proxies
3103          */     
3104         "System.Runtime.Remoting.Proxies.RealProxy::GetTransparentProxy", 
3105         ves_icall_Remoting_RealProxy_GetTransparentProxy,
3106
3107         /*
3108          * System.Threading.Interlocked
3109          */
3110         "System.Threading.Interlocked::Increment(int&)", ves_icall_System_Threading_Interlocked_Increment_Int,
3111         "System.Threading.Interlocked::Increment(long&)", ves_icall_System_Threading_Interlocked_Increment_Long,
3112         "System.Threading.Interlocked::Decrement(int&)", ves_icall_System_Threading_Interlocked_Decrement_Int,
3113         "System.Threading.Interlocked::Decrement(long&)", ves_icall_System_Threading_Interlocked_Decrement_Long,
3114         "System.Threading.Interlocked::CompareExchange(int&,int,int)", ves_icall_System_Threading_Interlocked_CompareExchange_Int,
3115         "System.Threading.Interlocked::CompareExchange(object&,object,object)", ves_icall_System_Threading_Interlocked_CompareExchange_Object,
3116         "System.Threading.Interlocked::CompareExchange(single&,single,single)", ves_icall_System_Threading_Interlocked_CompareExchange_Single,
3117         "System.Threading.Interlocked::Exchange(int&,int)", ves_icall_System_Threading_Interlocked_Exchange_Int,
3118         "System.Threading.Interlocked::Exchange(object&,object)", ves_icall_System_Threading_Interlocked_Exchange_Object,
3119         "System.Threading.Interlocked::Exchange(single&,single)", ves_icall_System_Threading_Interlocked_Exchange_Single,
3120
3121         /*
3122          * System.Diagnostics.Process
3123          */
3124         "System.Diagnostics.Process::GetProcess_internal(int)", ves_icall_System_Diagnostics_Process_GetProcess_internal,
3125         "System.Diagnostics.Process::GetProcesses_internal()", ves_icall_System_Diagnostics_Process_GetProcesses_internal,
3126         "System.Diagnostics.Process::GetPid_internal()", ves_icall_System_Diagnostics_Process_GetPid_internal,
3127         "System.Diagnostics.Process::Process_free_internal(intptr)", ves_icall_System_Diagnostics_Process_Process_free_internal,
3128         "System.Diagnostics.Process::GetModules_internal()", ves_icall_System_Diagnostics_Process_GetModules_internal,
3129         "System.Diagnostics.Process::Start_internal(string,string,intptr,intptr,intptr,ProcInfo&)", ves_icall_System_Diagnostics_Process_Start_internal,
3130         "System.Diagnostics.Process::WaitForExit_internal(intptr,int)", ves_icall_System_Diagnostics_Process_WaitForExit_internal,
3131         "System.Diagnostics.Process::ExitTime_internal(intptr)", ves_icall_System_Diagnostics_Process_ExitTime_internal,
3132         "System.Diagnostics.Process::StartTime_internal(intptr)", ves_icall_System_Diagnostics_Process_StartTime_internal,
3133         "System.Diagnostics.Process::ExitCode_internal(intptr)", ves_icall_System_Diagnostics_Process_ExitCode_internal,
3134         "System.Diagnostics.Process::ProcessName_internal(intptr)", ves_icall_System_Diagnostics_Process_ProcessName_internal,
3135         "System.Diagnostics.Process::GetWorkingSet_internal(intptr,int&,int&)", ves_icall_System_Diagnostics_Process_GetWorkingSet_internal,
3136         "System.Diagnostics.Process::SetWorkingSet_internal(intptr,int,int,bool)", ves_icall_System_Diagnostics_Process_SetWorkingSet_internal,
3137         "System.Diagnostics.FileVersionInfo::GetVersionInfo_internal(string)", ves_icall_System_Diagnostics_FileVersionInfo_GetVersionInfo_internal,
3138
3139         /* 
3140          * System.Delegate
3141          */
3142         "System.Delegate::CreateDelegate_internal", ves_icall_System_Delegate_CreateDelegate_internal,
3143
3144         /* 
3145          * System.Runtime.Serialization
3146          */
3147         "System.Runtime.Serialization.FormatterServices::GetUninitializedObjectInternal",
3148         ves_icall_System_Runtime_Serialization_FormatterServices_GetUninitializedObject_Internal,
3149
3150         /*
3151          * System.IO.Path
3152          */
3153         "System.IO.Path::get_temp_path", ves_icall_System_IO_get_temp_path,
3154
3155         /*
3156          * Private icalls for the Mono Debugger
3157          */
3158         "System.Reflection.Assembly::MonoDebugger_GetMethod",
3159         ves_icall_MonoDebugger_GetMethod,
3160
3161         "System.Reflection.Assembly::MonoDebugger_GetLocalTypeFromSignature",
3162         ves_icall_MonoDebugger_GetLocalTypeFromSignature,
3163
3164         "System.Reflection.Assembly::MonoDebugger_GetType",
3165         ves_icall_MonoDebugger_GetType,
3166
3167         /*
3168          * add other internal calls here
3169          */
3170         NULL, NULL
3171 };
3172
3173 void
3174 mono_init_icall (void)
3175 {
3176         const char *name;
3177         int i = 0;
3178
3179         while ((name = icall_map [i])) {
3180                 mono_add_internal_call (name, icall_map [i+1]);
3181                 i += 2;
3182         }
3183        
3184 }
3185
3186