Skip to content

Commit ef0ca4d

Browse files
authored
Merge pull request #432 from scratchcpp/data_monitor_blocks
Implement data monitor blocks
2 parents 0fcabea + e7ff662 commit ef0ca4d

File tree

6 files changed

+92
-0
lines changed

6 files changed

+92
-0
lines changed

src/blocks/listblocks.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ bool ListBlocks::categoryVisible() const
2121
void ListBlocks::registerBlocks(IEngine *engine)
2222
{
2323
// Blocks
24+
engine->addCompileFunction(this, "data_listcontents", &compileListContents);
2425
engine->addCompileFunction(this, "data_addtolist", &compileAddToList);
2526
engine->addCompileFunction(this, "data_deleteoflist", &compileDeleteFromList);
2627
engine->addCompileFunction(this, "data_deletealloflist", &compileDeleteAllOfList);
@@ -39,6 +40,14 @@ void ListBlocks::registerBlocks(IEngine *engine)
3940
engine->addField(this, "LIST", LIST);
4041
}
4142

43+
void ListBlocks::compileListContents(Compiler *compiler)
44+
{
45+
// NOTE: This block is only used by list monitors
46+
// Instead of returning the actual list contents, let's just return the index of the list
47+
// and let the renderer read the list using the index
48+
compiler->addConstValue(static_cast<size_t>(compiler->listIndex(compiler->field(LIST)->valuePtr())));
49+
}
50+
4251
void ListBlocks::compileAddToList(Compiler *compiler)
4352
{
4453
compiler->addInput(ITEM);

src/blocks/listblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class ListBlocks : public IBlockSection
2929

3030
void registerBlocks(IEngine *engine) override;
3131

32+
static void compileListContents(Compiler *compiler);
3233
static void compileAddToList(Compiler *compiler);
3334
static void compileDeleteFromList(Compiler *compiler);
3435
static void compileDeleteAllOfList(Compiler *compiler);

src/blocks/variableblocks.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ std::string VariableBlocks::name() const
1616
void VariableBlocks::registerBlocks(IEngine *engine)
1717
{
1818
// Blocks
19+
engine->addCompileFunction(this, "data_variable", &compileVariable);
1920
engine->addCompileFunction(this, "data_setvariableto", &compileSetVariable);
2021
engine->addCompileFunction(this, "data_changevariableby", &compileChangeVariableBy);
2122

@@ -26,6 +27,12 @@ void VariableBlocks::registerBlocks(IEngine *engine)
2627
engine->addField(this, "VARIABLE", VARIABLE);
2728
}
2829

30+
void VariableBlocks::compileVariable(Compiler *compiler)
31+
{
32+
// NOTE: This block is only used by variable monitors
33+
compiler->addInstruction(vm::OP_READ_VAR, { compiler->variableIndex(compiler->field(VARIABLE)->valuePtr()) });
34+
}
35+
2936
void VariableBlocks::compileSetVariable(Compiler *compiler)
3037
{
3138
compiler->addInput(VALUE);

src/blocks/variableblocks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class VariableBlocks : public IBlockSection
2727

2828
void registerBlocks(IEngine *engine) override;
2929

30+
static void compileVariable(Compiler *compiler);
3031
static void compileSetVariable(Compiler *compiler);
3132
static void compileChangeVariableBy(Compiler *compiler);
3233
};

test/blocks/list_blocks_test.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ TEST_F(ListBlocksTest, CategoryVisible)
9494
TEST_F(ListBlocksTest, RegisterBlocks)
9595
{
9696
// Blocks
97+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_listcontents", &ListBlocks::compileListContents)).Times(1);
9798
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_addtolist", &ListBlocks::compileAddToList)).Times(1);
9899
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_deleteoflist", &ListBlocks::compileDeleteFromList)).Times(1);
99100
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_deletealloflist", &ListBlocks::compileDeleteAllOfList)).Times(1);
@@ -114,6 +115,36 @@ TEST_F(ListBlocksTest, RegisterBlocks)
114115
m_section->registerBlocks(&m_engineMock);
115116
}
116117

118+
TEST_F(ListBlocksTest, ListContents)
119+
{
120+
Compiler compiler(&m_engine);
121+
122+
// [list1]
123+
auto list1 = std::make_shared<List>("b", "list1");
124+
auto block1 = createListBlock("a", "data_listcontents", list1);
125+
126+
// [list2]
127+
auto list2 = std::make_shared<List>("d", "list2");
128+
auto block2 = createListBlock("c", "data_listcontents", list2);
129+
130+
compiler.init();
131+
compiler.setBlock(block1);
132+
ListBlocks::compileListContents(&compiler);
133+
compiler.setBlock(block2);
134+
ListBlocks::compileListContents(&compiler);
135+
compiler.end();
136+
137+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_CONST, 0, vm::OP_CONST, 1, vm::OP_HALT }));
138+
ASSERT_EQ(compiler.constValues(), std::vector<Value>({ 0, 1 }));
139+
ASSERT_TRUE(compiler.variables().empty());
140+
ASSERT_EQ(
141+
compiler.lists(),
142+
std::vector<List *>({
143+
list1.get(),
144+
list2.get(),
145+
}));
146+
}
147+
117148
TEST_F(ListBlocksTest, AddToList)
118149
{
119150
Compiler compiler(&m_engine);

test/blocks/variable_blocks_test.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ class VariableBlocksTest : public testing::Test
3737
return block;
3838
}
3939

