PR164: Get rid of mysterious 10 trailing bytes in literal strings.
[cacao.git] / src / vm / string.cpp
index ee0b2c8669eee26fefa4839c3d761950bde16530..f8e419f182e1cb04aa52fdb92d6397f00a6cb36c 100644 (file)
@@ -1,6 +1,6 @@
 /* src/vm/string.cpp - java.lang.String related functions
 
-   Copyright (C) 1996-2005, 2006, 2007, 2008
+   Copyright (C) 1996-2005, 2006, 2007, 2008, 2010
    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
 #include <assert.h>
 
-#include "vmcore/os.hpp"
+#include "vm/os.hpp"
 
 #include "vm/types.h"
 
 #include "vm/global.h"
 
-#include "mm/memory.h"
+#include "mm/memory.hpp"
 
-#include "native/jni.h"
 #include "native/llni.h"
 
-#include "native/include/java_lang_String.h"
+#include "threads/lock.hpp"
 
-#include "threads/lock-common.h"
-
-#include "vm/array.h"
-#include "vm/builtin.h"
+#include "vm/array.hpp"
+#include "vm/jit/builtin.hpp"
 #include "vm/exceptions.hpp"
+#include "vm/globals.hpp"
+#include "vm/javaobjects.hpp"
+#include "vm/options.h"
 #include "vm/primitive.hpp"
+#include "vm/statistics.h"
 #include "vm/string.hpp"
-#include "vm/vm.hpp"
-
-#include "vmcore/globals.hpp"
-#include "vmcore/options.h"
-#include "vmcore/statistics.h"
-#include "vmcore/utf8.h"
+#include "vm/utf8.h"
 
 
 /* global variables ***********************************************************/
 
 #define HASHTABLE_STRING_SIZE    2048   /* initial size of javastring-hash    */
 
-hashtable hashtable_string;             /* hashtable for javastrings          */
-
-#if defined(ENABLE_THREADS)
-static java_object_t *lock_hashtable_string;
-#endif
-
-
-/* XXX preliminary typedef, will be removed once string.c and utf8.c are
-   unified. */
-
-#if defined(ENABLE_HANDLES)
-typedef heap_java_lang_String heapstring_t;
-#else
-typedef java_lang_String heapstring_t;
-#endif
+static hashtable hashtable_string;      /* hashtable for javastrings          */
+static Mutex* mutex;
 
 
 /* string_init *****************************************************************
@@ -92,13 +75,7 @@ bool string_init(void)
 
        hashtable_create(&hashtable_string, HASHTABLE_STRING_SIZE);
 
-#if defined(ENABLE_THREADS)
-       /* create string hashtable lock object */
-
-       lock_hashtable_string = NEW(java_object_t);
-
-       LOCK_INIT_OBJECT_LOCK(lock_hashtable_string);
-#endif
+       mutex = new Mutex();
 
        /* everything's ok */
 
