5 * Dietmar Maurer (dietmar@ximian.com)
7 * (C) 2001 Ximian, Inc.
15 #include <mono/metadata/object.h>
16 #include <mono/metadata/threads.h>
17 #include <mono/metadata/reflection.h>
20 ves_icall_System_Array_GetValue (MonoObject *this, MonoObject *idxs)
23 MonoArrayClass *ac, *ic;
24 gint32 i, pos, *ind, esize;
27 io = (MonoArray *)idxs;
28 ic = (MonoArrayClass *)io->obj.klass;
30 ao = (MonoArray *)this;
31 ac = (MonoArrayClass *)ao->obj.klass;
33 g_assert (ic->rank == 1);
34 g_assert (io->bounds [0].length == ac->rank);
36 ind = (guint32 *)io->vector;
38 pos = ind [0] - ao->bounds [0].lower_bound;
39 for (i = 1; i < ac->rank; i++)
40 pos = pos*ao->bounds [i].length + ind [i] -
41 ao->bounds [i].lower_bound;
43 esize = mono_array_element_size (ac);
44 ea = ao->vector + (pos * esize);
46 if (ac->element_class->valuetype)
47 return mono_value_box (ac->element_class, ea);
53 ves_icall_System_Array_SetValue (MonoObject *this, MonoObject *value,
56 MonoArray *ao, *io, *vo;
57 MonoArrayClass *ac, *ic, *vc;
58 gint32 i, pos, *ind, esize;
61 vo = (MonoArray *)value;
62 vc = (MonoArrayClass *)vo->obj.klass;
64 io = (MonoArray *)idxs;
65 ic = (MonoArrayClass *)io->obj.klass;
67 ao = (MonoArray *)this;
68 ac = (MonoArrayClass *)ao->obj.klass;
70 g_assert (ic->rank == 1);
71 g_assert (io->bounds [0].length == ac->rank);
72 g_assert (ac->element_class == vo->obj.klass);
74 ind = (guint32 *)io->vector;
76 pos = ind [0] - ao->bounds [0].lower_bound;
77 for (i = 1; i < ac->rank; i++)
78 pos = pos*ao->bounds [i].length + ind [i] -
79 ao->bounds [i].lower_bound;
81 esize = mono_array_element_size (ac);
82 ea = ao->vector + (pos * esize);
84 if (ac->element_class->valuetype) {
85 g_assert (vc->klass.valuetype);
87 memcpy (ea, (char *)vo + sizeof (MonoObject), esize);
94 ves_icall_array_ctor (MonoObject *this, gint32 n1, ...)
99 gint32 i, s, len, esize;
103 ao = (MonoArray *)this;
104 ac = (MonoArrayClass *)this->klass;
106 g_assert (ac->rank >= 1);
108 ao->bounds = g_malloc0 (ac->rank * sizeof (MonoArrayBounds));
111 ao->bounds [0].length = n1;
112 for (i = 1; i < ac->rank; i++) {
113 s = va_arg (ap, gint32);
115 ao->bounds [i].length = s;
118 esize = mono_array_element_size (ac);
119 ao->vector = g_malloc0 (len * esize);
123 ves_icall_array_bound_ctor (MonoObject *this, gint32 n1, ...)
128 gint32 i, s, len, esize;
132 ao = (MonoArray *)this;
133 ac = (MonoArrayClass *)this->klass;
135 g_assert (ac->rank >= 1);
137 ao->bounds = g_malloc0 (ac->rank * sizeof (MonoArrayBounds));
139 ao->bounds [0].lower_bound = n1;
140 for (i = 1; i < ac->rank; i++)
141 ao->bounds [i].lower_bound = va_arg (ap, gint32);
143 len = va_arg (ap, gint32);
144 ao->bounds [0].length = len;
145 for (i = 1; i < ac->rank; i++) {
146 s = va_arg (ap, gint32);
148 ao->bounds [i].length = s;
151 esize = mono_array_element_size (ac);
152 ao->vector = g_malloc0 (len * esize);
156 ves_icall_System_Array_CreateInstance ()
158 g_warning ("not implemented");
159 g_assert_not_reached ();
164 ves_icall_System_Array_GetRank (MonoObject *this)
166 return ((MonoArrayClass *)this->klass)->rank;
170 ves_icall_System_Array_GetLength (MonoObject *this, gint32 dimension)
172 return ((MonoArray *)this)->bounds [dimension].length;
176 ves_icall_System_Array_GetLowerBound (MonoObject *this, gint32 dimension)
178 return ((MonoArray *)this)->bounds [dimension].lower_bound;
182 ves_icall_System_Object_MemberwiseClone (MonoObject *this)
184 return mono_object_clone (this);
188 ves_icall_app_get_cur_domain ()
190 MonoClass *klass = mono_class_from_name (mono_defaults.corlib, "System", "AppDomain");
192 return mono_object_new (klass);
196 my_mono_new_object (MonoClass *klass, gpointer data)
198 MonoClassField *field;
199 MonoObject *res = mono_object_new (klass);
202 field = mono_class_get_field_from_name (klass, "_impl");
203 slot = (gpointer*)((char*)res + field->offset);
209 object_field_pointer (MonoObject *obj, char *name)
211 MonoClassField *field;
214 field = mono_class_get_field_from_name (obj->klass, name);
215 slot = (gpointer*)((char*)obj + field->offset);
220 object_impl_pointer (MonoObject *obj)
222 return object_field_pointer (obj, "_impl");
226 ves_icall_app_define_assembly (MonoObject *appdomain, MonoObject *assembly_name, int access)
228 MonoClass *klass = mono_class_from_name (mono_defaults.corlib, "System.Reflection.Emit", "AssemblyBuilder");
229 MonoDynamicAssembly *ass = g_new0 (MonoDynamicAssembly, 1);
230 MonoClassField *field;
233 field = mono_class_get_field_from_name (assembly_name->klass, "name");
234 name = *(MonoObject**)((char*)assembly_name + field->offset);
236 ass->name = mono_string_to_utf8 (name);
238 return my_mono_new_object (klass, ass);
242 ves_icall_get_data_chunk (MonoObject *assb, gint32 type, MonoArray *buf)
244 MonoDynamicAssembly *ass = object_impl_pointer (assb);
248 * get some info from the object
250 ep = object_field_pointer (assb, "entry_point");
252 ass->entry_point = object_impl_pointer (ep);
254 ass->entry_point = 0;
256 if (type == 0) { /* get the header */
257 count = mono_image_get_header (ass, buf->vector, buf->bounds->length);
261 count = ass->code.index + ass->meta_size;
262 if (count > buf->bounds->length)
264 memcpy (buf->vector, ass->code.data, ass->code.index);
265 memcpy (buf->vector + ass->code.index, ass->assembly.image->raw_metadata, ass->meta_size);
273 ves_icall_define_module (MonoObject *assb, MonoObject *name, MonoObject *fname)
275 MonoClass *klass = mono_class_from_name (mono_defaults.corlib, "System.Reflection.Emit", "ModuleBuilder");
276 MonoModuleBuilder *mb = g_new0 (MonoModuleBuilder, 1);
277 MonoDynamicAssembly *ass = object_impl_pointer (assb);
278 ass->modules = g_list_prepend (ass->modules, mb);
280 mb->name = mono_string_to_utf8 (name);
281 mb->fname = mono_string_to_utf8 (fname);
283 return my_mono_new_object (klass, mb);
287 ves_icall_define_type (MonoObject *moduleb, MonoObject *name, int attrs)
289 MonoClass *klass = mono_class_from_name (mono_defaults.corlib, "System.Reflection.Emit", "TypeBuilder");
290 MonoTypeBuilder *tb = g_new0 (MonoTypeBuilder, 1);
291 MonoModuleBuilder *mb = object_impl_pointer (moduleb);
292 char *nspace = mono_string_to_utf8 (name);
293 char *tname = strrchr (nspace, '.');
305 mb->types = g_list_prepend (mb->types, tb);
307 return my_mono_new_object (klass, tb);
311 ves_icall_define_method (MonoObject *typeb, MonoObject *name, int attrs, int callconv, MonoObject *rettype, MonoArray *paramtypes)
313 MonoClass *klass = mono_class_from_name (mono_defaults.corlib, "System.Reflection.Emit", "MethodBuilder");
314 MonoMethodBuilder *mb = g_new0 (MonoMethodBuilder, 1);
315 MonoTypeBuilder *tb = object_impl_pointer (typeb);
317 mb->name = mono_string_to_utf8 (name);
319 mb->callconv = callconv;
320 mb->ret = object_impl_pointer (rettype);
321 /* ignore params ... */
323 if (!tb->has_default_ctor) {
324 if (strcmp (".ctor", mb->name) == 0 && paramtypes->bounds->length == 0)
325 tb->has_default_ctor = 1;
328 tb->methods = g_list_prepend (tb->methods, mb);
330 return my_mono_new_object (klass, mb);
334 ves_icall_set_method_body (MonoObject *methodb, MonoArray *code, gint32 count)
336 MonoMethodBuilder *mb = object_impl_pointer (methodb);
337 if (code->bounds->length < count) {
338 g_warning ("code len is less than count");
342 mb->code = g_malloc (count);
343 mb->code_size = count;
344 memcpy (mb->code, code->vector, count);
348 ves_icall_type_from_name (MonoObject *name)
354 ves_icall_type_from_handle (gpointer handle)
356 return my_mono_new_object (mono_defaults.type_class, handle);
359 static gpointer icall_map [] = {
363 "__array_ctor", ves_icall_array_ctor,
364 "__array_bound_ctor", ves_icall_array_bound_ctor,
365 "System.Array::GetValue", ves_icall_System_Array_GetValue,
366 "System.Array::SetValue", ves_icall_System_Array_SetValue,
367 "System.Array::GetRank", ves_icall_System_Array_GetRank,
368 "System.Array::GetLength", ves_icall_System_Array_GetLength,
369 "System.Array::GetLowerBound", ves_icall_System_Array_GetLowerBound,
370 "System.Array::CreateInstance", ves_icall_System_Array_CreateInstance,
375 "System.Object::MemberwiseClone", ves_icall_System_Object_MemberwiseClone,
380 "System.String::IsInterned", mono_string_is_interned,
381 "System.String::Intern", mono_string_intern,
386 "System.AppDomain::getCurDomain", ves_icall_app_get_cur_domain,
387 "System.AppDomain::defineAssembly", ves_icall_app_define_assembly,
392 "System.Reflection.Emit.ModuleBuilder::defineType", ves_icall_define_type,
397 "System.Reflection.Emit.AssemblyBuilder::defineModule", ves_icall_define_module,
398 "System.Reflection.Emit.AssemblyBuilder::getDataChunk", ves_icall_get_data_chunk,
403 "System.Reflection.Emit.TypeBuilder::defineMethod", ves_icall_define_method,
408 "System.Reflection.Emit.MethodBuilder::set_method_body", ves_icall_set_method_body,
413 "System.Type::internal_from_name", ves_icall_type_from_name,
414 "System.Type::internal_from_handle", ves_icall_type_from_handle,
419 "System.Threading.Thread::Thread_internal", ves_icall_System_Threading_Thread_Thread_internal,
420 "System.Threading.Thread::Start_internal", ves_icall_System_Threading_Thread_Start_internal,
421 "System.Threading.Thread::Sleep_internal", ves_icall_System_Threading_Thread_Sleep_internal,
422 "System.Threading.Thread::Schedule_internal", ves_icall_System_Threading_Thread_Schedule_internal,
423 "System.Threading.Thread::CurrentThread_internal", ves_icall_System_Threading_Thread_CurrentThread_internal,
424 "System.Threading.Thread::Join_internal", ves_icall_System_Threading_Thread_Join_internal,
425 "System.Threading.Thread::DataSlot_register", ves_icall_System_Threading_Thread_DataSlot_register,
426 "System.Threading.Thread::DataSlot_store", ves_icall_System_Threading_Thread_DataSlot_store,
427 "System.Threading.Thread::DataSlot_retrieve", ves_icall_System_Threading_Thread_DataSlot_retrieve,
428 /* Not in the System.Threading namespace, but part of the same code */
429 "System.LocalDataStoreSlot::DataSlot_unregister", ves_icall_System_LocalDataStoreSlot_DataSlot_unregister,
430 "System.Threading.Monitor::Monitor_enter", ves_icall_System_Threading_Monitor_Monitor_enter,
431 "System.Threading.Monitor::Monitor_exit", ves_icall_System_Threading_Monitor_Monitor_exit,
432 "System.Threading.Monitor::Monitor_test_owner", ves_icall_System_Threading_Monitor_Monitor_test_owner,
433 "System.Threading.Monitor::Monitor_test_synchronised", ves_icall_System_Threading_Monitor_Monitor_test_synchronised,
434 "System.Threading.Monitor::Monitor_pulse", ves_icall_System_Threading_Monitor_Monitor_pulse,
435 "System.Threading.Monitor::Monitor_pulse_all", ves_icall_System_Threading_Monitor_Monitor_pulse_all,
436 "System.Threading.Monitor::Monitor_try_enter", ves_icall_System_Threading_Monitor_Monitor_try_enter,
437 "System.Threading.Monitor::Monitor_wait", ves_icall_System_Threading_Monitor_Monitor_wait,
440 * add other internal calls here
451 while ((n = icall_map [i])) {
452 mono_add_internal_call (n, icall_map [i+1]);