Skip to content

Commit 98fa65b

Browse files
committed
Thread: Use ExecutableCode when using LLVM
1 parent 43f3287 commit 98fa65b

File tree

3 files changed

+109
-13
lines changed

3 files changed

+109
-13
lines changed

src/engine/thread.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
#include <scratchcpp/thread.h>
44
#include <scratchcpp/virtualmachine.h>
5+
#ifdef USE_LLVM
6+
#include <scratchcpp/script.h>
7+
#include <scratchcpp/dev/executablecode.h>
8+
#endif
59

610
#include "thread_p.h"
711

@@ -12,11 +16,16 @@ Thread::Thread(Target *target, IEngine *engine, Script *script) :
1216
impl(spimpl::make_unique_impl<ThreadPrivate>(target, engine, script))
1317
{
1418
impl->vm = std::make_unique<VirtualMachine>(target, engine, script, this);
19+
#ifdef USE_LLVM
20+
impl->code = impl->script->code();
21+
impl->executionContext = impl->code->createExecutionContext(target);
22+
#endif
1523
}
1624

1725
/*! Returns the virtual machine of this thread. */
1826
VirtualMachine *Thread::vm() const
1927
{
28+
// TODO: Remove this method
2029
return impl->vm.get();
2130
}
2231

@@ -41,35 +50,59 @@ Script *Thread::script() const
4150
/*! Runs the script until it finishes or yields. */
4251
void Thread::run()
4352
{
53+
#ifdef USE_LLVM
54+
impl->code->run(impl->executionContext.get());
55+
#else
4456
impl->vm->run();
57+
#endif
4558
}
4659

4760
/*! Stops the script. */
4861
void Thread::kill()
4962
{
63+
#ifdef USE_LLVM
64+
impl->code->kill(impl->executionContext.get());
65+
#else
5066
impl->vm->kill();
67+
#endif
5168
}
5269

5370
/*! Resets the script to run from the start. */
5471
void Thread::reset()
5572
{
73+
#ifdef USE_LLVM
74+
impl->code->reset(impl->executionContext.get());
75+
#else
5676
impl->vm->reset();
77+
#endif
5778
}
5879

5980
/*! Returns true if the script is stopped or finished. */
6081
bool Thread::isFinished() const
6182
{
83+
#ifdef USE_LLVM
84+
return impl->code->isFinished(impl->executionContext.get());
85+
#else
6286
return impl->vm->atEnd();
87+
#endif
6388
}
6489

6590
/*! Pauses the script (when it's executed using run() again) until resolvePromise() is called. */
6691
void Thread::promise()
6792
{
93+
#ifdef USE_LLVM
94+
impl->code->promise();
95+
#else
6896
impl->vm->promise();
97+
#endif
6998
}
7099

71100
/*! Resolves the promise and resumes the script. */
72101
void Thread::resolvePromise()
73102
{
103+
#ifdef USE_LLVM
104+
impl->code->resolvePromise();
105+
#else
74106
impl->vm->resolvePromise();
107+
#endif
75108
}

src/engine/thread_p.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
namespace libscratchcpp
88
{
99

10+
#ifdef USE_LLVM
11+
class ExecutableCode;
12+
class ExecutionContext;
13+
#endif
14+
1015
struct ThreadPrivate
1116
{
1217
ThreadPrivate(Target *target, IEngine *engine, Script *script);
@@ -15,6 +20,10 @@ struct ThreadPrivate
1520
Target *target = nullptr;
1621
IEngine *engine = nullptr;
1722
Script *script = nullptr;
23+
#ifdef USE_LLVM
24+
ExecutableCode *code = nullptr;
25+
std::shared_ptr<ExecutionContext> executionContext;
26+
#endif
1827
};
1928

2029
} // namespace libscratchcpp

