5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
17 #include <mono/metadata/object.h>
18 #include <mono/metadata/threads.h>
19 #include <mono/metadata/reflection.h>
20 #include <mono/metadata/assembly.h>
24 ves_icall_System_Array_GetValue (MonoObject *this, MonoObject *idxs)
28 gint32 i, pos, *ind, esize;
31 io = (MonoArray *)idxs;
32 ic = (MonoClass *)io->obj.klass;
34 ao = (MonoArray *)this;
35 ac = (MonoClass *)ao->obj.klass;
37 g_assert (ic->rank == 1);
38 g_assert (io->bounds [0].length == ac->rank);
40 ind = (guint32 *)io->vector;
42 pos = ind [0] - ao->bounds [0].lower_bound;
43 for (i = 1; i < ac->rank; i++)
44 pos = pos*ao->bounds [i].length + ind [i] -
45 ao->bounds [i].lower_bound;
47 esize = mono_array_element_size (ac);
48 ea = (gpointer*)((char*)ao->vector + (pos * esize));
50 if (ac->element_class->valuetype)
51 return mono_value_box (ac->element_class, ea);
57 ves_icall_System_Array_SetValue (MonoObject *this, MonoObject *value,
60 MonoArray *ao, *io, *vo;
61 MonoClass *ac, *ic, *vc;
62 gint32 i, pos, *ind, esize;
65 vo = (MonoArray *)value;
66 vc = (MonoClass *)vo->obj.klass;
68 io = (MonoArray *)idxs;
69 ic = (MonoClass *)io->obj.klass;
71 ao = (MonoArray *)this;
72 ac = (MonoClass *)ao->obj.klass;
74 g_assert (ic->rank == 1);
75 g_assert (io->bounds [0].length == ac->rank);
76 g_assert (ac->element_class == vo->obj.klass);
78 ind = (guint32 *)io->vector;
80 pos = ind [0] - ao->bounds [0].lower_bound;
81 for (i = 1; i < ac->rank; i++)
82 pos = pos*ao->bounds [i].length + ind [i] -
83 ao->bounds [i].lower_bound;
85 esize = mono_array_element_size (ac);
86 ea = (gpointer*)((char*)ao->vector + (pos * esize));
88 if (ac->element_class->valuetype) {
89 g_assert (vc->valuetype);
91 memcpy (ea, (char *)vo + sizeof (MonoObject), esize);
98 ves_icall_System_Array_CreateInstance ()
100 g_warning ("not implemented");
101 g_assert_not_reached ();
106 ves_icall_System_Array_GetRank (MonoObject *this)
108 return this->klass->rank;
112 ves_icall_System_Array_GetLength (MonoObject *this, gint32 dimension)
114 return ((MonoArray *)this)->bounds [dimension].length;
118 ves_icall_System_Array_GetLowerBound (MonoObject *this, gint32 dimension)
120 return ((MonoArray *)this)->bounds [dimension].lower_bound;
124 ves_icall_InitializeArray (MonoArray *array, MonoClassField *field_handle)
126 guint32 size = mono_array_element_size (((MonoObject*) array)->klass) * mono_array_length (array);
128 * FIXME: ENOENDIAN: we need to byteswap as needed.
130 memcpy (mono_array_addr (array, char, 0), field_handle->data, size);
134 ves_icall_System_Object_MemberwiseClone (MonoObject *this)
136 return mono_object_clone (this);
140 ves_icall_app_get_cur_domain ()
142 MonoClass *klass = mono_class_from_name (mono_defaults.corlib, "System", "AppDomain");
144 return mono_object_new (klass);
147 static MonoReflectionType *
148 my_mono_new_mono_type (MonoType *type)
150 MonoReflectionType *res = (MonoReflectionType *)mono_object_new (mono_defaults.monotype_class);
157 mono_type_type_from_obj (MonoReflectionType *mtype, MonoObject *obj)
159 mtype->type = &obj->klass->byval_arg;
163 ves_icall_get_data_chunk (MonoReflectionAssemblyBuilder *assb, gint32 type, MonoArray *buf)
167 if (type == 0) { /* get the header */
168 count = mono_image_get_header (assb, (char*)buf->vector, buf->bounds->length);
172 MonoDynamicAssembly *ass = assb->dynamic_assembly;
173 char *p = mono_array_addr (buf, char, 0);
174 count = ass->code.index + ass->meta_size;
175 if (count > buf->bounds->length)
177 memcpy (p, ass->code.data, ass->code.index);
178 memcpy (p + ass->code.index, ass->assembly.image->raw_metadata, ass->meta_size);
186 ves_icall_type_from_name (MonoObject *name)
191 static MonoReflectionType*
192 ves_icall_type_from_handle (MonoType *handle)
194 return my_mono_new_mono_type (handle);
198 ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr (gpointer ptr)
200 return (gpointer)(*(int *)ptr);
204 ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAuto (gpointer ptr)
206 return mono_string_new ((char *)ptr);
209 static MonoReflectionAssembly*
210 ves_icall_System_Reflection_Assembly_LoadFrom (MonoString *assName, MonoObject *evidence)
212 /* FIXME : examine evidence? */
213 char *name = mono_string_to_utf8 (assName);
214 enum MonoImageOpenStatus status = MONO_IMAGE_OK;
215 MonoClass *klass = mono_class_from_name (mono_defaults.corlib, "System.Reflection", "Assembly");
216 MonoAssembly *ass = mono_assembly_open (name, NULL, &status);
217 MonoReflectionAssembly *res;
219 g_assert (ass != NULL);
220 g_assert (status == MONO_IMAGE_OK);
222 res = (MonoReflectionAssembly *)mono_object_new (klass);
230 static MonoReflectionType*
231 ves_icall_System_Reflection_Assembly_GetType (MonoReflectionAssembly *assembly, MonoString *type) /* , char throwOnError, char ignoreCase) */
233 /* FIXME : use throwOnError and ignoreCase */
234 gchar *name, *namespace, **parts;
238 name = mono_string_to_utf8 (type);
240 parts = g_strsplit (name, ".", 0);
248 namespace = g_strjoinv (".", parts);
251 klass = mono_class_from_name (assembly->assembly->image, namespace, name);
257 mono_class_init (klass);
259 return my_mono_new_mono_type (&klass->byval_arg);
263 ves_icall_System_MonoType_assQualifiedName (MonoReflectionType *object)
265 /* FIXME : real rules are more complicated (internal classes,
266 reference types, array types, etc. */
272 switch (object->type->type) {
273 case MONO_TYPE_SZARRAY:
274 klass = object->type->data.type->data.klass;
278 klass = object->type->data.klass;
282 fullname = g_strconcat (klass->name_space, ".",
284 klass->image->assembly_name, append, NULL);
285 res = mono_string_new (fullname);
292 ves_icall_System_PAL_GetCurrentDirectory (MonoObject *object)
295 gchar *path = g_get_current_dir ();
296 res = mono_string_new (path);
302 ves_icall_System_DateTime_GetNow ()
306 // fixme: it seems that .Net has another base time than Unix??
307 if (gettimeofday (&tv, NULL) == 0) {
308 return (gint64)tv.tv_sec * 1000000000 + tv.tv_usec*10;
315 static gpointer icall_map [] = {
319 "System.Array::GetValue", ves_icall_System_Array_GetValue,
320 "System.Array::SetValue", ves_icall_System_Array_SetValue,
321 "System.Array::GetRank", ves_icall_System_Array_GetRank,
322 "System.Array::GetLength", ves_icall_System_Array_GetLength,
323 "System.Array::GetLowerBound", ves_icall_System_Array_GetLowerBound,
324 "System.Array::CreateInstance", ves_icall_System_Array_CreateInstance,
329 "System.Object::MemberwiseClone", ves_icall_System_Object_MemberwiseClone,
334 "System.String::IsInterned", mono_string_is_interned,
335 "System.String::Intern", mono_string_intern,
340 "System.AppDomain::getCurDomain", ves_icall_app_get_cur_domain,
345 "System.Decimal::decimal2UInt64", mono_decimal2UInt64,
346 "System.Decimal::decimal2Int64", mono_decimal2Int64,
347 "System.Decimal::double2decimal", mono_double2decimal, /* FIXME: wrong signature. */
348 "System.Decimal::decimalIncr", mono_decimalIncr,
349 "System.Decimal::decimalSetExponent", mono_decimalSetExponent,
350 "System.Decimal::decimal2double", mono_decimal2double,
351 "System.Decimal::decimalFloorAndTrunc", mono_decimalFloorAndTrunc,
352 "System.Decimal::decimalRound", mono_decimalRound,
353 "System.Decimal::decimalMult", mono_decimalMult,
354 "System.Decimal::decimalDiv", mono_decimalDiv,
355 "System.Decimal::decimalIntDiv", mono_decimalIntDiv,
356 "System.Decimal::decimalCompare", mono_decimalCompare,
357 "System.Decimal::string2decimal", mono_string2decimal,
358 "System.Decimal::decimal2string", mono_decimal2string,
367 "System.Reflection.Emit.AssemblyBuilder::getDataChunk", ves_icall_get_data_chunk,
368 "System.Reflection.Emit.AssemblyBuilder::getUSIndex", mono_image_insert_string,
381 "System.Type::internal_from_name", ves_icall_type_from_name,
382 "System.Type::internal_from_handle", ves_icall_type_from_handle,
385 * System.Runtime.CompilerServices.RuntimeHelpers
387 "System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray", ves_icall_InitializeArray,
392 "System.Threading.Thread::Thread_internal", ves_icall_System_Threading_Thread_Thread_internal,
393 "System.Threading.Thread::Start_internal", ves_icall_System_Threading_Thread_Start_internal,
394 "System.Threading.Thread::Sleep_internal", ves_icall_System_Threading_Thread_Sleep_internal,
395 "System.Threading.Thread::CurrentThread_internal", ves_icall_System_Threading_Thread_CurrentThread_internal,
396 "System.Threading.Thread::Join_internal", ves_icall_System_Threading_Thread_Join_internal,
397 "System.Threading.Thread::SlotHash_lookup", ves_icall_System_Threading_Thread_SlotHash_lookup,
398 "System.Threading.Thread::SlotHash_store", ves_icall_System_Threading_Thread_SlotHash_store,
399 "System.Threading.Monitor::Monitor_exit", ves_icall_System_Threading_Monitor_Monitor_exit,
400 "System.Threading.Monitor::Monitor_test_owner", ves_icall_System_Threading_Monitor_Monitor_test_owner,
401 "System.Threading.Monitor::Monitor_test_synchronised", ves_icall_System_Threading_Monitor_Monitor_test_synchronised,
402 "System.Threading.Monitor::Monitor_pulse", ves_icall_System_Threading_Monitor_Monitor_pulse,
403 "System.Threading.Monitor::Monitor_pulse_all", ves_icall_System_Threading_Monitor_Monitor_pulse_all,
404 "System.Threading.Monitor::Monitor_try_enter", ves_icall_System_Threading_Monitor_Monitor_try_enter,
405 "System.Threading.Monitor::Monitor_wait", ves_icall_System_Threading_Monitor_Monitor_wait,
407 "System.Runtime.InteropServices.Marshal::ReadIntPtr", ves_icall_System_Runtime_InteropServices_Marshal_ReadIntPtr,
408 "System.Runtime.InteropServices.Marshal::PtrToStringAuto", ves_icall_System_Runtime_InteropServices_Marshal_PtrToStringAuto,
410 "System.Reflection.Assembly::LoadFrom", ves_icall_System_Reflection_Assembly_LoadFrom,
411 "System.Reflection.Assembly::GetType", ves_icall_System_Reflection_Assembly_GetType,
416 "System.MonoType::assQualifiedName", ves_icall_System_MonoType_assQualifiedName,
417 "System.MonoType::type_from_obj", mono_type_type_from_obj,
419 "System.PAL.OpSys::GetCurrentDirectory", ves_icall_System_PAL_GetCurrentDirectory,
420 "System.DateTime::GetNow", ves_icall_System_DateTime_GetNow,
422 * add other internal calls here
433 while ((n = icall_map [i])) {
434 mono_add_internal_call (n, icall_map [i+1]);