|
| 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 | +} |
0 commit comments