1 /* src/vm/array.cpp - Java array functions
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 Copyright (C) 2008 Theobroma Systems Ltd.
7 This file is part of CACAO.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2, or (at
12 your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
31 #include "native/llni.h"
33 #include "vm/array.hpp"
34 #include "vm/exceptions.hpp"
35 #include "vm/global.h"
36 #include "vm/globals.hpp"
37 #include "vm/primitive.hpp"
42 * Returns a boxed element of the given Java array.
44 java_handle_t* Array::get_boxed_element(int32_t index)
52 exceptions_throw_nullpointerexception();
56 v = LLNI_vftbl_direct(_handle);
58 type = v->arraydesc->arraytype;
60 value = get_primitive_element(index);
62 o = Primitive::box(type, value);
69 * Sets a boxed element in the given Java array.
71 void Array::set_boxed_element(int32_t index, java_handle_t *o)
78 exceptions_throw_nullpointerexception();
82 v = LLNI_vftbl_direct(_handle);
84 type = v->arraydesc->arraytype;
86 // Special handling for object arrays.
87 if (type == ARRAYTYPE_OBJECT) {
88 ObjectArray array(_handle);
89 array.set_element(index, o);
93 // Check if primitive type can be stored.
94 if (!Primitive::unbox_typed(o, type, &value)) {
95 /* exceptions_throw_illegalargumentexception("argument type mismatch"); */
96 exceptions_throw_illegalargumentexception();
100 set_primitive_element(index, value);
105 * Returns a primitive element of the given Java array.
107 imm_union Array::get_primitive_element(int32_t index)
114 exceptions_throw_nullpointerexception();
119 java_handle_array_t* a = _handle;
121 v = LLNI_vftbl_direct(a);
123 type = v->arraydesc->arraytype;
126 case ARRAYTYPE_BOOLEAN:
128 BooleanArray array(a);
129 value.i = array.get_element(index);
135 value.i = array.get_element(index);
141 value.i = array.get_element(index);
144 case ARRAYTYPE_SHORT:
147 value.i = array.get_element(index);
153 value.i = array.get_element(index);
159 value.l = array.get_element(index);
162 case ARRAYTYPE_FLOAT:
165 value.f = array.get_element(index);
168 case ARRAYTYPE_DOUBLE:
170 DoubleArray array(a);
171 value.d = array.get_element(index);
174 case ARRAYTYPE_OBJECT:
176 ObjectArray array(a);
177 value.a = array.get_element(index);
181 vm_abort("Array::primitive_element_get: invalid array element type %d",
190 * Sets a primitive element in the given Java array.
192 void Array::set_primitive_element(int32_t index, imm_union value)
198 exceptions_throw_nullpointerexception();
202 java_handle_array_t* a = _handle;
204 v = LLNI_vftbl_direct(a);
206 type = v->arraydesc->arraytype;
209 case ARRAYTYPE_BOOLEAN:
211 BooleanArray array(a);
212 array.set_element(index, value.i);
218 array.set_element(index, value.i);
224 array.set_element(index, value.i);
227 case ARRAYTYPE_SHORT:
230 array.set_element(index, value.i);
236 array.set_element(index, value.i);
242 array.set_element(index, value.l);
245 case ARRAYTYPE_FLOAT:
248 array.set_element(index, value.f);
251 case ARRAYTYPE_DOUBLE:
253 DoubleArray array(a);
254 array.set_element(index, value.d);
257 case ARRAYTYPE_OBJECT:
259 ObjectArray array(a);
260 array.set_element(index, static_cast<java_handle_t*>(value.a));
264 vm_abort("array_element_primitive_set: invalid array element type %d",
271 * Creates an array of references to the given class type on the heap.
272 * The handle pointer to the array can be NULL in case of an exception.
274 ObjectArray::ObjectArray(int32_t length, classinfo* componentclass)
275 : ArrayTemplate<java_handle_t*>(NULL)
278 assert(componentclass->state & CLASS_LOADED);
281 if (!(componentclass->state & CLASS_LINKED))
282 if (!link_class(componentclass)) {
287 classinfo* arrayclass = class_array_of(componentclass, true);
289 if (arrayclass == NULL) {
294 // Delegate allocation to generic array class
295 Array a(length, arrayclass);
297 _handle = a.get_handle();
302 * Creates an array of references to classinfos on the heap.
303 * The handle pointer to the array can be NULL in case of an exception.
305 ClassArray::ClassArray(int32_t length)
306 : ArrayTemplate<classinfo*>(NULL)
308 // Delegate allocation to object array class
309 ObjectArray oa(length, class_java_lang_Class);
311 _handle = oa.get_handle();
316 * These are local overrides for various environment variables in Emacs.
317 * Please do not remove this and leave it at the end of the file, where
318 * Emacs will automagically detect them.
319 * ---------------------------------------------------------------------
322 * indent-tabs-mode: t
326 * vim:noexpandtab:sw=4:ts=4: