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 <ostream>
11#include <string>
12#include <variant>
13#include <wasmtime/error.h>
14
15namespace wasmtime {
16
17class Trace;
18
25class Error {
26 struct deleter {
27 void operator()(wasmtime_error_t *p) const { wasmtime_error_delete(p); }
28 };
29
30 std::unique_ptr<wasmtime_error_t, deleter> ptr;
31
32public:
36 Error(wasmtime_error_t *error) : ptr(error) {}
37
39 Error(const std::string &s) : ptr(wasmtime_error_new(s.c_str())) {}
40
42 std::string message() const {
43 wasm_byte_vec_t msg_bytes;
44 wasmtime_error_message(ptr.get(), &msg_bytes);
45 auto ret = std::string(msg_bytes.data, msg_bytes.size);
46 wasm_byte_vec_delete(&msg_bytes);
47 return ret;
48 }
49
52 std::optional<int32_t> i32_exit() const {
53 int32_t status = 0;
54 if (wasmtime_error_exit_status(ptr.get(), &status)) {
55 return status;
56 }
57 return std::nullopt;
58 }
59
63 Trace trace() const;
64
66 wasmtime_error_t *release() { return ptr.release(); }
67};
68
70inline std::ostream &operator<<(std::ostream &os, const Error &e) {
71 os << e.message();
72 return os;
73}
74
82template <typename T, typename E = Error> class [[nodiscard]] Result {
83 std::variant<T, E> data;
84
85public:
87 Result(T t) : data(std::move(t)) {}
89 Result(E e) : data(std::move(e)) {}
90
93 explicit operator bool() const { return data.index() == 0; }
94
96 E &&err() { return std::get<E>(std::move(data)); }
98 const E &&err() const { return std::get<E>(std::move(data)); }
99
101 T &&ok() { return std::get<T>(std::move(data)); }
103 const T &&ok() const { return std::get<T>(std::move(data)); }
104
106 T unwrap() {
107 if (*this) {
108 return this->ok();
109 }
110 unwrap_failed();
111 }
112
113private:
114 [[noreturn]] void unwrap_failed() {
115 fprintf(stderr, "error: %s\n", this->err().message().c_str()); // NOLINT
116 std::abort();
117 }
118};
119
120} // namespace wasmtime
121
122#endif // WASMTIME_ERROR_HH
Errors coming from Wasmtime.
Definition: error.hh:25
Error(const std::string &s)
Creates an error with the provided message.
Definition: error.hh:39
Error(wasmtime_error_t *error)
Creates an error from the raw C API representation.
Definition: error.hh:36
wasmtime_error_t * release()
Release ownership of this error, acquiring the underlying C raw pointer.
Definition: error.hh:66
Trace trace() const
Definition: trap.hh:97
std::optional< int32_t > i32_exit() const
Definition: error.hh:52
std::string message() const
Returns the error message associated with this error.
Definition: error.hh:42
Fallible result type used for Wasmtime.
Definition: error.hh:82
const T && ok() const
Returns the success, if present, aborts if this is an error.
Definition: error.hh:103
E && err()
Returns the error, if present, aborts if this is not an error.
Definition: error.hh:96
T unwrap()
Returns the success, if present, aborts if this is an error.
Definition: error.hh:106
T && ok()
Returns the success, if present, aborts if this is an error.
Definition: error.hh:101
Result(T t)
Creates a Result from its successful value.
Definition: error.hh:87
const E && err() const
Returns the error, if present, aborts if this is not an error.
Definition: error.hh:98
Result(E e)
Creates a Result from an error value.
Definition: error.hh:89
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:70
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.