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#include <wasmtime/helpers.hh>
15
16namespace wasmtime {
17
18class Trace;
19
26class Error {
27 WASMTIME_OWN_WRAPPER(Error, wasmtime_error);
28
30 Error(const std::string &s) : ptr(wasmtime_error_new(s.c_str())) {}
31
33 std::string message() const {
34 wasm_byte_vec_t msg_bytes;
35 wasmtime_error_message(ptr.get(), &msg_bytes);
36 auto ret = std::string(msg_bytes.data, msg_bytes.size);
37 wasm_byte_vec_delete(&msg_bytes);
38 return ret;
39 }
40
43 std::optional<int32_t> i32_exit() const {
44 int32_t status = 0;
45 if (wasmtime_error_exit_status(ptr.get(), &status)) {
46 return status;
47 }
48 return std::nullopt;
49 }
50
54 Trace trace() const;
55};
56
58inline std::ostream &operator<<(std::ostream &os, const Error &e) {
59 os << e.message();
60 return os;
61}
62
70template <typename T, typename E = Error> class [[nodiscard]] Result {
71 std::variant<T, E> data;
72
73public:
75 Result(T t) : data(std::move(t)) {}
77 Result(E e) : data(std::move(e)) {}
78
81 explicit operator bool() const { return data.index() == 0; }
82
84 E &&err() { return std::get<E>(std::move(data)); }
86 const E &&err() const { return std::get<E>(std::move(data)); }
87
89 T &&ok() { return std::get<T>(std::move(data)); }
91 const T &&ok() const { return std::get<T>(std::move(data)); }
92
94 T &ok_ref() { return std::get<T>(data); }
96 const T &ok_ref() const { return std::get<T>(data); }
97
99 E &err_ref() { return std::get<T>(data); }
101 const E &err_ref() const { return std::get<T>(data); }
102
104 T unwrap() {
105 if (*this) {
106 return this->ok();
107 }
108 unwrap_failed();
109 }
110
111private:
112 [[noreturn]] void unwrap_failed() {
113 fprintf(stderr, "error: %s\n", this->err().message().c_str()); // NOLINT
114 std::abort();
115 }
116};
117
118} // namespace wasmtime
119
120#endif // WASMTIME_ERROR_HH
Errors coming from Wasmtime.
Definition: error.hh:26
Fallible result type used for Wasmtime.
Definition: error.hh:70
const T && ok() const
Returns the success, if present, aborts if this is an error.
Definition: error.hh:91
E && err()
Returns the error, if present, aborts if this is not an error.
Definition: error.hh:84
T unwrap()
Returns the success, if present, aborts if this is an error.
Definition: error.hh:104
T && ok()
Returns the success, if present, aborts if this is an error.
Definition: error.hh:89
Result(T t)
Creates a Result from its successful value.
Definition: error.hh:75
const E && err() const
Returns the error, if present, aborts if this is not an error.
Definition: error.hh:86
const T & ok_ref() const
Returns the success, if present, aborts if this is an error.
Definition: error.hh:96
E & err_ref()
Returns the error, if present, aborts if this is not an error.
Definition: error.hh:99
const E & err_ref() const
Returns the error, if present, aborts if this is not an error.
Definition: error.hh:101
T & ok_ref()
Returns the success, if present, aborts if this is an error.
Definition: error.hh:94
Result(E e)
Creates a Result from an error value.
Definition: error.hh:77
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.
WASM_API_EXTERN void wasmtime_error_message(const wasmtime_error_t *error, wasm_name_t *message)
Returns the string description of this error.
WASM_API_EXTERN bool wasmtime_error_exit_status(const wasmtime_error_t *, int *status)
Attempts to extract a WASI-specific exit status from this error.
WASM_API_EXTERN wasmtime_error_t * wasmtime_error_new(const char *)
Creates a new error with the provided message.
std::ostream & operator<<(std::ostream &os, const Error &e)
Used to print an error.
Definition: error.hh:58
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.