Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/blocks/controlblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
#include <cassert>

#include "controlblocks.h"
#include "../engine/internal/clock.h"

using namespace libscratchcpp;

IClock *ControlBlocks::clock = nullptr;

std::string ControlBlocks::name() const
{
return "Control";
Expand Down Expand Up @@ -207,14 +210,20 @@ unsigned int ControlBlocks::stopOtherScriptsInSprite(VirtualMachine *vm)

unsigned int ControlBlocks::startWait(VirtualMachine *vm)
{
auto currentTime = std::chrono::steady_clock::now();
if (!clock)
clock = Clock::instance().get();

auto currentTime = clock->currentSteadyTime();
m_timeMap[vm] = { currentTime, vm->getInput(0, 1)->toDouble() * 1000 };
return 1;
}

unsigned int ControlBlocks::wait(VirtualMachine *vm)
{
auto currentTime = std::chrono::steady_clock::now();
if (!clock)
clock = Clock::instance().get();

auto currentTime = clock->currentSteadyTime();
assert(m_timeMap.count(vm) == 1);
if (std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - m_timeMap[vm].first).count() >= m_timeMap[vm].second) {
m_timeMap.erase(vm);
Expand Down
3 changes: 3 additions & 0 deletions src/blocks/controlblocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace libscratchcpp

class Compiler;
class VirtualMachine;
class IClock;

/*! \brief The ControlBlocks class contains the implementation of control blocks. */
class ControlBlocks : public IBlockSection
Expand Down Expand Up @@ -69,6 +70,8 @@ class ControlBlocks : public IBlockSection
static unsigned int deleteThisClone(VirtualMachine *vm);

static inline std::unordered_map<VirtualMachine *, std::pair<std::chrono::steady_clock::time_point, int>> m_timeMap;

static IClock *clock;
};

} // namespace libscratchcpp
15 changes: 13 additions & 2 deletions test/blocks/control_blocks_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
#include <scratchcpp/variable.h>
#include <scratchcpp/sprite.h>
#include <enginemock.h>
#include <clockmock.h>

#include "../common.h"
#include "blocks/controlblocks.h"
#include "blocks/operatorblocks.h"
#include "engine/internal/engine.h"
#include "engine/internal/clock.h"

using namespace libscratchcpp;

Expand Down Expand Up @@ -732,6 +734,11 @@ TEST_F(ControlBlocksTest, WaitImpl)
vm.setConstValues(constValues);
vm.setBytecode(bytecode);

ClockMock clock;
ControlBlocks::clock = &clock;

std::chrono::steady_clock::time_point startTime(std::chrono::milliseconds(1000));
EXPECT_CALL(clock, currentSteadyTime()).Times(2).WillRepeatedly(Return(startTime));
EXPECT_CALL(m_engineMock, lockFrame()).Times(1);
EXPECT_CALL(m_engineMock, breakFrame()).Times(1);
vm.run();
Expand All @@ -740,18 +747,20 @@ TEST_F(ControlBlocksTest, WaitImpl)
ASSERT_TRUE(ControlBlocks::m_timeMap.find(&vm) != ControlBlocks::m_timeMap.cend());
ASSERT_FALSE(vm.atEnd());

std::chrono::steady_clock::time_point time1(std::chrono::milliseconds(6450));
EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time1));
EXPECT_CALL(m_engineMock, lockFrame()).Times(1);
EXPECT_CALL(m_engineMock, breakFrame()).Times(1);
ControlBlocks::m_timeMap[&vm].first = std::chrono::steady_clock::now() - std::chrono::milliseconds(5250);
vm.run();

ASSERT_EQ(vm.registerCount(), 0);
ASSERT_TRUE(ControlBlocks::m_timeMap.find(&vm) != ControlBlocks::m_timeMap.cend());
ASSERT_FALSE(vm.atEnd());

std::chrono::steady_clock::time_point time2(std::chrono::milliseconds(6500));
EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time2));
EXPECT_CALL(m_engineMock, lockFrame()).Times(1);
EXPECT_CALL(m_engineMock, breakFrame()).Times(1);
ControlBlocks::m_timeMap[&vm].first = std::chrono::steady_clock::now() - std::chrono::milliseconds(5500);
vm.run();

ASSERT_EQ(vm.registerCount(), 0);
Expand All @@ -765,6 +774,8 @@ TEST_F(ControlBlocksTest, WaitImpl)
ASSERT_EQ(vm.registerCount(), 0);
ASSERT_TRUE(ControlBlocks::m_timeMap.find(&vm) == ControlBlocks::m_timeMap.cend());
ASSERT_TRUE(vm.atEnd());

ControlBlocks::clock = Clock::instance().get();
}

TEST_F(ControlBlocksTest, WaitUntil)
Expand Down