Skip to content

Commit be044ef

Browse files
committed
Add string stack to ExecutionContext
1 parent 7ebdc2b commit be044ef

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

‎include/scratchcpp/executioncontext.h‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
#pragma once
44

5-
#include "global.h"
5+
#include <stack>
6+
#include <vector>
7+
8+
#include "string_pool.h"
69
#include "spimpl.h"
710

811
namespace libscratchcpp
@@ -13,6 +16,7 @@ class IEngine;
1316
class Promise;
1417
class IStackTimer;
1518
class IRandomGenerator;
19+
struct StringPtr;
1620
class ExecutionContextPrivate;
1721

1822
/*! \brief The ExecutionContext represents the execution context of a target (can be a clone) with variables, lists, etc. */
@@ -21,7 +25,7 @@ class LIBSCRATCHCPP_EXPORT ExecutionContext
2125
public:
2226
ExecutionContext(Thread *thread);
2327
ExecutionContext(const ExecutionContext &) = delete;
24-
virtual ~ExecutionContext() { }
28+
virtual ~ExecutionContext();
2529

2630
Thread *thread() const;
2731
IEngine *engine() const;
@@ -35,8 +39,35 @@ class LIBSCRATCHCPP_EXPORT ExecutionContext
3539
IRandomGenerator *rng() const;
3640
void setRng(IRandomGenerator *newRng);
3741

42+
inline StringPtr **pushStringStack(size_t stringCount)
43+
{
44+
m_stringStack.push({});
45+
46+
auto &strings = m_stringStack.top();
47+
48+
for (size_t i = 0; i < stringCount; i++) {
49+
StringPtr *str = string_pool_new();
50+
strings.push_back(str);
51+
}
52+
53+
return strings.data();
54+
}
55+
56+
inline void popStringStack()
57+
{
58+
assert(!m_stringStack.empty());
59+
const auto &strings = m_stringStack.top();
60+
61+
for (StringPtr *str : strings)
62+
string_pool_free(str);
63+
64+
m_stringStack.pop();
65+
}
66+
3867
private:
3968
spimpl::unique_impl_ptr<ExecutionContextPrivate> impl;
69+
70+
std::stack<std::vector<StringPtr *>> m_stringStack;
4071
};
4172

4273
} // namespace libscratchcpp

‎src/engine/executioncontext.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ ExecutionContext::ExecutionContext(Thread *thread) :
1313
{
1414
}
1515

16+
/*! Destroys ExecutionContext. */
17+
ExecutionContext::~ExecutionContext()
18+
{
19+
// Deallocate strings
20+
// NOTE: The stack should be already empty
21+
assert(m_stringStack.empty());
22+
23+
while (!m_stringStack.empty()) {
24+
popStringStack();
25+
}
26+
}
27+
1628
/*! Returns the thread of this context. */
1729
Thread *ExecutionContext::thread() const
1830
{

‎test/executioncontext/executioncontext_test.cpp‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <scratchcpp/executioncontext.h>
22
#include <scratchcpp/thread.h>
33
#include <scratchcpp/promise.h>
4+
#include <scratchcpp/stringptr.h>
5+
#include <scratchcpp/string_functions.h>
46

57
#include <enginemock.h>
68
#include <stacktimermock.h>
@@ -51,3 +53,29 @@ TEST(ExecutionContextTest, Rng)
5153
ctx.setRng(&rng);
5254
ASSERT_EQ(ctx.rng(), &rng);
5355
}
56+
57+
TEST(ExecutionContextTest, StringStack)
58+
{
59+
EngineMock engine;
60+
CompilerContext compilerCtx(&engine, nullptr);
61+
ExecutionContext ctx(&compilerCtx, nullptr);
62+
63+
StringPtr **frame1 = ctx.pushStringStack(3);
64+
65+
for (int i = 0; i < 3; i++) {
66+
string_assign_cstring(frame1[i], "test"); // address sanitizer will detect potential buffer overflow
67+
}
68+
69+
StringPtr **frame2 = ctx.pushStringStack(5);
70+
71+
for (int i = 0; i < 5; i++) {
72+
string_assign_cstring(frame2[i], "foo bar");
73+
}
74+
75+
ctx.popStringStack();
76+
77+
StringPtr testString("test");
78+
ASSERT_EQ(string_compare_case_sensitive(frame1[2], &testString), 0);
79+
80+
ctx.popStringStack();
81+
}

0 commit comments

Comments
 (0)