18
18
#include < wolv/io/file.hpp>
19
19
#include < wolv/utils/string.hpp>
20
20
21
+ #include < pl/core/errors/runtime_errors.hpp>
22
+
21
23
namespace pl {
22
24
23
25
static std::string getFunctionName (const api::Namespace &ns, const std::string &name) {
@@ -43,6 +45,8 @@ namespace pl {
43
45
44
46
this ->m_internals .evaluator ->setRuntime (this );
45
47
48
+ this ->m_sectionData = std::make_shared<SectionData>();
49
+
46
50
if (addLibStd)
47
51
lib::libstd::registerFunctions (*this );
48
52
@@ -97,6 +101,8 @@ namespace pl {
97
101
this ->m_runId .exchange (other.m_runId .load ());
98
102
this ->m_subRuntime = other.m_subRuntime ;
99
103
104
+ this ->m_sectionData = std::move (other.m_sectionData );
105
+
100
106
m_startAddress = std::move (other.m_startAddress );
101
107
m_defaultEndian = other.m_defaultEndian ;
102
108
m_runningTime = other.m_runningTime ;
@@ -124,6 +130,7 @@ namespace pl {
124
130
125
131
runtime.m_functions = this ->m_functions ;
126
132
runtime.m_subRuntime = true ;
133
+ runtime.m_sectionData = this ->m_sectionData ;
127
134
128
135
return runtime;
129
136
}
@@ -470,16 +477,45 @@ namespace pl {
470
477
return this ->m_internals .evaluator ->getPatternLimit ();
471
478
}
472
479
473
- const std::vector<u8 >& PatternLanguage::getSection (u64 id) const {
480
+ std::vector<u8 >& PatternLanguage::getSection (u64 id) {
474
481
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 ;
477
490
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) );
479
492
}
480
493
481
494
[[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 ();
483
519
}
484
520
485
521
[[nodiscard]] const std::vector<std::shared_ptr<ptrn::Pattern>> &PatternLanguage::getPatterns (u64 section) const {
@@ -516,6 +552,11 @@ namespace pl {
516
552
this ->m_internals .parser ->setParserManager (&m_parserManager);
517
553
this ->m_patternsValid = false ;
518
554
555
+ if (!this ->m_subRuntime ) {
556
+ this ->m_sectionData ->nextSectionId = 1 ;
557
+ this ->m_sectionData ->sections .clear ();
558
+ }
559
+
519
560
this ->m_resolvers .setDefaultResolver ([this ](const std::string& path) {
520
561
return this ->m_fileResolver .resolve (path);
521
562
});
0 commit comments