From f61b626fad3ed2225b86c19a61b292813967c80e Mon Sep 17 00:00:00 2001 From: adazem009 <68537469+adazem009@users.noreply.github.com> Date: Wed, 27 Dec 2023 13:48:43 +0100 Subject: [PATCH] Refactor script starting logic --- include/scratchcpp/iengine.h | 2 +- src/engine/internal/engine.cpp | 143 ++++++++++-------- src/engine/internal/engine.h | 4 +- test/engine/engine_test.cpp | 35 ++++- test/mocks/enginemock.h | 2 +- .../395_reset_running_hats.sb3 | Bin 0 -> 1017 bytes 6 files changed, 114 insertions(+), 72 deletions(-) create mode 100644 test/regtest_projects/395_reset_running_hats.sb3 diff --git a/include/scratchcpp/iengine.h b/include/scratchcpp/iengine.h index 7b52d008..fa05e8dd 100644 --- a/include/scratchcpp/iengine.h +++ b/include/scratchcpp/iengine.h @@ -54,7 +54,7 @@ class LIBSCRATCHCPP_EXPORT IEngine virtual void stop() = 0; /*! Starts a script with the given top level block as the given Target (a sprite or the stage). */ - virtual void startScript(std::shared_ptr topLevelBlock, std::shared_ptr target) = 0; + virtual VirtualMachine *startScript(std::shared_ptr topLevelBlock, Target *) = 0; /*! Starts the script of the broadcast with the given index. */ virtual void broadcast(unsigned int index, VirtualMachine *sourceScript, bool wait = false) = 0; diff --git a/src/engine/internal/engine.cpp b/src/engine/internal/engine.cpp index 3683761d..6f99df71 100644 --- a/src/engine/internal/engine.cpp +++ b/src/engine/internal/engine.cpp @@ -159,7 +159,7 @@ void Engine::start() for (auto target : m_targets) { auto gfBlocks = target->greenFlagBlocks(); for (auto block : gfBlocks) - startScript(block, target); + startScript(block, target.get()); } m_eventLoopMutex.unlock(); @@ -171,23 +171,27 @@ void Engine::stop() deleteClones(); } -void Engine::startScript(std::shared_ptr topLevelBlock, std::shared_ptr target) +VirtualMachine *Engine::startScript(std::shared_ptr topLevelBlock, Target *target) { if (!topLevelBlock) { std::cout << "warning: starting a script with a null top level block (nothing will happen)" << std::endl; - return; + return nullptr; } if (!target) { std::cout << "error: scripts must be started by a target"; assert(false); - return; + return nullptr; } if (topLevelBlock->next()) { auto script = m_scripts[topLevelBlock]; - addRunningScript(script->start()); + std::shared_ptr vm = script->start(target); + addRunningScript(vm); + return vm.get(); } + + return nullptr; } void Engine::broadcast(unsigned int index, VirtualMachine *sourceScript, bool wait) @@ -201,61 +205,22 @@ void Engine::broadcast(unsigned int index, VirtualMachine *sourceScript, bool wa void Engine::broadcastByPtr(Broadcast *broadcast, VirtualMachine *sourceScript, bool wait) { const std::vector