diff --git a/CMakeLists.txt b/CMakeLists.txt index 803e24b..42717f7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ add_executable(${PROJECT_NAME} src/DynamicArray.h src/VM/VM.cpp src/VM/VM.h + src/VM/Configuration.h src/Data/Attribute.h src/Data/Attribute.cpp @@ -43,7 +44,6 @@ add_executable(${PROJECT_NAME} src/ClassLoader/DescriptorParser.cpp - ) if(CMAKE_SYSTEM_NAME STREQUAL "Windows") diff --git a/src/ClassLoader/ClassLoader.cpp b/src/ClassLoader/ClassLoader.cpp index f635b5d..144b677 100644 --- a/src/ClassLoader/ClassLoader.cpp +++ b/src/ClassLoader/ClassLoader.cpp @@ -243,7 +243,7 @@ ClassInfo* ClassLoader::readClass(ByteArray& byteArray) return classInfo; } -ClassInfo* ClassLoader::readClass(const char* className, Memory* memory) +ClassInfo* ClassLoader::readClass(const char* className, Memory* memory, const char* classPath) { this->memory = memory; char name[300] = {0}; @@ -253,10 +253,20 @@ ClassInfo* ClassLoader::readClass(const char* className, Memory* memory) if (file == NULL) { - fprintf(stderr, "Class file not found for className: %s\n", className); - Platform::exitProgram(-6); + char cpName[300] = {0}; + strcat(cpName, classPath); + strcat(cpName, name); + file = Platform::getFile(cpName); + + + if (file == NULL) + { + fprintf(stderr, "Class file not found for className: %s\n", className); + Platform::exitProgram(-6); + } } + size_t size; uint8_t* fileContent = Platform::readEntireFile(file, &size); ByteArray byteArray(fileContent, size); diff --git a/src/ClassLoader/ClassLoader.h b/src/ClassLoader/ClassLoader.h index b3b783b..2c93ea6 100644 --- a/src/ClassLoader/ClassLoader.h +++ b/src/ClassLoader/ClassLoader.h @@ -23,5 +23,5 @@ class ClassLoader { ClassInfo* readClass(ByteArray& byteArray); Memory* memory; public: - ClassInfo* readClass(const char* className, Memory* memory); + ClassInfo* readClass(const char* className, Memory* memory, const char* classPath); }; \ No newline at end of file diff --git a/src/VM/Configuration.h b/src/VM/Configuration.h new file mode 100644 index 0000000..a0d8f39 --- /dev/null +++ b/src/VM/Configuration.h @@ -0,0 +1,7 @@ + +#pragma once + +struct Configuration +{ + const char* classPath = 0; +}; \ No newline at end of file diff --git a/src/VM/VM.cpp b/src/VM/VM.cpp index 6e56e67..cebc2e3 100644 --- a/src/VM/VM.cpp +++ b/src/VM/VM.cpp @@ -1,15 +1,17 @@ #include "VM.h" +#include "Configuration.h" #include "../Memory.h" VM::VM() { } -void VM::start() +void VM::start(Configuration configuration) { - // getClass("java/lang/Object"); - // getClass("java/lang/String"); + this->configuration = configuration; + getClass("java/lang/Object"); + getClass("java/lang/String"); } ClassInfo* VM::getClassByName(const char* class_name) @@ -31,7 +33,7 @@ ClassInfo* VM::getClass(const char* className) if (classInfo == NULL) { Memory *memory = new Memory(2000, MIB(20)); printf("Loading class %s...\n", className); - ClassInfo *classInfo = bootstrapClassLoader.readClass(className, memory); + ClassInfo *classInfo = bootstrapClassLoader.readClass(className, memory, configuration.classPath); // TODO: Run static initializers (clinit) heap.methodArea.classes.add(classInfo); return classInfo; diff --git a/src/VM/VM.h b/src/VM/VM.h index a6e5a45..019c1e1 100644 --- a/src/VM/VM.h +++ b/src/VM/VM.h @@ -2,13 +2,12 @@ #include "Core.h" #include "DynamicArray.h" - +#include "Configuration.h" #include "ClassLoader/ClassLoader.h" #include - class MethodArea { public: MethodArea() : classes(1000) {} @@ -76,9 +75,10 @@ class VM { ClassLoader bootstrapClassLoader; JavaHeap heap; VMThread thread; + Configuration configuration; public: VM(); - void start(); + void start(Configuration configuration); ClassInfo* getClassByName(const char* class_name); ClassInfo* getClass(const char* className); void runMain(const char* className); diff --git a/src/main.cpp b/src/main.cpp index ee8e0b2..f6347b9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,13 @@ +#include "VM/Configuration.h" #include "VM/VM.h" int main(int argc, char* argv[]) { VM vm; - vm.start(); + Configuration config; + config.classPath = "C:/Users/Sebastiaan/.jdks/temurin-1.8.0_392/jre/lib/rt/"; + vm.start(config); vm.runMain("Main"); return 0; }