Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/scratchcpp/iengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ class LIBSCRATCHCPP_EXPORT IEngine
/*! Returns the index of the given block function. */
virtual unsigned int functionIndex(BlockFunc f) = 0;

/*! Returns the list of block functions. */
virtual const std::vector<BlockFunc> &blockFunctions() const = 0;

/*!
* Call this from IBlockSection#registerBlocks() to add a compile function to a block section.
* \see <a href="blockSections.html">Block sections</a>
Expand Down
3 changes: 2 additions & 1 deletion include/scratchcpp/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class LIBSCRATCHCPP_EXPORT Script
bool runHatPredicate();

void setProcedures(const std::vector<unsigned int *> &procedures);
void setFunctions(const std::vector<BlockFunc> &functions);
void setConstValues(const std::vector<Value> &values);
void setVariables(const std::vector<Variable *> &variables);
void setLists(const std::vector<List *> &lists);
Expand All @@ -47,6 +46,8 @@ class LIBSCRATCHCPP_EXPORT Script
std::shared_ptr<VirtualMachine> start(Target *target);

private:
BlockFunc *getFunctions() const;

spimpl::unique_impl_ptr<ScriptPrivate> impl;
};

Expand Down
7 changes: 5 additions & 2 deletions src/engine/internal/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ void Engine::compile()

for (auto block : blocks) {
if (m_scripts.count(block) == 1) {
m_scripts[block]->setFunctions(m_functions);
m_scripts[block]->setProcedures(procedureBytecodes);
m_scripts[block]->setConstValues(compiler.constValues());
m_scripts[block]->setVariables(compiler.variables());
Expand Down Expand Up @@ -293,7 +292,6 @@ void Engine::compile()
compiler.end();

script->setBytecode(compiler.bytecode());
script->setFunctions(m_functions);
script->setConstValues(compiler.constValues());
script->setVariables(compiler.variables());
script->setLists(compiler.lists());
Expand Down Expand Up @@ -887,6 +885,11 @@ unsigned int Engine::functionIndex(BlockFunc f)
return m_functions.size() - 1;
}

const std::vector<BlockFunc> &Engine::blockFunctions() const
{
return m_functions;
}

void Engine::addCompileFunction(IBlockSection *section, const std::string &opcode, BlockComp f)
{
auto container = blockSectionContainer(section);
Expand Down
1 change: 1 addition & 0 deletions src/engine/internal/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class Engine : public IEngine
void registerSection(std::shared_ptr<IBlockSection> section) override;
std::vector<std::shared_ptr<IBlockSection>> registeredSections() const;
unsigned int functionIndex(BlockFunc f) override;
const std::vector<BlockFunc> &blockFunctions() const override;

void addCompileFunction(IBlockSection *section, const std::string &opcode, BlockComp f) override;
void addHatPredicateCompileFunction(IBlockSection *section, const std::string &opcode, HatPredicateCompileFunc f) override;
Expand Down
24 changes: 11 additions & 13 deletions src/engine/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ void Script::setHatPredicateBytecode(const std::vector<unsigned int> &code)
if (impl->engine && !code.empty()) {
impl->hatPredicateVm = std::make_shared<VirtualMachine>(impl->engine->stage(), impl->engine, this);
impl->hatPredicateVm->setBytecode(impl->hatPredicateBytecodeVector.data());
impl->hatPredicateVm->setFunctions(impl->functions);
impl->hatPredicateVm->setConstValues(impl->constValues);
}
}
Expand All @@ -70,6 +69,7 @@ bool Script::runHatPredicate()
{
if (impl->hatPredicateVm && impl->hatPredicateVm->bytecode()) {
impl->hatPredicateVm->reset();
impl->hatPredicateVm->setFunctions(getFunctions());
impl->hatPredicateVm->run();
assert(impl->hatPredicateVm->registerCount() == 1);

Expand All @@ -92,14 +92,14 @@ std::shared_ptr<VirtualMachine> Script::start(Target *target)
auto vm = std::make_shared<VirtualMachine>(target, impl->engine, this);
vm->setBytecode(impl->bytecode);
vm->setProcedures(impl->procedures);
vm->setFunctions(impl->functions);
vm->setFunctions(getFunctions());
vm->setConstValues(impl->constValues);

Sprite *sprite = nullptr;
if (target && !target->isStage())
sprite = dynamic_cast<Sprite *>(target);

if (impl->target && sprite && sprite->isClone() && impl->engine) {
if (impl->target && sprite && sprite->isClone()) {
Target *root = sprite->cloneSprite();

if (root != impl->target) {
Expand Down Expand Up @@ -156,16 +156,6 @@ void Script::setProcedures(const std::vector<unsigned int *> &procedures)
impl->procedures = impl->proceduresVector.data();
}

/*! Sets the list of functions. */
void Script::setFunctions(const std::vector<BlockFunc> &functions)
{
impl->functionsVector = functions;
impl->functions = impl->functionsVector.data();

if (impl->hatPredicateVm)
impl->hatPredicateVm->setFunctions(impl->functions);
}

/*! Sets the list of constant values. */
void Script::setConstValues(const std::vector<Value> &values)
{
Expand All @@ -191,3 +181,11 @@ void Script::setLists(const std::vector<List *> &lists)
{
impl->lists = lists;
}

BlockFunc *Script::getFunctions() const
{
if (impl->engine)
return const_cast<BlockFunc *>(impl->engine->blockFunctions().data());

return nullptr;
}
3 changes: 0 additions & 3 deletions src/engine/script_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ struct ScriptPrivate
unsigned int **procedures = nullptr;
std::vector<unsigned int *> proceduresVector;

BlockFunc *functions = nullptr;
std::vector<BlockFunc> functionsVector;

const Value *constValues = nullptr;
std::vector<Value> constValuesVector;

Expand Down
2 changes: 2 additions & 0 deletions test/engine/engine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,8 @@ TEST(EngineTest, Functions)
ASSERT_EQ(engine.functionIndex(&testFunction2), 1);
ASSERT_EQ(engine.functionIndex(&testFunction1), 0);
ASSERT_EQ(engine.functionIndex(&testFunction2), 1);

ASSERT_EQ(engine.blockFunctions(), std::vector<BlockFunc>({ &testFunction1, &testFunction2 }));
}

