Skip to content

Commit ca66a7c

Browse files
committed
Add code property to Script
1 parent 005e479 commit ca66a7c

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed

include/scratchcpp/script.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ namespace libscratchcpp
1414
class Target;
1515
class Block;
1616
class IEngine;
17+
#ifdef USE_LLVM
18+
class ExecutableCode;
19+
#endif
1720
class Value;
1821
class Thread;
1922
class Variable;
@@ -34,6 +37,11 @@ class LIBSCRATCHCPP_EXPORT Script
3437
const std::vector<unsigned int> &bytecodeVector() const;
3538
void setBytecode(const std::vector<unsigned int> &code);
3639

40+
#ifdef USE_LLVM
41+
ExecutableCode *code() const;
42+
void setCode(std::shared_ptr<ExecutableCode> code);
43+
#endif
44+
3745
void setHatPredicateBytecode(const std::vector<unsigned int> &code);
3846
bool runHatPredicate(Target *target);
3947

src/engine/script.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ void Script::setBytecode(const std::vector<unsigned int> &code)
5050
impl->bytecode = impl->bytecodeVector.data();
5151
}
5252

53+
#ifdef USE_LLVM
54+
/*! Returns the executable code of the script. */
55+
ExecutableCode *Script::code() const
56+
{
57+
return impl->code.get();
58+
}
59+
60+
/*! Sets the executable code of the script. */
61+
void Script::setCode(std::shared_ptr<ExecutableCode> code)
62+
{
63+
impl->code = code;
64+
}
65+
#endif // USE_LLVM
66+
5367
/*! Sets the edge-activated hat predicate bytecode. */
5468
void Script::setHatPredicateBytecode(const std::vector<unsigned int> &code)
5569
{

src/engine/script_p.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ namespace libscratchcpp
1212
class Target;
1313
class Block;
1414
class IEngine;
15+
class ExecutableCode;
1516
class Variable;
1617
class List;
1718

@@ -24,6 +25,10 @@ struct ScriptPrivate
2425
std::vector<unsigned int> bytecodeVector;
2526
std::vector<unsigned int> hatPredicateBytecodeVector;
2627

28+
#ifdef USE_LLVM
29+
std::shared_ptr<ExecutableCode> code;
30+
#endif
31+
2732
Target *target = nullptr;
2833
std::shared_ptr<Block> topBlock;
2934
IEngine *engine = nullptr;

test/mocks/executablecodemock.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <scratchcpp/dev/executablecode.h>
4+
#include <gmock/gmock.h>
5+
6+
using namespace libscratchcpp;
7+
8+
class ExecutableCodeMock : public ExecutableCode
9+
{
10+
public:
11+
MOCK_METHOD(void, run, (ExecutionContext *), (override));
12+
MOCK_METHOD(void, kill, (ExecutionContext *), (override));
13+
MOCK_METHOD(void, reset, (ExecutionContext *), (override));
14+
15+
MOCK_METHOD(bool, isFinished, (ExecutionContext *), (const, override));
16+
17+
MOCK_METHOD(void, promise, (), (override));
18+
MOCK_METHOD(void, resolvePromise, (), (override));
19+
20+
MOCK_METHOD(std::shared_ptr<ExecutionContext>, createExecutionContext, (Target *), (const, override));
21+
};

test/script/script_test.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <scratchcpp/variable.h>
88
#include <scratchcpp/list.h>
99
#include <enginemock.h>
10+
#include <executablecodemock.h>
1011

1112
#include "../common.h"
1213

@@ -31,6 +32,17 @@ TEST_F(ScriptTest, Constructors)
3132
ASSERT_EQ(script.topBlock(), block);
3233
}
3334

35+
#ifdef USE_LLVM
36+
TEST_F(ScriptTest, Code)
37+
{
38+
Script script(nullptr, nullptr, nullptr);
39+
ASSERT_EQ(script.code(), nullptr);
40+
41+
auto code = std::make_shared<ExecutableCodeMock>();
42+
script.setCode(code);
43+
ASSERT_EQ(script.code(), code.get());
44+
}
45+
#else
3446
TEST_F(ScriptTest, Bytecode)
3547
{
3648
Script script(nullptr, nullptr, nullptr);
@@ -248,3 +260,4 @@ TEST_F(ScriptTest, Start)
248260
EXPECT_CALL(m_engine, deinitClone(clone));
249261
clone->deleteClone();
250262
}
263+
#endif

0 commit comments

Comments
 (0)