@@ -116,8 +93,6 @@ bool string_init(void)
  
 void stringtable_update(void)
 {
-       heapstring_t     *js;
-       java_chararray_t *a;
        literalstring    *s;       /* hashtable entry */
 
        for (unsigned int i = 0; i < hashtable_string.size; i++) {
@@ -125,22 +100,21 @@ void stringtable_update(void)
 
                if (s) {
                        while (s) {
-                               js = (heapstring_t *) s->string;
+                               // FIXME
+                               java_lang_String js(LLNI_WRAP(s->string));
                                
-                               if ((js == NULL) || (js->value == NULL)) {
+                               if (js.is_null() || (js.get_value() == NULL)) {
                                        /* error in hashtable found */
-
-                                       vm_abort("stringtable_update: invalid literalstring in hashtable");
+                                       os::abort("stringtable_update: invalid literalstring in hashtable");
                                }
 
-                               a = js->value;
+                               java_chararray_t* a = (java_chararray_t*) js.get_value();
 
-                               if (!js->header.vftbl) 
-                                       /* vftbl of javastring is NULL */ 
-                                       js->header.vftbl = class_java_lang_String->vftbl;
+                               if (js.get_vftbl() == NULL)
+                                       // FIXME
+                                       LLNI_UNWRAP(js.get_handle())->vftbl = class_java_lang_String->vftbl;
 
-                               if (!a->header.objheader.vftbl) 
-                                       /* vftbl of character-array is NULL */ 
+                               if (a->header.objheader.vftbl == NULL)
                                        a->header.objheader.vftbl = Primitive::get_arrayclass_by_type(ARRAYTYPE_CHAR)->vftbl;
 
                                /* follow link in external hash chain */
@@ -166,44 +140,37 @@ void stringtable_update(void)
 
 *******************************************************************************/
 
-static java_handle_t *javastring_new_from_utf_buffer(const char *buffer,
-                                                                                                                u4 blength)
+static java_handle_t *javastring_new_from_utf_buffer(const char *buffer, u4 blength)
 {
        const char *utf_ptr;            /* current utf character in utf string    */
-       u4 utflength;                   /* length of utf-string if uncompressed   */
-       java_handle_t     *o;
-       java_lang_String  *s;           /* result-string                          */
-       java_handle_chararray_t *a;
-       u4 i;
 
        assert(buffer);
 
-       utflength = utf_get_number_of_u2s_for_buffer(buffer,blength);
+       int32_t utflength = utf_get_number_of_u2s_for_buffer(buffer, blength);
 
-       o = builtin_new(class_java_lang_String);
-       a = builtin_newarray_char(utflength);
+       java_handle_t*           h  = builtin_new(class_java_lang_String);
+       CharArray ca(utflength);
 
        /* javastring or character-array could not be created */
 
-       if ((o == NULL) || (a == NULL))
+       if ((h == NULL) || ca.is_null())
                return NULL;
 
+       // XXX: Fix me!
+       uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
+
        /* decompress utf-string */
 
        utf_ptr = buffer;
 
-       for (i = 0; i < utflength; i++)
-               LLNI_array_direct(a, i) = utf_nextu2((char **) &utf_ptr);
+       for (int32_t i = 0; i < utflength; i++)
+               ptr[i] = utf_nextu2((char **) &utf_ptr);
        
        /* set fields of the javastring-object */
 
-       s = (java_lang_String *) o;
+       java_lang_String jls(h, ca.get_handle(), utflength);
 
-       LLNI_field_set_ref(s, value , a);
-       LLNI_field_set_val(s, offset, 0);
-       LLNI_field_set_val(s, count , utflength);
-
-       return o;
+       return jls.get_handle();
 }
 
 
@@ -224,47 +191,40 @@ static java_handle_t *javastring_new_from_utf_buffer(const char *buffer,
 
 java_handle_t *javastring_safe_new_from_utf8(const char *text)
 {
-       java_handle_t           *o;
-       java_handle_chararray_t *a;
-       java_lang_String        *s;
-       s4 nbytes;
-       s4 len;
-
        if (text == NULL)
                return NULL;
 
        /* Get number of bytes. We need this to completely emulate the messy */
        /* behaviour of the RI. :(                                           */
 
-       nbytes = strlen(text);
+       int32_t nbytes = strlen(text);
 
        /* calculate number of Java characters */
 
-       len = utf8_safe_number_of_u2s(text, nbytes);
+       int32_t len = utf8_safe_number_of_u2s(text, nbytes);
 
        /* allocate the String object and the char array */
 
-       o = builtin_new(class_java_lang_String);
-       a = builtin_newarray_char(len);
+       java_handle_t*           h  = builtin_new(class_java_lang_String);
+       CharArray ca(len);
 
        /* javastring or character-array could not be created? */
 
-       if ((o == NULL) || (a == NULL))
+       if ((h == NULL) || ca.is_null())
                return NULL;
 
+       // XXX: Fix me!
+       uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
+
        /* decompress UTF-8 string */
 
-       utf8_safe_convert_to_u2s(text, nbytes, LLNI_array_data(a));
+       utf8_safe_convert_to_u2s(text, nbytes, ptr);
 
        /* set fields of the String object */
 
-       s = (java_lang_String *) o;
-
-       LLNI_field_set_ref(s, value , a);
-       LLNI_field_set_val(s, offset, 0);
-       LLNI_field_set_val(s, count , len);
+       java_lang_String jls(h, ca.get_handle(), len);
 
-       return o;
+       return jls.get_handle();
 }
 
 
@@ -302,42 +262,35 @@ java_handle_t *javastring_new_from_utf_string(const char *utfstr)
 
 java_handle_t *javastring_new(utf *u)
 {
-       char *utf_ptr;                  /* current utf character in utf string    */
-       int32_t utflength;                   /* length of utf-string if uncompressed   */
-       java_handle_t           *o;
-       java_handle_chararray_t *a;
-       java_lang_String        *s;
-
        if (u == NULL) {
                exceptions_throw_nullpointerexception();
                return NULL;
        }
 
-       utf_ptr = u->text;
-       utflength = utf_get_number_of_u2s(u);
+       char*   utf_ptr   = u->text;
+       int32_t utflength = utf_get_number_of_u2s(u);
 
-       o = builtin_new(class_java_lang_String);
-       a = builtin_newarray_char(utflength);
+       java_handle_t*           h  = builtin_new(class_java_lang_String);
+       CharArray ca(utflength);
 
        /* javastring or character-array could not be created */
 
-       if ((o == NULL) || (a == NULL))
+       if ((h == NULL) || ca.is_null())
                return NULL;
 
+       // XXX: Fix me!
+       uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
+
        /* decompress utf-string */
 
        for (int32_t i = 0; i < utflength; i++)
-               LLNI_array_direct(a, i) = utf_nextu2(&utf_ptr);
+               ptr[i] = utf_nextu2(&utf_ptr);
        
        /* set fields of the javastring-object */
 
-       s = (java_lang_String *) o;
-
-       LLNI_field_set_ref(s, value , a);
-       LLNI_field_set_val(s, offset, 0);
-       LLNI_field_set_val(s, count , utflength);
+       java_lang_String jls(h, ca.get_handle(), utflength);
 
-       return o;
+       return jls.get_handle();
 }
 
 
@@ -352,46 +305,40 @@ java_handle_t *javastring_new(utf *u)
 
 java_handle_t *javastring_new_slash_to_dot(utf *u)
 {
-       char *utf_ptr;                  /* current utf character in utf string    */
-       int32_t utflength;                   /* length of utf-string if uncompressed   */
-       java_handle_t           *o;
-       java_handle_chararray_t *a;
-       java_lang_String        *s;
-       u2 ch;
-
        if (u == NULL) {
                exceptions_throw_nullpointerexception();
                return NULL;
        }
 
-       utf_ptr = u->text;
-       utflength = utf_get_number_of_u2s(u);
+       char*   utf_ptr   = u->text;
+       int32_t utflength = utf_get_number_of_u2s(u);
 
-       o = builtin_new(class_java_lang_String);
-       a = builtin_newarray_char(utflength);
+       java_handle_t*           h  = builtin_new(class_java_lang_String);
+       CharArray ca(utflength);
 
        /* javastring or character-array could not be created */
-       if ((o == NULL) || (a == NULL))
+       if ((h == NULL) || ca.is_null())
                return NULL;
 
+       // XXX: Fix me!
+       uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
+
        /* decompress utf-string */
 
        for (int32_t i = 0; i < utflength; i++) {
-               ch = utf_nextu2(&utf_ptr);
+               uint16_t ch = utf_nextu2(&utf_ptr);
+
                if (ch == '/')
                        ch = '.';
-               LLNI_array_direct(a, i) = ch;
+
+               ptr[i] = ch;
        }
        
        /* set fields of the javastring-object */
 
-       s = (java_lang_String *) o;
-
-       LLNI_field_set_ref(s, value , a);
-       LLNI_field_set_val(s, offset, 0);
-       LLNI_field_set_val(s, count , utflength);
+       java_lang_String jls(h, ca.get_handle(), utflength);
 
-       return o;
+       return jls.get_handle();
 }
 
 
@@ -411,41 +358,34 @@ java_handle_t *javastring_new_slash_to_dot(utf *u)
 
 java_handle_t *javastring_new_from_ascii(const char *text)
 {
-       s4 i;
-       s4 len;                             /* length of the string               */
-       java_handle_t           *o;
-       java_lang_String        *s;
-       java_handle_chararray_t *a;
-
        if (text == NULL) {
                exceptions_throw_nullpointerexception();
                return NULL;
        }
 
-       len = strlen(text);
+       int32_t len = strlen(text);
 
-       o = builtin_new(class_java_lang_String);
-       a = builtin_newarray_char(len);
+       java_handle_t*           h  = builtin_new(class_java_lang_String);
+       CharArray ca(len);
 
        /* javastring or character-array could not be created */
 
-       if ((o == NULL) || (a == NULL))
+       if ((h == NULL) || ca.is_null())
                return NULL;
 
+       // XXX: Fix me!
+       uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
+
        /* copy text */
 
-       for (i = 0; i < len; i++)
-               LLNI_array_direct(a, i) = text[i];
+       for (int32_t i = 0; i < len; i++)
+               ptr[i] = text[i];
        
        /* set fields of the javastring-object */
 
-       s = (java_lang_String *) o;
-
-       LLNI_field_set_ref(s, value , a);
-       LLNI_field_set_val(s, offset, 0);
-       LLNI_field_set_val(s, count , len);
+       java_lang_String jls(h, ca.get_handle(), len);
 
-       return o;
+       return jls.get_handle();
 }
 
 
@@ -459,30 +399,29 @@ java_handle_t *javastring_new_from_ascii(const char *text)
        
 *******************************************************************************/
 
-char *javastring_tochar(java_handle_t *so) 
+char* javastring_tochar(java_handle_t* h)
 {
-       java_lang_String        *s = (java_lang_String *) so;
-       java_handle_chararray_t *a;
-       int32_t                  count;
-       int32_t                  offset;
-       char *buf;
-       s4 i;
-       
-       if (!s)
+       java_lang_String jls(h);
+
+       if (jls.is_null())
                return (char*) "";
 
-       LLNI_field_get_ref(s, value, a);
+       CharArray ca(jls.get_value());
 
-       if (!a)
+       if (ca.is_null())
                return (char*) "";
 
-       LLNI_field_get_val(s, count, count);
-       LLNI_field_get_val(s, offset, offset);
+       int32_t count  = jls.get_count();
+       int32_t offset = jls.get_offset();
 
-       buf = MNEW(char, count + 1);
+       char* buf = MNEW(char, count + 1);
 
+       // XXX: Fix me!
+       uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
+
+       int32_t i;
        for (i = 0; i < count; i++)
-               buf[i] = LLNI_array_direct(a, offset + i);
+               buf[i] = ptr[offset + i];
 
        buf[i] = '\0';
 
@@ -498,25 +437,23 @@ char *javastring_tochar(java_handle_t *so)
 
 utf *javastring_toutf(java_handle_t *string, bool isclassname)
 {
-       java_lang_String        *s;
-       java_handle_chararray_t *value;
-       int32_t                  count;
-       int32_t                  offset;
-
-       s = (java_lang_String *) string;
+       java_lang_String jls(string);
 
-       if (s == NULL)
+       if (jls.is_null())
                return utf_null;
 
-       LLNI_field_get_ref(s, value, value);
+       CharArray ca(jls.get_value());
 
-       if (value == NULL)
+       if (ca.is_null())
                return utf_null;
 
-       LLNI_field_get_val(s, count, count);
-       LLNI_field_get_val(s, offset, offset);
+       int32_t count  = jls.get_count();
+       int32_t offset = jls.get_offset();
 
-       return utf_new_u2(LLNI_array_data(value) + offset, count, isclassname);
+       // XXX: Fix me!
+       uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
+
+       return utf_new_u2(ptr + offset, count, isclassname);
 }
 
 
@@ -529,86 +466,93 @@ utf *javastring_toutf(java_handle_t *string, bool isclassname)
 
 *******************************************************************************/
 
-static java_object_t *literalstring_u2(java_chararray_t *a, int32_t length,
+static java_handle_t *literalstring_u2(java_handle_chararray_t *a, int32_t length,
                                                                           u4 offset, bool copymode)
 {
-    literalstring    *s;                /* hashtable element                  */
-    heapstring_t     *js;               /* u2-array wrapped in javastring     */
-    java_chararray_t *ca;               /* copy of u2-array                   */
-    u4                key;
-    u4                slot;
-    u2                i;
+       literalstring    *s;                /* hashtable element                  */
+       u4                key;
+       u4                slot;
+
+       mutex->lock();
 
-       LOCK_MONITOR_ENTER(lock_hashtable_string);
+       // XXX: Fix me!
+       CharArray ca(a);
+       uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
 
-    /* find location in hashtable */
+       /* find location in hashtable */
 
-    key  = unicode_hashkey(a->data + offset, length);
-    slot = key & (hashtable_string.size - 1);
-    s    = (literalstring*) hashtable_string.ptr[slot];
+       key  = unicode_hashkey(ptr + offset, length);
+       slot = key & (hashtable_string.size - 1);
+       s    = (literalstring*) hashtable_string.ptr[slot];
 
-    while (s) {
-               js = (heapstring_t *) s->string;
+       while (s) {
+               // FIXME
+               java_lang_String js(LLNI_WRAP(s->string));
 
-               if (length == js->count) {
+               if (length == js.get_count()) {
                        /* compare text */
 
-                       for (i = 0; i < length; i++)
-                               if (a->data[offset + i] != js->value->data[i])
+                       for (int32_t i = 0; i < length; i++) {
+                               // FIXME This is not handle capable!
+                               CharArray jsca(js.get_value());
+                               uint16_t* sptr = (uint16_t*) jsca.get_raw_data_ptr();
+                               
+                               if (ptr[offset + i] != sptr[i])
                                        goto nomatch;
+                       }
 
                        /* string already in hashtable, free memory */
 
                        if (!copymode)
-                               mem_free(a, sizeof(java_chararray_t) + sizeof(u2) * (length - 1) + 10);
+                               mem_free(a, sizeof(java_chararray_t) + sizeof(u2) * (length - 1));
 
-                       LOCK_MONITOR_EXIT(lock_hashtable_string);
+                       mutex->unlock();
 
-                       return (java_object_t *) js;
+                       return js.get_handle();
                }
 
        nomatch:
                /* follow link in external hash chain */
                s = s->hashlink;
-    }
+       }
 
-    if (copymode) {
+       java_chararray_t* acopy;
+       if (copymode) {
                /* create copy of u2-array for new javastring */
-               u4 arraysize = sizeof(java_chararray_t) + sizeof(u2) * (length - 1) + 10;
-               ca = (java_chararray_t*) mem_alloc(arraysize);
+               u4 arraysize = sizeof(java_chararray_t) + sizeof(u2) * (length - 1);
+               acopy = (java_chararray_t*) mem_alloc(arraysize);
 /*             memcpy(ca, a, arraysize); */
-               memcpy(&(ca->header), &(a->header), sizeof(java_array_t));
-               memcpy(&(ca->data), &(a->data) + offset, sizeof(u2) * (length - 1) + 10);
-
-    } else {
-               ca = a;
+               memcpy(&(acopy->header), &(((java_chararray_t*) a)->header), sizeof(java_array_t));
+               memcpy(&(acopy->data), &(((java_chararray_t*) a)->data) + offset, sizeof(u2) * length);
+       }
+       else {
+               acopy = (java_chararray_t*) a;
        }
 
-    /* location in hashtable found, complete arrayheader */
+       /* location in hashtable found, complete arrayheader */
 
-    ca->header.objheader.vftbl = Primitive::get_arrayclass_by_type(ARRAYTYPE_CHAR)->vftbl;
-    ca->header.size            = length;
+       acopy->header.objheader.vftbl = Primitive::get_arrayclass_by_type(ARRAYTYPE_CHAR)->vftbl;
+       acopy->header.size            = length;
 
        assert(class_java_lang_String);
        assert(class_java_lang_String->state & CLASS_LOADED);
 
-       /* create new javastring */
-
-       js = NEW(heapstring_t);
+       // Create a new java.lang.String object on the system heap.
+       java_object_t* o = (java_object_t*) MNEW(uint8_t, class_java_lang_String->instancesize);
 
 #if defined(ENABLE_STATISTICS)
        if (opt_stat)
-               size_string += sizeof(heapstring_t);
+               size_string += sizeof(class_java_lang_String->instancesize);
 #endif
 
 #if defined(ENABLE_THREADS)
-       lock_init_object_lock(&js->header);
+       Lockword(o->lockword).init();
 #endif
 
-       js->header.vftbl = class_java_lang_String->vftbl;
-       js->value  = ca;
-       js->offset = 0;
-       js->count  = length;
+       o->vftbl = class_java_lang_String->vftbl;
+
+       CharArray cacopy((java_handle_chararray_t*) acopy);
+       java_lang_String jls(o, cacopy.get_handle(), length);
 
        /* create new literalstring */
 
@@ -620,7 +564,7 @@ static java_object_t *literalstring_u2(java_chararray_t *a, int32_t length,
 #endif
 
        s->hashlink = (literalstring*) hashtable_string.ptr[slot];
-       s->string   = (java_object_t *) js;
+       s->string   = (java_object_t*) LLNI_UNWRAP(jls.get_handle());
        hashtable_string.ptr[slot] = s;
 
        /* update number of hashtable entries */
@@ -636,7 +580,6 @@ static java_object_t *literalstring_u2(java_chararray_t *a, int32_t length,
                u4             i;
                literalstring *s;
                literalstring *nexts;
-               heapstring_t  *tmpjs;
                hashtable      newhash;                          /* the new hashtable */
       
                /* create new hashtable, double the size */
@@ -651,8 +594,9 @@ static java_object_t *literalstring_u2(java_chararray_t *a, int32_t length,
 
                        while (s) {
                                nexts = s->hashlink;
-                               tmpjs = (heapstring_t *) s->string;
-                               slot  = unicode_hashkey(tmpjs->value->data, tmpjs->count) & (newhash.size - 1);
+                               java_lang_String tmpjls(LLNI_WRAP(s->string));
+                               // FIXME This is not handle capable!
+                               slot  = unicode_hashkey(((java_chararray_t*) LLNI_UNWRAP(tmpjls.get_value()))->data, tmpjls.get_count()) & (newhash.size - 1);
          
                                s->hashlink = (literalstring*) newhash.ptr[slot];
                                newhash.ptr[slot] = s;
@@ -668,9 +612,9 @@ static java_object_t *literalstring_u2(java_chararray_t *a, int32_t length,
                hashtable_string = newhash;
        }
 
-       LOCK_MONITOR_EXIT(lock_hashtable_string);
+       mutex->unlock();
 
-       return (java_object_t *) js;
+       return (java_object_t*) LLNI_UNWRAP(jls.get_handle());
 }
 
 
@@ -683,23 +627,22 @@ static java_object_t *literalstring_u2(java_chararray_t *a, int32_t length,
 
 java_object_t *literalstring_new(utf *u)
 {
-    char             *utf_ptr;       /* pointer to current unicode character  */
+       char             *utf_ptr;       /* pointer to current unicode character  */
                                         /* utf string                            */
-    u4                utflength;     /* length of utf-string if uncompressed  */
-    java_chararray_t *a;             /* u2-array constructed from utf string  */
-    u4                i;
+       int32_t           utflength;     /* length of utf-string if uncompressed  */
+       java_chararray_t *a;             /* u2-array constructed from utf string  */
 
        utf_ptr = u->text;
        utflength = utf_get_number_of_u2s(u);
 
-    /* allocate memory */ 
-    a = (java_chararray_t*) mem_alloc(sizeof(java_chararray_t) + sizeof(u2) * (utflength - 1) + 10);
+       /* allocate memory */ 
+       a = (java_chararray_t*) mem_alloc(sizeof(java_chararray_t) + sizeof(u2) * (utflength - 1));
 
-    /* convert utf-string to u2-array */
-    for (i = 0; i < utflength; i++)
+       /* convert utf-string to u2-array */
+       for (int32_t i = 0; i < utflength; i++)
                a->data[i] = utf_nextu2(&utf_ptr);
 
-    return literalstring_u2(a, utflength, 0, false);
+       return literalstring_u2((java_handle_chararray_t*) a, utflength, 0, false);
 }
 
 
@@ -724,7 +667,7 @@ static void literalstring_free(java_object_t* string)
        FREE(s, heapstring_t);
 
        /* dispose memory of java-characterarray */
-       FREE(a, sizeof(java_chararray_t) + sizeof(u2) * (a->header.size - 1)); /* +10 ?? */
+       FREE(a, sizeof(java_chararray_t) + sizeof(u2) * (a->header.size - 1));
 }
 #endif
 
@@ -733,29 +676,20 @@ static void literalstring_free(java_object_t* string)
 
    Intern the given Java string.
 
-   XXX NOTE: Literal Strings are direct references since they are not placed
-   onto the GC-Heap. That's why this function looks so "different".
-
 *******************************************************************************/
 
-java_handle_t *javastring_intern(java_handle_t *s)
+java_handle_t *javastring_intern(java_handle_t *string)
 {
-       java_lang_String *so;
-       java_chararray_t *value;
-       int32_t           count;
-       int32_t           offset;
-/*     java_lang_String *o; */
-       java_object_t    *o; /* XXX see note above */
+       java_lang_String jls(string);
 
-       so = (java_lang_String *) s;
+       CharArray ca(jls.get_value());
 
-       value  = LLNI_field_direct(so, value); /* XXX see note above */
-       LLNI_field_get_val(so, count, count);
-       LLNI_field_get_val(so, offset, offset);
+       int32_t count  = jls.get_count();
+       int32_t offset = jls.get_offset();
 
-       o = literalstring_u2(value, count, offset, true);
+       java_handle_t* o = literalstring_u2(ca.get_handle(), count, offset, true);
 
-       return LLNI_WRAP(o); /* XXX see note above */
+       return o;
 }
 
 
@@ -767,21 +701,18 @@ java_handle_t *javastring_intern(java_handle_t *s)
 
 void javastring_fprint(java_handle_t *s, FILE *stream)
 {
-       java_lang_String        *so;
-       java_handle_chararray_t *value;
-       int32_t                  count;
-       int32_t                  offset;
-       uint16_t                 c;
-       int                      i;
+       java_lang_String jls(s);
+
+       CharArray ca(jls.get_value());
 
-       so = (java_lang_String *) s;
+       int32_t count  = jls.get_count();
+       int32_t offset = jls.get_offset();
 
-       LLNI_field_get_ref(so, value, value);
-       LLNI_field_get_val(so, count, count);
-       LLNI_field_get_val(so, offset, offset);
+       // XXX: Fix me!
+       uint16_t* ptr = (uint16_t*) ca.get_raw_data_ptr();
 
-       for (i = offset; i < offset + count; i++) {
-               c = LLNI_array_direct(value, i);
+       for (int32_t i = offset; i < offset + count; i++) {
+               uint16_t c = ptr[i];
                fputc(c, stream);
        }
 }