Wasmtime
_externref_class.hh
1#ifndef WASMTIME_EXTERNREF_CLASS_HH
2#define WASMTIME_EXTERNREF_CLASS_HH
3
4#include <wasmtime/conf.h>
5
6#ifdef WASMTIME_FEATURE_GC
7
8#include <wasmtime/_store_class.hh>
10#include <wasmtime/helpers.hh>
11
12namespace wasmtime {
13
27class ExternRef {
28 WASMTIME_TOP_REF_WRAPPER(ExternRef, wasmtime_externref);
29
30private:
31 static void finalizer(void *ptr) {
32 std::unique_ptr<std::any> _ptr(static_cast<std::any *>(ptr));
33 }
34
35public:
41 explicit ExternRef(Store::Context cx, std::any val) {
42 void *ptr = std::make_unique<std::any>(val).release();
43 bool ok = wasmtime_externref_new(cx.capi(), ptr, finalizer, &this->raw);
44 if (!ok) {
45 fprintf(stderr, "failed to allocate a new externref");
46 abort();
47 }
48 }
49
51 std::any &data(Store::Context cx) {
52 return *static_cast<std::any *>(wasmtime_externref_data(cx.capi(), &raw));
53 }
54};
55
56} // namespace wasmtime
57
58#endif // WASMTIME_FEATURE_GC
59
60#endif // WASMTIME_EXTERNREF_CLASS_HH
Representation of a WebAssembly externref value.
Definition: _externref_class.hh:27
ExternRef(Store::Context cx, std::any val)
Definition: _externref_class.hh:41
std::any & data(Store::Context cx)
Returns the underlying host data associated with this ExternRef.
Definition: _externref_class.hh:51
An interior pointer into a Store.
Definition: _store_class.hh:65
const wasmtime_context_t * capi() const
Returns the underlying C API pointer.
Definition: _store_class.hh:183
Build-time defines for how the C API was built.
WASM_API_EXTERN bool wasmtime_externref_new(wasmtime_context_t *context, void *data, void(*finalizer)(void *), wasmtime_externref_t *out)
Create a new externref value.
WASM_API_EXTERN void * wasmtime_externref_data(wasmtime_context_t *context, const wasmtime_externref_t *data)
Get an externref's wrapped data.
A host-defined un-forgeable reference to pass into WebAssembly.
Definition: val.h:136