Skip to content

Commit

Permalink
Add simple java.lang.Thread.currentThread native implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandezseb committed Jan 27, 2024
1 parent c705569 commit e1f18ff
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/Library/java/lang/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

3 changes: 2 additions & 1 deletion src/Library/java/lang/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

#include "Library/NativeDefs.h"

JCALL void lib_java_lang_Thread_registerNatives(NATIVE_ARGS);
JCALL void lib_java_lang_Thread_registerNatives(NATIVE_ARGS);
JCALL void lib_java_lang_Thread_currentThread(NATIVE_ARGS);
5 changes: 5 additions & 0 deletions src/VM/JavaStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ struct StackFrame {
operands.emplace_back(VariableType_FLOAT, std::bit_cast<u4>(value));
}

void pushObject(const u4 objectRef)
{
operands.emplace_back(VariableType_REFERENCE, objectRef);
}

[[nodiscard]] Variable peekOperand() const
{
return operands.back();
Expand Down
1 change: 1 addition & 0 deletions src/VM/VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Variable> VM::createVariableForDescriptor(const char* descriptor)
Expand Down

0 comments on commit e1f18ff

Please sign in to comment.