Skip to content

Commit 4fb010b

Browse files
committed
Use Thread instead of Target in ExecutionContext constructor
1 parent 4f3d6dc commit 4fb010b

16 files changed

+266
-126
lines changed

include/scratchcpp/dev/executablecode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace libscratchcpp
1010
{
1111

1212
class ExecutionContext;
13-
class Target;
13+
class Thread;
1414

1515
/*! \brief The ExecutableCode class represents the code of a compiled Scratch script. */
1616
class LIBSCRATCHCPP_EXPORT ExecutableCode
@@ -31,7 +31,7 @@ class LIBSCRATCHCPP_EXPORT ExecutableCode
3131
virtual bool isFinished(ExecutionContext *context) const = 0;
3232

3333
/*! Creates an execution context for the given Target. */
34-
virtual std::shared_ptr<ExecutionContext> createExecutionContext(Target *target) const = 0;
34+
virtual std::shared_ptr<ExecutionContext> createExecutionContext(Thread *thread) const = 0;
3535
};
3636

3737
} // namespace libscratchcpp

include/scratchcpp/dev/executioncontext.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace libscratchcpp
99
{
1010

11+
class Thread;
1112
class Target;
1213
class Promise;
1314
class ExecutionContextPrivate;
@@ -16,11 +17,11 @@ class ExecutionContextPrivate;
1617
class LIBSCRATCHCPP_EXPORT ExecutionContext
1718
{
1819
public:
19-
ExecutionContext(Target *target);
20+
ExecutionContext(Thread *thread);
2021
ExecutionContext(const ExecutionContext &) = delete;
2122
virtual ~ExecutionContext() { }
2223

23-
Target *target() const;
24+
Thread *thread() const;
2425

2526
std::shared_ptr<Promise> promise() const;
2627
void setPromise(std::shared_ptr<Promise> promise);

src/dev/engine/executioncontext.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
// SPDX-License-Identifier: Apache-2.0
22

33
#include <scratchcpp/dev/executioncontext.h>
4+
#include <scratchcpp/thread.h>
45

56
#include "executioncontext_p.h"
67

78
using namespace libscratchcpp;
89

910
/*! Constructs ExecutionContext. */
10-
ExecutionContext::ExecutionContext(Target *target) :
11-
impl(spimpl::make_unique_impl<ExecutionContextPrivate>(target))
11+
ExecutionContext::ExecutionContext(Thread *thread) :
12+
impl(spimpl::make_unique_impl<ExecutionContextPrivate>(thread))
1213
{
1314
}
1415

15-
/*! Returns the Target of this context. */
16-
Target *ExecutionContext::target() const
16+
/*! Returns the thread of this context. */
17+
Thread *ExecutionContext::thread() const
1718
{
18-
return impl->target;
19+
return impl->thread;
1920
}
2021

2122
/*! Returns the script promise. */

src/dev/engine/executioncontext_p.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
using namespace libscratchcpp;
66

7-
ExecutionContextPrivate::ExecutionContextPrivate(Target *target) :
8-
target(target)
7+
ExecutionContextPrivate::ExecutionContextPrivate(Thread *thread) :
8+
thread(thread)
99
{
1010
}

src/dev/engine/executioncontext_p.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
namespace libscratchcpp
88
{
99

10+
class Thread;
1011
class Target;
1112
class Promise;
1213

1314
struct ExecutionContextPrivate
1415
{
15-
ExecutionContextPrivate(Target *target);
16+
ExecutionContextPrivate(Thread *thread);
1617

17-
Target *target = nullptr;
18+
Thread *thread = nullptr;
1819
std::shared_ptr<Promise> promise;
1920
};
2021

src/dev/engine/internal/llvm/llvmexecutablecode.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <scratchcpp/stage.h>
66
#include <scratchcpp/iengine.h>
77
#include <scratchcpp/dev/promise.h>
8+
#include <scratchcpp/thread.h>
89
#include <llvm/Support/Error.h>
910
#include <iostream>
1011

@@ -64,7 +65,7 @@ void LLVMExecutableCode::run(ExecutionContext *context)
6465

6566
ctx->setFinished(done);
6667
} else {
67-
Target *target = ctx->target();
68+
Target *target = ctx->thread()->target();
6869
void *handle = m_mainFunction(context, target, target->variableData(), target->listData());
6970

7071
if (!handle)
@@ -95,9 +96,9 @@ bool LLVMExecutableCode::isFinished(ExecutionContext *context) const
9596
return getContext(context)->finished();
9697
}
9798

98-
std::shared_ptr<ExecutionContext> LLVMExecutableCode::createExecutionContext(Target *target) const
99+
std::shared_ptr<ExecutionContext> LLVMExecutableCode::createExecutionContext(Thread *thread) const
99100
{
100-
return std::make_shared<LLVMExecutionContext>(target);
101+
return std::make_shared<LLVMExecutionContext>(thread);
101102
}
102103

103104
uint64_t LLVMExecutableCode::lookupFunction(const std::string &name)

src/dev/engine/internal/llvm/llvmexecutablecode.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class LLVMExecutableCode : public ExecutableCode
2525

2626
bool isFinished(ExecutionContext *context) const override;
2727

28-
std::shared_ptr<ExecutionContext> createExecutionContext(Target *target) const override;
28+
std::shared_ptr<ExecutionContext> createExecutionContext(Thread *thread) const override;
2929

3030
private:
3131
uint64_t lookupFunction(const std::string &name);

src/dev/engine/internal/llvm/llvmexecutioncontext.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
using namespace libscratchcpp;
66

7-
LLVMExecutionContext::LLVMExecutionContext(Target *target) :
8-
ExecutionContext(target)
7+
LLVMExecutionContext::LLVMExecutionContext(Thread *thread) :
8+
ExecutionContext(thread)
99
{
1010
}
1111

src/dev/engine/internal/llvm/llvmexecutioncontext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace libscratchcpp
1010
class LLVMExecutionContext : public ExecutionContext
1111
{
1212
public:
13-
LLVMExecutionContext(Target *target);
13+
LLVMExecutionContext(Thread *thread);
1414

1515
void *coroutineHandle() const;
1616
void setCoroutineHandle(void *newCoroutineHandle);

src/engine/thread.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ Thread::Thread(Target *target, IEngine *engine, Script *script) :
1818
{
1919
impl->vm = std::make_unique<VirtualMachine>(target, engine, script, this);
2020
#ifdef USE_LLVM
21-
impl->code = impl->script->code();
22-
impl->executionContext = impl->code->createExecutionContext(target);
21+
if (impl->script) {
22+
impl->code = impl->script->code();
23+
24+
if (impl->code)
25+
impl->executionContext = impl->code->createExecutionContext(this);
26+
}
2327
#endif
2428
}
2529

0 commit comments

Comments
 (0)