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