Skip to content

Commit ce6c1e5

Browse files
committed
Add topBlock property to Script
1 parent 87a3db9 commit ce6c1e5

File tree

7 files changed

+29
-21
lines changed

7 files changed

+29
-21
lines changed

include/scratchcpp/script.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace libscratchcpp
1212
{
1313

1414
class Target;
15+
class Block;
1516
class IEngine;
1617
class Value;
1718
class VirtualMachine;
@@ -23,10 +24,11 @@ class ScriptPrivate;
2324
class LIBSCRATCHCPP_EXPORT Script
2425
{
2526
public:
26-
Script(Target *target, IEngine *engine);
27+
Script(Target *target, std::shared_ptr<Block> topBlock, IEngine *engine);
2728
Script(const Script &) = delete;
2829

2930
Target *target() const;
31+
std::shared_ptr<Block> topBlock() const;
3032

3133
unsigned int *bytecode() const;
3234
const std::vector<unsigned int> &bytecodeVector() const;

src/engine/internal/engine.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void Engine::compile()
120120
if (block->topLevel() && !block->shadow()) {
121121
auto section = blockSection(block->opcode());
122122
if (section) {
123-
auto script = std::make_shared<Script>(target.get(), this);
123+
auto script = std::make_shared<Script>(target.get(), block, this);
124124
m_scripts[block] = script;
125125

126126
compiler.compile(block);
@@ -581,11 +581,8 @@ bool Engine::broadcastByPtrRunning(Broadcast *broadcast)
581581

582582
for (auto thread : m_threads) {
583583
if (!thread->atEnd()) {
584-
// TODO: Store the top block in Script
585584
Script *script = thread->script();
586-
auto it = std::find_if(m_scripts.begin(), m_scripts.end(), [script](const std::pair<std::shared_ptr<Block>, std::shared_ptr<Script>> pair) { return pair.second.get() == script; });
587-
assert(it != m_scripts.end());
588-
auto topBlock = it->first;
585+
auto topBlock = script->topBlock();
589586

590587
const auto &scripts = m_backdropChangeHats[script->target()];
591588
auto scriptIt = std::find(scripts.begin(), scripts.end(), script);
@@ -1301,10 +1298,7 @@ std::vector<std::shared_ptr<VirtualMachine>> Engine::startHats(HatType hatType,
13011298
allScriptsByOpcodeDo(
13021299
hatType,
13031300
[this, hatType, &optMatchFields, &newThreads](Script *script, Target *target) {
1304-
// TODO: Store the top block in Script
1305-
auto it = std::find_if(m_scripts.begin(), m_scripts.end(), [script](const std::pair<std::shared_ptr<Block>, std::shared_ptr<Script>> pair) { return pair.second.get() == script; });
1306-
assert(it != m_scripts.end());
1307-
auto topBlock = it->first;
1301+
auto topBlock = script->topBlock();
13081302

13091303
if (!optMatchFields.empty()) {
13101304
// Get the field map for this script

src/engine/script.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
using namespace libscratchcpp;
1414

1515
/*! Constructs Script. */
16-
Script::Script(Target *target, IEngine *engine) :
17-
impl(spimpl::make_unique_impl<ScriptPrivate>(target, engine))
16+
Script::Script(Target *target, std::shared_ptr<Block> topBlock, IEngine *engine) :
17+
impl(spimpl::make_unique_impl<ScriptPrivate>(target, topBlock, engine))
1818
{
1919
}
2020

@@ -24,6 +24,11 @@ Target *Script::target() const
2424
return impl->target;
2525
}
2626

27+
std::shared_ptr<Block> Script::topBlock() const
28+
{
29+
return impl->topBlock;
30+
}
31+
2732
/*! Returns the bytecode array. */
2833
unsigned int *Script::bytecode() const
2934
{

src/engine/script_p.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
using namespace libscratchcpp;
66

7-
ScriptPrivate::ScriptPrivate(Target *target, IEngine *engine) :
7+
ScriptPrivate::ScriptPrivate(Target *target, std::shared_ptr<Block> topBlock, IEngine *engine) :
88
target(target),
9+
topBlock(topBlock),
910
engine(engine)
1011
{
1112
}

src/engine/script_p.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,28 @@
33
#pragma once
44

55
#include <vector>
6+
#include <memory>
67
#include <scratchcpp/value.h>
78

89
namespace libscratchcpp
910
{
1011

1112
class Target;
13+
class Block;
1214
class IEngine;
1315
class Variable;
1416
class List;
1517

1618
struct ScriptPrivate
1719
{
18-
ScriptPrivate(Target *target, IEngine *engine);
20+
ScriptPrivate(Target *target, std::shared_ptr<Block> topBlock, IEngine *engine);
1921
ScriptPrivate(const ScriptPrivate &) = delete;
2022

2123
unsigned int *bytecode = nullptr;
2224
std::vector<unsigned int> bytecodeVector;
2325

2426
Target *target = nullptr;
27+
std::shared_ptr<Block> topBlock;
2528
IEngine *engine = nullptr;
2629

2730
unsigned int **procedures = nullptr;

test/script/script_test.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <scratchcpp/script.h>
2+
#include <scratchcpp/block.h>
23
#include <scratchcpp/virtualmachine.h>
34
#include <scratchcpp/target.h>
45
#include <scratchcpp/sprite.h>
@@ -22,13 +23,15 @@ class ScriptTest : public testing::Test
2223

2324
TEST_F(ScriptTest, Constructors)
2425
{
25-
Script script(&m_target, &m_engine);
26+
auto block = std::make_shared<Block>("", "");
27+
Script script(&m_target, block, &m_engine);
2628
ASSERT_EQ(script.target(), &m_target);
29+
ASSERT_EQ(script.topBlock(), block);
2730
}
2831

2932
TEST_F(ScriptTest, Bytecode)
3033
{
31-
Script script(nullptr, nullptr);
34+
Script script(nullptr, nullptr, nullptr);
3235
ASSERT_EQ(script.bytecode(), nullptr);
3336
ASSERT_TRUE(script.bytecodeVector().empty());
3437

@@ -58,7 +61,7 @@ TEST_F(ScriptTest, Start)
5861
std::shared_ptr<List> list2 = std::make_unique<List>("d", "");
5962
static std::vector<List *> lists = { list1.get(), list2.get() };
6063

61-
Script script1(nullptr, nullptr);
64+
Script script1(nullptr, nullptr, nullptr);
6265

6366
std::shared_ptr<VirtualMachine> vm = script1.start();
6467
ASSERT_TRUE(vm);
@@ -71,14 +74,14 @@ TEST_F(ScriptTest, Start)
7174
ASSERT_EQ(vm->variables(), nullptr);
7275
ASSERT_EQ(vm->lists(), nullptr);
7376

74-
Script script2(&m_target, &m_engine);
77+
Script script2(&m_target, nullptr, &m_engine);
7578

7679
vm = script2.start();
7780
ASSERT_TRUE(vm);
7881
ASSERT_EQ(vm->target(), &m_target);
7982
ASSERT_EQ(vm->engine(), &m_engine);
8083

81-
Script script3(&m_target, &m_engine);
84+
Script script3(&m_target, nullptr, &m_engine);
8285
script3.setBytecode(bytecode);
8386
script3.setProcedures(procedures);
8487
script3.setFunctions(functions);
@@ -120,7 +123,7 @@ TEST_F(ScriptTest, Start)
120123
EXPECT_CALL(m_engine, requestRedraw());
121124
auto clone = root.clone();
122125

123-
Script script4(&root, &m_engine);
126+
Script script4(&root, nullptr, &m_engine);
124127
script4.setBytecode(bytecode);
125128
script4.setProcedures(procedures);
126129
script4.setFunctions(functions);

test/virtual_machine/virtual_machine_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ TEST(VirtualMachineTest, Constructors)
2323

2424
Target target;
2525
Engine engine;
26-
Script script(&target, &engine);
26+
Script script(&target, nullptr, &engine);
2727
VirtualMachine vm2(&target, &engine, &script);
2828
ASSERT_EQ(vm2.target(), &target);
2929
ASSERT_EQ(vm2.engine(), &engine);

0 commit comments

Comments
 (0)