Safety first.
[cacao.git] / nat / 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 873 2004-01-11 20:59:29Z twisti $
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 #if 0
46 /*
47  * Class:     java/lang/System
48  * Method:    currentTimeMillis
49  * Signature: ()J
50  */
51 JNIEXPORT s8 JNICALL Java_java_lang_VMSystem_currentTimeMillis(JNIEnv *env)
52 {
53         struct timeval tv;
54
55         (void) gettimeofday(&tv, NULL);
56         return ((s8) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
57 }
58 #endif
59
60
61 /*
62  * Class:     java/lang/System
63  * Method:    arraycopy
64  * Signature: (Ljava/lang/Object;ILjava/lang/Object;II)V
65  */
66 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)
67 {
68         s4 i;
69         java_arrayheader *s = (java_arrayheader *) source;
70         java_arrayheader *d = (java_arrayheader *) dest;
71         arraydescriptor *sdesc;
72         arraydescriptor *ddesc;
73
74         /*      printf("arraycopy: %p:%x->%p:%x\n",source,sp,dest,dp);
75                 fflush(stdout);*/
76
77         if (!s || !d) { 
78                 *exceptionptr = proto_java_lang_NullPointerException; 
79                 return; 
80         }
81
82         sdesc = s->objheader.vftbl->arraydesc;
83         ddesc = d->objheader.vftbl->arraydesc;
84
85         if (!sdesc || !ddesc || (sdesc->arraytype != ddesc->arraytype)) {
86                 *exceptionptr = proto_java_lang_ArrayStoreException; 
87                 return; 
88         }
89
90         if ((len < 0) || (sp < 0) || (sp + len > s->size) || (dp < 0) || (dp + len > d->size)) {
91                 *exceptionptr = proto_java_lang_ArrayIndexOutOfBoundsException; 
92                 return; 
93         }
94
95         if (sdesc->componentvftbl == ddesc->componentvftbl) {
96                 /* We copy primitive values or references of exactly the same type */
97                 s4 dataoffset = sdesc->dataoffset;
98                 s4 componentsize = sdesc->componentsize;
99                 memmove(((u1*)d) + dataoffset + componentsize * dp,
100                                 ((u1*)s) + dataoffset + componentsize * sp,
101                                 (size_t) len * componentsize);
102         }
103         else {
104                 /* We copy references of different type */
105                 java_objectarray *oas = (java_objectarray*) s;
106                 java_objectarray *oad = (java_objectarray*) d;
107                 
108                 if (dp <= sp) {
109                         for (i = 0; i < len; i++) {
110                                 java_objectheader *o = oas->data[sp + i];
111                                 if (!builtin_canstore(oad, o)) {
112                                         *exceptionptr = proto_java_lang_ArrayStoreException;
113                                         return;
114                                 }
115                                 oad->data[dp + i] = o;
116                         }
117
118                 } else {
119                         /* XXX this does not completely obey the specification!
120                          * If an exception is thrown only the elements above
121                          * the current index have been copied. The
122                          * specification requires that only the elements
123                          * *below* the current index have been copied before
124                          * the throw.
125                          */
126                         for (i = len - 1; i >= 0; i--) {
127                                 java_objectheader *o = oas->data[sp + i];
128                                 if (!builtin_canstore(oad, o)) {
129                                         *exceptionptr = proto_java_lang_ArrayStoreException;
130                                         return;
131                                 }
132                                 oad->data[dp + i] = o;
133                         }
134                 }
135         }
136 }
137
138
139 void attach_property(char *name, char *value)
140 {
141         log_text("attach_property not supported");
142 #if 0
143         if (activeprops >= MAXPROPS) panic("Too many properties defined");
144         proplist[activeprops][0] = name;
145         proplist[activeprops][1] = value;
146         activeprops++;
147 #endif
148 }
149
150
151 /*
152  * Class:     java/lang/System
153  * Method:    identityHashCode
154  * Signature: (Ljava/lang/Object;)I
155  */
156 JNIEXPORT s4 JNICALL Java_java_lang_VMSystem_identityHashCode(JNIEnv *env, jclass clazz, java_lang_Object *par1)
157 {
158         return ((char*) par1) - ((char*) 0);    
159 }
160
161
162 /*
163  * These are local overrides for various environment variables in Emacs.
164  * Please do not remove this and leave it at the end of the file, where
165  * Emacs will automagically detect them.
166  * ---------------------------------------------------------------------
167  * Local variables:
168  * mode: c
169  * indent-tabs-mode: t
170  * c-basic-offset: 4
171  * tab-width: 4
172  * End:
173  */