This commit introduces C++ wrapper classes for Java heap objects.
[cacao.git] / src / native / vm / gnuclasspath / gnu_java_lang_VMCPStringBuilder.cpp
1 /* src/native/vm/gnuclasspath/gnu_java_lang_VMCPStringBuilder.cpp
2
3    Copyright (C) 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <stdint.h>
29
30 #include "native/jni.h"
31 #include "native/llni.h"
32 #include "native/native.h"
33
34 // FIXME
35 //#include "native/include/gnu_java_lang_VMCPStringBuilder.h"
36
37 #include "vm/builtin.h"
38 #include "vm/exceptions.hpp"
39
40 #include "vmcore/globals.hpp"
41 #include "vmcore/javaobjects.hpp"
42
43
44 /*
45  * Class:     gnu/java/lang/VMCPStringBuilder
46  * Method:    toString
47  * Signature: ([CII)Ljava/lang/String;
48  */
49 JNIEXPORT jstring JNICALL Java_gnu_java_lang_VMCPStringBuilder_toString(JNIEnv *env, jclass clazz, jcharArray value, jint startIndex, jint count)
50 {
51         /* This is a native version of
52            java.lang.String.<init>([CIIZ)Ljava/lang/String; */
53
54     if (startIndex < 0) {
55 /*              exceptions_throw_stringindexoutofboundsexception("offset: " + offset); */
56                 exceptions_throw_stringindexoutofboundsexception();
57                 return NULL;
58         }
59
60     if (count < 0) {
61 /*              exceptions_throw_stringindexoutofboundsexception("count: " + count); */
62                 exceptions_throw_stringindexoutofboundsexception();
63                 return NULL;
64         }
65
66     /* equivalent to: offset + count < 0 || offset + count > data.length */
67
68         java_handle_chararray_t* ca = (java_handle_chararray_t*) value;
69
70         LLNI_CRITICAL_START;
71         int32_t length = LLNI_array_size(ca);
72         LLNI_CRITICAL_END;
73
74     if (length - startIndex < count) {
75 /*              exceptions_throw_stringindexoutofboundsexception("offset + count: " + (offset + count)); */
76                 exceptions_throw_stringindexoutofboundsexception();
77                 return NULL;
78         }
79
80         java_handle_t* h = builtin_new(class_java_lang_String);
81
82         if (h == NULL)
83                 return NULL;
84
85         java_lang_String s(h, ca, (int32_t) count, (int32_t) startIndex);
86
87         return (jstring) s.get_handle();
88 }
89
90
91 /* native methods implemented by this file ************************************/
92
93 static JNINativeMethod methods[] = {
94         { (char*) "toString", (char*) "([CII)Ljava/lang/String;", (void*) (uintptr_t) &Java_gnu_java_lang_VMCPStringBuilder_toString },
95 };
96
97
98 /* _Jv_gnu_java_lang_VMCPStringBuilder *****************************************
99
100    Register native functions.
101
102 *******************************************************************************/
103
104 // FIXME
105 extern "C" {
106 void _Jv_gnu_java_lang_VMCPStringBuilder_init(void)
107 {
108         utf *u;
109
110         u = utf_new_char("gnu/java/lang/VMCPStringBuilder");
111
112         native_method_register(u, methods, NATIVE_METHODS_COUNT);
113 }
114 }
115
116
117 /*
118  * These are local overrides for various environment variables in Emacs.
119  * Please do not remove this and leave it at the end of the file, where
120  * Emacs will automagically detect them.
121  * ---------------------------------------------------------------------
122  * Local variables:
123  * mode: c++
124  * indent-tabs-mode: t
125  * c-basic-offset: 4
126  * tab-width: 4
127  * End:
128  */