void compileTest1(Compiler *)
Expand Down
1 change: 1 addition & 0 deletions test/mocks/enginemock.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class EngineMock : public IEngine

MOCK_METHOD(void, registerSection, (std::shared_ptr<IBlockSection>), (override));
MOCK_METHOD(unsigned int, functionIndex, (BlockFunc), (override));
MOCK_METHOD(const std::vector<BlockFunc> &, blockFunctions, (), (const, override));

MOCK_METHOD(void, addCompileFunction, (IBlockSection *, const std::string &, BlockComp), (override));
MOCK_METHOD(void, addHatPredicateCompileFunction, (IBlockSection *, const std::string &, HatPredicateCompileFunc), (override));
Expand Down
16 changes: 12 additions & 4 deletions test/script/script_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using namespace libscratchcpp;

using ::testing::Return;
using ::testing::ReturnRef;
using ::testing::_;

class ScriptTest : public testing::Test
Expand Down Expand Up @@ -73,10 +74,13 @@ TEST_F(ScriptTest, HatPredicate)
return 0;
};

std::vector<BlockFunc> functions1 = { f1 };
std::vector<BlockFunc> functions2 = { f1, f2, f3 };

EXPECT_CALL(m_engine, blockFunctions()).WillOnce(ReturnRef(functions1));
stageTest = nullptr;
engineTest = nullptr;
scriptTest = nullptr;
script.setFunctions({ f1 });
script.setConstValues({ "test" });
script.setHatPredicateBytecode({ vm::OP_START, vm::OP_CONST, 0, vm::OP_PRINT, vm::OP_EXEC, 0, vm::OP_HALT });
testing::internal::CaptureStdout();
Expand All @@ -86,11 +90,11 @@ TEST_F(ScriptTest, HatPredicate)
ASSERT_EQ(engineTest, &m_engine);
ASSERT_EQ(scriptTest, &script);

EXPECT_CALL(m_engine, blockFunctions()).WillOnce(ReturnRef(functions2));
stageTest = nullptr;
engineTest = nullptr;
scriptTest = nullptr;
script.setHatPredicateBytecode({ vm::OP_START, vm::OP_CONST, 0, vm::OP_PRINT, vm::OP_EXEC, 1, vm::OP_HALT });
script.setFunctions({ f1, f2, f3 });
script.setConstValues({ 5 });
testing::internal::CaptureStdout();
ASSERT_TRUE(script.runHatPredicate());
Expand All @@ -99,6 +103,7 @@ TEST_F(ScriptTest, HatPredicate)
ASSERT_EQ(engineTest, &m_engine);
ASSERT_EQ(scriptTest, &script);

EXPECT_CALL(m_engine, blockFunctions()).WillOnce(ReturnRef(functions2));
stageTest = nullptr;
engineTest = nullptr;
scriptTest = nullptr;
Expand All @@ -119,6 +124,7 @@ TEST_F(ScriptTest, Start)
static std::vector<unsigned int> bytecode = { vm::OP_START, vm::OP_HALT };
static std::vector<unsigned int *> procedures = { bytecode.data() };
static std::vector<BlockFunc> functions = { &testFunction };
static std::vector<BlockFunc> noFunctions;
static std::vector<Value> constValues = { "test" };

std::shared_ptr<Variable> var1 = std::make_unique<Variable>("a", "", Value());
Expand All @@ -144,6 +150,7 @@ TEST_F(ScriptTest, Start)

Script script2(&m_target, nullptr, &m_engine);

EXPECT_CALL(m_engine, blockFunctions()).WillOnce(ReturnRef(noFunctions));
vm = script2.start();
ASSERT_TRUE(vm);
ASSERT_EQ(vm->target(), &m_target);
Expand All @@ -152,11 +159,11 @@ TEST_F(ScriptTest, Start)
Script script3(&m_target, nullptr, &m_engine);
script3.setBytecode(bytecode);
script3.setProcedures(procedures);
script3.setFunctions(functions);
script3.setConstValues(constValues);
script3.setVariables(variables);
script3.setLists(lists);

EXPECT_CALL(m_engine, blockFunctions()).WillOnce(ReturnRef(functions));
vm = script3.start();
ASSERT_TRUE(vm);
ASSERT_EQ(vm->bytecode()[0], bytecode[0]);
Expand All @@ -166,6 +173,7 @@ TEST_F(ScriptTest, Start)
ASSERT_EQ(vm->variables()[0], variables[0]->valuePtr());
ASSERT_EQ(vm->lists()[0], lists[0]);

EXPECT_CALL(m_engine, blockFunctions()).WillOnce(ReturnRef(functions));
Target target;
target.addVariable(var1);
target.addList(list1);
Expand Down Expand Up @@ -194,11 +202,11 @@ TEST_F(ScriptTest, Start)
Script script4(&root, nullptr, &m_engine);
script4.setBytecode(bytecode);
script4.setProcedures(procedures);
script4.setFunctions(functions);
script4.setConstValues(constValues);
script4.setVariables(variables);
script4.setLists(lists);

EXPECT_CALL(m_engine, blockFunctions()).WillOnce(ReturnRef(functions));
vm = script4.start(clone.get());

ASSERT_TRUE(vm);
Expand Down