Skip to content

Commit baa7933

Browse files
committed
ScriptBuilder: Set compile functions of blocks in currentBlock()
1 parent c9e4f1e commit baa7933

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

include/scratchcpp/dev/test/scriptbuilder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class LIBSCRATCHCPP_EXPORT ScriptBuilder
4444
void addDropdownInput(const std::string &name, const std::string &selectedValue);
4545
void addDropdownField(const std::string &name, const std::string &selectedValue);
4646

47-
std::shared_ptr<Block> currentBlock() const;
47+
std::shared_ptr<Block> currentBlock();
4848

4949
void build();
5050
void run();
@@ -53,6 +53,7 @@ class LIBSCRATCHCPP_EXPORT ScriptBuilder
5353

5454
private:
5555
void addBlock(std::shared_ptr<Block> block);
56+
void build(std::shared_ptr<Target> target);
5657

5758
spimpl::unique_impl_ptr<ScriptBuilderPrivate> impl;
5859
};

src/dev/test/scriptbuilder.cpp

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <scratchcpp/field.h>
77
#include <scratchcpp/dev/compilerconstant.h>
88
#include <scratchcpp/dev/executablecode.h>
9-
#include <scratchcpp/stage.h>
9+
#include <scratchcpp/sprite.h>
1010
#include <scratchcpp/iengine.h>
1111
#include <scratchcpp/list.h>
1212

@@ -153,31 +153,26 @@ void ScriptBuilder::addDropdownField(const std::string &name, const std::string
153153
impl->blocks.back()->addField(field);
154154
}
155155

156-
/*! Returns the current block (can be used e. g. with a custom Compiler instance). */
157-
std::shared_ptr<Block> ScriptBuilder::currentBlock() const
156+
/*!
157+
* Returns the current block (can be used e. g. with a custom Compiler instance).\n
158+
* The script is automatically built to set the compile function of the block.
159+
* \note This method is not intended for building scripts, use build() for that.
160+
*/
161+
std::shared_ptr<Block> ScriptBuilder::currentBlock()
158162
{
163+
if (!impl->lastBlock)
164+
return nullptr;
165+
166+
if (!impl->lastBlock->compileFunction())
167+
build(std::make_shared<Sprite>());
168+
159169
return impl->lastBlock;
160170
}
161171

162172
/*! Builds and compiles the script. */
163173
void ScriptBuilder::build()
164174
{
165-
if (impl->target->blocks().empty()) {
166-
for (auto block : impl->blocks)
167-
impl->target->addBlock(block);
168-
169-
for (auto block : impl->inputBlocks)
170-
impl->target->addBlock(block);
171-
}
172-
173-
std::vector<std::shared_ptr<Target>> targets = impl->engine->targets();
174-
175-
if (std::find(targets.begin(), targets.end(), impl->target) == targets.end()) {
176-
targets.push_back(impl->target);
177-
impl->engine->setTargets({ impl->target });
178-
}
179-
180-
impl->engine->compile();
175+
build(impl->target);
181176
}
182177

183178
/*! Runs the built script. */
@@ -202,3 +197,25 @@ void ScriptBuilder::addBlock(std::shared_ptr<Block> block)
202197

203198
impl->blocks.push_back(block);
204199
}
200+
201+
void ScriptBuilder::build(std::shared_ptr<Target> target)
202+
{
203+
impl->engine->clear();
204+
205+
if (target->blocks().empty()) {
206+
for (auto block : impl->blocks)
207+
target->addBlock(block);
208+
209+
for (auto block : impl->inputBlocks)
210+
target->addBlock(block);
211+
}
212+
213+
std::vector<std::shared_ptr<Target>> targets = impl->engine->targets();
214+
215+
if (std::find(targets.begin(), targets.end(), target) == targets.end()) {
216+
targets.push_back(target);
217+
impl->engine->setTargets({ target });
218+
}
219+
220+
impl->engine->compile();
221+
}

test/dev/test_api/scriptbuilder_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ TEST_F(ScriptBuilderTest, AddBlock)
4040
auto block = m_builder->currentBlock();
4141
ASSERT_TRUE(block);
4242
ASSERT_EQ(block->opcode(), "test_simple");
43+
ASSERT_TRUE(block->compileFunction());
4344

4445
m_builder->build();
4546

0 commit comments

Comments
 (0)