Skip to content

Commit a7eefef

Browse files
committed
Add Thread class
1 parent 9481957 commit a7eefef

File tree

14 files changed

+213
-8
lines changed

14 files changed

+213
-8
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ target_sources(scratchcpp
3232
include/scratchcpp/iengine.h
3333
include/scratchcpp/iextension.h
3434
include/scratchcpp/iblocksection.h
35+
include/scratchcpp/thread.h
3536
include/scratchcpp/asset.h
3637
include/scratchcpp/costume.h
3738
include/scratchcpp/sound.h

include/scratchcpp/thread.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include "global.h"
6+
#include "spimpl.h"
7+
8+
namespace libscratchcpp
9+
{
10+
11+
class VirtualMachine;
12+
class Target;
13+
class IEngine;
14+
class Script;
15+
class ThreadPrivate;
16+
17+
/*! \brief The Thread class represents a running Scratch script. */
18+
class LIBSCRATCHCPP_EXPORT Thread
19+
{
20+
public:
21+
Thread(Target *target, IEngine *engine, Script *script);
22+
Thread(const Thread &) = delete;
23+
24+
VirtualMachine *vm() const;
25+
Target *target() const;
26+
IEngine *engine() const;
27+
Script *script() const;
28+
29+
void run();
30+
void kill();
31+
void reset();
32+
33+
bool isFinished() const;
34+
35+
void promise();
36+
void resolvePromise();
37+
38+
private:
39+
spimpl::unique_impl_ptr<ThreadPrivate> impl;
40+
};
41+
42+
} // namespace libscratchcpp

include/scratchcpp/virtualmachine.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,15 @@ enum Opcode
8888
class Target;
8989
class IEngine;
9090
class Script;
91+
class Thread;
9192
class List;
9293

9394
/*! \brief The VirtualMachine class is a virtual machine for compiled Scratch scripts. */
9495
class LIBSCRATCHCPP_EXPORT VirtualMachine
9596
{
9697
public:
9798
VirtualMachine();
98-
VirtualMachine(Target *target, IEngine *engine, Script *script);
99+
VirtualMachine(Target *target, IEngine *engine, Script *script, Thread *thread = nullptr);
99100
VirtualMachine(const VirtualMachine &) = delete;
100101

101102
void setProcedures(unsigned int **procedures);
@@ -121,6 +122,7 @@ class LIBSCRATCHCPP_EXPORT VirtualMachine
121122
Target *target() const;
122123
IEngine *engine() const;
123124
Script *script() const;
125+
Thread *thread() const;
124126

125127
const Value *getInput(unsigned int index, unsigned int argCount) const;
126128

src/engine/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ target_sources(scratchcpp
99
script.cpp
1010
script_p.cpp
1111
script_p.h
12+
thread.cpp
13+
thread_p.cpp
14+
thread_p.h
1215
internal/engine.cpp
1316
internal/engine.h
1417
internal/clock.cpp

src/engine/thread.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include <scratchcpp/thread.h>
4+
#include <scratchcpp/virtualmachine.h>
5+
6+
#include "thread_p.h"
7+
8+
using namespace libscratchcpp;
9+
10+
/*! Constructs Thread. */
11+
Thread::Thread(Target *target, IEngine *engine, Script *script) :
12+
impl(spimpl::make_unique_impl<ThreadPrivate>(target, engine, script))
13+
{
14+
impl->vm = std::make_unique<VirtualMachine>(target, engine, script, this);
15+
}
16+
17+
/*! Returns the virtual machine of this thread. */
18+
VirtualMachine *Thread::vm() const
19+
{
20+
return impl->vm.get();
21+
}
22+
23+
/*! Returns the Target of the script. */
24+
Target *Thread::target() const
25+
{
26+
return impl->target;
27+
}
28+
29+
/*! Returns the engine of the project. */
30+
IEngine *Thread::engine() const
31+
{
32+
return impl->engine;
33+
}
34+
35+
/*! Returns the Script. */
36+
Script *Thread::script() const
37+
{
38+
return impl->script;
39+
}
40+
41+
/*! Runs the script until it finishes or yields. */
42+
void Thread::run()
43+
{
44+
impl->vm->run();
45+
}
46+
47+
/*! Stops the script. */
48+
void Thread::kill()
49+
{
50+
impl->vm->kill();
51+
}
52+
53+
/*! Resets the script to run from the start. */
54+
void Thread::reset()
55+
{
56+
impl->vm->reset();
57+
}
58+
59+
/*! Returns true if the script is stopped or finished. */
60+
bool Thread::isFinished() const
61+
{
62+
return impl->vm->atEnd();
63+
}
64+
65+
/*! Pauses the script (when it's executed using run() again) until resolvePromise() is called. */
66+
void Thread::promise()
67+
{
68+
impl->vm->promise();
69+
}
70+
71+
/*! Resolves the promise and resumes the script. */
72+
void Thread::resolvePromise()
73+
{
74+
impl->vm->resolvePromise();
75+
}

src/engine/thread_p.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#include "thread_p.h"
4+
5+
using namespace libscratchcpp;
6+
7+
ThreadPrivate::ThreadPrivate(Target *target, IEngine *engine, Script *script) :
8+
target(target),
9+
engine(engine),
10+
script(script)
11+
{
12+
}

src/engine/thread_p.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
#pragma once
4+
5+
#include <scratchcpp/virtualmachine.h>
6+
7+
namespace libscratchcpp
8+
{
9+
10+
struct ThreadPrivate
11+
{
12+
ThreadPrivate(Target *target, IEngine *engine, Script *script);
13+
14+
std::unique_ptr<VirtualMachine> vm;
15+
Target *target = nullptr;
16+
IEngine *engine = nullptr;
17+
Script *script = nullptr;
18+
};
19+
20+
} // namespace libscratchcpp

src/engine/virtualmachine.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ using namespace vm;
1010

1111
/*! Constructs VirtualMachine. */
1212
VirtualMachine::VirtualMachine() :
13-
VirtualMachine(nullptr, nullptr, nullptr)
13+
VirtualMachine(nullptr, nullptr, nullptr, nullptr)
1414
{
1515
}
1616

1717
/*! \copydoc VirtualMachine() */
18-
VirtualMachine::VirtualMachine(Target *target, IEngine *engine, Script *script) :
19-
impl(spimpl::make_unique_impl<VirtualMachinePrivate>(this, target, engine, script))
18+
VirtualMachine::VirtualMachine(Target *target, IEngine *engine, Script *script, Thread *thread) :
19+
impl(spimpl::make_unique_impl<VirtualMachinePrivate>(this, target, engine, script, thread))
2020
{
2121
}
2222

@@ -137,6 +137,12 @@ Script *VirtualMachine::script() const
137137
return impl->script;
138138
}
139139

140+
/*! Returns the Thread this VM belongs to. */
141+
Thread *VirtualMachine::thread() const
142+
{
143+
return impl->thread;
144+
}
145+
140146
/*! Returns the register at the given index with the given argument (register) count. */
141147
const Value *VirtualMachine::getInput(unsigned int index, unsigned int argCount) const
142148
{

src/engine/virtualmachine_p.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,12 @@ const unsigned int VirtualMachinePrivate::instruction_arg_count[] = {
110110
0 // OP_WARP
111111
};
112112

113-
VirtualMachinePrivate::VirtualMachinePrivate(VirtualMachine *vm, Target *target, IEngine *engine, Script *script) :
113+
VirtualMachinePrivate::VirtualMachinePrivate(VirtualMachine *vm, Target *target, IEngine *engine, Script *script, Thread *thread) :
114114
vm(vm),
115115
target(target),
116116
engine(engine),
117-
script(script)
117+
script(script),
118+
thread(thread)
118119
{
119120
regsVector.reserve(1024);
120121
for (int i = 0; i < 1024; i++)

src/engine/virtualmachine_p.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ class VirtualMachine;
1313
class Target;
1414
class IEngine;
1515
class Script;
16+
class Thread;
1617
class Value;
1718
class List;
1819
class IRandomGenerator;
1920

2021
struct VirtualMachinePrivate
2122
{
22-
VirtualMachinePrivate(VirtualMachine *vm, Target *target, IEngine *engine, Script *script);
23+
VirtualMachinePrivate(VirtualMachine *vm, Target *target, IEngine *engine, Script *script, Thread *thread);
2324
VirtualMachinePrivate(const VirtualMachinePrivate &) = delete;
2425
~VirtualMachinePrivate();
2526

@@ -41,6 +42,7 @@ struct VirtualMachinePrivate
4142
Target *target = nullptr;
4243
IEngine *engine = nullptr;
4344
Script *script = nullptr;
45+
Thread *thread = nullptr;
4446
unsigned int *pos = nullptr;
4547
unsigned int *checkpoint = nullptr;
4648
bool running = false;

0 commit comments

Comments
 (0)