Skip to content

Commit 77f0e66

Browse files
committed
Add broadcast promise only for wait blocks
1 parent f3fb3c7 commit 77f0e66

File tree

10 files changed

+82
-81
lines changed

10 files changed

+82
-81
lines changed

include/scratchcpp/iengine.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ class LIBSCRATCHCPP_EXPORT IEngine
6262
virtual Thread *startScript(std::shared_ptr<Block> topLevelBlock, Target *) = 0;
6363

6464
/*! Starts the scripts of the broadcast with the given index. */
65-
virtual void broadcast(int index, Thread *sender) = 0;
65+
virtual void broadcast(int index, Thread *sender, bool wait) = 0;
6666

6767
/*! Starts the scripts of the given broadcast. */
68-
virtual void broadcastByPtr(Broadcast *broadcast, Thread *sender) = 0;
68+
virtual void broadcastByPtr(Broadcast *broadcast, Thread *sender, bool wait) = 0;
6969

7070
/*! Starts the "when backdrop switches to" scripts for the given backdrop broadcast. */
71-
virtual void startBackdropScripts(Broadcast *broadcast, Thread *sender) = 0;
71+
virtual void startBackdropScripts(Broadcast *broadcast, Thread *sender, bool wait) = 0;
7272

7373
/*! Stops the given script. */
7474
virtual void stopScript(Thread *vm) = 0;

src/blocks/eventblocks.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ unsigned int EventBlocks::broadcast(VirtualMachine *vm)
181181
std::vector<int> broadcasts = vm->engine()->findBroadcasts(vm->getInput(0, 1)->toString());
182182

183183
for (int index : broadcasts)
184-
vm->engine()->broadcast(index, vm->thread());
184+
vm->engine()->broadcast(index, vm->thread(), false);
185185

186186
return 1;
187187
}
@@ -191,7 +191,7 @@ unsigned int EventBlocks::broadcastAndWait(VirtualMachine *vm)
191191
std::vector<int> broadcasts = vm->engine()->findBroadcasts(vm->getInput(0, 1)->toString());
192192

193193
for (int index : broadcasts)
194-
vm->engine()->broadcast(index, vm->thread());
194+
vm->engine()->broadcast(index, vm->thread(), true);
195195

196196
vm->promise();
197197

src/blocks/looksblocks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ void LooksBlocks::startBackdropScripts(VirtualMachine *vm, bool wait)
893893
{
894894
if (Stage *stage = vm->engine()->stage()) {
895895
if (stage->costumes().size() > 0)
896-
vm->engine()->startBackdropScripts(stage->currentCostume()->broadcast(), vm->thread());
896+
vm->engine()->startBackdropScripts(stage->currentCostume()->broadcast(), vm->thread(), wait);
897897
}
898898
}
899899

src/engine/internal/engine.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,24 +384,24 @@ Thread *Engine::startScript(std::shared_ptr<Block> topLevelBlock, Target *target
384384
return pushThread(topLevelBlock, target).get();
385385
}
386386

387-
void Engine::broadcast(int index, Thread *sender)
387+
void Engine::broadcast(int index, Thread *sender, bool wait)
388388
{
389389
if (index < 0 || index >= m_broadcasts.size())
390390
return;
391391

392-
broadcastByPtr(m_broadcasts[index].get(), sender);
392+
broadcastByPtr(m_broadcasts[index].get(), sender, wait);
393393
}
394394

395-
void Engine::broadcastByPtr(Broadcast *broadcast, Thread *sender)
395+
void Engine::broadcastByPtr(Broadcast *broadcast, Thread *sender, bool wait)
396396
{
397397
startHats(HatType::BroadcastReceived, { { HatField::BroadcastOption, broadcast } }, nullptr);
398-
addBroadcastPromise(broadcast, sender);
398+
addBroadcastPromise(broadcast, sender, wait);
399399
}
400400

401-
void Engine::startBackdropScripts(Broadcast *broadcast, Thread *sender)
401+
void Engine::startBackdropScripts(Broadcast *broadcast, Thread *sender, bool wait)
402402
{
403403
startHats(HatType::BackdropChanged, { { HatField::Backdrop, broadcast->name() } }, nullptr);
404-
addBroadcastPromise(broadcast, sender);
404+
addBroadcastPromise(broadcast, sender, wait);
405405
}
406406

