Skip to content

Commit 1536356

Browse files
committed
VirtualMachine: Add support for non-atomic scripts
1 parent 2bafb88 commit 1536356

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/engine/virtualmachine.cpp

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

33
#include "virtualmachine.h"
44
#include "../scratch/list.h"
5+
#include "../engine/engine.h"
56
#include <iostream>
67

78
#define MAX_REG_COUNT 1024
@@ -93,10 +94,10 @@ void VirtualMachine::setBytecode(const std::vector<unsigned int> &code)
9394
/*! Runs the script. */
9495
unsigned int *VirtualMachine::run()
9596
{
96-
m_atEnd = false;
9797
return run(m_bytecode);
9898
}
9999

100+
/*! Continues running the script from the given position (the first instruction is skipped). */
100101
unsigned int *VirtualMachine::run(unsigned int *pos)
101102
{
102103
static const void *dispatch_table[] = {
@@ -157,11 +158,14 @@ unsigned int *VirtualMachine::run(unsigned int *pos)
157158
&&do_init_procedure,
158159
&&do_call_procedure,
159160
&&do_add_arg,
160-
&&do_read_arg
161+
&&do_read_arg,
162+
&&do_break_atomic
161163
};
162164
unsigned int *loopStart;
163165
unsigned int *loopEnd;
164166
size_t loopCount;
167+
m_atEnd = false;
168+
m_atomic = true;
165169
DISPATCH();
166170

167171
do_halt:
@@ -257,8 +261,16 @@ do_loop_end : {
257261
pos = l.start;
258262
else
259263
m_loops.pop_back();
264+
if (!m_atomic) {
265+
m_engine->breakFrame();
266+
return pos;
267+
}
260268
DISPATCH();
261269
} else {
270+
if (!m_atomic) {
271+
m_engine->breakFrame();
272+
return pos - 1;
273+
}
262274
loopStart = run(l.start);
263275
if (!READ_LAST_REG()->toBool())
264276
pos = loopStart;
@@ -626,4 +638,8 @@ do_list_get_item : {
626638
do_read_arg:
627639
ADD_RET_VALUE(m_procedureArgs->operator[](*++pos));
628640
DISPATCH();
641+
642+
do_break_atomic:
643+
m_atomic = false;
644+
DISPATCH();
629645
}

src/engine/virtualmachine.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ enum Opcode
7272
OP_INIT_PROCEDURE, /*!< Initializes the list of procedure (custom block) arguments. */
7373
OP_CALL_PROCEDURE, /*! Calls the procedure (custom block) with the index in the argument. */
7474
OP_ADD_ARG, /*!< Adds a procedure (custom block) argument with the value from the last register. */
75-
OP_READ_ARG /*!< Reads the procedure (custom block) argument with the index in the argument and stores the value in the last register. */
75+
OP_READ_ARG, /*!< Reads the procedure (custom block) argument with the index in the argument and stores the value in the last register. */
76+
OP_BREAK_ATOMIC /*!< Breaks current frame at the end of the loop. */
7677
};
7778

7879
}
@@ -108,15 +109,14 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
108109
void replaceReturnValue(const Value &v, unsigned int offset) { *m_regs[m_regCount - offset] = v; };
109110

110111
unsigned int *run();
112+
unsigned int *run(unsigned int *pos);
111113

112114
/*! Returns true if the VM has reached the vm::OP_HALT instruction. */
113115
bool atEnd() const { return m_atEnd; };
114116

115117
private:
116-
unsigned int *run(unsigned int *pos);
117-
118118
static inline const unsigned int instruction_arg_count[] = {
119-
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0
119+
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0
120120
};
121121

122122
typedef struct
@@ -137,6 +137,7 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
137137
std::vector<std::vector<Value>> m_procedureArgTree;
138138
std::vector<Value> *m_procedureArgs = nullptr;
139139
std::vector<Value> *m_nextProcedureArgs = nullptr;
140+
bool m_atomic;
140141

141142
unsigned int **m_procedures = nullptr;
142143
std::vector<unsigned int *> m_proceduresVector;

0 commit comments

Comments
 (0)