From e1f18ffca70d4c8be056322de71d543ba9987a28 Mon Sep 17 00:00:00 2001 From: Sebastiaan Fernandez Date: Sun, 28 Jan 2024 00:45:13 +0100 Subject: [PATCH] Add simple java.lang.Thread.currentThread native implementation --- src/Library/java/lang/Thread.cpp | 10 ++++++++++ src/Library/java/lang/Thread.h | 3 ++- src/VM/JavaStack.h | 5 +++++ src/VM/VM.cpp | 1 + 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Library/java/lang/Thread.cpp b/src/Library/java/lang/Thread.cpp index 8e331cc..e1ca572 100644 --- a/src/Library/java/lang/Thread.cpp +++ b/src/Library/java/lang/Thread.cpp @@ -17,5 +17,15 @@ JCALL void lib_java_lang_Thread_registerNatives(NATIVE_ARGS) { + registerNative("java/lang/Thread/currentThread", "()Ljava/lang/Thread;", lib_java_lang_Thread_currentThread); +} + +JCALL void lib_java_lang_Thread_currentThread(NATIVE_ARGS) +{ + // TODO: Maybe check if an object was already created? + ClassInfo* threadClass = VM->getClass("java/lang/Thread", thread); + const u4 objectReference = heap->createObject(threadClass, VM); + StackFrame* returnFrame = thread->getTopFrameNonNative(); + returnFrame->pushObject(objectReference); } diff --git a/src/Library/java/lang/Thread.h b/src/Library/java/lang/Thread.h index c3db132..31f7e65 100644 --- a/src/Library/java/lang/Thread.h +++ b/src/Library/java/lang/Thread.h @@ -2,4 +2,5 @@ #include "Library/NativeDefs.h" -JCALL void lib_java_lang_Thread_registerNatives(NATIVE_ARGS); \ No newline at end of file +JCALL void lib_java_lang_Thread_registerNatives(NATIVE_ARGS); +JCALL void lib_java_lang_Thread_currentThread(NATIVE_ARGS); \ No newline at end of file diff --git a/src/VM/JavaStack.h b/src/VM/JavaStack.h index d855d7b..1e3b84e 100644 --- a/src/VM/JavaStack.h +++ b/src/VM/JavaStack.h @@ -79,6 +79,11 @@ struct StackFrame { operands.emplace_back(VariableType_FLOAT, std::bit_cast(value)); } + void pushObject(const u4 objectRef) + { + operands.emplace_back(VariableType_REFERENCE, objectRef); + } + [[nodiscard]] Variable peekOperand() const { return operands.back(); diff --git a/src/VM/VM.cpp b/src/VM/VM.cpp index a56fb20..423137e 100644 --- a/src/VM/VM.cpp +++ b/src/VM/VM.cpp @@ -40,6 +40,7 @@ void VM::start() getClass("java/lang/Class", &m_mainThread); getClass("java/lang/String", &m_mainThread); getClass("java/lang/System", &m_mainThread); + getClass("java/lang/Thread", &m_mainThread); } std::vector VM::createVariableForDescriptor(const char* descriptor)