40+
// For read variable
41+
std::shared_ptr<Block> createVariableBlock(const std::string &id, const std::string &opcode, std::shared_ptr<Variable> variable) const
42+
{
43+
auto block = std::make_shared<Block>(id, opcode);
44+
45+
auto variableField = std::make_shared<Field>("VARIABLE", Value(), variable);
46+
variableField->setFieldId(VariableBlocks::VARIABLE);
47+
block->addField(variableField);
48+
49+
return block;
50+
}
51+
4052
std::unique_ptr<IBlockSection> m_section;
4153
EngineMock m_engineMock;
4254
Engine m_engine;
@@ -55,6 +67,7 @@ TEST_F(VariableBlocksTest, CategoryVisible)
5567
TEST_F(VariableBlocksTest, RegisterBlocks)
5668
{
5769
// Blocks
70+
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_variable", &VariableBlocks::compileVariable)).Times(1);
5871
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_setvariableto", &VariableBlocks::compileSetVariable)).Times(1);
5972
EXPECT_CALL(m_engineMock, addCompileFunction(m_section.get(), "data_changevariableby", &VariableBlocks::compileChangeVariableBy)).Times(1);
6073

@@ -67,6 +80,36 @@ TEST_F(VariableBlocksTest, RegisterBlocks)
6780
m_section->registerBlocks(&m_engineMock);
6881
}
6982

83+
TEST_F(VariableBlocksTest, Variable)
84+
{
85+
Compiler compiler(&m_engine);
86+
87+
// [var1]
88+
auto var1 = std::make_shared<Variable>("b", "var1");
89+
auto block1 = createVariableBlock("a", "data_variable", var1);
90+
91+
// [var2]
92+
auto var2 = std::make_shared<Variable>("d", "var2");
93+
auto block2 = createVariableBlock("c", "data_variable", var2);
94+
95+
compiler.init();
96+
compiler.setBlock(block1);
97+
VariableBlocks::compileVariable(&compiler);
98+
compiler.setBlock(block2);
99+
VariableBlocks::compileVariable(&compiler);
100+
compiler.end();
101+
102+
ASSERT_EQ(compiler.bytecode(), std::vector<unsigned int>({ vm::OP_START, vm::OP_READ_VAR, 0, vm::OP_READ_VAR, 1, vm::OP_HALT }));
103+
ASSERT_TRUE(compiler.constValues().empty());
104+
ASSERT_EQ(
105+
compiler.variables(),
106+
std::vector<Variable *>({
107+
var1.get(),
108+
var2.get(),
109+
}));
110+
ASSERT_TRUE(compiler.lists().empty());
111+
}
112+
70113
TEST_F(VariableBlocksTest, SetVariableTo)
71114
{
72115
Compiler compiler(&m_engine);

0 commit comments

Comments
 (0)