exports.cpp (2589B)
1 #include "exports.hpp" 2 3 #include "data.hpp" 4 #include "header.hpp" 5 #include "instruction.hpp" 6 #include "../context.hpp" 7 #include "../../sink.hpp" 8 9 #include <libshit/container/vector.lua.hpp> 10 #include <libshit/string_utils.hpp> 11 12 #include <iostream> 13 14 namespace Neptools::Stcm 15 { 16 17 void ExportsItem::Entry::Validate(FilePosition file_size) const 18 { 19 #define VALIDATE(x) LIBSHIT_VALIDATE_FIELD("Stcm::ExportsItem::Entry", x) 20 VALIDATE(type == Type::CODE || type == Type::DATA); 21 VALIDATE(name.is_valid()); 22 VALIDATE(offset < file_size); 23 #undef VALIDATE 24 } 25 26 ExportsItem::ExportsItem(Key k, Context& ctx, Source src, uint32_t export_count) 27 : Item{k, ctx} 28 { 29 ADD_SOURCE(Parse_(ctx, src, export_count), src); 30 } 31 32 void ExportsItem::Parse_(Context& ctx, Source& src, uint32_t export_count) 33 { 34 entries.reserve(export_count); 35 auto size = ctx.GetSize(); 36 for (uint32_t i = 0; i < export_count; ++i) 37 { 38 auto e = src.ReadGen<Entry>(); 39 e.Validate(size); 40 entries.push_back( 41 Libshit::MakeSmart<EntryType>( 42 static_cast<Type>(static_cast<uint32_t>(e.type)), 43 e.name, 44 ctx.CreateLabelFallback(e.name.c_str(), e.offset))); 45 } 46 } 47 48 ExportsItem& ExportsItem::CreateAndInsert( 49 ItemPointer ptr, uint32_t export_count) 50 { 51 auto x = RawItem::GetSource(ptr, export_count*sizeof(Entry)); 52 53 auto& ret = x.ritem.SplitCreate<ExportsItem>( 54 ptr.offset, x.src, export_count); 55 56 for (const auto& e : ret.entries) 57 switch (e->type) 58 { 59 case Type::CODE: 60 MaybeCreate<InstructionItem>(e->lbl->GetPtr()); 61 break; 62 case Type::DATA: 63 MaybeCreate<DataItem>(e->lbl->GetPtr()); 64 break; 65 } 66 return ret; 67 } 68 69 void ExportsItem::Dispose() noexcept 70 { 71 entries.clear(); 72 Item::Dispose(); 73 } 74 75 void ExportsItem::Dump_(Sink& sink) const 76 { 77 Entry ee; 78 79 for (auto& e : entries) 80 { 81 ee.type = e->type; 82 ee.name = e->name; 83 ee.offset = ToFilePos(e->lbl->GetPtr()); 84 sink.WriteGen(ee); 85 } 86 } 87 88 void ExportsItem::Inspect_(std::ostream& os, unsigned indent) const 89 { 90 Item::Inspect_(os, indent); 91 92 os << "exports{\n"; 93 for (auto& e : entries) 94 { 95 Indent(os, indent+1) 96 << '{' << e->type << ", " << Libshit::Quoted(e->name.c_str()) << ", " 97 << PrintLabel(e->lbl) << "},\n"; 98 } 99 Indent(os, indent) << '}'; 100 } 101 102 } 103 104 LIBSHIT_STD_VECTOR_LUAGEN( 105 stcm_exports_item_entry_type, Libshit::NotNull<Libshit::RefCountedPtr< 106 Neptools::Stcm::ExportsItem::EntryType>>); 107 #include "exports.binding.hpp"