* src/vm/array.hpp: Implemented array access classes in C++.
[cacao.git] / src / native / vm / cldc1.1 / java_lang_String.cpp
index 94ee1a4e181be78e634884bd2ccf108d874eeb7a..a1f72ae7448efcd67ebf7bdca976aa1afa410221 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
-#include "native/jni.h"
-#include "native/llni.h"
-#include "native/native.h"
+#include "native/jni.hpp"
+#include "native/native.hpp"
 
-#include "native/include/java_lang_Object.h"
-
-// FIXME
-extern "C" {
-#include "native/include/java_lang_String.h"
-}
-
-#include "vm/string.hpp"
-
-
-/* native methods implemented by this file ************************************/
-static JNINativeMethod methods[] = {
-       { (char*) "hashCode",    (char*) "()I",                    (void*) (uintptr_t) &Java_java_lang_String_hashCode        },
-       { (char*) "indexOf",     (char*) "(I)I",                   (void*) (uintptr_t) &Java_java_lang_String_indexOf__I      },
-       { (char*) "indexOf",     (char*) "(II)I",                  (void*) (uintptr_t) &Java_java_lang_String_indexOf__II     },
-       { (char*) "lastIndexOf", (char*) "(I)I",                   (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__I  },
-       { (char*) "lastIndexOf", (char*) "(II)I",                  (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__II },
-#if 0
-       { (char*) "equals",      (char*) "(Ljava/lang/Object;)Z;", (void*) (uintptr_t) &Java_java_lang_String_equals          },
+#if defined(ENABLE_JNI_HEADERS)
+# include "native/include/java_lang_String.h"
 #endif
-       { (char*) "intern",      (char*) "()Ljava/lang/String;",   (void*) (uintptr_t) &Java_java_lang_String_intern          },
-};
 
-
-/* _Jv_java_lang_String_init ***************************************************
-   Register native functions.
-*******************************************************************************/
-// FIXME
-extern "C" {
-void _Jv_java_lang_String_init(void)
-{
-       utf *u;
-       u = utf_new_char("java/lang/String");
-       native_method_register(u, methods, NATIVE_METHODS_COUNT);
-}
-}
+#include "vm/javaobjects.hpp"
+#include "vm/string.hpp"
 
 
 // Native functions are exported as C functions.
@@ -85,24 +48,19 @@ extern "C" {
  * Method:    hashCode
  * Signature: ()I
  */
-JNIEXPORT s4 JNICALL Java_java_lang_String_hashCode(JNIEnv *env, java_lang_String *_this)
+JNIEXPORT jint JNICALL Java_java_lang_String_hashCode(JNIEnv *env, jstring _this)
 {
-       java_handle_chararray_t *value;
-       int32_t                 offset;
-       int32_t                 count;
-       s4                              hash;
-       s4                              i;
+       java_lang_String jls(_this);
 
-       /* get values from Java object */
-       
-       LLNI_field_get_val(_this, offset, offset);
-       LLNI_field_get_val(_this, count, count);
-       LLNI_field_get_ref(_this, value, value);
+       CharArray value(jls.get_value());
 
-       hash = 0;
+       int32_t offset = jls.get_offset();
+       int32_t count  = jls.get_count();
 
-       for (i = 0; i < count; i++) {
-               hash = (31 * hash) + LLNI_array_direct(value, offset + i);
+       int32_t hash = 0;
+
+       for (int32_t i = 0; i < count; i++) {
+               hash = (31 * hash) + value.get_element(offset + i);
        }
 
        return hash;
@@ -114,21 +72,17 @@ JNIEXPORT s4 JNICALL Java_java_lang_String_hashCode(JNIEnv *env, java_lang_Strin
  * Method:    indexOf
  * Signature: (I)I
  */
-JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__I(JNIEnv *env, java_lang_String *_this, s4 ch)
+JNIEXPORT jint JNICALL Java_java_lang_String_indexOf__I(JNIEnv *env, jstring _this, jint ch)
 {
-       java_handle_chararray_t *value;
-       int32_t                 offset;
-       int32_t                 count;
-       s4                              i;
+       java_lang_String jls(_this);
 
-       /* get values from Java object */
+       CharArray value(jls.get_value());
 
-       LLNI_field_get_val(_this, offset, offset);
-       LLNI_field_get_val(_this, count, count);
-       LLNI_field_get_ref(_this, value, value);
+       int32_t offset = jls.get_offset();
+       int32_t count  = jls.get_count();
 
-       for (i = 0; i < count; i++) {
-               if (LLNI_array_direct(value, offset + i) == ch) {
+       for (int32_t i = 0; i < count; i++) {
+               if (value.get_element(offset + i) == ch) {
                        return i;
                }
        }
@@ -142,29 +96,25 @@ JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__I(JNIEnv *env, java_lang_Str
  * Method:    indexOf
  * Signature: (II)I
  */
-JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, java_lang_String *_this, s4 ch, s4 fromIndex)
+JNIEXPORT jint JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, jstring _this, jint ch, jint fromIndex)
 {
-       java_handle_chararray_t *value;
-       int32_t                 offset;
-       int32_t                 count;
-       s4                              i;
+       java_lang_String jls(_this);
 
-       /* get values from Java object */
+       CharArray value(jls.get_value());
 
-       LLNI_field_get_val(_this, offset, offset);
-       LLNI_field_get_val(_this, count, count);
-       LLNI_field_get_ref(_this, value, value);
+       int32_t offset = jls.get_offset();
+       int32_t count  = jls.get_count();
 
        if (fromIndex < 0) {
                fromIndex = 0;
        }
        else if (fromIndex >= count) {
-               /* Note: fromIndex might be near -1>>>1. */
+               // Note: fromIndex might be near -1>>>1.
                return -1;
        }
 
-       for (i = fromIndex ; i < count ; i++) {
-               if (LLNI_array_direct(value, offset + i) == ch) {
+       for (int32_t i = fromIndex ; i < count ; i++) {
+               if (value.get_element(offset + i) == ch) {
                        return i;
                }
        }
@@ -173,44 +123,24 @@ JNIEXPORT s4 JNICALL Java_java_lang_String_indexOf__II(JNIEnv *env, java_lang_St
 }
 
 
-/*
- * Class:     java/lang/String
- * Method:    lastIndexOf
- * Signature: (I)I
- */
-JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, java_lang_String *_this, s4 ch)
-{
-       int32_t count;
-       
-       LLNI_field_get_val(_this, count, count);
-       
-       return Java_java_lang_String_lastIndexOf__II(env, _this, ch, count - 1);
-}
-
-
 /*
  * Class:     java/lang/String
  * Method:    lastIndexOf
  * Signature: (II)I
  */
-JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, java_lang_String *_this, s4 ch, s4 fromIndex)
+JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, jstring _this, jint ch, jint fromIndex)
 {
-       java_handle_chararray_t *value;
-       int32_t                 offset;
-       int32_t                 count;
-       s4                              start;
-       s4                              i;
+       java_lang_String jls(_this);
 
-       /* get values from Java object */
+       CharArray value(jls.get_value());
 
-       LLNI_field_get_val(_this, offset, offset);
-       LLNI_field_get_val(_this, count, count);
-       LLNI_field_get_ref(_this, value, value);
+       int32_t offset = jls.get_offset();
+       int32_t count  = jls.get_count();
 
-       start = ((fromIndex >= count) ? count - 1 : fromIndex);
+       int32_t start = ((fromIndex >= count) ? count - 1 : fromIndex);
 
-       for (i = start; i >= 0; i--) {
-               if (LLNI_array_direct(value, offset + i) == ch) {
+       for (int32_t i = start; i >= 0; i--) {
+               if (value.get_element(offset + i) == ch) {
                        return i;
                }
        }
@@ -219,49 +149,17 @@ JNIEXPORT s4 JNICALL Java_java_lang_String_lastIndexOf__II(JNIEnv *env, java_lan
 }
 
 
-#if 0
 /*
  * Class:     java/lang/String
- * Method:    equals
- * Signature: (Ljava/lang/Object;)Z;
+ * Method:    lastIndexOf
+ * Signature: (I)I
  */
-JNIEXPORT s4 JNICALL Java_java_lang_String_equals(JNIEnv *env, java_lang_String* _this, java_lang_Object *o)
+JNIEXPORT jint JNICALL Java_java_lang_String_lastIndexOf__I(JNIEnv *env, jstring _this, jint ch)
 {
-       java_lang_String*               s;
-       java_handle_chararray_t *value;
-       int32_t                 offset;
-       int32_t                 count;
-       java_handle_chararray_t *dvalue;
-       int32_t                 doffset;
-       int32_t                 dcount;
-       classinfo                       *c;
-       
-       LLNI_field_get_val(_this, offset, offset);
-       LLNI_field_get_val(_this, count, count);
-       LLNI_field_get_ref(_this, value, value);
-       LLNI_class_get(o, c);
-
-       /* TODO: is this the correct implementation for short-circuiting on object identity? */
-       if ((java_lang_Object*)_this == o)
-               return 1;
+       java_lang_String jls(_this);
        
-       if (c != class_java_lang_String) 
-               return 0;
-
-       s = (java_lang_String *) o;
-       LLNI_field_get_val(_this, offset, doffset);
-       LLNI_field_get_val(_this, count, dcount);
-       LLNI_field_get_ref(_this, value, dvalue);
-
-       if (count != dcount)
-               return 0;
-
-       return ( 0 == memcmp((void*)(LLNI_array_direct(value, offset)),
-                                                (void*)(LLNI_array_direct(dvalue, doffset),
-                                                count) );
-
+       return Java_java_lang_String_lastIndexOf__II(env, _this, ch, jls.get_count() - 1);
 }
-#endif
 
 
 /*
@@ -269,17 +167,49 @@ JNIEXPORT s4 JNICALL Java_java_lang_String_equals(JNIEnv *env, java_lang_String*
  * Method:    intern
  * Signature: ()Ljava/lang/String;
  */
-JNIEXPORT java_lang_String* JNICALL Java_java_lang_String_intern(JNIEnv *env, java_lang_String *_this)
+JNIEXPORT jstring JNICALL Java_java_lang_String_intern(JNIEnv *env, jstring _this)
 {
-       if (_this == NULL)
+       java_lang_String jls(_this);
+
+       if (jls.is_null())
                return NULL;
 
-       return (java_lang_String *) javastring_intern((java_handle_t *) _this);
+       return (jstring) javastring_intern(jls.get_handle());
 }
 
 } // extern "C"
 
 
+/* native methods implemented by this file ************************************/
+static JNINativeMethod methods[] = {
+       { (char*) "hashCode",    (char*) "()I",                    (void*) (uintptr_t) &Java_java_lang_String_hashCode        },
+       { (char*) "indexOf",     (char*) "(I)I",                   (void*) (uintptr_t) &Java_java_lang_String_indexOf__I      },
+       { (char*) "indexOf",     (char*) "(II)I",                  (void*) (uintptr_t) &Java_java_lang_String_indexOf__II     },
+       { (char*) "lastIndexOf", (char*) "(II)I",                  (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__II },
+       { (char*) "lastIndexOf", (char*) "(I)I",                   (void*) (uintptr_t) &Java_java_lang_String_lastIndexOf__I  },
+#if 0
+       { (char*) "equals",      (char*) "(Ljava/lang/Object;)Z;", (void*) (uintptr_t) &Java_java_lang_String_equals          },
+#endif
+       { (char*) "intern",      (char*) "()Ljava/lang/String;",   (void*) (uintptr_t) &Java_java_lang_String_intern          },
+};
+
+
+/* _Jv_java_lang_String_init ***************************************************
+   Register native functions.
+*******************************************************************************/
+void _Jv_java_lang_String_init(void)
+{
+       utf* u = utf_new_char("java/lang/String");
+       NativeMethods& nm = VM::get_current()->get_nativemethods();
+       nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
+}
+
+
 /*
  * These are local overrides for various environment variables in Emacs.
  * Please do not remove this and leave it at the end of the file, where