From 65e88b7f76988400d124b9b56b8080e0a63a7bb0 Mon Sep 17 00:00:00 2001 From: calcium Date: Mon, 19 Aug 2024 20:10:15 -0400 Subject: [PATCH] Added improved support for the .class file spec --- src/Method/MethodParser.cpp | 81 +++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 16 deletions(-) diff --git a/src/Method/MethodParser.cpp b/src/Method/MethodParser.cpp index 93c21f9..0820eeb 100644 --- a/src/Method/MethodParser.cpp +++ b/src/Method/MethodParser.cpp @@ -1,18 +1,21 @@ -#include "ConstantPoolTags/Tags.h" -#include -#include +#include "Attributes.h" #include -#include +#include +#include #include +#include +#include +#include #include #include -#include +#include #include +#include void Methods::MethodParser(method_info &p, JavaClassFormat &jc) { - p.access_flags = Utils::ReadFromStartIntoVal(Globals::buffer()); - p.name_index = Utils::ReadFromStartIntoVal(Globals::buffer()); + p.access_flags = Utils::ReadFromStartIntoVal(Globals::buffer()); + p.name_index = Utils::ReadFromStartIntoVal(Globals::buffer()); p.descriptor_index = Utils::ReadFromStartIntoVal(Globals::buffer()); p.attributes_count = Utils::ReadFromStartIntoVal(Globals::buffer()); @@ -22,24 +25,70 @@ void Methods::MethodParser(method_info &p, JavaClassFormat &jc) for (int i = 0; i < p.attributes_count.ToHostFormat(); i++) { - attribute_info a; + Attributes::CODE_Attribute a; a.attribute_name_index = Utils::ReadFromStartIntoVal(Globals::buffer()); - a.attribute_length = Utils::ReadFromStartIntoVal(Globals::buffer()); - for (int j = 0; j < a.attribute_length.ToHostFormat(); j++) + auto v = jc.constant_pool[a.attribute_name_index.ToHostFormat() - 1]; + ASSERT(VEC_ELEM_TYPE(v) == TYPE(ConstantPoolTags::ConstantUtf8Info)); + ConstantPoolTags::ConstantUtf8Info str = std::any_cast(v); + + ASSERT(strcmp(reinterpret_cast(str.bytes), "Code") == 0); + + u4 attribute_length = Utils::ReadFromStartIntoVal(Globals::buffer()); + + std::cout << "(attr)" << a.attribute_name_index.ToHostFormat() << std::endl; + + a.max_stack = Utils::ReadFromStartIntoVal(Globals::buffer()); + a.max_locals = Utils::ReadFromStartIntoVal(Globals::buffer()); + a.code_length = Utils::ReadFromStartIntoVal(Globals::buffer()); + + for (int i = 0; i < a.code_length.ToHostFormat(); i++) { - a.info.push_back(Utils::ReadFromStartIntoVal(Globals::buffer())); + a.code.push_back(Utils::ReadFromStartIntoVal(Globals::buffer())); } + a.exception_table_length = Utils::ReadFromStartIntoVal(Globals::buffer()); + + for (int i = 0; i < a.exception_table_length.ToHostFormat(); i++) + { + a.exceptions.push_back({.start_pc = Utils::ReadFromStartIntoVal(Globals::buffer()), + .end_pc = Utils::ReadFromStartIntoVal(Globals::buffer()), + .handler_pc = Utils::ReadFromStartIntoVal(Globals::buffer()), + .catch_type = Utils::ReadFromStartIntoVal(Globals::buffer())}); + } + + a.attributes_count = Utils::ReadFromStartIntoVal(Globals::buffer()); + + for (int i = 0; i < a.attributes_count.ToHostFormat(); i++) + { + u2 attribute_name_index = Utils::ReadFromStartIntoVal(Globals::buffer()); + u4 attribute_length = Utils::ReadFromStartIntoVal(Globals::buffer()); + + for (int i = 0; i < attribute_length.ToHostFormat(); i++) + { + Utils::ReadFromStartIntoVal(Globals::buffer()); + } + + // std::cout << attribute_name_index.ToHostFormat() << std::endl; + } + + // for (int j = 0; j < attribute_length.ToHostFormat() - 8; j++) + // { + // uint8_t data = Utils::ReadFromStartIntoVal(Globals::buffer()); + // std::cout << std::dec << std::to_string(data) << std::endl; + // // a.info.push_back(Utils::ReadFromStartIntoVal(Globals::buffer())); + // } + std::cout << a.attribute_name_index.ToHostFormat() << std::endl; - // std::cout << a.info.size() << std::endl; + // std::cout << info.size() << std::endl; + std::cout << std::endl; } // if ((p.access_flags.ToHostFormat() & (int)Method::ACCESS_FLAGS::ACC_NATIVE) == 0 && - // (p.access_flags.ToHostFormat() & (int)Method::ACCESS_FLAGS::ACC_ABSTRACT) == 0) - // { - // std::cout << "Not Set" << std::endl; - // } + // (p.access_flags.ToHostFormat() & (int)Method::ACCESS_FLAGS::ACC_ABSTRACT) == 0) + // { + // std::cout << "Not Set" << std::endl; + // } jc.methods.push_back(p); } \ No newline at end of file