Skip to content

Commit

Permalink
Fix static initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandezseb committed Dec 15, 2023
1 parent 166b361 commit 2893fe8
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/VM/VM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void VM::start(Configuration configuration)
thread.pc = 0;
thread.currentClass = nullptr;
thread.currentMethod = nullptr;
thread.currentFrame = nullptr;

getClass("java/lang/OutOfMemoryError", &thread);
getClass("java/lang/VirtualMachineError", &thread);
Expand Down Expand Up @@ -218,7 +219,7 @@ void VM::executeLoop(VMThread* thread)
{
thread->currentFrame = 0;
}
return;
break;
}
case i_putstatic:
{
Expand Down Expand Up @@ -321,16 +322,16 @@ void VM::executeLoop(VMThread* thread)
// TODO: Do method call by pushing stack frame
fprintf(stderr, "Error: Running non-static method as static\n");
Platform::exitProgram(-10);
pushStackFrameStatic(targetClass, methodInfo, topFrame, thread);
}

if (methodInfo->isNative())
{
printf("Running native code of method: %s\n", methodInfo->name);
} else
{
fprintf(stderr, "Error: Running static methods not implemented yet\n");
Platform::exitProgram(-10);
pushStackFrameStatic(targetClass, methodInfo, topFrame, thread);
printf("> Created new stack frame for constructor call on: %s\n",
topFrame->constantPool->getString(targetClassInfo->nameIndex));
}
break;
}
Expand Down Expand Up @@ -449,10 +450,24 @@ void VM::runStaticInitializer(ClassInfo* classInfo, VMThread* thread)
return;
}

JavaStack oldStack = thread->stack;
u4 oldPc = thread->pc;
ClassInfo* oldCurrentClass = thread->currentClass;
MethodInfo* oldCurrentMethod = thread->currentMethod;
StackFrame* oldFrame = thread->currentFrame;
thread->stack.frames = std::vector<StackFrame>();
thread->stack.frames.reserve(200);

pushStackFrameWithoutParams(classInfo, entryPoint, thread);

printf("Executing static initializers...\n");
executeLoop(thread);

thread->pc = oldPc;
thread->currentClass = oldCurrentClass;
thread->currentMethod = oldCurrentMethod;
thread->stack.frames = oldStack.frames;
thread->currentFrame = oldFrame;
}

ClassInfo* VM::getClass(const char* className, VMThread* thread)
Expand Down Expand Up @@ -499,6 +514,5 @@ void VM::runMain(const char* className)

printf("> Executing main method...\n");
executeLoop(mainThread);
ClassInfo* clasInfo = heap.getClassByName("Brol");
printf("> Done executing\n");
}

0 comments on commit 2893fe8

Please sign in to comment.