Skip to content

Commit 0b36baf

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

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ 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_FALSE(thread.promise());
169170
}
170171

171172
{
@@ -180,6 +181,7 @@ TEST_F(EventBlocksTest, Broadcast)
180181
EXPECT_CALL(m_engineMock, broadcast(7, &thread, false));
181182
EXPECT_CALL(m_engineMock, broadcast(8, &thread, false));
182183
thread.run();
184+
ASSERT_FALSE(thread.promise());
183185
}
184186
}
185187

@@ -213,6 +215,8 @@ TEST_F(EventBlocksTest, BroadcastAndWait)
213215
EXPECT_CALL(m_engineMock, broadcast(1, &thread, true));
214216
EXPECT_CALL(m_engineMock, broadcast(4, &thread, true));
215217
thread.run();
218+
ASSERT_FALSE(thread.isFinished());
219+
ASSERT_TRUE(thread.promise());
216220
}
217221

218222
{
@@ -227,6 +231,8 @@ TEST_F(EventBlocksTest, BroadcastAndWait)
227231
EXPECT_CALL(m_engineMock, broadcast(7, &thread, true));
228232
EXPECT_CALL(m_engineMock, broadcast(8, &thread, true));
229233
thread.run();
234+
ASSERT_FALSE(thread.isFinished());
235+
ASSERT_TRUE(thread.promise());
230236
}
231237
}
232238

0 commit comments

Comments
 (0)