diff --git a/Makefile b/Makefile index da83501..04e6d61 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ CC ?= gcc ARCH = -m32 CFLAGS += -Wall -Werror -Wfatal-errors -Wno-error=unused-variable -fstack-protector -std=c2x -g CFLAGS += -DDEBUG -CFLAGS += -DDEBUG_PRINT +#CFLAGS += -DDEBUG_PRINT LDFLAGS = -lm OPT ?= -O0 DEPFLAGS = -MMD -MP diff --git a/c/class_resolver.c b/c/class_resolver.c index e4819b4..a03d7ef 100644 --- a/c/class_resolver.c +++ b/c/class_resolver.c @@ -464,6 +464,8 @@ struct method_entry class_resolver_lookup_method_from_interfacemethodref_index(i assert(class_entry != nullptr); } + print__class_file__class_name(objectref_class_entry->class_file); + printc('\n'); assert(!"interfacemethodref method does not exist"); } diff --git a/c/debug.c b/c/debug.c index e5545ac..7b6b9cd 100644 --- a/c/debug.c +++ b/c/debug.c @@ -39,6 +39,13 @@ void debug_print__method_info__method_name(struct class_file * class_file, struc debug_print__constant__method_name(method_name_constant, method_descriptor_constant); } +void print__string(const uint8_t * bytes, int length) +{ + for (int i = 0; i < length; i++) { + printc(bytes[i]); + } +} + void print__constant__utf8_string(struct constant * constant) { assert(constant->tag == CONSTANT_Utf8); diff --git a/c/debug.h b/c/debug.h index face1d8..c6a7dd7 100644 --- a/c/debug.h +++ b/c/debug.h @@ -9,6 +9,7 @@ void debug_print__class_file__class_name(struct class_file * class_file); void debug_print__constant__method_name(struct constant * name_constant, struct constant * descriptor_constant); void debug_print__method_info__method_name(struct class_file * class_file, struct method_info * method_info); +void print__string(const uint8_t * bytes, int length); void print__constant__utf8_string(struct constant * constant); void print__class_file__class_name(struct class_file * class_file); void print__constant__method_name(struct constant * name_constant, struct constant * descriptor_constant); diff --git a/c/frame.c b/c/frame.c index f96feab..fd0d2d3 100644 --- a/c/frame.c +++ b/c/frame.c @@ -497,7 +497,14 @@ void vm_exception(struct vm * vm, int32_t * objectref) while (vm->frame_stack.ix > 0) { for (int i = 0; i < vm->current_frame->code_attribute->exception_table_length; i++) { struct exception_table_entry * entry = &vm->current_frame->code_attribute->exception_table[i]; - if (vm->current_frame->pc >= entry->start_pc && vm->current_frame->pc < entry->end_pc) { + bool caught = + vm->current_frame->pc >= entry->start_pc && + vm->current_frame->pc < entry->end_pc && + exception_class_entry == class_resolver_lookup_class_from_class_index(vm->class_hash_table.length, + vm->class_hash_table.entry, + vm->current_frame->class_entry, + entry->catch_type); + if (caught) { operand_stack_push_u32(vm->current_frame, (uint32_t)objectref); vm->current_frame->next_pc = entry->handler_pc; @@ -515,10 +522,27 @@ void vm_exception(struct vm * vm, int32_t * objectref) vm->current_frame = stack_pop_frame(&vm->frame_stack, 1); } + prints("exception: "); + print__class_file__class_name(exception_class_entry->class_file); + printc('\n'); + { + int32_t * string_objectref = (int32_t *)objectref[2]; + if (string_objectref != nullptr) { + prints(" message: "); + struct class_entry * string_class_entry = (struct class_entry *)string_objectref[0]; + prints("(class: "); + print__class_file__class_name(string_class_entry->class_file); + printc(')'); + prints("\n "); + int32_t * arrayref = (int32_t *)string_objectref[1]; + int32_t length = arrayref[0]; + uint8_t * bytes = (uint8_t *)&arrayref[1]; + print__string(bytes, length); + printc('\n'); + } + } assert(objectref[1] != 0); backtrace_print((struct backtrace *)objectref[1]); - - assert(!"exception"); } static void print_vm_stack(struct vm * vm) diff --git a/java/io/PrintStream.java b/java/io/PrintStream.java index 4af2b96..297a9bf 100644 --- a/java/io/PrintStream.java +++ b/java/io/PrintStream.java @@ -48,6 +48,10 @@ public class PrintStream write(String.valueOf(obj)); } + public void print(String s) { + write(String.valueOf(s)); + } + public void println() { write(newline); } @@ -91,4 +95,9 @@ public class PrintStream write(String.valueOf(obj)); write(newline); } + + public void println(String s) { + write(String.valueOf(s)); + write(newline); + } } diff --git a/p/TestException.java b/p/TestException.java index c4ec118..6db2e0a 100644 --- a/p/TestException.java +++ b/p/TestException.java @@ -1,20 +1,27 @@ package p; +class Exception2 extends Exception { + public Exception2(String message) { + super(message); + } +} + class TestException { - static void test2() throws Exception { - throw new Exception("asdf exception"); + static void test2() throws Exception2 { + throw new Exception2("asdf exception"); } - static void test() throws Exception { - // try { + static void test() throws Exception2 { + try { test2(); - //} catch (Exception e) { - //System.out.println(e); - //throw e; - //} + } catch (Exception e) { + System.out.print("exception handler: "); + System.out.println(e); + throw e; + } } - public static void main() throws Exception { + public static void main() throws Exception2 { test(); } }