Skip to content

Commit 464868f

Browse files
committed
Refactor thread creation and add more implementations
1 parent 013aa4f commit 464868f

File tree

9 files changed

+56
-10
lines changed

9 files changed

+56
-10
lines changed

src/Library/Builtin.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ void registerBuiltinRegisterNatives()
4747
registerNative("sun/misc/VM/initialize", "()V", lib_sun_misc_VM_initialize);
4848
// Security API
4949
registerNative("java/security/AccessController/doPrivileged", "(Ljava/security/PrivilegedAction;)Ljava/lang/Object;", lib_java_security_AccessController_doPriviliged);
50+
registerNative("java/security/AccessController/getStackAccessControlContext", "()Ljava/security/AccessControlContext;", lib_java_security_AccessController_getStackAccessControlContext);
5051
// Vigur/lang package
5152
// registerNative("Vigur/lang/System/registerNatives", "()V", lib_Vigur_lang_System_registerNatives);
5253
// Reflection API

src/Library/java/lang/Thread.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ JCALL void lib_java_lang_Thread_registerNatives(NATIVE_ARGS)
2222

2323
JCALL void lib_java_lang_Thread_currentThread(NATIVE_ARGS)
2424
{
25-
// TODO: Maybe check if an object was already created?
26-
ClassInfo* threadClass = VM->getClass("java/lang/Thread", thread);
27-
const u4 objectReference = heap->createObject(threadClass, VM);
2825
StackFrame* returnFrame = thread->getTopFrameNonNative();
29-
returnFrame->pushObject(objectReference);
26+
returnFrame->pushObject(thread->threadObject);
3027
}
3128

src/Library/java/security/AccessController.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717

1818
void lib_java_security_AccessController_doPriviliged(NATIVE_ARGS)
1919
{
20-
StackFrame* currentFrame = thread->m_currentFrame;
21-
Variable objectVar = currentFrame->localVariables[0];
20+
const StackFrame* currentFrame = thread->m_currentFrame;
21+
const Variable objectVar = currentFrame->localVariables[0];
2222
const Object* method = heap->getObject(currentFrame->localVariables[0].data);
23-
MethodInfo* methodInfo = method->classInfo->findMethodWithNameAndDescriptor("run", "()Ljava/lang/Object;");
23+
const MethodInfo* methodInfo = method->classInfo->findMethodWithNameAndDescriptor("run", "()Ljava/lang/Object;");
2424
ClassInfo* classInfo = method->classInfo;
2525

2626
thread->pushStackFrameWithoutParams(classInfo, methodInfo);
27-
2827
thread->m_currentFrame->localVariables[0] = objectVar;
2928

3029
VM->executeLoop(thread);
3130
}
31+
32+
void lib_java_security_AccessController_getStackAccessControlContext(NATIVE_ARGS)
33+
{
34+
thread->returnVar(Variable{VariableType_REFERENCE, 0});
35+
}

src/Library/java/security/AccessController.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
#include "Library/NativeDefs.h"
44

5-
JCALL void lib_java_security_AccessController_doPriviliged(NATIVE_ARGS);
5+
JCALL void lib_java_security_AccessController_doPriviliged(NATIVE_ARGS);
6+
JCALL void lib_java_security_AccessController_getStackAccessControlContext(NATIVE_ARGS);

src/VM/Instructions/ComparisonInstructions.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,22 @@ void if_icmplt(INSTRUCTION_ARGS)
210210
}
211211
}
212212

