We actually need two functions for allocating strings -- one that converts from the class-format UTF-8 variant and one that converts from regular UTF-8. The second one is what we would use from C programs.
Note: We don't actually support code points >= 0x80 in the C variant (regular UTF-8) yet. Signed-off-by: Vegard Nossum <vegard.nos...@gmail.com> --- include/vm/object.h | 3 ++- jit/load-store-bc.c | 3 ++- test/vm/object-stub.c | 9 ++++++++- vm/jato.c | 2 +- vm/object.c | 36 ++++++++++++++++++++++++++++++++++-- vm/stack-trace.c | 4 ++-- 6 files changed, 49 insertions(+), 8 deletions(-) diff --git a/include/vm/object.h b/include/vm/object.h index 27ae579..64472b2 100644 --- a/include/vm/object.h +++ b/include/vm/object.h @@ -33,7 +33,8 @@ struct vm_object *vm_object_alloc_multi_array(struct vm_class *class, struct vm_object *vm_object_alloc_array(struct vm_class *class, int count); struct vm_object * -vm_object_alloc_string(const uint8_t bytes[], unsigned int length); +vm_object_alloc_string_from_utf8(const uint8_t bytes[], unsigned int length); +struct vm_object *vm_object_alloc_string_from_c(const char *bytes); struct vm_object *new_exception(const char *class_name, const char *message); diff --git a/jit/load-store-bc.c b/jit/load-store-bc.c index 7676630..4ca2ab3 100644 --- a/jit/load-store-bc.c +++ b/jit/load-store-bc.c @@ -120,7 +120,8 @@ static int __convert_ldc(struct parse_context *ctx, unsigned long cp_idx) } struct vm_object *string - = vm_object_alloc_string(utf8->bytes, utf8->length); + = vm_object_alloc_string_from_utf8(utf8->bytes, + utf8->length); if (!string) { NOT_IMPLEMENTED; break; diff --git a/test/vm/object-stub.c b/test/vm/object-stub.c index fc0a47d..24502a1 100644 --- a/test/vm/object-stub.c +++ b/test/vm/object-stub.c @@ -6,7 +6,14 @@ #include <vm/object.h> struct vm_object * -vm_object_alloc_string(const uint8_t bytes[], unsigned int length) +vm_object_alloc_string_from_utf8(const uint8_t bytes[], unsigned int length) +{ + NOT_IMPLEMENTED; + return NULL; +} + +struct vm_object * +vm_object_alloc_string_from_c(const char *bytes) { NOT_IMPLEMENTED; return NULL; diff --git a/vm/jato.c b/vm/jato.c index 26ae739..c7f31ac 100644 --- a/vm/jato.c +++ b/vm/jato.c @@ -124,7 +124,7 @@ native_vmclass_getname(struct vm_object *object) class = object->java_lang_Class_class; assert(class != NULL); - return vm_object_alloc_string(class->name, strlen(class->name)); + return vm_object_alloc_string_from_c(class->name); } static void jit_init_natives(void) diff --git a/vm/object.c b/vm/object.c index 149be66..26b1f76 100644 --- a/vm/object.c +++ b/vm/object.c @@ -179,7 +179,7 @@ void vm_object_unlock(struct vm_object *obj) } struct vm_object * -vm_object_alloc_string(const uint8_t bytes[], unsigned int length) +vm_object_alloc_string_from_utf8(const uint8_t bytes[], unsigned int length) { struct vm_object *array = utf8_to_char_array(bytes, length); if (!array) { @@ -200,6 +200,38 @@ vm_object_alloc_string(const uint8_t bytes[], unsigned int length) return string; } +struct vm_object * +vm_object_alloc_string_from_c(const char *bytes) +{ + struct vm_object *string = vm_object_alloc(vm_java_lang_String); + if (!string) { + NOT_IMPLEMENTED; + return NULL; + } + + unsigned int n = strlen(bytes); + struct vm_object *array + = vm_object_alloc_native_array(J_CHAR, n); + if (!array) { + NOT_IMPLEMENTED; + return NULL; + } + + /* XXX: Need to handle code points >= 0x80 */ + NOT_IMPLEMENTED; + + uint16_t *utf16_chars = (uint16_t *) &array->fields; + for (unsigned int i = 0; i < n; ++i) { + utf16_chars[i] = bytes[i]; + } + + field_set_int32(string, vm_java_lang_String_offset, 0); + field_set_int32(string, vm_java_lang_String_count, array->array_length); + field_set_object(string, vm_java_lang_String_value, array); + + return string; +} + typedef void (*exception_init_fn)(struct vm_object *, struct vm_object *); struct vm_object *new_exception(const char *class_name, const char *message) @@ -221,7 +253,7 @@ struct vm_object *new_exception(const char *class_name, const char *message) if (message == NULL) message_str = NULL; else - message_str = vm_object_alloc_string(message, strlen(message)); + message_str = vm_object_alloc_string_from_c(message); mb = vm_class_get_method(e_class, "<init>", "(Ljava/lang/String;)V"); diff --git a/vm/stack-trace.c b/vm/stack-trace.c index 265521e..08bbfe2 100644 --- a/vm/stack-trace.c +++ b/vm/stack-trace.c @@ -316,8 +316,8 @@ new_stack_trace_element(struct vm_method *mb, unsigned long bc_offset) file_name = NULL; class_dot_name = slash2dots(cb->name); - class_name = vm_object_alloc_string(class_dot_name, strlen(class_dot_name)); - method_name = vm_object_alloc_string(mb->name, strlen(mb->name)); + class_name = vm_object_alloc_string_from_c(class_dot_name); + method_name = vm_object_alloc_string_from_c(mb->name); free(class_dot_name); ste = vm_object_alloc(ste_class); -- 1.6.0.4 ------------------------------------------------------------------------------ _______________________________________________ Jatovm-devel mailing list Jatovm-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jatovm-devel