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