Skip to content

Commit f9ee2fc

Browse files
committed
Add target parameter to Script::runHatPredicate()
1 parent e922390 commit f9ee2fc

File tree

5 files changed

+30
-40
lines changed

5 files changed

+30
-40
lines changed

include/scratchcpp/script.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class LIBSCRATCHCPP_EXPORT Script
3535
void setBytecode(const std::vector<unsigned int> &code);
3636

3737
void setHatPredicateBytecode(const std::vector<unsigned int> &code);
38-
bool runHatPredicate();
38+
bool runHatPredicate(Target *target);
3939

4040
void setProcedures(const std::vector<unsigned int *> &procedures);
4141
void setConstValues(const std::vector<Value> &values);

src/engine/internal/engine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ void Engine::step()
531531
oldValue = fieldIt->second;
532532
}
533533

534-
bool newValue = thread->script()->runHatPredicate();
534+
bool newValue = thread->script()->runHatPredicate(hatBlock->target());
535535
bool edgeWasActivated = !oldValue && newValue; // changed from false true
536536
m_edgeActivatedHatValues[hatType][fieldValueId] = newValue;
537537

src/engine/script.cpp

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,26 @@ void Script::setBytecode(const std::vector<unsigned int> &code)
5353
void Script::setHatPredicateBytecode(const std::vector<unsigned int> &code)
5454
{
5555
impl->hatPredicateBytecodeVector = code;
56-
57-
if (impl->engine && !code.empty()) {
58-
impl->hatPredicateVm = std::make_shared<VirtualMachine>(impl->engine->stage(), impl->engine, this);
59-
impl->hatPredicateVm->setBytecode(impl->hatPredicateBytecodeVector.data());
60-
impl->hatPredicateVm->setConstValues(impl->constValues);
61-
}
6256
}
6357

6458
/*!
65-
* Runs the edge-activated hat predicate and returns the reported value.
59+
* Runs the edge-activated hat predicate as the given target and returns the reported value.
6660
* \note If there isn't any predicate, nothing will happen and the returned value will be false.
6761
*/
68-
bool Script::runHatPredicate()
62+
bool Script::runHatPredicate(Target *target)
6963
{
70-
if (impl->hatPredicateVm && impl->hatPredicateVm->bytecode()) {
71-
impl->hatPredicateVm->reset();
72-
impl->hatPredicateVm->setFunctions(getFunctions());
73-
impl->hatPredicateVm->run();
74-
assert(impl->hatPredicateVm->registerCount() == 1);
75-
76-
if (impl->hatPredicateVm->registerCount() == 1)
77-
return impl->hatPredicateVm->getInput(0, 1)->toBool();
78-
}
64+
if (!target || !impl->engine || impl->hatPredicateBytecodeVector.empty())
65+
return false;
66+
67+
auto vm = std::make_shared<VirtualMachine>(target, impl->engine, this);
68+
vm->setBytecode(impl->hatPredicateBytecodeVector.data());
69+
vm->setConstValues(impl->constValues);
70+
vm->setFunctions(getFunctions());
71+
vm->run();
72+
assert(vm->registerCount() == 1);
73+
74+
if (vm->registerCount() == 1)
75+
return vm->getInput(0, 1)->toBool();
7976

8077
return false;
8178
}
@@ -161,9 +158,6 @@ void Script::setConstValues(const std::vector<Value> &values)
161158
{
162159
impl->constValuesVector = values;
163160
impl->constValues = impl->constValuesVector.data();
164-
165-
if (impl->hatPredicateVm)
166-
impl->hatPredicateVm->setConstValues(impl->constValues);
167161
}
168162

169163
/*! Sets the list of variables. */

src/engine/script_p.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ struct ScriptPrivate
2222

2323
unsigned int *bytecode = nullptr;
2424
std::vector<unsigned int> bytecodeVector;
25-
2625
std::vector<unsigned int> hatPredicateBytecodeVector;
27-
std::shared_ptr<VirtualMachine> hatPredicateVm;
2826

2927
Target *target = nullptr;
3028
std::shared_ptr<Block> topBlock;

test/script/script_test.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,20 @@ TEST_F(ScriptTest, Bytecode)
4242
ASSERT_EQ(script.bytecodeVector(), std::vector<unsigned int>({ vm::OP_START, vm::OP_HALT }));
4343
}
4444

