Skip to content

Commit

Permalink
Add loading of classes from simple classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandezseb committed Dec 9, 2023
1 parent b36a00a commit 0b9d329
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,7 +44,6 @@ add_executable(${PROJECT_NAME}
src/ClassLoader/DescriptorParser.cpp



)

if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
Expand Down
16 changes: 13 additions & 3 deletions src/ClassLoader/ClassLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/ClassLoader/ClassLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
7 changes: 7 additions & 0 deletions src/VM/Configuration.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#pragma once

struct Configuration
{
const char* classPath = 0;
};
10 changes: 6 additions & 4 deletions src/VM/VM.cpp
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/VM/VM.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

#include "Core.h"
#include "DynamicArray.h"

#include "Configuration.h"
#include "ClassLoader/ClassLoader.h"

#include <vector>



class MethodArea {
public:
MethodArea() : classes(1000) {}
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 0b9d329

Please sign in to comment.