Skip to content

Commit 67ad176

Browse files
committed
Use Compiler::addConstValue() in broadcast blocks
Project data shouldn't be altered when compiling or running projects.
1 parent 5ba52f6 commit 67ad176

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/blocks/eventblocks.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,28 @@ void EventBlocks::registerBlocks(IEngine *engine)
3434
void EventBlocks::compileBroadcast(Compiler *compiler)
3535
{
3636
auto input = compiler->input(BROADCAST_INPUT);
37-
compiler->addInput(input);
37+
3838
if (input->type() != Input::Type::ObscuredShadow) {
39-
input->primaryValue()->setValue(compiler->engine()->findBroadcast(input->primaryValue()->value().toString()));
39+
compiler->addConstValue(compiler->engine()->findBroadcast(input->primaryValue()->value().toString()));
4040
compiler->addFunctionCall(&broadcastByIndex);
41-
} else
41+
} else {
42+
compiler->addInput(input);
4243
compiler->addFunctionCall(&broadcast);
44+
}
4345
}
4446

4547
void EventBlocks::compileBroadcastAndWait(Compiler *compiler)
4648
{
4749
auto input = compiler->input(BROADCAST_INPUT);
48-
compiler->addInput(input);
50+
4951
if (input->type() != Input::Type::ObscuredShadow) {
50-
input->primaryValue()->setValue(compiler->engine()->findBroadcast(input->primaryValue()->value().toString()));
52+
int index = compiler->engine()->findBroadcast(input->primaryValue()->value().toString());
53+
compiler->addConstValue(index);
5154
compiler->addFunctionCall(&broadcastByIndexAndWait);
52-
compiler->addInput(input);
55+
compiler->addConstValue(index);
5356
compiler->addFunctionCall(&checkBroadcastByIndex);
5457
} else {
58+
compiler->addInput(input);
5559
compiler->addFunctionCall(&broadcastAndWait);
5660
compiler->addInput(input);
5761
compiler->addFunctionCall(&checkBroadcast);

test/blocks/event_blocks_test.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,8 @@ TEST_F(EventBlocksTest, BroadcastAndWait)
180180
ASSERT_EQ(
181181
compiler.bytecode(),
182182
std::vector<unsigned int>(
183-
{ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_CONST, 0, vm::OP_EXEC, 1, vm::OP_NULL, vm::OP_NOT, vm::OP_EXEC, 2, vm::OP_NULL, vm::OP_NOT, vm::OP_EXEC, 3, vm::OP_HALT }));
184-
ASSERT_EQ(compiler.constValues().size(), 1);
185-
ASSERT_EQ(compiler.constValues()[0].toDouble(), 0);
183+
{ vm::OP_START, vm::OP_CONST, 0, vm::OP_EXEC, 0, vm::OP_CONST, 1, vm::OP_EXEC, 1, vm::OP_NULL, vm::OP_NOT, vm::OP_EXEC, 2, vm::OP_NULL, vm::OP_NOT, vm::OP_EXEC, 3, vm::OP_HALT }));
184+
ASSERT_EQ(compiler.constValues(), std::vector<Value>({ 0, 0 }));
186185
ASSERT_TRUE(compiler.variables().empty());
187186
ASSERT_TRUE(compiler.lists().empty());
188187
}

0 commit comments

Comments
 (0)