Skip to content

Commit e7ff662

Browse files
committed
Implement data_listcontents block
1 parent 22cf854 commit e7ff662

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-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);

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);

0 commit comments

Comments
 (0)