System: add identityHashCode

This commit is contained in:
Zack Buhman 2025-01-14 00:03:42 -06:00
parent 8036060321
commit 00e505b667
8 changed files with 39 additions and 22 deletions

View File

@ -10,6 +10,7 @@
#include "native/object.h" #include "native/object.h"
#include "native/printstream.h" #include "native/printstream.h"
#include "native/runtime.h" #include "native/runtime.h"
#include "native/system.h"
typedef void (* native_func_t)(struct vm * vm, uint32_t * args); 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", .class_name = "java/lang/Object",
.method_name = "_getClass", .method_name = "_getClass",
.method_descriptor = "()Ljava/lang/Class;", .method_descriptor = "(Ljava/lang/Object;)Ljava/lang/Class;",
.func = native_java_lang_object_getclass_1, .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", .class_name = "java/lang/Runtime",
.method_name = "_freeMemory", .method_name = "_freeMemory",
@ -153,6 +148,12 @@ const static struct native_method native_method[] = {
.method_descriptor = "()I", .method_descriptor = "()I",
.func = native_java_lang_runtime_totalmemory_0, .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) struct hash_table_entry * native_init_hash_table(int * hash_table_length)

View File

@ -1,5 +1,4 @@
#include "object.h" #include "object.h"
#include "printf.h"
#include "vm_instance.h" #include "vm_instance.h"
void native_java_lang_object_getclass_1(struct vm * vm, uint32_t * args) 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); 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
}

View File

@ -5,5 +5,3 @@
#include "frame.h" #include "frame.h"
void native_java_lang_object_getclass_1(struct vm * vm, uint32_t * args); 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);

7
c/native/system.c Normal file
View File

@ -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
}

7
c/native/system.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include <stdint.h>
#include "frame.h"
void native_java_lang_system_hashcode_1(struct vm * vm, uint32_t * args);

View File

@ -12,16 +12,14 @@ public class Object {
return this == obj; return this == obj;
} }
private final native Class<?> _getClass(); private final native static Class<?> _getClass(Object o);
public final Class<?> getClass() { public final Class<?> getClass() {
return _getClass(); return _getClass(this);
} }
private native int _hashCode();
public int hashCode() { public int hashCode() {
return _hashCode(); return System.identityHashCode(this);
} }
public String toString() { public String toString() {

View File

@ -3,11 +3,23 @@ package java.lang;
import java.io.PrintStream; import java.io.PrintStream;
public final class System { public final class System {
public static PrintStream out = null; public static PrintStream err;
public static PrintStream out;
private System() { private System() {
} }
static { static {
out = new PrintStream(); 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);
} }
} }

View File

@ -28,6 +28,7 @@ OBJ = \
c/native/object.o \ c/native/object.o \
c/native/printstream.o \ c/native/printstream.o \
c/native/runtime.o \ c/native/runtime.o \
c/native/system.o \
c/parse.o \ c/parse.o \
c/parse_type.o \ c/parse_type.o \
c/printf.o \ c/printf.o \