Skip to content

Commit 55effde

Browse files
committed
Use shared_ptr for blocks only in targets
1 parent 9e01278 commit 55effde

32 files changed

+435
-431
lines changed

include/scratchcpp/block.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ class InputValue;
1818
class BlockPrivate;
1919

2020
/*! \brief The Block class represents a Scratch block. */
21-
class LIBSCRATCHCPP_EXPORT Block : public Entity
21+
class LIBSCRATCHCPP_EXPORT Block
22+
: public Entity
23+
, public std::enable_shared_from_this<Block>
2224
{
2325
public:
2426
friend class Engine;
@@ -30,14 +32,14 @@ class LIBSCRATCHCPP_EXPORT Block : public Entity
3032

3133
const std::string &opcode() const;
3234

33-
std::shared_ptr<Block> next() const;
35+
Block *next() const;
3436
const std::string &nextId() const;
35-
void setNext(std::shared_ptr<Block> block);
37+
void setNext(Block *block);
3638
void setNextId(const std::string &nextId);
3739

38-
std::shared_ptr<Block> parent() const;
40+
Block *parent() const;
3941
const std::string &parentId() const;
40-
void setParent(std::shared_ptr<Block> block);
42+
void setParent(Block *block);
4143
void setParentId(const std::string &id);
4244

4345
const std::vector<std::shared_ptr<Input>> &inputs() const;

include/scratchcpp/compiler.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ class LIBSCRATCHCPP_EXPORT Compiler
5454

5555
IEngine *engine() const;
5656
Target *target() const;
57-
std::shared_ptr<Block> block() const;
57+
Block *block() const;
5858

59-
std::shared_ptr<ExecutableCode> compile(std::shared_ptr<Block> startBlock, CodeType codeType = CodeType::Script);
59+
std::shared_ptr<ExecutableCode> compile(Block *startBlock, CodeType codeType = CodeType::Script);
6060
void preoptimize();
6161

6262
CompilerValue *addFunctionCall(const std::string &functionName, StaticType returnType = StaticType::Void, const ArgTypes &argTypes = {}, const Args &args = {});
@@ -137,11 +137,11 @@ class LIBSCRATCHCPP_EXPORT Compiler
137137
void beginLoopCondition();
138138
void endLoop();
139139

140-
void moveToIf(CompilerValue *cond, std::shared_ptr<Block> substack);
141-
void moveToIfElse(CompilerValue *cond, std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2);
142-
void moveToRepeatLoop(CompilerValue *count, std::shared_ptr<Block> substack);
143-
void moveToWhileLoop(CompilerValue *cond, std::shared_ptr<Block> substack);
144-
void moveToRepeatUntilLoop(CompilerValue *cond, std::shared_ptr<Block> substack);
140+
void moveToIf(CompilerValue *cond, Block *substack);
141+
void moveToIfElse(CompilerValue *cond, Block *substack1, Block *substack2);
142+
void moveToRepeatLoop(CompilerValue *count, Block *substack);
143+
void moveToWhileLoop(CompilerValue *cond, Block *substack);
144+
void moveToRepeatUntilLoop(CompilerValue *cond, Block *substack);
145145
void warp();
146146

147147
void createYield();

include/scratchcpp/iengine.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class LIBSCRATCHCPP_EXPORT IEngine
6060
virtual void stop() = 0;
6161

6262
/*! Starts a script with the given top level block as the given Target (a sprite or the stage). */
63-
virtual Thread *startScript(std::shared_ptr<Block> topLevelBlock, Target *) = 0;
63+
virtual Thread *startScript(Block *topLevelBlock, Target *) = 0;
6464

6565
/*! Starts the scripts of the broadcast with the given index. */
6666
virtual void broadcast(int index, Thread *sender, bool wait) = 0;
@@ -283,28 +283,28 @@ class LIBSCRATCHCPP_EXPORT IEngine
283283
virtual int findBroadcastById(const std::string &broadcastId) const = 0;
284284

285285
/* Registers the given "when touching object" script. */
286-
virtual void addWhenTouchingObjectScript(std::shared_ptr<Block> hatBlock) = 0;
286+
virtual void addWhenTouchingObjectScript(Block *hatBlock) = 0;
287287

288288
/*! Registers the "green flag" script. */
289-
virtual void addGreenFlagScript(std::shared_ptr<Block> hatBlock) = 0;
289+
virtual void addGreenFlagScript(Block *hatBlock) = 0;
290290

291291
/*! Registers the broadcast script. */
292-
virtual void addBroadcastScript(std::shared_ptr<Block> whenReceivedBlock, Field *field, Broadcast *broadcast) = 0;
292+
virtual void addBroadcastScript(Block *whenReceivedBlock, Field *field, Broadcast *broadcast) = 0;
293293

294294
/*! Registers the backdrop change script. */
295-
virtual void addBackdropChangeScript(std::shared_ptr<Block> hatBlock, Field *field) = 0;
295+
virtual void addBackdropChangeScript(Block *hatBlock, Field *field) = 0;
296296

297297
/* Registers the given "when I start as clone" script. */
298-
virtual void addCloneInitScript(std::shared_ptr<Block> hatBlock) = 0;
298+
virtual void addCloneInitScript(Block *hatBlock) = 0;
299299

300300
/* Registers the given "when key pressed" script. */
301-
virtual void addKeyPressScript(std::shared_ptr<Block> hatBlock, Field *field) = 0;
301+
virtual void addKeyPressScript(Block *hatBlock, Field *field) = 0;
302302

303303
/* Registers the given "when this sprite/stage clicked" script. */
304-
virtual void addTargetClickScript(std::shared_ptr<Block> hatBlock) = 0;
304+
virtual void addTargetClickScript(Block *hatBlock) = 0;
305305

306306
/* Registers the given "when greater than" script. */
307-
virtual void addWhenGreaterThanScript(std::shared_ptr<Block> hatBlock) = 0;
307+
virtual void addWhenGreaterThanScript(Block *hatBlock) = 0;
308308

309309
/*! Returns the list of targets. */
310310
virtual const std::vector<std::shared_ptr<Target>> &targets() const = 0;
@@ -377,7 +377,7 @@ class LIBSCRATCHCPP_EXPORT IEngine
377377
virtual void setExtensions(const std::vector<std::string> &newExtensions) = 0;
378378

379379
/*! Returns the map of scripts (each top level block has a Script object). */
380-
virtual const std::unordered_map<std::shared_ptr<Block>, std::shared_ptr<Script>> &scripts() const = 0;
380+
virtual const std::unordered_map<Block *, std::shared_ptr<Script>> &scripts() const = 0;
381381

382382
/*! Returns the user agent of the last person to edit the project. */
383383
virtual const std::string &userAgent() const = 0;

include/scratchcpp/input.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class LIBSCRATCHCPP_EXPORT Input
4040
void setPrimaryValue(const Value &value);
4141
void setSecondaryValue(const Value &value);
4242

43-
std::shared_ptr<Block> valueBlock() const;
43+
Block *valueBlock() const;
4444
const std::string &valueBlockId() const;
45-
void setValueBlock(std::shared_ptr<Block> block);
45+
void setValueBlock(Block *block);
4646
void setValueBlockId(const std::string &id);
4747

4848
bool pointsToDropdownMenu() const;

include/scratchcpp/inputvalue.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class LIBSCRATCHCPP_EXPORT InputValue
4343
const Value &value() const;
4444
void setValue(const Value &newValue);
4545

46-
const std::shared_ptr<Block> &valueBlock() const;
47-
void setValueBlock(const std::shared_ptr<Block> &newValueBlock);
46+
Block *valueBlock() const;
47+
void setValueBlock(Block *newValueBlock);
4848

4949
const std::string &valueBlockId() const;
5050
void setValueBlockId(const std::string &newValueBlockId);

include/scratchcpp/script.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ class ScriptPrivate;
2222
class LIBSCRATCHCPP_EXPORT Script
2323
{
2424
public:
25-
Script(Target *target, std::shared_ptr<Block> topBlock, IEngine *engine);
25+
Script(Target *target, Block *topBlock, IEngine *engine);
2626
Script(const Script &) = delete;
2727

2828
Target *target() const;
29-
std::shared_ptr<Block> topBlock() const;
29+
Block *topBlock() const;
3030

3131
ExecutableCode *code() const;
3232
void setCode(std::shared_ptr<ExecutableCode> code);

include/scratchcpp/test/scriptbuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class LIBSCRATCHCPP_EXPORT ScriptBuilder
4646
void addEntityInput(const std::string &name, const std::string &entityName, InputValue::Type entityType, std::shared_ptr<Entity> entity);
4747
void addEntityField(const std::string &name, std::shared_ptr<Entity> entity);
4848

49-
std::shared_ptr<Block> currentBlock();
49+
Block *currentBlock();
5050
std::shared_ptr<Block> takeBlock();
5151

5252
void build();

src/engine/compiler.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ Target *Compiler::target() const
3838
}
3939

4040
/*! Returns currently compiled block. */
41-
std::shared_ptr<libscratchcpp::Block> Compiler::block() const
41+
Block *Compiler::block() const
4242
{
4343
return impl->block;
4444
}
4545

4646
/*! Compiles the script starting with the given block. */
47-
std::shared_ptr<ExecutableCode> Compiler::compile(std::shared_ptr<Block> startBlock, CodeType codeType)
47+
std::shared_ptr<ExecutableCode> Compiler::compile(Block *startBlock, CodeType codeType)
4848
{
4949
BlockPrototype *procedurePrototype = nullptr;
5050

@@ -607,7 +607,7 @@ void Compiler::endLoop()
607607
}
608608

609609
/*! Jumps to the given if substack. */
610-
void Compiler::moveToIf(CompilerValue *cond, std::shared_ptr<Block> substack)
610+
void Compiler::moveToIf(CompilerValue *cond, Block *substack)
611611
{
612612
if (!substack)
613613
return; // ignore empty if statements
@@ -619,7 +619,7 @@ void Compiler::moveToIf(CompilerValue *cond, std::shared_ptr<Block> substack)
619619
}
620620

621621
/*! Jumps to the given if/else substack. The second substack is used for the else branch. */
622-
void Compiler::moveToIfElse(CompilerValue *cond, std::shared_ptr<Block> substack1, std::shared_ptr<Block> substack2)
622+
void Compiler::moveToIfElse(CompilerValue *cond, Block *substack1, Block *substack2)
623623
{
624624
if (!substack1 && !substack2)
625625
return; // ignore empty if statements
@@ -634,7 +634,7 @@ void Compiler::moveToIfElse(CompilerValue *cond, std::shared_ptr<Block> substack
634634
}
635635

636636
/*! Jumps to the given repeat loop substack. */
637-
void Compiler::moveToRepeatLoop(CompilerValue *count, std::shared_ptr<Block> substack)
637+
void Compiler::moveToRepeatLoop(CompilerValue *count, Block *substack)
638638
{
639639
impl->substackHit = true;
640640
impl->substackTree.push_back({ { impl->block, nullptr }, CompilerPrivate::SubstackType::Loop });
@@ -646,7 +646,7 @@ void Compiler::moveToRepeatLoop(CompilerValue *count, std::shared_ptr<Block> sub
646646
}
647647

648648
/*! Jumps to the given while loop substack. */
649-
void Compiler::moveToWhileLoop(CompilerValue *cond, std::shared_ptr<Block> substack)
649+
void Compiler::moveToWhileLoop(CompilerValue *cond, Block *substack)
650650
{
651651
impl->substackHit = true;
652652
impl->substackTree.push_back({ { impl->block, nullptr }, CompilerPrivate::SubstackType::Loop });
@@ -658,7 +658,7 @@ void Compiler::moveToWhileLoop(CompilerValue *cond, std::shared_ptr<Block> subst
658658
}
659659

660660
/*! Jumps to the given until loop substack. */
661-
void Compiler::moveToRepeatUntilLoop(CompilerValue *cond, std::shared_ptr<Block> substack)
661+
void Compiler::moveToRepeatUntilLoop(CompilerValue *cond, Block *substack)
662662
{
663663
impl->substackHit = true;
664664
impl->substackTree.push_back({ { impl->block, nullptr }, CompilerPrivate::SubstackType::Loop });

src/engine/compiler_p.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ struct CompilerPrivate
3535
std::shared_ptr<CompilerContext> ctxPtr; // for self-managed contexts
3636
CompilerContext *ctx = nullptr;
3737

38-
std::shared_ptr<Block> block;
38+
Block *block = nullptr;
3939
int customIfStatementCount = 0;
4040
int customLoopCount = 0;
41-
std::vector<std::pair<std::pair<std::shared_ptr<Block>, std::shared_ptr<Block>>, SubstackType>> substackTree;
41+
std::vector<std::pair<std::pair<Block *, Block *>, SubstackType>> substackTree;
4242
bool substackHit = false;
4343
bool emptySubstack = false;
4444
bool warp = false;

src/engine/internal/engine.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ void Engine::resolveIds()
106106
const auto &blocks = target->blocks();
107107
for (auto block : blocks) {
108108
IExtension *ext = blockExtension(block->opcode());
109-
block->setNext(getBlock(block->nextId(), target.get()));
110-
block->setParent(getBlock(block->parentId(), target.get()));
109+
block->setNext(getBlock(block->nextId(), target.get()).get());
110+
block->setParent(getBlock(block->parentId(), target.get()).get());
111111

112112
if (ext) {
113113
block->setCompileFunction(resolveBlockCompileFunc(ext, block->opcode()));
@@ -116,7 +116,7 @@ void Engine::resolveIds()
116116

117117
const auto &inputs = block->inputs();
118118
for (const auto &input : inputs) {
119-
input->setValueBlock(getBlock(input->valueBlockId(), target.get()));
119+
input->setValueBlock(getBlock(input->valueBlockId(), target.get()).get());
120120

121121
if (ext)
122122
input->setInputId(resolveInput(ext, input->name()));
@@ -271,12 +271,12 @@ void Engine::compile()
271271
if (block->topLevel() && !block->isTopLevelReporter() && !block->shadow()) {
272272
auto ext = blockExtension(block->opcode());
273273
if (ext) {
274-
auto script = std::make_shared<Script>(target.get(), block, this);
275-
m_scripts[block] = script;
276-
script->setCode(compiler.compile(block));
274+
auto script = std::make_shared<Script>(target.get(), block.get(), this);
275+
m_scripts[block.get()] = script;
276+
script->setCode(compiler.compile(block.get()));
277277

278278
if (block->hatPredicateCompileFunction())
279-
script->setHatPredicateCode(compiler.compile(block, Compiler::CodeType::HatPredicate));
279+
script->setHatPredicateCode(compiler.compile(block.get(), Compiler::CodeType::HatPredicate));
280280
} else {
281281
std::cout << "warning: unsupported top level block: " << block->opcode() << std::endl;
282282
m_unsupportedBlocks.insert(block->opcode());
@@ -364,7 +364,7 @@ void Engine::stop()
364364
m_stopped();
365365
}
366366

367-
Thread *Engine::startScript(std::shared_ptr<Block> topLevelBlock, Target *target)
367+
Thread *Engine::startScript(Block *topLevelBlock, Target *target)
368368
{
369369
return pushThread(topLevelBlock, target).get();
370370
}
@@ -499,10 +499,10 @@ void Engine::step()
499499

500500
Target *target = hatBlock->target();
501501
assert(target);
502-
auto it = m_edgeActivatedHatValues.find(hatBlock.get());
502+
auto it = m_edgeActivatedHatValues.find(hatBlock);
503503

504504
if (it == m_edgeActivatedHatValues.cend()) {
505-
m_edgeActivatedHatValues[hatBlock.get()] = {};
505+
m_edgeActivatedHatValues[hatBlock] = {};
506506
} else {
507507
auto &map = it->second;
508508
auto it = map.find(target);
@@ -513,7 +513,7 @@ void Engine::step()
513513

514514
bool newValue = thread->script()->runHatPredicate(hatBlock->target());
515515
bool edgeWasActivated = !oldValue && newValue; // changed from false true
516-
m_edgeActivatedHatValues[hatBlock.get()][target] = newValue;
516+
m_edgeActivatedHatValues[hatBlock][target] = newValue;
517517

518518
if (!edgeWasActivated)
519519
stopThread(thread.get());
@@ -1036,18 +1036,18 @@ int Engine::findBroadcastById(const std::string &broadcastId) const
10361036
return it - m_broadcasts.begin();
10371037
}
10381038

1039-
void Engine::addWhenTouchingObjectScript(std::shared_ptr<Block> hatBlock)
1039+
void Engine::addWhenTouchingObjectScript(Block *hatBlock)
10401040
{
10411041
addHatToMap(m_whenTouchingObjectHats, m_scripts[hatBlock].get());
10421042
}
10431043

1044-
void Engine::addGreenFlagScript(std::shared_ptr<Block> hatBlock)
1044+
void Engine::addGreenFlagScript(Block *hatBlock)
10451045
{
10461046
std::cout << "passed block: " << hatBlock << std::endl;
10471047
addHatToMap(m_greenFlagHats, m_scripts[hatBlock].get());
10481048
}
10491049

1050-
void Engine::addBroadcastScript(std::shared_ptr<Block> whenReceivedBlock, Field *field, Broadcast *broadcast)
1050+
void Engine::addBroadcastScript(Block *whenReceivedBlock, Field *field, Broadcast *broadcast)
10511051
{
10521052
assert(!broadcast->isBackdropBroadcast());
10531053
Script *script = m_scripts[whenReceivedBlock].get();
@@ -1066,7 +1066,7 @@ void Engine::addBroadcastScript(std::shared_ptr<Block> whenReceivedBlock, Field
10661066
addHatField(script, HatField::BroadcastOption, field);
10671067
}
10681068

1069-
void Engine::addBackdropChangeScript(std::shared_ptr<Block> hatBlock, Field *field)
1069+
void Engine::addBackdropChangeScript(Block *hatBlock, Field *field)
10701070
{
10711071
Stage *stage = this->stage();
10721072

@@ -1103,24 +1103,24 @@ void Engine::addBackdropChangeScript(std::shared_ptr<Block> hatBlock, Field *fie
11031103
addHatField(script, HatField::Backdrop, field);
11041104
}
11051105

1106-
void Engine::addCloneInitScript(std::shared_ptr<Block> hatBlock)
1106+
void Engine::addCloneInitScript(Block *hatBlock)
11071107
{
11081108
addHatToMap(m_cloneInitHats, m_scripts[hatBlock].get());
11091109
}
11101110

1111-
void Engine::addKeyPressScript(std::shared_ptr<Block> hatBlock, Field *field)
1111+
void Engine::addKeyPressScript(Block *hatBlock, Field *field)
11121112
{
11131113
Script *script = m_scripts[hatBlock].get();
11141114
addHatToMap(m_whenKeyPressedHats, script);
11151115
addHatField(script, HatField::KeyOption, field);
11161116
}
11171117

1118-
void Engine::addTargetClickScript(std::shared_ptr<Block> hatBlock)
1118+
void Engine::addTargetClickScript(Block *hatBlock)
11191119
{
11201120
addHatToMap(m_whenTargetClickedHats, m_scripts[hatBlock].get());
11211121
}
11221122

1123-
void Engine::addWhenGreaterThanScript(std::shared_ptr<Block> hatBlock)
1123+
void Engine::addWhenGreaterThanScript(Block *hatBlock)
11241124
{
11251125
Script *script = m_scripts[hatBlock].get();
11261126
addHatToMap(m_whenGreaterThanHats, script);
@@ -1434,7 +1434,7 @@ void Engine::setExtensions(const std::vector<std::string> &newExtensions)
14341434
}
14351435
}
14361436

1437-
const std::unordered_map<std::shared_ptr<Block>, std::shared_ptr<Script>> &Engine::scripts() const
1437+
const std::unordered_map<Block *, std::shared_ptr<Script>> &Engine::scripts() const
14381438
{
14391439
return m_scripts;
14401440
}
@@ -1866,9 +1866,9 @@ void Engine::compileMonitor(std::shared_ptr<Monitor> monitor)
18661866
MonitorChangeFunc changeFunc = resolveMonitorChangeFunc(ext, block->opcode());
18671867
monitor->setValueChangeFunction(changeFunc);
18681868

1869-
auto script = std::make_shared<Script>(target, block, this);
1869+
auto script = std::make_shared<Script>(target, block.get(), this);
18701870
monitor->setScript(script);
1871-
auto code = compiler.compile(block, Compiler::CodeType::Reporter);
1871+
auto code = compiler.compile(block.get(), Compiler::CodeType::Reporter);
18721872
script->setCode(code);
18731873
m_monitorCompilerContexts[monitor.get()] = ctx;
18741874

@@ -1975,7 +1975,7 @@ void Engine::addBroadcastPromise(Broadcast *broadcast, Thread *sender, bool wait
19751975
m_broadcastSenders[broadcast] = sender;
19761976
}
19771977

1978-
std::shared_ptr<Thread> Engine::pushThread(std::shared_ptr<Block> block, Target *target)
1978+
std::shared_ptr<Thread> Engine::pushThread(Block *block, Target *target)
19791979
{
19801980
// https://github.com/scratchfoundation/scratch-vm/blob/f1aa92fad79af17d9dd1c41eeeadca099339a9f1/src/engine/runtime.js#L1649-L1661
19811981
if (!block) {

0 commit comments

Comments
 (0)