Skip to content

Commit ebf17cf

Browse files
committed
Add variable monitor name functions
1 parent 703f1fc commit ebf17cf

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/blocks/variableblocks.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <scratchcpp/iengine.h>
44
#include <scratchcpp/compiler.h>
55
#include <scratchcpp/field.h>
6+
#include <scratchcpp/block.h>
7+
#include <scratchcpp/variable.h>
68

79
#include "variableblocks.h"
810

@@ -20,6 +22,9 @@ void VariableBlocks::registerBlocks(IEngine *engine)
2022
engine->addCompileFunction(this, "data_setvariableto", &compileSetVariable);
2123
engine->addCompileFunction(this, "data_changevariableby", &compileChangeVariableBy);
2224

25+
// Monitor names
26+
engine->addMonitorNameFunction(this, "data_variable", &variableMonitorName);
27+
2328
// Inputs
2429
engine->addInput(this, "VALUE", VALUE);
2530

@@ -44,3 +49,15 @@ void VariableBlocks::compileChangeVariableBy(Compiler *compiler)
4449
compiler->addInput(VALUE);
4550
compiler->addInstruction(vm::OP_CHANGE_VAR, { compiler->variableIndex(compiler->field(VARIABLE)->valuePtr()) });
4651
}
52+
53+
const std::string &VariableBlocks::variableMonitorName(Block *block)
54+
{
55+
Variable *var = dynamic_cast<Variable *>(block->findFieldById(VARIABLE)->valuePtr().get());
56+
57+
if (var)
58+
return var->name();
59+
else {
60+
static const std::string empty = "";
61+
return empty;
62+
}
63+
}

src/blocks/variableblocks.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class VariableBlocks : public IBlockSection
3030
static void compileVariable(Compiler *compiler);
3131
static void compileSetVariable(Compiler *compiler);
3232
static void compileChangeVariableBy(Compiler *compiler);
33+
34+
static const std::string &variableMonitorName(Block *block);
3335
};
3436

3537
} // namespace libscratchcpp

test/blocks/variable_blocks_test.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ TEST_F(VariableBlocksTest, RegisterBlocks)
7171
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_setvariableto", &VariableBlocks::compileSetVariable)).Times(1);
7272
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_changevariableby", &VariableBlocks::compileChangeVariableBy)).Times(1);
7373

74+
// Monitor names
75+
EXPECT_CALL(m_engineMock, addMonitorNameFunction(m_section.get(), "data_variable", &VariableBlocks::variableMonitorName));
76+
7477
// Inputs
7578
EXPECT_CALL(m_engineMock, addInput(m_section.get(), "VALUE", VariableBlocks::VALUE));
7679

@@ -110,6 +113,20 @@ TEST_F(VariableBlocksTest, Variable)
110113
ASSERT_TRUE(compiler.lists().empty());
111114
}
112115

116+
TEST_F(VariableBlocksTest, VariableMonitorName)
117+
{
118+
// [var1]
119+
auto var1 = std::make_shared<Variable>("b", "var1");
120+
auto block1 = createVariableBlock("a", "data_variable", var1);
121+
122+
// [var2]
123+
auto var2 = std::make_shared<Variable>("d", "var2");
124+
auto block2 = createVariableBlock("c", "data_variable", var2);
125+
126+
ASSERT_EQ(VariableBlocks::variableMonitorName(block1.get()), "var1");
127+
ASSERT_EQ(VariableBlocks::variableMonitorName(block2.get()), "var2");
128+
}
129+
113130
TEST_F(VariableBlocksTest, SetVariableTo)
114131
{
115132
Compiler compiler(&m_engine);

0 commit comments

Comments
 (0)