native functions, getClassContext, currentClassLoader implementation (with stack...
[cacao.git] / src / native / vm / VMSystem.c
1 /* nat/VMSystem.c - java/lang/System
2
3    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4    R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5    M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6    P. Tomsich, J. Wenninger
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Roman Obermaiser
28
29    Changes: Joseph Wenninger
30
31    $Id: VMSystem.c 930 2004-03-02 21:18:23Z jowenn $
32
33 */
34
35
36 #include <string.h>
37 #include <time.h>
38 #include "jni.h"
39 #include "builtin.h"
40 #include "native.h"
41 #include "toolbox/loging.h"
42 #include "java_lang_Object.h"
43
44
45 /*
46  * Class:     java/lang/System
47  * Method:    arraycopy
48  * Signature: (Ljava/lang/Object;ILjava/lang/Object;II)V
49  */
50 JNIEXPORT void JNICALL Java_java_lang_VMSystem_arraycopy(JNIEnv *env, jclass clazz, java_lang_Object *source, s4 sp, java_lang_Object *dest, s4 dp, s4 len)
51 {
52         s4 i;
53         java_arrayheader *s = (java_arrayheader *) source;
54         java_arrayheader *d = (java_arrayheader *) dest;
55         arraydescriptor *sdesc;
56         arraydescriptor *ddesc;
57
58         /*      printf("arraycopy: %p:%x->%p:%x\n",source,sp,dest,dp);
59                 fflush(stdout);*/
60
61         if (!s || !d) { 
62                 *exceptionptr = proto_java_lang_NullPointerException; 
63                 return; 
64         }
65
66         sdesc = s->objheader.vftbl->arraydesc;
67         ddesc = d->objheader.vftbl->arraydesc;
68
69         if (!sdesc || !ddesc || (sdesc->arraytype != ddesc->arraytype)) {
70                 *exceptionptr = proto_java_lang_ArrayStoreException; 
71                 return; 
72         }
73
74         if ((len < 0) || (sp < 0) || (sp + len > s->size) || (dp < 0) || (dp + len > d->size)) {
75                 *exceptionptr = proto_java_lang_ArrayIndexOutOfBoundsException; 
76                 return; 
77         }
78
79         if (sdesc->componentvftbl == ddesc->componentvftbl) {
80                 /* We copy primitive values or references of exactly the same type */
81                 s4 dataoffset = sdesc->dataoffset;
82                 s4 componentsize = sdesc->componentsize;
83                 memmove(((u1*)d) + dataoffset + componentsize * dp,
84                                 ((u1*)s) + dataoffset + componentsize * sp,
85                                 (size_t) len * componentsize);
86         }
87         else {
88                 /* We copy references of different type */
89                 java_objectarray *oas = (java_objectarray*) s;
90                 java_objectarray *oad = (java_objectarray*) d;
91                 
92                 if (dp <= sp) {
93                         for (i = 0; i < len; i++) {
94                                 java_objectheader *o = oas->data[sp + i];
95                                 if (!builtin_canstore(oad, o)) {
96                                         *exceptionptr = proto_java_lang_ArrayStoreException;
97                                         return;
98                                 }
99                                 oad->data[dp + i] = o;
100                         }
101
102                 } else {
103                         /* XXX this does not completely obey the specification!
104                          * If an exception is thrown only the elements above
105                          * the current index have been copied. The
106                          * specification requires that only the elements
107                          * *below* the current index have been copied before
108                          * the throw.
109                          */
110                         for (i = len - 1; i >= 0; i--) {
111                                 java_objectheader *o = oas->data[sp + i];
112                                 if (!builtin_canstore(oad, o)) {
113                                         *exceptionptr = proto_java_lang_ArrayStoreException;
114                                         return;
115                                 }
116                                 oad->data[dp + i] = o;
117                         }
118                 }
119         }
120 }
121
122 /*
123  * Class:     java/lang/System
124  * Method:    identityHashCode
125  * Signature: (Ljava/lang/Object;)I
126  */
127 JNIEXPORT s4 JNICALL Java_java_lang_VMSystem_identityHashCode(JNIEnv *env, jclass clazz, java_lang_Object *par1)
128 {
129         return ((char*) par1) - ((char*) 0);    
130 }
131
132
133 /*
134  * These are local overrides for various environment variables in Emacs.
135  * Please do not remove this and leave it at the end of the file, where
136  * Emacs will automagically detect them.
137  * ---------------------------------------------------------------------
138  * Local variables:
139  * mode: c
140  * indent-tabs-mode: t
141  * c-basic-offset: 4
142  * tab-width: 4
143  * End:
144  */