Changes: Christian Thalinger
Edwin Steiner
- $Id: string.c 4876 2006-05-05 15:26:04Z edwin $
+ $Id: string.c 5823 2006-10-24 23:24:19Z edwin $
*/
#include "mm/memory.h"
#include "native/include/java_lang_String.h"
+
+#if defined(ENABLE_THREADS)
+# include "threads/native/lock.h"
+#else
+# include "threads/none/lock.h"
+#endif
+
+#include "vm/builtin.h"
#include "vm/exceptions.h"
#include "vm/loader.h"
#include "vm/options.h"
hashtable hashtable_string; /* hashtable for javastrings */
-#if defined(USE_THREADS)
+#if defined(ENABLE_THREADS)
static java_objectheader *lock_hashtable_string;
#endif
hashtable_create(&hashtable_string, HASHTABLE_STRING_SIZE);
-#if defined(USE_THREADS)
+#if defined(ENABLE_THREADS)
/* create string hashtable lock object */
lock_hashtable_string = NEW(java_objectheader);
-# if defined(NATIVE_THREADS)
- initObjectLock(lock_hashtable_string);
-# endif
+ lock_init_object_lock(lock_hashtable_string);
#endif
/* everything's ok */
}
+/* javastring_safe_new_from_utf8 ***********************************************
+
+ Create a new object of type java/lang/String with the text from
+ the specified UTF-8 string. This function is safe for invalid UTF-8.
+ (Invalid characters will be replaced by U+fffd.)
+
+ IN:
+ text.........the UTF-8 string, zero-terminated.
+
+ RETURN VALUE:
+ the java.lang.String object, or
+ NULL if an exception has been thrown
+
+*******************************************************************************/
+
+java_lang_String *javastring_safe_new_from_utf8(const char *text)
+{
+ java_lang_String *s; /* result-string */
+ java_chararray *a;
+ s4 nbytes;
+ s4 len;
+
+ assert(text);
+
+ /* Get number of bytes. We need this to completely emulate the messy */
+ /* behaviour of the RI. :( */
+
+ nbytes = strlen(text);
+
+ /* calculate number of Java characters */
+
+ len = utf8_safe_number_of_u2s(text, nbytes);
+
+ /* allocate the String object and the char array */
+
+ s = (java_lang_String *) builtin_new(class_java_lang_String);
+ a = builtin_newarray_char(len);
+
+ /* javastring or character-array could not be created? */
+
+ if (!a || !s)
+ return NULL;
+
+ /* decompress UTF-8 string */
+
+ utf8_safe_convert_to_u2s(text, nbytes, a->data);
+
+ /* set fields of the String object */
+
+ s->value = a;
+ s->offset = 0;
+ s->count = len;
+
+ return s;
+}
+
+
/* javastring_new_from_utf_string **********************************************
Create a new object of type java/lang/String with the text from
*******************************************************************************/
-utf *javastring_toutf(java_lang_String *string, bool isclassname)
+utf *javastring_toutf(java_lang_String *s, bool isclassname)
{
- java_lang_String *str = (java_lang_String *) string;
+ if (s == NULL)
+ return utf_null;
- return utf_new_u2(str->value->data + str->offset, str->count, isclassname);
+ return utf_new_u2(s->value->data + s->offset, s->count, isclassname);
}
s4 javastring_strlen(java_lang_String *s)
{
- if (!s)
+ if (s == NULL)
return 0;
return s->count;
u4 slot;
u2 i;
-#if defined(USE_THREADS)
- builtin_monitorenter(lock_hashtable_string);
-#endif
+ LOCK_MONITOR_ENTER(lock_hashtable_string);
/* find location in hashtable */
if (!copymode)
mem_free(a, sizeof(java_chararray) + sizeof(u2) * (length - 1) + 10);
-#if defined(USE_THREADS)
- builtin_monitorexit(lock_hashtable_string);
-#endif
+ LOCK_MONITOR_EXIT(lock_hashtable_string);
return (java_objectheader *) js;
}
primitivetype_table[ARRAYTYPE_CHAR].arrayvftbl;
stringdata->header.size = length;
- /* XXX TWISTI: is this necessary? */
- if (!class_java_lang_String)
- class_java_lang_String = load_class_bootstrap(utf_java_lang_String);
-
assert(class_java_lang_String);
assert(class_java_lang_String->state & CLASS_LOADED);
/* if we use eager loading, we have to check loaded String class */
if (opt_eager)
- list_addfirst(&unlinkedclasses, class_java_lang_String);
+ list_add_first(&unlinkedclasses, class_java_lang_String);
/* create new javastring */
js = NEW(java_lang_String);
-#if defined(USE_THREADS) && defined(NATIVE_THREADS)
- initObjectLock(&js->header);
+#if defined(ENABLE_THREADS)
+ lock_init_object_lock(&js->header);
#endif
js->header.vftbl = class_java_lang_String->vftbl;
hashtable_string = newhash;
}
-#if defined(USE_THREADS)
- builtin_monitorexit(lock_hashtable_string);
-#endif
+ LOCK_MONITOR_EXIT(lock_hashtable_string);
return (java_objectheader *) js;
}