Skip to content

Commit 61ff8bc

Browse files
committed
Add simple promise API to VirtualMachine
1 parent 0471a59 commit 61ff8bc

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

include/scratchcpp/virtualmachine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{

src/engine/virtualmachine_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ struct VirtualMachinePrivate
5656
bool savePos = true;
5757
bool goBack = false;
5858
bool updatePos = false;
59+
bool promisePending = false;
5960

6061
unsigned int **procedures = nullptr;
6162
BlockFunc *functions = nullptr;

test/virtual_machine/virtual_machine_test.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,6 +1639,35 @@ TEST(VirtualMachineTest, ResetAndKill)
16391639
ASSERT_TRUE(vm.atEnd());
16401640
}
16411641

1642+
unsigned int promiseTest(VirtualMachine *vm)
1643+
{
1644+
vm->promise();
1645+
return 1;
1646+
}
1647+
1648+
TEST(VirtualMachineTest, Promise)
1649+
{
1650+
static unsigned int bytecode[] = { OP_START, OP_NULL, OP_EXEC, 0, vm::OP_NULL, vm::OP_NULL, vm::OP_NULL, OP_HALT };
1651+
static BlockFunc functions[] = { &promiseTest };
1652+
1653+
VirtualMachine vm;
1654+
vm.setBytecode(bytecode);
1655+
vm.setFunctions(functions);
1656+
1657+
vm.run();
1658+
ASSERT_FALSE(vm.atEnd());
1659+
ASSERT_EQ(vm.registerCount(), 0);
1660+
1661+
vm.run();
1662+
ASSERT_FALSE(vm.atEnd());
1663+
ASSERT_EQ(vm.registerCount(), 0);
1664+
1665+
vm.resolvePromise();
1666+
vm.run();
1667+
ASSERT_TRUE(vm.atEnd());
1668+
ASSERT_EQ(vm.registerCount(), 3);
1669+
}
1670+
16421671
TEST(VirtualMachineTest, NoCrashWhenRepeatingZeroTimes)
16431672
{
16441673
// Regtest for #362

0 commit comments

Comments
 (0)