Skip to content

Commit a30bce3

Browse files
committed
Script: Add hat predicate code
1 parent 2266149 commit a30bce3

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

include/scratchcpp/script.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class LIBSCRATCHCPP_EXPORT Script
3131
ExecutableCode *code() const;
3232
void setCode(std::shared_ptr<ExecutableCode> code);
3333

34+
ExecutableCode *hatPredicateCode() const;
35+
void setHatPredicateCode(std::shared_ptr<ExecutableCode> code);
36+
3437
bool runHatPredicate(Target *target);
3538

3639
std::shared_ptr<Thread> start();

src/engine/script.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ void Script::setCode(std::shared_ptr<ExecutableCode> code)
4242
impl->code = code;
4343
}
4444

45+
/*! Returns the executable code of the hat predicate. */
46+
ExecutableCode *Script::hatPredicateCode() const
47+
{
48+
return impl->hatPredicateCode.get();
49+
}
50+
51+
/*! Sets the executable code of the hat predicate. */
52+
void Script::setHatPredicateCode(std::shared_ptr<ExecutableCode> code)
53+
{
54+
impl->hatPredicateCode = code;
55+
}
56+
4557
/*!
4658
* Runs the edge-activated hat predicate as the given target and returns the reported value.
4759
* \note If there isn't any predicate, nothing will happen and the returned value will be false.

src/engine/script_p.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct ScriptPrivate
2222
ScriptPrivate(const ScriptPrivate &) = delete;
2323

2424
std::shared_ptr<ExecutableCode> code;
25+
std::shared_ptr<ExecutableCode> hatPredicateCode;
2526

2627
Target *target = nullptr;
2728
std::shared_ptr<Block> topBlock;

test/script/script_test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ TEST_F(ScriptTest, Code)
4141
ASSERT_EQ(script.code(), code.get());
4242
}
4343

44+
TEST_F(ScriptTest, HatPredicateCode)
45+
{
46+
Script script(nullptr, nullptr, nullptr);
47+
ASSERT_EQ(script.hatPredicateCode(), nullptr);
48+
49+
auto code = std::make_shared<ExecutableCodeMock>();
50+
script.setHatPredicateCode(code);
51+
ASSERT_EQ(script.hatPredicateCode(), code.get());
52+
}
53+
4454
TEST_F(ScriptTest, Start)
4555
{
4656
Script script1(nullptr, nullptr, nullptr);

0 commit comments

Comments
 (0)