Skip to content

Commit a415da4

Browse files
committed
ExecutionContext: Add promise API
1 parent 826935f commit a415da4

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

include/scratchcpp/dev/executioncontext.h

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

1111
class Target;
12+
class Promise;
1213
class ExecutionContextPrivate;
1314

1415
/*! \brief The ExecutionContext represents the execution context of a target (can be a clone) with variables, lists, etc. */
@@ -21,6 +22,9 @@ class LIBSCRATCHCPP_EXPORT ExecutionContext
2122

2223
Target *target() const;
2324

25+
std::shared_ptr<Promise> promise() const;
26+
void setPromise(std::shared_ptr<Promise> promise);
27+
2428
private:
2529
spimpl::unique_impl_ptr<ExecutionContextPrivate> impl;
2630
};

src/dev/engine/executioncontext.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,15 @@ Target *ExecutionContext::target() const
1717
{
1818
return impl->target;
1919
}
20+
21+
/*! Returns the script promise. */
22+
std::shared_ptr<Promise> ExecutionContext::promise() const
23+
{
24+
return impl->promise;
25+
}
26+
27+
/*! Sets the script promise (yields until the promise is resolved). */
28+
void ExecutionContext::setPromise(std::shared_ptr<Promise> promise)
29+
{
30+
impl->promise = promise;
31+
}

src/dev/engine/executioncontext_p.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,20 @@
22

33
#pragma once
44

5+
#include <memory>
6+
57
namespace libscratchcpp
68
{
79

810
class Target;
11+
class Promise;
912

1013
struct ExecutionContextPrivate
1114
{
1215
ExecutionContextPrivate(Target *target);
1316

1417
Target *target = nullptr;
18+
std::shared_ptr<Promise> promise;
1519
};
1620

1721
} // namespace libscratchcpp

test/dev/executioncontext/executioncontext_test.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <scratchcpp/dev/executioncontext.h>
22
#include <scratchcpp/target.h>
3+
#include <scratchcpp/dev/promise.h>
34

45
#include "../../common.h"
56

@@ -11,3 +12,17 @@ TEST(ExecutionContextTest, Constructor)
1112
ExecutionContext ctx(&target);
1213
ASSERT_EQ(ctx.target(), &target);
1314
}
15+
16+
TEST(ExecutionContextTest, Promise)
17+
{
18+
Target target;
19+
ExecutionContext ctx(&target);
20+
ASSERT_EQ(ctx.promise(), nullptr);
21+
22+
auto promise = std::make_shared<Promise>();
23+
ctx.setPromise(promise);
24+
ASSERT_EQ(ctx.promise(), promise);
25+
26+
ctx.setPromise(nullptr);
27+
ASSERT_EQ(ctx.promise(), nullptr);
28+
}

0 commit comments

Comments
 (0)