test/thread/thread_test.cpp

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,80 @@
11
#include <scratchcpp/thread.h>
22
#include <scratchcpp/script.h>
33
#include <scratchcpp/virtualmachine.h>
4+
#include <scratchcpp/dev/executioncontext.h>
45
#include <targetmock.h>
56
#include <enginemock.h>
7+
#include <executablecodemock.h>
68

79
#include "../common.h"
810

911
using namespace libscratchcpp;
1012

11-
TEST(ThreadTest, Constructor)
13+
using ::testing::Return;
14+
15+
class ThreadTest : public testing::Test
1216
{
13-
TargetMock target;
14-
EngineMock engine;
15-
Script script(nullptr, nullptr, nullptr);
16-
Thread thread(&target, &engine, &script);
17-
ASSERT_EQ(thread.target(), &target);
18-
ASSERT_EQ(thread.engine(), &engine);
19-
ASSERT_EQ(thread.script(), &script);
20-
ASSERT_TRUE(thread.vm());
21-
ASSERT_EQ(thread.vm()->target(), &target);
22-
ASSERT_EQ(thread.vm()->engine(), &engine);
23-
ASSERT_EQ(thread.vm()->script(), &script);
17+
public:
18+
void SetUp() override
19+
{
20+
m_script = std::make_unique<Script>(nullptr, nullptr, nullptr);
21+
#ifdef USE_LLVM
22+
m_code = std::make_shared<ExecutableCodeMock>();
23+
m_script->setCode(m_code);
24+
m_ctx = std::make_shared<ExecutionContext>(&m_target);
25+
EXPECT_CALL(*m_code, createExecutionContext(&m_target)).WillOnce(Return(m_ctx));
26+
#endif
27+
m_thread = std::make_unique<Thread>(&m_target, &m_engine, m_script.get());
28+
}
29+
30+
std::unique_ptr<Thread> m_thread;
31+
TargetMock m_target;
32+
EngineMock m_engine;
33+
std::unique_ptr<Script> m_script;
34+
#ifdef USE_LLVM
35+
std::shared_ptr<ExecutableCodeMock> m_code;
36+
std::shared_ptr<ExecutionContext> m_ctx;
37+
#endif
38+
};
39+
40+
TEST_F(ThreadTest, Constructor)
41+
{
42+
ASSERT_EQ(m_thread->target(), &m_target);
43+
ASSERT_EQ(m_thread->engine(), &m_engine);
44+
ASSERT_EQ(m_thread->script(), m_script.get());
45+
ASSERT_TRUE(m_thread->vm());
46+
ASSERT_EQ(m_thread->vm()->target(), &m_target);
47+
ASSERT_EQ(m_thread->vm()->engine(), &m_engine);
48+
ASSERT_EQ(m_thread->vm()->script(), m_script.get());
49+
}
50+
51+
#ifdef USE_LLVM
52+
TEST_F(ThreadTest, Run)
53+
{
54+
EXPECT_CALL(*m_code, run(m_ctx.get()));
55+
m_thread->run();
56+
}
57+
58+
TEST_F(ThreadTest, Kill)
59+
{
60+
EXPECT_CALL(*m_code, kill(m_ctx.get()));
61+
m_thread->kill();
62+
}
63+
64+
TEST_F(ThreadTest, Reset)
65+
{
66+
EXPECT_CALL(*m_code, reset(m_ctx.get()));
67+
m_thread->reset();
68+
}
69+
70+
TEST_F(ThreadTest, IsFinished)
71+
{
72+
EXPECT_CALL(*m_code, isFinished(m_ctx.get())).WillOnce(Return(false));
73+
ASSERT_FALSE(m_thread->isFinished());
74+
75+
EXPECT_CALL(*m_code, isFinished(m_ctx.get())).WillOnce(Return(true));
76+
ASSERT_TRUE(m_thread->isFinished());
2477
}
2578

26-
// NOTE: Since VirtualMachine is going to be removed, tests for the other methods will be added later
79+
// TODO: Test promise() and resolvePromise()
80+
#endif // USE_LLVM

0 commit comments

Comments
 (0)