forked from GordonSmith/component-model-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.hpp
More file actions
173 lines (143 loc) · 4.94 KB
/
context.hpp
File metadata and controls
173 lines (143 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#ifndef CMCPP_CONTEXT_HPP
#define CMCPP_CONTEXT_HPP
#include "traits.hpp"
#include <future>
#if __has_include(<span>)
#include <span>
#else
#include <string>
#include <sstream>
#endif
namespace cmcpp
{
using HostTrap = std::function<void(const char *msg) noexcept(false)>;
using GuestRealloc = std::function<int(int ptr, int old_size, int align, int new_size)>;
using GuestMemory = std::span<uint8_t>;
using GuestPostReturn = std::function<void()>;
using GuestCallback = std::function<void()>;
using HostUnicodeConversion = std::function<std::pair<void *, size_t>(void *dest, uint32_t dest_byte_len, const void *src, uint32_t src_byte_len, Encoding from_encoding, Encoding to_encoding)>;
// Canonical ABI Options ---
class LiftOptions
{
public:
Encoding string_encoding = Encoding::Utf8;
GuestMemory memory;
LiftOptions(const Encoding &string_encoding = Encoding::Utf8, const GuestMemory &memory = {})
: string_encoding(string_encoding), memory(memory) {}
bool operator==(const LiftOptions &rhs) const
{
return string_encoding == rhs.string_encoding && (memory.data() == rhs.memory.data());
}
};
class LiftLowerOptions : public LiftOptions
{
public:
GuestRealloc realloc;
LiftLowerOptions(Encoding string_encoding = Encoding::Utf8, GuestMemory memory = {}, GuestRealloc realloc = {})
: LiftOptions(string_encoding, memory), realloc(realloc) {}
};
struct CanonicalOptions : LiftLowerOptions
{
std::optional<GuestPostReturn> post_return;
bool sync = true;
std::optional<GuestCallback> callback;
bool allways_task_return = false;
};
// Runtime State ---
struct ResourceHandle
{
};
struct Waitable
{
};
struct WaitableSet
{
};
struct ErrorContext
{
};
struct ComponentInstance
{
std::vector<ResourceHandle> resources;
std::vector<Waitable> waitables;
std::vector<WaitableSet> waitable_sets;
std::vector<ErrorContext> error_contexts;
bool may_leave = true;
bool backpressure = false;
bool calling_sync_export = false;
bool calling_sync_import = false;
// std::vector<std::tuple<Task<R, Args...>, Future>> pending_tasks;
bool starting_pending_tasks = false;
};
class ContextLocalStorage
{
public:
static constexpr int LENGTH = 2;
int array[LENGTH] = {0, 0};
ContextLocalStorage() = default;
void set(int i, int v)
{
array[i] = v;
}
int get(int i)
{
return array[i];
}
};
template <Field R, Field... Args>
class Task
{
public:
CanonicalOptions opts;
ComponentInstance inst;
func_t<R(Args...)> ft;
std::optional<Task> supertask;
std::optional<std::function<void()>> on_return;
std::function<std::future<void>(std::future<void>)> on_block;
int num_borrows = 0;
ContextLocalStorage context();
Task(CanonicalOptions &opts, ComponentInstance &inst, func_t<R(Args...)> &ft, std::optional<Task> &supertask = std::nullopt, std::optional<std::function<void()>> &on_return = std::nullopt, std::function<std::future<void>(std::future<void>)> &on_block = std::nullopt)
: opts(opts), inst(inst), ft(ft), supertask(supertask), on_return(on_return), on_block(on_block) {}
};
struct Subtask : Waitable
{
};
struct Future
{
};
// Lifting and Lowering Context ---
// template <Field R, Field... Args>
class LiftLowerContext
{
public:
HostTrap trap;
HostUnicodeConversion convert;
LiftLowerOptions opts;
// ComponentInstance inst;
// std::optional<std::variant<Task<R, Args...>, Subtask>> borrow_scope;
LiftLowerContext(const HostTrap &trap, const HostUnicodeConversion &convert, const LiftLowerOptions &opts)
: trap(trap), convert(convert), opts(opts) {}
};
// ----------------------------
struct InstanceContext
{
HostTrap trap;
HostUnicodeConversion convert;
GuestRealloc realloc;
std::unique_ptr<LiftLowerContext> createLiftLowerContext(const GuestMemory &memory, const Encoding &string_encoding = Encoding::Utf8, const std::optional<GuestPostReturn> &post_return = std::nullopt)
{
LiftLowerOptions opts(string_encoding, memory, realloc);
auto retVal = std::make_unique<LiftLowerContext>(trap, convert, opts);
return retVal;
}
};
inline std::unique_ptr<InstanceContext> createInstanceContext(const HostTrap &trap, HostUnicodeConversion convert, const GuestRealloc &realloc)
{
auto retVal = std::make_unique<InstanceContext>();
retVal->trap = trap;
retVal->convert = convert;
retVal->realloc = realloc;
return retVal;
}
}
#endif