Skip to content

Commit 9e8440d

Browse files
committed
ScriptBuilder: Add support for substack sequences
1 parent 53e30e2 commit 9e8440d

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

src/dev/test/scriptbuilder.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,23 @@ void ScriptBuilder::addObscuredInput(const std::string &name, std::shared_ptr<Bl
103103
if (!impl->lastBlock)
104104
return;
105105

106-
valueBlock->setId(std::to_string(impl->blockId++));
107-
impl->inputBlocks.push_back(valueBlock);
106+
auto block = valueBlock;
107+
108+
while (block) {
109+
block->setId(std::to_string(impl->blockId++));
110+
impl->inputBlocks.push_back(block);
111+
112+
auto parent = block->parent();
113+
auto next = block->next();
114+
115+
if (parent)
116+
parent->setNext(block);
117+
118+
if (next)
119+
next->setParent(block);
120+
121+
block = next;
122+
}
108123

109124
auto input = std::make_shared<Input>(name, Input::Type::ObscuredShadow);
110125
input->setValueBlock(valueBlock);

test/dev/test_api/scriptbuilder_test.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,25 @@ TEST_F(ScriptBuilderTest, AddObscuredInput)
110110
ASSERT_EQ(testing::internal::GetCapturedStdout(), "test\n");
111111
}
112112

113+
TEST_F(ScriptBuilderTest, AddObscuredInputMultipleBlocks)
114+
{
115+
m_builder->addBlock("test_substack");
116+
auto substack = std::make_shared<Block>("", "test_simple");
117+
auto block1 = std::make_shared<Block>("", "test_simple");
118+
substack->setNext(block1);
119+
block1->setParent(substack);
120+
auto block2 = std::make_shared<Block>("", "test_simple");
121+
block1->setNext(block2);
122+
block2->setParent(block1);
123+
m_builder->addObscuredInput("SUBSTACK", substack);
124+
125+
m_builder->build();
126+
127+
testing::internal::CaptureStdout();
128+
m_builder->run();
129+
ASSERT_EQ(testing::internal::GetCapturedStdout(), "test\ntest\ntest\n");
130+
}
131+
113132
TEST_F(ScriptBuilderTest, AddNullObscuredInput)
114133
{
115134
m_builder->addBlock("test_print");

test/dev/test_api/testextension.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void TestExtension::registerBlocks(IEngine *engine)
2929
engine->addCompileFunction(this, "test_print_field", &compilePrintField);
3030
engine->addCompileFunction(this, "test_teststr", &compileTestStr);
3131
engine->addCompileFunction(this, "test_input", &compileInput);
32+
engine->addCompileFunction(this, "test_substack", &compileSubstack);
3233
}
3334

3435
CompilerValue *TestExtension::compileSimple(Compiler *compiler)
@@ -69,6 +70,13 @@ CompilerValue *TestExtension::compileInput(Compiler *compiler)
6970
return compiler->addInput("INPUT");
7071
}
7172

73+
CompilerValue *TestExtension::compileSubstack(Compiler *compiler)
74+
{
75+
auto substack = compiler->input("SUBSTACK");
76+
compiler->moveToIf(compiler->addConstValue(true), substack->valueBlock());
77+
return nullptr;
78+
}
79+
7280
extern "C" void test_simple()
7381
{
7482
std::cout << "test" << std::endl;

test/dev/test_api/testextension.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class TestExtension : public IExtension
2020
static CompilerValue *compilePrintField(Compiler *compiler);
2121
static CompilerValue *compileTestStr(Compiler *compiler);
2222
static CompilerValue *compileInput(Compiler *compiler);
23+
static CompilerValue *compileSubstack(Compiler *compiler);
2324
};
2425

2526
} // namespace libscratchcpp

0 commit comments

Comments
 (0)