Wasmtime
error.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_ERROR_HH
6#define WASMTIME_ERROR_HH
7
8#include <memory>
9#include <optional>
10#include <string>
11#include <variant>
12#include <wasmtime/error.h>
13
14namespace wasmtime {
15
16class Trace;
17
24class Error {
25 struct deleter {
26 void operator()(wasmtime_error_t *p) const { wasmtime_error_delete(p); }
27 };
28
29 std::unique_ptr<wasmtime_error_t, deleter> ptr;
30
31public:
35 Error(wasmtime_error_t *error) : ptr(error) {}
36
38 Error(const std::string &s) : ptr(wasmtime_error_new(s.c_str())) {}
39
41 std::string message() const {
42 wasm_byte_vec_t msg_bytes;
43 wasmtime_error_message(ptr.get(), &msg_bytes);
44 auto ret = std::string(msg_bytes.data, msg_bytes.size);
45 wasm_byte_vec_delete(&msg_bytes);
46 return ret;
47 }
48
51 std::optional<int32_t> i32_exit() const {
52 int32_t status = 0;
53 if (wasmtime_error_exit_status(ptr.get(), &status)) {
54 return status;
55 }
56 return std::nullopt;
57 }
58
62 Trace trace() const;
63
66 return ptr.release();
67 }
68};
69
71inline std::ostream &operator<<(std::ostream &os, const Error &e) {
72 os << e.message();
73 return os;
74}
75
83template <typename T, typename E = Error> class [[nodiscard]] Result {
84 std::variant<T, E> data;
85
86public:
88 Result(T t) : data(std::move(t)) {}
90 Result(E e) : data(std::move(e)) {}
91
94 explicit operator bool() const { return data.index() == 0; }
95
97 E &&err() { return std::get<E>(std::move(data)); }
99 const E &&err() const { return std::get<E>(std::move(data)); }
100
102 T &&ok() { return std::get<T>(std::move(data)); }
104 const T &&ok() const { return std::get<T>(std::move(data)); }
105
107 T unwrap() {
108 if (*this) {
109 return this->ok();
110 }
111 unwrap_failed();
112 }
113
114private:
115 [[noreturn]] void unwrap_failed() {
116 fprintf(stderr, "error: %s\n", this->err().message().c_str()); // NOLINT
117 std::abort();
118 }
119};
120
121} // namespace wasmtime
122
123#endif // WASMTIME_ERROR_HH
124
Errors coming from Wasmtime.
Definition: error.hh:24
Error(const std::string &s)
Creates an error with the provided message.
Definition: error.hh:38
Error(wasmtime_error_t *error)
Creates an error from the raw C API representation.
Definition: error.hh:35
wasmtime_error_t * release()
Release ownership of this error, acquiring the underlying C raw pointer.
Definition: error.hh:65
Trace trace() const
Definition: trap.hh:97
std::optional< int32_t > i32_exit() const
Definition: error.hh:51
std::string message() const
Returns the error message associated with this error.
Definition: error.hh:41
Fallible result type used for Wasmtime.
Definition: error.hh:83
const T && ok() const
Returns the success, if present, aborts if this is an error.
Definition: error.hh:104
E && err()
Returns the error, if present, aborts if this is not an error.
Definition: error.hh:97
T unwrap()
Returns the success, if present, aborts if this is an error.
Definition: error.hh:107
T && ok()
Returns the success, if present, aborts if this is an error.
Definition: error.hh:102
Result(T t)
Creates a Result from its successful value.
Definition: error.hh:88
const E && err() const
Returns the error, if present, aborts if this is not an error.
Definition: error.hh:99
Result(E e)
Creates a Result from an error value.
Definition: error.hh:90
An owned vector of FrameRef instances representing the WebAssembly call-stack on a trap.
Definition: trap.hh:66
Definition and accessors of wasmtime_error_t.
void wasmtime_error_message(const wasmtime_error_t *error, wasm_name_t *message)
Returns the string description of this error.
void wasmtime_error_delete(wasmtime_error_t *error)
Deletes an error.
wasmtime_error_t * wasmtime_error_new(const char *)
Creates a new error with the provided message.
bool wasmtime_error_exit_status(const wasmtime_error_t *, int *status)
Attempts to extract a WASI-specific exit status from this error.
std::ostream & operator<<(std::ostream &os, const Error &e)
Used to print an error.
Definition: error.hh:71
A list of bytes.
Definition: wasm.h:102
size_t size
Length of this vector.
Definition: wasm.h:102
wasm_byte_t * data
Pointer to the base of this vector.
Definition: wasm.h:102
Errors generated by Wasmtime.
void wasm_byte_vec_delete(wasm_byte_vec_t *)
Deletes a byte vector.