Skip to content

Commit fb311bf

Browse files
committed
Add kill method to VirtualMachine
1 parent b3feac6 commit fb311bf

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

include/scratchcpp/virtualmachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
128128
void replaceReturnValue(const Value &v, unsigned int offset);
129129

130130
void run();
131+
void kill();
131132
void reset();
132133
void moveToLastCheckpoint();
133134

src/engine/virtualmachine.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,13 @@ void VirtualMachine::run()
175175
impl->running = false;
176176
}
177177

178+
/*! Stops the execution and sets atEnd to true. */
179+
void VirtualMachine::kill()
180+
{
181+
reset();
182+
impl->atEnd = true;
183+
}
184+
178185
/*! Jumps back to the initial position. */
179186
void VirtualMachine::reset()
180187
{

test/virtual_machine/virtual_machine_test.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1590,7 +1590,7 @@ TEST(VirtualMachineTest, OP_WARP)
15901590
ASSERT_EQ(vm.registerCount(), 0);
15911591
}
15921592

1593-
TEST(VirtualMachineTest, Reset)
1593+
TEST(VirtualMachineTest, ResetAndKill)
15941594
{
15951595
static unsigned int bytecode1[] = { OP_START, OP_NULL, OP_EXEC, 0, OP_HALT };
15961596
static unsigned int bytecode2[] = { OP_START, OP_NULL, OP_EXEC, 1, OP_HALT };
@@ -1629,6 +1629,14 @@ TEST(VirtualMachineTest, Reset)
16291629

16301630
vm.reset();
16311631
ASSERT_FALSE(vm.atEnd());
1632+
1633+
// kill()
1634+
vm.run();
1635+
ASSERT_FALSE(vm.atEnd());
1636+
ASSERT_EQ(vm.registerCount(), 1);
1637+
vm.kill();
1638+
ASSERT_EQ(vm.registerCount(), 0);
1639+
ASSERT_TRUE(vm.atEnd());
16321640
}
16331641

16341642
TEST(VirtualMachineTest, NoCrashWhenRepeatingZeroTimes)

0 commit comments

Comments
 (0)