Skip to content

Commit 548df99

Browse files
authored
Merge pull request #477 from scratchcpp/promise_api
Add promise API to VirtualMachine
2 parents 2fa60f7 + 61ff8bc commit 548df99

File tree

5 files changed

+457
-382
lines changed

5 files changed

+457
-382
lines changed

include/scratchcpp/virtualmachine.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ enum Opcode
7171
OP_LIST_LENGTH, /*!< Stores the length of the list with the index in the argument, in the last register. */
7272
OP_LIST_CONTAINS, /*!< Stores true in the last register if the list with the index in the argument contains the value from the last register. */
7373
OP_STR_CONCAT, /*!< Concatenates the strings stored in the last 2 registers and stores the result in the last register, deleting the input registers. */
74-
OP_STR_AT, /*! Stores the character at index in the last register of the string in the second last register, in the last register. */
75-
OP_STR_LENGTH, /*! Stores the length of the string in the last register, in the last register. */
76-
OP_STR_CONTAINS, /*! Stores true in the last register if the string stored in the second last register contains the substring in the last register. */
74+
OP_STR_AT, /*!< Stores the character at index in the last register of the string in the second last register, in the last register. */
75+
OP_STR_LENGTH, /*!< Stores the length of the string in the last register, in the last register. */
76+
OP_STR_CONTAINS, /*!< Stores true in the last register if the string stored in the second last register contains the substring in the last register. */
7777
OP_EXEC, /*!< Calls the function with the index in the argument. */
7878
OP_INIT_PROCEDURE, /*!< Initializes the list of procedure (custom block) arguments. */
7979
OP_CALL_PROCEDURE, /*! Calls the procedure (custom block) with the index in the argument. */
8080
OP_ADD_ARG, /*!< Adds a procedure (custom block) argument with the value from the last register. */
8181
OP_READ_ARG, /*!< Reads the procedure (custom block) argument with the index in the argument and stores the value in the last register. */
8282
OP_BREAK_FRAME, /*!< Breaks current frame at the end of the loop. */
83-
OP_WARP /*! Runs the script without screen refresh. */
83+
OP_WARP /*!< Runs the script without screen refresh. */
8484
};
8585

8686
}
@@ -133,6 +133,8 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
133133
void moveToLastCheckpoint();
134134

135135
void stop(bool savePos = true, bool breakFrame = false, bool goBack = false);
136+
void promise();
137+
void resolvePromise();
136138

137139
bool atEnd() const;
138140

src/engine/virtualmachine.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,9 @@ void VirtualMachine::run()
166166
{
167167
impl->running = true;
168168

169+
if (impl->promisePending)
170+
return;
171+
169172
unsigned int *ret = impl->run(impl->pos);
170173
assert(ret);
171174

@@ -187,6 +190,7 @@ void VirtualMachine::reset()
187190
{
188191
impl->pos = impl->bytecode;
189192
impl->atEnd = false;
193+
impl->promisePending = false;
190194

191195
if (!impl->running) // Registers will be freed when the script stops running
192196
impl->regCount = 0;
@@ -216,6 +220,22 @@ void VirtualMachine::stop(bool savePos, bool breakFrame, bool goBack)
216220
impl->goBack = goBack;
217221
}
218222

223+
/*!
224+
* Use this to pause the execution of the script.
225+
* The execution will continue after calling resolvePromise()
226+
*/
227+
void VirtualMachine::promise()
228+
{
229+
impl->promisePending = true;
230+
stop();
231+
}
232+
233+
/*! Resolves the promise so that the VM can continue with the execution. */
234+
void VirtualMachine::resolvePromise()
235+
{
236+
impl->promisePending = false;
237+
}
238+
219239
/*! Returns true if the VM has reached the vm::OP_HALT instruction. */
220240
bool VirtualMachine::atEnd() const
221241
{

0 commit comments

Comments
 (0)