1818#include < wolv/io/file.hpp>
1919#include < wolv/utils/string.hpp>
2020
21+ #include < pl/core/errors/runtime_errors.hpp>
22+
2123namespace pl {
2224
2325 static std::string getFunctionName (const api::Namespace &ns, const std::string &name) {
@@ -43,6 +45,8 @@ namespace pl {
4345
4446 this ->m_internals .evaluator ->setRuntime (this );
4547
48+ this ->m_sectionData = std::make_shared<SectionData>();
49+
4650 if (addLibStd)
4751 lib::libstd::registerFunctions (*this );
4852
@@ -97,6 +101,8 @@ namespace pl {
97101 this ->m_runId .exchange (other.m_runId .load ());
98102 this ->m_subRuntime = other.m_subRuntime ;
99103
104+ this ->m_sectionData = std::move (other.m_sectionData );
105+
100106 m_startAddress = std::move (other.m_startAddress );
101107 m_defaultEndian = other.m_defaultEndian ;
102108 m_runningTime = other.m_runningTime ;
@@ -124,6 +130,7 @@ namespace pl {
124130
125131 runtime.m_functions = this ->m_functions ;
126132 runtime.m_subRuntime = true ;
133+ runtime.m_sectionData = this ->m_sectionData ;
127134
128135 return runtime;
129136 }
@@ -470,16 +477,45 @@ namespace pl {
470477 return this ->m_internals .evaluator ->getPatternLimit ();
471478 }
472479
473- const std::vector<u8 >& PatternLanguage::getSection (u64 id) const {
480+ std::vector<u8 >& PatternLanguage::getSection (u64 id) {
474481 static std::vector<u8 > empty;
475- if (id > this ->m_internals .evaluator ->getSectionCount () || id == ptrn::Pattern::MainSectionId || id == ptrn::Pattern::HeapSectionId)
476- return empty;
482+ if (id == ptrn::Pattern::MainSectionId)
483+ core::err::E0012 .throwError (" Cannot access main section." );
484+ else if (id == ptrn::Pattern::HeapSectionId)
485+ return this ->getInternals ().evaluator ->m_heap .back ();
486+ else if (id == ptrn::Pattern::InstantiationSectionId)
487+ core::err::E0012 .throwError (" Cannot access data of type that hasn't been placed in memory." );
488+ else if (this ->m_sectionData ->sections .contains (id))
489+ return this ->m_sectionData ->sections .at (id).data ;
477490 else
478- return this -> m_internals . evaluator -> getSection (id );
491+ core::err:: E0012 . throwError ( fmt::format ( " Tried accessing a non-existing section with id {}. " , id) );
479492 }
480493
481494 [[nodiscard]] const std::map<u64 , api::Section>& PatternLanguage::getSections () const {
482- return this ->m_internals .evaluator ->getSections ();
495+ return this ->m_sectionData ->sections ;
496+ }
497+
498+ u64 PatternLanguage::createSection (const std::string &name) {
499+ auto id = this ->m_sectionData ->nextSectionId ;
500+ this ->m_sectionData ->nextSectionId ++;
501+
502+ this ->m_sectionData ->sections .insert ({ id, { name, { } } });
503+ return id;
504+ }
505+
506+ void PatternLanguage::removeSection (u64 id) {
507+ this ->m_sectionData ->sections .erase (id);
508+ }
509+
510+ u64 PatternLanguage::getSectionSize (u64 id) {
511+ if (id == ptrn::Pattern::MainSectionId)
512+ return this ->m_dataSize ;
513+ else
514+ return this ->getSection (id).size ();
515+ }
516+
517+ u64 PatternLanguage::getSectionCount () const {
518+ return this ->m_sectionData ->sections .size ();
483519 }
484520
485521 [[nodiscard]] const std::vector<std::shared_ptr<ptrn::Pattern>> &PatternLanguage::getPatterns (u64 section) const {
@@ -516,6 +552,11 @@ namespace pl {
516552 this ->m_internals .parser ->setParserManager (&m_parserManager);
517553 this ->m_patternsValid = false ;
518554
555+ if (!this ->m_subRuntime ) {
556+ this ->m_sectionData ->nextSectionId = 1 ;
557+ this ->m_sectionData ->sections .clear ();
558+ }
559+
519560 this ->m_resolvers .setDefaultResolver ([this ](const std::string& path) {
520561 return this ->m_fileResolver .resolve (path);
521562 });
0 commit comments