Skip to content

Commit

Permalink
[feature] to_list added for classfile
Browse files Browse the repository at this point in the history
  • Loading branch information
lvntky committed Jun 20, 2024
1 parent be9a780 commit 728dd06
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/cvm/classfile/classfile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Classfile
uint64_t readLong(const uint8_t *bytes, size_t &offset);
double readDouble(const uint8_t *bytes, size_t &offset);
Classfile parseClassfile(const uint8_t *classBytes, size_t fileSize);
std::string to_string();
};

#endif //__CLASSFILE_HPP__
24 changes: 24 additions & 0 deletions src/classfile/classfile.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "../../include/cvm/classfile/classfile.hpp"

#include <spdlog/fmt/ostr.h>

#include <cstring>

#include "../../include/cvm/log.hpp"
Expand Down Expand Up @@ -47,6 +49,25 @@ double Classfile::readDouble(const uint8_t* bytes, size_t& offset)
spdlog::debug("Read double: {}, new offset: {}", value, offset);
return value;
}


std::string Classfile::to_string()
{
std::ostringstream oss;
oss << "Magic: " << std::hex << magic << std::dec << "\n"
<< "Minor Version: " << minor_version << "\n"
<< "Major Version: " << major_version << "\n"
<< "Constant Pool Count: " << constant_pool_count << "\n"
<< "Access Flags: " << access_flags << "\n"
<< "This Class: " << this_class << "\n"
<< "Super Class: " << super_class << "\n"
<< "Interfaces Count: " << interfaces_count << "\n"
<< "Fields Count: " << fields_count << "\n"
<< "Methods Count: " << methods_count << "\n"
<< "Attributes Count: " << attributes_count;
return oss.str();
}

/**
* Helper methods for validating etc.
*/
Expand Down Expand Up @@ -195,5 +216,8 @@ Classfile Classfile::parseClassfile(const uint8_t* classBytes, size_t fileSize)
cf.super_class = readShort(classBytes, offset);
cf.interfaces_count = readShort(classBytes, offset);

std::string classFileString = cf.to_string();
spdlog::info("The Parsed Classfile:\n{}", classFileString);

return cf;
}

0 comments on commit 728dd06

Please sign in to comment.