Skip to content

Commit 9a61a36

Browse files
committed
Add missing yield and promise to broadcast and wait block
1 parent 1da38a7 commit 9a61a36

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/dev/blocks/eventblocks.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <scratchcpp/dev/executioncontext.h>
99
#include <scratchcpp/thread.h>
1010
#include <scratchcpp/dev/compilerconstant.h>
11+
#include <scratchcpp/dev/promise.h>
1112

1213
#include "eventblocks.h"
1314

@@ -104,6 +105,7 @@ CompilerValue *EventBlocks::compileBroadcastAndWait(Compiler *compiler)
104105
auto input = compiler->addInput("BROADCAST_INPUT");
105106
auto wait = compiler->addConstValue(true);
106107
compiler->addFunctionCallWithCtx("event_broadcast", Compiler::StaticType::Void, { Compiler::StaticType::String, Compiler::StaticType::Bool }, { input, wait });
108+
compiler->createYield();
107109
return nullptr;
108110
}
109111

@@ -126,4 +128,7 @@ extern "C" void event_broadcast(ExecutionContext *ctx, const char *name, bool wa
126128

127129
for (int index : broadcasts)
128130
engine->broadcast(index, thread, wait);
131+
132+
if (wait)
133+
ctx->setPromise(std::make_shared<Promise>());
129134
}

test/dev/blocks/event_blocks_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ TEST_F(EventBlocksTest, Broadcast)
166166
EXPECT_CALL(m_engineMock, broadcast(1, &thread, false));
167167
EXPECT_CALL(m_engineMock, broadcast(4, &thread, false));
168168
thread.run();
169+
ASSERT_TRUE(thread.isFinished());
170+
ASSERT_FALSE(thread.promise());
169171
}
170172

171173
{
@@ -180,6 +182,8 @@ TEST_F(EventBlocksTest, Broadcast)
180182
EXPECT_CALL(m_engineMock, broadcast(7, &thread, false));
181183
EXPECT_CALL(m_engineMock, broadcast(8, &thread, false));
182184
thread.run();
185+
ASSERT_TRUE(thread.isFinished());
186+
ASSERT_FALSE(thread.promise());
183187
}
184188
}
185189

@@ -213,6 +217,8 @@ TEST_F(EventBlocksTest, BroadcastAndWait)
213217
EXPECT_CALL(m_engineMock, broadcast(1, &thread, true));
214218
EXPECT_CALL(m_engineMock, broadcast(4, &thread, true));
215219
thread.run();
220+
ASSERT_FALSE(thread.isFinished());
221+
ASSERT_TRUE(thread.promise());
216222
}
217223

218224
{
@@ -227,6 +233,8 @@ TEST_F(EventBlocksTest, BroadcastAndWait)
227233
EXPECT_CALL(m_engineMock, broadcast(7, &thread, true));
228234
EXPECT_CALL(m_engineMock, broadcast(8, &thread, true));
229235
thread.run();
236+
ASSERT_FALSE(thread.isFinished());
237+
ASSERT_TRUE(thread.promise());
230238
}
231239
}
232240

0 commit comments

Comments
 (0)