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
#ifndef WASMTIME_HELPERS_HH
#define WASMTIME_HELPERS_HH
#include <memory>
#define WASMTIME_OWN_WRAPPER(name, capi_type) \
public: \
/** \
* \brief Non-owning reference to an instance of this type. \
*/ \
using Raw = capi_type##_t; \
\
private: \
struct deleter { \
void operator()(Raw *p) const { capi_type##_delete(p); } \
}; \
\
std::unique_ptr<Raw, deleter> ptr; \
\
public: \
/** \
* \brief Takes ownership of `raw` and wraps it with this class. \
*/ \
explicit name(Raw *raw) : ptr(raw) {} \
\
~name() = default; \
\
/** \
* \brief Moves type information from another type into this one. \
*/ \
name(name &&other) = default; \
\
/** \
* \brief Moves type information from another type into this one. \
*/ \
name &operator=(name &&other) = default; \
\
/** \
* \brief Returns the underlying C API pointer. \
*/ \
const Raw *capi() const { return ptr.get(); } \
\
/** \
* \brief Returns the underlying C API pointer. \
*/ \
Raw *capi() { return ptr.get(); } \
\
/** \
* \brief Releases the underlying C API pointer. \
*/ \
Raw *capi_release() { return ptr.release(); } \
\
/** \
* Converts the raw C API representation to this class without taking \
* ownership. \
*/ \
static const name *from_capi(Raw *const *capi) { \
static_assert(sizeof(name) == sizeof(void *)); \
return reinterpret_cast<const name *>(capi); \
}
#define WASMTIME_CLONE_WRAPPER(name, capi_type) \
WASMTIME_OWN_WRAPPER(name, capi_type) \
\
public: \
/** \
* \brief Copies another type into this one. \
*/ \
name(const name &other) : ptr(capi_type##_clone(other.ptr.get())) {} \
\
/** \
* \brief Copies another type into this one. \
*/ \
name &operator=(const name &other) { \
ptr.reset(capi_type##_clone(other.ptr.get())); \
return *this; \
}
#define WASMTIME_CLONE_EQUAL_WRAPPER(name, capi_type) \
WASMTIME_CLONE_WRAPPER(name, capi_type) \
\
/** \
* \brief Compares two types for equality. \
*/ \
bool operator==(const name &other) const { \
return capi_type##_equal(ptr.get(), other.ptr.get()); \
} \
\
/** \
* \brief Compares two types for inequality. \
*/ \
bool operator!=(const name &other) const { return !(*this == other); }
#define WASMTIME_REF_WRAPPER(name, capi_type) \
public: \
/** \
* \brief Non-owning reference to an instance of this type. \
*/ \
using Raw = capi_type##_t; \
\
private: \
Raw raw; \
\
public: \
/** \
* \brief Takes ownership of `val` and wraps it with this class. \
*/ \
explicit name(Raw raw) : raw(raw) {} \
name(const name &other) { capi_type##_clone(&other.raw, &raw); } \
\
name &operator=(const name &other) { \
capi_type##_unroot(&raw); \
capi_type##_clone(&other.raw, &raw); \
return *this; \
} \
name(name &&other) { \
raw = other.raw; \
capi_type##_set_null(&other.raw); \
} \
name &operator=(name &&other) { \
capi_type##_unroot(&raw); \
raw = other.raw; \
capi_type##_set_null(&other.raw); \
return *this; \
} \
\
~name() { capi_type##_unroot(&raw); } \
\
/** \
* \brief Returns the underlying C API pointer. \
*/ \
const Raw *capi() const { return &raw; } \
\
/** \
* \brief Returns the underlying C API pointer. \
*/ \
Raw *capi() { return &raw; }
#define WASMTIME_TOP_REF_WRAPPER(name, capi_type) \
WASMTIME_REF_WRAPPER(name, capi_type) \
\
/** \
* \brief Consumes ownership of the underlying `wasmtime_externref_t` \
* and returns the result of `wasmtime_externref_to_raw`. \
*/ \
uint32_t take_raw(Store::Context cx) { \
uint32_t ret = capi_type##_to_raw(cx.capi(), &raw); \
capi_type##_set_null(&raw); \
return ret; \
} \
\
/** \
* \brief Returns `wasmtime_externref_to_raw`. \
*/ \
uint32_t borrow_raw(Store::Context cx) const { \
return capi_type##_to_raw(cx.capi(), &raw); \
}
#endif // WASMTIME_HELPERS_HH