Skip to content

Commit 4b435e9

Browse files
committed
Store position in VirtualMachine
1 parent c8df9d5 commit 4b435e9

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

src/engine/engine.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,7 @@ void Engine::frame()
123123
m_breakFrame = false;
124124

125125
do {
126-
auto ret = script->run(m_scriptPositions[i]);
127-
if (script->savePos())
128-
m_scriptPositions[i] = ret;
126+
script->run();
129127
if (script->atEnd()) {
130128
for (auto &[key, value] : m_broadcastMap)
131129
value.erase(std::remove(value.begin(), value.end(), script->script()), value.end());
@@ -144,7 +142,6 @@ void Engine::frame()
144142
}
145143
assert(index != -1);
146144
m_runningScripts.erase(m_runningScripts.begin() + index);
147-
m_scriptPositions.erase(m_scriptPositions.begin() + index);
148145
}
149146
m_scriptsToRemove.clear();
150147
}
@@ -166,7 +163,6 @@ void Engine::start()
166163
void Engine::stop()
167164
{
168165
m_runningScripts.clear();
169-
m_scriptPositions.clear();
170166
}
171167

172168
/*! Starts a script with the given top level block as the given Target (a sprite or the stage). */
@@ -186,7 +182,6 @@ void Engine::startScript(std::shared_ptr<Block> topLevelBlock, std::shared_ptr<T
186182
if (topLevelBlock->next()) {
187183
auto script = m_scripts[topLevelBlock];
188184
m_runningScripts.push_back(script->start());
189-
m_scriptPositions.push_back(script->bytecode());
190185
}
191186
}
192187

@@ -204,12 +199,11 @@ void libscratchcpp::Engine::broadcast(unsigned int index, VirtualMachine *source
204199
}
205200
if (index != -1) {
206201
// Reset the script if it's already running
207-
m_scriptPositions[index] = m_runningScripts[index]->bytecode();
202+
m_runningScripts[index]->reset();
208203
if (script == sourceScript->script())
209204
sourceScript->stop(false, true);
210205
} else {
211206
m_runningScripts.push_back(script->start());
212-
m_scriptPositions.push_back(script->bytecode());
213207
}
214208
}
215209
}

src/engine/engine.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ class LIBSCRATCHCPP_EXPORT Engine
7777
std::unordered_map<unsigned int, std::vector<Script *>> m_broadcastMap;
7878
std::vector<std::string> m_extensions;
7979
std::vector<std::shared_ptr<VirtualMachine>> m_runningScripts;
80-
std::vector<unsigned int *> m_scriptPositions;
8180
std::vector<VirtualMachine *> m_scriptsToRemove;
8281
std::unordered_map<std::shared_ptr<Block>, std::shared_ptr<Script>> m_scripts;
8382
std::vector<BlockFunc> m_functions;

src/engine/virtualmachine.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,25 @@ void VirtualMachine::setLists(List **lists)
149149
void VirtualMachine::setBytecode(unsigned int *code)
150150
{
151151
m_bytecode = code;
152+
m_pos = code;
152153
}
153154

154-
/*! Runs the script. */
155-
unsigned int *VirtualMachine::run()
155+
/*! Continues running the script from last position (the first instruction is skipped). */
156+
void VirtualMachine::run()
156157
{
157-
return run(m_bytecode);
158+
unsigned int *ret = run(m_pos);
159+
assert(ret);
160+
if (m_savePos)
161+
m_pos = ret;
162+
}
163+
164+
/*! Jumps back to the initial position. */
165+
void VirtualMachine::reset()
166+
{
167+
assert(m_bytecode);
168+
m_pos = m_bytecode;
158169
}
159170

160-
/*! Continues running the script from the given position (the first instruction is skipped). */
161171
unsigned int *VirtualMachine::run(unsigned int *pos)
162172
{
163173
static const void *dispatch_table[] = {

src/engine/virtualmachine.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
111111
void addReturnValue(const Value &v) { *m_regs[m_regCount++] = v; };
112112
void replaceReturnValue(const Value &v, unsigned int offset) { *m_regs[m_regCount - offset] = v; };
113113

114-
unsigned int *run();
115-
unsigned int *run(unsigned int *pos);
114+
void run();
115+
void reset();
116116

117117
/*!
118118
* Use this to stop the script from a function.
@@ -134,10 +134,12 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
134134
/*! Returns true if the VM has reached the vm::OP_HALT instruction. */
135135
bool atEnd() const { return m_atEnd; };
136136

137-
/*! Used by Engine to check whether the position can be preserved. */
137+
/*! Used to check whether the position can be preserved. */
138138
bool savePos() const { return m_savePos; }
139139

140140
private:
141+
unsigned int *run(unsigned int *pos);
142+
141143
static const unsigned int instruction_arg_count[];
142144

143145
typedef struct
@@ -153,6 +155,7 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
153155
Target *m_target = nullptr;
154156
Engine *m_engine = nullptr;
155157
Script *m_script = nullptr;
158+
unsigned int *m_pos = nullptr;
156159
bool m_atEnd = false;
157160
std::vector<Loop> m_loops;
158161
std::vector<unsigned int *> m_callTree;

0 commit comments

Comments
 (0)