Wasmtime
exn.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_EXN_HH
6#define WASMTIME_EXN_HH
7
8#include <wasmtime/conf.h>
9
10#ifdef WASMTIME_FEATURE_GC
11
12#include <vector>
13#include <wasmtime/error.hh>
14#include <wasmtime/exn.h>
15#include <wasmtime/store.hh>
16#include <wasmtime/tag.hh>
17#include <wasmtime/val.hh>
18
19namespace wasmtime {
20
30class Exn {
31 struct deleter {
32 void operator()(wasmtime_exn_t *p) const { wasmtime_exn_delete(p); }
33 };
34
35 std::unique_ptr<wasmtime_exn_t, deleter> ptr;
36
37public:
39 explicit Exn(wasmtime_exn_t *raw) : ptr(raw) {}
40
42 Exn(Exn &&other) = default;
44 Exn &operator=(Exn &&other) = default;
45 Exn(const Exn &) = delete;
46 Exn &operator=(const Exn &) = delete;
48 ~Exn() = default;
49
58 const std::vector<Val> &fields) {
59 wasmtime_exn_t *exn = nullptr;
60 auto *error = wasmtime_exn_new(
61 cx.capi(), &tag.capi(),
62 reinterpret_cast<const wasmtime_val_t *>(fields.data()), fields.size(),
63 &exn);
64 if (error != nullptr) {
65 return Error(error);
66 }
67 return Exn(exn);
68 }
69
73 auto *error = wasmtime_exn_tag(cx.capi(), ptr.get(), &tag);
74 if (error != nullptr) {
75 return Error(error);
76 }
77 return Tag(tag);
78 }
79
81 size_t field_count(Store::Context cx) const {
82 return wasmtime_exn_field_count(cx.capi(), ptr.get());
83 }
84
86 Result<Val> field(Store::Context cx, size_t index) const {
88 auto *error = wasmtime_exn_field(cx.capi(), ptr.get(), index, &val);
89 if (error != nullptr) {
90 return Error(error);
91 }
92 return Val(val);
93 }
94
96 wasmtime_exn_t *capi() const { return ptr.get(); }
97
99 wasmtime_exn_t *release() { return ptr.release(); }
100};
101
104}
105
106inline std::optional<Exn> Store::Context::take_exception() {
107 wasmtime_exn_t *exn = nullptr;
108 if (wasmtime_context_take_exception(capi(), &exn)) {
109 return Exn(exn);
110 }
111 return std::nullopt;
112}
113
115 return wasmtime_context_has_exception(capi());
116}
117
118} // namespace wasmtime
119
120#endif // WASMTIME_FEATURE_GC
121
122#endif // WASMTIME_EXN_HH
Errors coming from Wasmtime.
Definition: error.hh:26
A WebAssembly exception object.
Definition: exn.hh:30
Result< Tag > tag(Store::Context cx) const
Returns the tag associated with this exception.
Definition: exn.hh:71
~Exn()=default
Destroys an exception.
Exn(Exn &&other)=default
Constructs a new exception.
static Result< Exn > create(Store::Context cx, const Tag &tag, const std::vector< Val > &fields)
Create a new exception object.
Definition: exn.hh:57
wasmtime_exn_t * capi() const
Returns the raw underlying C API pointer (non-owning).
Definition: exn.hh:96
Exn & operator=(Exn &&other)=default
Moves an exception.
Exn(wasmtime_exn_t *raw)
Takes ownership of a raw wasmtime_exn_t pointer.
Definition: exn.hh:39
wasmtime_exn_t * release()
Releases ownership of the underlying C API pointer.
Definition: exn.hh:99
Result< Val > field(Store::Context cx, size_t index) const
Reads a field value by index.
Definition: exn.hh:86
size_t field_count(Store::Context cx) const
Returns the number of fields in this exception.
Definition: exn.hh:81
Fallible result type used for Wasmtime.
Definition: error.hh:70
An interior pointer into a Store.
Definition: store.hh:69
Trap throw_exception(Exn exn)
Sets the pending exception on the store and returns a Trap.
Definition: exn.hh:102
bool has_exception()
Tests whether there is a pending exception on the store.
Definition: exn.hh:114
std::optional< Exn > take_exception()
Takes the pending exception from the store, if any.
Definition: exn.hh:106
const wasmtime_context_t * capi() const
Returns the underlying C API pointer.
Definition: store.hh:190
A WebAssembly tag.
Definition: tag.hh:26
Information about a WebAssembly trap.
Definition: trap.hh:113
Representation of a generic WebAssembly value.
Definition: val.hh:240
Build-time defines for how the C API was built.
Wasmtime APIs for WebAssembly exception objects.
struct wasmtime_exn wasmtime_exn_t
An opaque type representing a WebAssembly exception object.
Definition: exn.h:54
WASM_API_EXTERN size_t wasmtime_exn_field_count(wasmtime_context_t *store, const wasmtime_exn_t *exn)
Returns the number of fields in this exception.
WASM_API_EXTERN wasmtime_error_t * wasmtime_exn_tag(wasmtime_context_t *store, const wasmtime_exn_t *exn, wasmtime_tag_t *tag_ret)
Returns the tag associated with this exception.
WASM_API_EXTERN wasmtime_error_t * wasmtime_exn_field(wasmtime_context_t *store, const wasmtime_exn_t *exn, size_t index, wasmtime_val_t *val_ret)
Reads a field value from this exception by index.
WASM_API_EXTERN void wasmtime_exn_delete(wasmtime_exn_t *exn)
Deletes a wasmtime_exn_t.
WASM_API_EXTERN wasmtime_error_t * wasmtime_exn_new(wasmtime_context_t *store, const wasmtime_tag_t *tag, const wasmtime_val_t *fields, size_t nfields, wasmtime_exn_t **exn_ret)
Creates a new exception object.
WASM_API_EXTERN bool wasmtime_context_has_exception(wasmtime_context_t *store)
Tests whether there is a pending exception on the store.
WASM_API_EXTERN bool wasmtime_context_take_exception(wasmtime_context_t *store, wasmtime_exn_t **exn_ret)
Takes the pending exception from the store, if any.
WASM_API_EXTERN wasm_trap_t * wasmtime_context_set_exception(wasmtime_context_t *store, wasmtime_exn_t *exn)
Sets the pending exception on the store and returns a trap.
Representation of a tag in Wasmtime.
Definition: tag.h:28
Container for different kinds of wasm values.
Definition: val.h:546