Skip to content

Commit 8377276

Browse files
committed
lib: Let read_unsigned and friends access the current section
1 parent 76119db commit 8377276

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

lib/include/pl/core/evaluator.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ namespace pl::core {
159159
void pushSectionId(u64 id);
160160
void popSectionId();
161161
[[nodiscard]] u64 getSectionId() const;
162+
[[nodiscard]] u64 getUserSectionId() const;
162163
[[nodiscard]] u64 createSection(const std::string &name);
163164
void removeSection(u64 id);
164165
[[nodiscard]] std::vector<u8>& getSection(u64 id);

lib/source/pl/core/evaluator.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,19 @@ namespace pl::core {
947947
return this->m_sectionIdStack.back();
948948
}
949949

950+
u64 Evaluator::getUserSectionId() const {
951+
if (this->m_sectionIdStack.empty())
952+
return 0;
953+
954+
for (auto it = this->m_sectionIdStack.rbegin(); it != this->m_sectionIdStack.rend(); ++it) {
955+
if (*it != ptrn::Pattern::MainSectionId && *it != ptrn::Pattern::HeapSectionId && *it != ptrn::Pattern::PatternLocalSectionId && *it != ptrn::Pattern::InstantiationSectionId)
956+
return *it;
957+
}
958+
959+
return 0;
960+
}
961+
962+
950963
u64 Evaluator::createSection(const std::string &name) {
951964
auto id = this->m_sectionId;
952965
this->m_sectionId++;

lib/source/pl/lib/std/mem.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ namespace pl::lib::libstd::mem {
8888
sequence.push_back(u8(byte));
8989
}
9090

91-
return findSequence(ctx, occurrenceIndex, offsetFrom, offsetTo, ctx->getSectionId(), sequence).value_or(-1);
91+
return findSequence(ctx, occurrenceIndex, offsetFrom, offsetTo, ctx->getUserSectionId(), sequence).value_or(-1);
9292
});
9393

9494
/* find_string_in_range(occurrence_index, start_offset, end_offset, string) */
@@ -98,15 +98,17 @@ namespace pl::lib::libstd::mem {
9898
const u64 offsetTo = params[2].toUnsigned();
9999
const auto string = params[3].toString(false);
100100

101-
return findSequence(ctx, occurrenceIndex, offsetFrom, offsetTo, ctx->getSectionId(), std::vector<u8>(string.data(), string.data() + string.size())).value_or(-1);
101+
return findSequence(ctx, occurrenceIndex, offsetFrom, offsetTo, ctx->getUserSectionId(), std::vector<u8>(string.data(), string.data() + string.size())).value_or(-1);
102102
});
103103

104104
/* read_unsigned(address, size, endian, section) */
105105
runtime.addFunction(nsStdMem, "read_unsigned", FunctionParameterCount::between(3, 4), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
106106
const u64 address = params[0].toUnsigned();
107107
const size_t size = params[1].toSigned();
108108
const types::Endian endian = params[2].toUnsigned();
109-
const u64 section = params.size() == 4 ? params[3].toUnsigned() : ptrn::Pattern::MainSectionId;
109+
u64 section = params.size() == 4 ? params[3].toUnsigned() : ptrn::Pattern::MainSectionId;
110+
if (section == 0xFFFF'FFFF'FFFF'FFFF)
111+
section = ctx->getUserSectionId();
110112

111113
if (size < 1 || size > 16)
112114
err::E0012.throwError(fmt::format("Read size {} is out of range.", size), "Try a value between 1 and 16.");
@@ -123,7 +125,9 @@ namespace pl::lib::libstd::mem {
123125
const u64 address = params[0].toUnsigned();
124126
const size_t size = params[1].toSigned();
125127
const types::Endian endian = params[2].toUnsigned();
126-
const u64 section = params.size() == 4 ? params[3].toUnsigned() : ptrn::Pattern::MainSectionId;
128+
u64 section = params.size() == 4 ? params[3].toUnsigned() : ptrn::Pattern::MainSectionId;
129+
if (section == 0xFFFF'FFFF'FFFF'FFFF)
130+
section = ctx->getUserSectionId();
127131

128132
if (size < 1 || size > 16)
129133
err::E0012.throwError(fmt::format("Read size {} is out of range.", size), "Try a value between 1 and 16.");
@@ -140,7 +144,9 @@ namespace pl::lib::libstd::mem {
140144
runtime.addFunction(nsStdMem, "read_string", FunctionParameterCount::between(2, 3), [](Evaluator *ctx, auto params) -> std::optional<Token::Literal> {
141145
const u64 address = params[0].toUnsigned();
142146
const size_t size = params[1].toSigned();
143-
const u64 section = params.size() == 3 ? params[2].toUnsigned() : ptrn::Pattern::MainSectionId;
147+
u64 section = params.size() == 3 ? params[2].toUnsigned() : ptrn::Pattern::MainSectionId;
148+
if (section == 0xFFFF'FFFF'FFFF'FFFF)
149+
section = ctx->getUserSectionId();
144150

145151
std::string result(size, '\x00');
146152
ctx->readData(address, result.data(), size, section);

0 commit comments

Comments
 (0)