45-
static Target *stageTest = nullptr;
45+
static Target *targetTest = nullptr;
4646
static IEngine *engineTest = nullptr;
4747
static Script *scriptTest = nullptr;
4848

4949
TEST_F(ScriptTest, HatPredicate)
5050
{
5151
Script script(&m_target, nullptr, &m_engine);
52-
ASSERT_FALSE(script.runHatPredicate());
53-
54-
Stage stage;
55-
EXPECT_CALL(m_engine, stage()).Times(3).WillRepeatedly(Return(&stage));
52+
Target target;
53+
ASSERT_FALSE(script.runHatPredicate(&target));
5654

5755
BlockFunc f1 = [](VirtualMachine *vm) -> unsigned int {
5856
vm->addReturnValue(true);
5957
vm->stop(false, false, false);
60-
stageTest = vm->target();
58+
targetTest = vm->target();
6159
engineTest = vm->engine();
6260
scriptTest = vm->script();
6361
return 0;
@@ -68,7 +66,7 @@ TEST_F(ScriptTest, HatPredicate)
6866
BlockFunc f3 = [](VirtualMachine *vm) -> unsigned int {
6967
vm->addReturnValue(false);
7068
vm->stop(false, false, false);
71-
stageTest = vm->target();
69+
targetTest = vm->target();
7270
engineTest = vm->engine();
7371
scriptTest = vm->script();
7472
return 0;
@@ -78,38 +76,38 @@ TEST_F(ScriptTest, HatPredicate)
7876
std::vector<BlockFunc> functions2 = { f1, f2, f3 };
7977

8078
EXPECT_CALL(m_engine, blockFunctions()).WillOnce(ReturnRef(functions1));
81-
stageTest = nullptr;
79+
targetTest = nullptr;
8280
engineTest = nullptr;
8381
scriptTest = nullptr;
8482
script.setConstValues({ "test" });
8583
script.setHatPredicateBytecode({ vm::OP_START, vm::OP_CONST, 0, vm::OP_PRINT, vm::OP_EXEC, 0, vm::OP_HALT });
8684
testing::internal::CaptureStdout();
87-
ASSERT_TRUE(script.runHatPredicate());
85+
ASSERT_TRUE(script.runHatPredicate(&target));
8886
ASSERT_EQ(testing::internal::GetCapturedStdout(), "test\n");
89-
ASSERT_EQ(stageTest, &stage);
87+
ASSERT_EQ(targetTest, &target);
9088
ASSERT_EQ(engineTest, &m_engine);
9189
ASSERT_EQ(scriptTest, &script);
9290

9391
EXPECT_CALL(m_engine, blockFunctions()).WillOnce(ReturnRef(functions2));
94-
stageTest = nullptr;
92+
targetTest = nullptr;
9593
engineTest = nullptr;
9694
scriptTest = nullptr;
9795
script.setHatPredicateBytecode({ vm::OP_START, vm::OP_CONST, 0, vm::OP_PRINT, vm::OP_EXEC, 1, vm::OP_HALT });
9896
script.setConstValues({ 5 });
9997
testing::internal::CaptureStdout();
100-
ASSERT_TRUE(script.runHatPredicate());
98+
ASSERT_TRUE(script.runHatPredicate(&m_target));
10199
ASSERT_EQ(testing::internal::GetCapturedStdout(), "5\n");
102-
ASSERT_EQ(stageTest, &stage);
100+
ASSERT_EQ(targetTest, &m_target);
103101
ASSERT_EQ(engineTest, &m_engine);
104102
ASSERT_EQ(scriptTest, &script);
105103

106104
EXPECT_CALL(m_engine, blockFunctions()).WillOnce(ReturnRef(functions2));
107-
stageTest = nullptr;
105+
targetTest = nullptr;
108106
engineTest = nullptr;
109107
scriptTest = nullptr;
110108
script.setHatPredicateBytecode({ vm::OP_START, vm::OP_EXEC, 2, vm::OP_HALT });
111-
ASSERT_FALSE(script.runHatPredicate());
112-
ASSERT_EQ(stageTest, &stage);
109+
ASSERT_FALSE(script.runHatPredicate(&target));
110+
ASSERT_EQ(targetTest, &target);
113111
ASSERT_EQ(engineTest, &m_engine);
114112
ASSERT_EQ(scriptTest, &script);
115113
}

0 commit comments

Comments
 (0)