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)
75 value = Primitive::unbox(o);
77 set_primitive_element(index, value);
82 * Returns a primitive element of the given Java array.
84 imm_union Array::get_primitive_element(int32_t index)
91 exceptions_throw_nullpointerexception();
96 java_handle_array_t* a = _handle;
98 v = LLNI_vftbl_direct(a);
100 type = v->arraydesc->arraytype;
103 case ARRAYTYPE_BOOLEAN:
105 BooleanArray array(a);
106 value.i = array.get_element(index);
112 value.i = array.get_element(index);
118 value.i = array.get_element(index);
121 case ARRAYTYPE_SHORT:
124 value.i = array.get_element(index);
130 value.i = array.get_element(index);
136 value.l = array.get_element(index);
139 case ARRAYTYPE_FLOAT:
142 value.f = array.get_element(index);
145 case ARRAYTYPE_DOUBLE:
147 DoubleArray array(a);
148 value.d = array.get_element(index);
151 case ARRAYTYPE_OBJECT:
153 ObjectArray array(a);
154 value.a = array.get_element(index);
158 vm_abort("Array::primitive_element_get: invalid array element type %d",
167 * Sets a primitive element in the given Java array.
169 void Array::set_primitive_element(int32_t index, imm_union value)
175 exceptions_throw_nullpointerexception();
179 java_handle_array_t* a = _handle;
181 v = LLNI_vftbl_direct(a);
183 type = v->arraydesc->arraytype;
186 case ARRAYTYPE_BOOLEAN:
188 BooleanArray array(a);
189 array.set_element(index, value.i);
195 array.set_element(index, value.i);
201 array.set_element(index, value.i);
204 case ARRAYTYPE_SHORT:
207 array.set_element(index, value.i);
213 array.set_element(index, value.i);
219 array.set_element(index, value.l);
222 case ARRAYTYPE_FLOAT:
225 array.set_element(index, value.f);
228 case ARRAYTYPE_DOUBLE:
230 DoubleArray array(a);
231 array.set_element(index, value.d);
234 case ARRAYTYPE_OBJECT:
236 ObjectArray array(a);
237 array.set_element(index, static_cast<java_handle_t*>(value.a));
241 vm_abort("array_element_primitive_set: invalid array element type %d",
248 * Creates an array of references to the given class type on the heap.
249 * The handle pointer to the array can be NULL in case of an exception.
251 ObjectArray::ObjectArray(int32_t length, classinfo* componentclass)
252 : ArrayTemplate<java_handle_t*>(NULL)
255 assert(componentclass->state & CLASS_LOADED);
258 if (!(componentclass->state & CLASS_LINKED))
259 if (!link_class(componentclass)) {
264 classinfo* arrayclass = class_array_of(componentclass, true);
266 if (arrayclass == NULL) {
271 // Delegate allocation to generic array class
272 Array a(length, arrayclass);
274 _handle = a.get_handle();
279 * Creates an array of references to classinfos on the heap.
280 * The handle pointer to the array can be NULL in case of an exception.
282 ClassArray::ClassArray(int32_t length)
283 : ArrayTemplate<classinfo*>(NULL)
285 // Delegate allocation to object array class
286 ObjectArray oa(length, class_java_lang_Class);
288 _handle = oa.get_handle();
293 * These are local overrides for various environment variables in Emacs.
294 * Please do not remove this and leave it at the end of the file, where
295 * Emacs will automagically detect them.
296 * ---------------------------------------------------------------------
299 * indent-tabs-mode: t
303 * vim:noexpandtab:sw=4:ts=4: