diff --git a/c/native.c b/c/native.c index 1d0f22c..7ac847a 100644 --- a/c/native.c +++ b/c/native.c @@ -10,6 +10,7 @@ #include "native/object.h" #include "native/printstream.h" #include "native/runtime.h" +#include "native/system.h" typedef void (* native_func_t)(struct vm * vm, uint32_t * args); @@ -126,15 +127,9 @@ const static struct native_method native_method[] = { { .class_name = "java/lang/Object", .method_name = "_getClass", - .method_descriptor = "()Ljava/lang/Class;", + .method_descriptor = "(Ljava/lang/Object;)Ljava/lang/Class;", .func = native_java_lang_object_getclass_1, }, - { - .class_name = "java/lang/Object", - .method_name = "_hashCode", - .method_descriptor = "()I", - .func = native_java_lang_object_hashcode_1, - }, { .class_name = "java/lang/Runtime", .method_name = "_freeMemory", @@ -153,6 +148,12 @@ const static struct native_method native_method[] = { .method_descriptor = "()I", .func = native_java_lang_runtime_totalmemory_0, }, + { + .class_name = "java/lang/System", + .method_name = "_hashCode", + .method_descriptor = "(Ljava/lang/Object;)I", + .func = native_java_lang_system_hashcode_1, + }, }; struct hash_table_entry * native_init_hash_table(int * hash_table_length) diff --git a/c/native/object.c b/c/native/object.c index 1475f66..8e7c3d6 100644 --- a/c/native/object.c +++ b/c/native/object.c @@ -1,5 +1,4 @@ #include "object.h" -#include "printf.h" #include "vm_instance.h" void native_java_lang_object_getclass_1(struct vm * vm, uint32_t * args) @@ -15,9 +14,3 @@ void native_java_lang_object_getclass_1(struct vm * vm, uint32_t * args) operand_stack_push_ref(vm->current_frame, class_objectref); } - -void native_java_lang_object_hashcode_1(struct vm * vm, uint32_t * args) -{ - struct objectref * objectref = (struct objectref *)args[0]; - operand_stack_push_u32(vm->current_frame, (uint32_t)objectref); // objectref as integer -} diff --git a/c/native/object.h b/c/native/object.h index fb7e369..b9740a7 100644 --- a/c/native/object.h +++ b/c/native/object.h @@ -5,5 +5,3 @@ #include "frame.h" void native_java_lang_object_getclass_1(struct vm * vm, uint32_t * args); - -void native_java_lang_object_hashcode_1(struct vm * vm, uint32_t * args); diff --git a/c/native/system.c b/c/native/system.c new file mode 100644 index 0000000..ab573b9 --- /dev/null +++ b/c/native/system.c @@ -0,0 +1,7 @@ +#include "system.h" + +void native_java_lang_system_hashcode_1(struct vm * vm, uint32_t * args) +{ + struct objectref * objectref = (struct objectref *)args[0]; + operand_stack_push_u32(vm->current_frame, (uint32_t)objectref); // objectref as integer +} diff --git a/c/native/system.h b/c/native/system.h new file mode 100644 index 0000000..b624b8e --- /dev/null +++ b/c/native/system.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +#include "frame.h" + +void native_java_lang_system_hashcode_1(struct vm * vm, uint32_t * args); diff --git a/classes/java/lang/Object.java b/classes/java/lang/Object.java index 7f37027..e9f6db2 100644 --- a/classes/java/lang/Object.java +++ b/classes/java/lang/Object.java @@ -12,16 +12,14 @@ public class Object { return this == obj; } - private final native Class _getClass(); + private final native static Class _getClass(Object o); public final Class getClass() { - return _getClass(); + return _getClass(this); } - private native int _hashCode(); - public int hashCode() { - return _hashCode(); + return System.identityHashCode(this); } public String toString() { diff --git a/classes/java/lang/System.java b/classes/java/lang/System.java index c7e2da8..63b809d 100644 --- a/classes/java/lang/System.java +++ b/classes/java/lang/System.java @@ -3,11 +3,23 @@ package java.lang; import java.io.PrintStream; public final class System { - public static PrintStream out = null; + public static PrintStream err; + public static PrintStream out; private System() { } static { out = new PrintStream(); + err = new PrintStream(); + } + + public static void gc() { + Runtime.getRuntime().gc(); + } + + private final native static int _hashCode(Object x); + + public static int identityHashCode(Object x) { + return _hashCode(x); } } diff --git a/java.mk b/java.mk index 51b4fa4..b7ebe53 100644 --- a/java.mk +++ b/java.mk @@ -28,6 +28,7 @@ OBJ = \ c/native/object.o \ c/native/printstream.o \ c/native/runtime.o \ + c/native/system.o \ c/parse.o \ c/parse_type.o \ c/printf.o \