Signed-off-by: Tomek Grabiec <tgrab...@gmail.com> --- include/vm/object.h | 1 + vm/object.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 0 deletions(-)
diff --git a/include/vm/object.h b/include/vm/object.h index 375b6ba..26d5e31 100644 --- a/include/vm/object.h +++ b/include/vm/object.h @@ -44,5 +44,6 @@ void array_store_check(struct vm_object *arrayref, struct vm_object *obj); void array_store_check_vmtype(struct vm_object *arrayref, enum vm_type vm_type); void array_size_check(int size); void multiarray_size_check(int n, ...); +char *vm_string_to_cstr(struct vm_object *string); #endif diff --git a/vm/object.c b/vm/object.c index e0e35ff..70d6252 100644 --- a/vm/object.c +++ b/vm/object.c @@ -2,6 +2,7 @@ #include <errno.h> #include <stdbool.h> #include <stdlib.h> +#include <ctype.h> #include <jit/exception.h> @@ -382,3 +383,46 @@ void multiarray_size_check(int n, ...) va_end(ap); return; } + +char *vm_string_to_cstr(struct vm_object *string_obj) +{ + struct vm_object *array_object; + struct string *str; + int16_t *array; + int32_t offset; + int32_t count; + char *result; + + offset = *(int32_t *) + &string_obj->fields[vm_java_lang_String_offset->offset]; + count = *(int32_t *) + &string_obj->fields[vm_java_lang_String_count->offset]; + array_object = *(struct vm_object **) + &string_obj->fields[vm_java_lang_String_value->offset]; + array = (int16_t *) array_object->fields; + + str = alloc_str(); + if (!str) + return NULL; + + result = NULL; + + for (int32_t i = 0; i < count; ++i) { + int16_t ch = array[offset + i]; + int err; + + if (ch < 128 && isprint(ch)) + err = str_append(str, "%c", ch); + else + err = str_append(str, "<%d>", ch); + + if (err) + goto exit; + } + + result = strdup(str->value); + + exit: + free_str(str); + return result; +} -- 1.6.0.6 ------------------------------------------------------------------------------ _______________________________________________ Jatovm-devel mailing list Jatovm-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/jatovm-devel