213+
void if_icmpge(INSTRUCTION_ARGS)
214+
{
215+
const u1 byte1 = args[0];
216+
const u1 byte2 = args[1];
217+
218+
const i2 branchByte = (byte1 << 8) | byte2;
219+
220+
Variable var2 = thread->m_currentFrame->popOperand();
221+
Variable var1 = thread->m_currentFrame->popOperand();
222+
223+
if (std::bit_cast<i4>(var1.data) >= std::bit_cast<i4>(var2.data))
224+
{
225+
thread->m_pc = thread->m_pc-3+branchByte;
226+
}
227+
}
228+
213229
void if_icmpgt(INSTRUCTION_ARGS)
214230
{
215231
const u1 byte1 = args[0];

src/VM/Instructions/ComparisonInstructions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ void ifgt(INSTRUCTION_ARGS);
1616
void ifle(INSTRUCTION_ARGS);
1717
void if_icmpne(INSTRUCTION_ARGS);
1818
void if_icmplt(INSTRUCTION_ARGS);
19+
void if_icmpge(INSTRUCTION_ARGS);
1920
void if_icmpgt(INSTRUCTION_ARGS);
2021
void if_icmple(INSTRUCTION_ARGS);
2122
void if_acmpne(INSTRUCTION_ARGS);

src/VM/VM.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ void VM::start()
4141
getClass("java/lang/String", &m_mainThread);
4242
getClass("java/lang/System", &m_mainThread);
4343
getClass("java/lang/Thread", &m_mainThread);
44+
getClass("java/lang/ThreadGroup", &m_mainThread);
45+
46+
const u4 threadGroupReference = createThreadGroupObject(&m_mainThread);
47+
m_mainThread.threadObject = createThreadObject(&m_mainThread, threadGroupReference);
48+
}
49+
50+
u4 VM::createThreadGroupObject(VMThread* thread)
51+
{
52+
ClassInfo* threadGroupClass = getClass("java/lang/ThreadGroup", thread);
53+
return m_heap.createObject(threadGroupClass, this);
54+
}
55+
56+
u4 VM::createThreadObject(VMThread* thread, const u4 threadGroupReference)
57+
{
58+
ClassInfo* threadClass = getClass("java/lang/Thread", thread);
59+
const u4 objectReference = m_heap.createObject(threadClass, this);
60+
const Object* threadObject = m_heap.getObject(objectReference);
61+
62+
FieldData* field = threadObject->getField("group", "Ljava/lang/ThreadGroup;", &m_heap);
63+
field->data->data = threadGroupReference;
64+
65+
return objectReference;
4466
}
4567

4668
std::vector<Variable> VM::createVariableForDescriptor(const char* descriptor)

src/VM/VM.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class VM {
4848
void executeLoop(VMThread* thread);
4949
static void checkType(Variable var, VariableType type, VMThread *thread);
5050
private:
51-
inline static constexpr std::array<Instruction,107> m_instructions{{
51+
inline static constexpr std::array<Instruction,108> m_instructions{{
5252
// Constants
5353
{i_nop, 0, "nop", 0, nop},
5454
{i_aconst_null, 0, "aconst_null", 0, aconst_null},
@@ -136,6 +136,7 @@ class VM {
136136
{i_ifle, 2, "ifle", 0, ifle},
137137
{i_if_icmpne, 2, "if_icmpne", 0, if_icmpne},
138138
{i_if_icmplt, 2, "if_icmplt", 0, if_icmplt},
139+
{i_if_icmpge, 2, "if_icmpge", 0, if_icmpge},
139140
{i_if_icmpgt, 2, "if_icmpgt", 0, if_icmpgt},
140141
{i_if_icmple, 2, "if_icmple", 0, if_icmple},
141142
{i_if_acmpne, 2, "if_acmpne", 0, if_acmpne},
@@ -173,6 +174,8 @@ class VM {
173174
Configuration m_configuration;
174175
void initStaticFields(ClassInfo* class_info, VMThread* thread);
175176
void runStaticInitializer(ClassInfo* classInfo, VMThread* thread);
177+
u4 createThreadObject(VMThread* thread, u4 threadGroupReference);
178+
u4 createThreadGroupObject(VMThread* thread);
176179
};
177180

178181

src/VM/VMThread.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class VMThread
1717
// current class
1818
ClassInfo* m_currentClass{nullptr};
1919
std::string_view m_name;
20+
u4 threadObject{0};
2021

2122
explicit VMThread(const std::string_view name, const size_t frameSize) noexcept
2223
: m_stack(frameSize), m_name(name)

0 commit comments

Comments
 (0)