407407
void Engine::stopScript(Thread *vm)
@@ -1868,7 +1868,7 @@ void Engine::addRunningScript(std::shared_ptr<Thread> thread)
18681868
m_threads.push_back(thread);
18691869
}
18701870

1871-
void Engine::addBroadcastPromise(Broadcast *broadcast, Thread *sender)
1871+
void Engine::addBroadcastPromise(Broadcast *broadcast, Thread *sender, bool wait)
18721872
{
18731873
assert(broadcast);
18741874
assert(sender);
@@ -1879,7 +1879,8 @@ void Engine::addBroadcastPromise(Broadcast *broadcast, Thread *sender)
18791879
if (it != m_broadcastSenders.cend() && std::find_if(m_threads.begin(), m_threads.end(), [&it](std::shared_ptr<Thread> thread) { return thread.get() == it->second; }) != m_threads.end())
18801880
it->second->resolvePromise();
18811881

1882-
m_broadcastSenders[broadcast] = sender;
1882+
if (wait)
1883+
m_broadcastSenders[broadcast] = sender;
18831884
}
18841885

18851886
std::shared_ptr<Thread> Engine::pushThread(std::shared_ptr<Block> block, Target *target)

src/engine/internal/engine.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class Engine : public IEngine
3636
void start() override;
3737
void stop() override;
3838
Thread *startScript(std::shared_ptr<Block> topLevelBlock, Target *target) override;
39-
void broadcast(int index, Thread *sender) override;
40-
void broadcastByPtr(Broadcast *broadcast, Thread *sender) override;
41-
void startBackdropScripts(Broadcast *broadcast, Thread *sender) override;
39+
void broadcast(int index, Thread *sender, bool wait) override;
40+
void broadcastByPtr(Broadcast *broadcast, Thread *sender, bool wait) override;
41+
void startBackdropScripts(Broadcast *broadcast, Thread *sender, bool wait) override;
4242
void stopScript(Thread *vm) override;
4343
void stopTarget(Target *target, Thread *exceptScript) override;
4444
void initClone(std::shared_ptr<Sprite> clone) override;
@@ -221,7 +221,7 @@ class Engine : public IEngine
221221
void updateFrameDuration();
222222
void addRunningScript(std::shared_ptr<Thread> thread);
223223

224-
void addBroadcastPromise(Broadcast *broadcast, Thread *sender);
224+
void addBroadcastPromise(Broadcast *broadcast, Thread *sender, bool wait);
225225

226226
std::shared_ptr<Thread> pushThread(std::shared_ptr<Block> block, Target *target);
227227
void stopThread(Thread *thread);

test/blocks/event_blocks_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ TEST_F(EventBlocksTest, BroadcastImpl)
401401
vm->setConstValues(constValues);
402402

403403
EXPECT_CALL(m_engineMock, findBroadcasts("test")).WillOnce(Return(std::vector<int>({ 1, 4 })));
404-
EXPECT_CALL(m_engineMock, broadcast(1, &thread));
405-
EXPECT_CALL(m_engineMock, broadcast(4, &thread));
404+
EXPECT_CALL(m_engineMock, broadcast(1, &thread, false));
405+
EXPECT_CALL(m_engineMock, broadcast(4, &thread, false));
406406

407407
vm->setBytecode(bytecode1);
408408
vm->run();
@@ -451,8 +451,8 @@ TEST_F(EventBlocksTest, BroadcastAndWaitImpl)
451451
vm->setConstValues(constValues);
452452

453453
EXPECT_CALL(m_engineMock, findBroadcasts("test")).WillOnce(Return(std::vector<int>({ 1, 4 })));
454-
EXPECT_CALL(m_engineMock, broadcast(1, &thread));
455-
EXPECT_CALL(m_engineMock, broadcast(4, &thread));
454+
EXPECT_CALL(m_engineMock, broadcast(1, &thread, true));
455+
EXPECT_CALL(m_engineMock, broadcast(4, &thread, true));
456456

457457
vm->setBytecode(bytecode);
458458
vm->run();

0 commit comments

Comments
 (0)