1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/**
* \file wasmtime/error.hh
*/
#ifndef WASMTIME_ERROR_HH
#define WASMTIME_ERROR_HH
#include <memory>
#include <optional>
#include <ostream>
#include <string>
#include <variant>
#include <wasmtime/error.h>
#include <wasmtime/helpers.hh>
namespace wasmtime {
class Trace;
/**
* \brief Errors coming from Wasmtime
*
* This class represents an error that came from Wasmtime and contains a textual
* description of the error that occurred.
*/
class Error {
WASMTIME_OWN_WRAPPER(Error, wasmtime_error);
/// \brief Creates an error with the provided message.
Error(const std::string &s) : ptr(wasmtime_error_new(s.c_str())) {}
/// \brief Returns the error message associated with this error.
std::string message() const {
wasm_byte_vec_t msg_bytes;
wasmtime_error_message(ptr.get(), &msg_bytes);
auto ret = std::string(msg_bytes.data, msg_bytes.size);
wasm_byte_vec_delete(&msg_bytes);
return ret;
}
/// If this trap represents a call to `exit` for WASI, this will return the
/// optional error code associated with the exit trap.
std::optional<int32_t> i32_exit() const {
int32_t status = 0;
if (wasmtime_error_exit_status(ptr.get(), &status)) {
return status;
}
return std::nullopt;
}
/// Returns the trace of WebAssembly frames associated with this error.
///
/// Note that the `trace` cannot outlive this error object.
Trace trace() const;
};
/// \brief Used to print an error.
inline std::ostream &operator<<(std::ostream &os, const Error &e) {
os << e.message();
return os;
}
/**
* \brief Fallible result type used for Wasmtime.
*
* This type is used as the return value of many methods in the Wasmtime API.
* This behaves similarly to Rust's `Result<T, E>` and will be replaced with a
* C++ standard when it exists.
*/
template <typename T, typename E = Error> class [[nodiscard]] Result {
std::variant<T, E> data;
public:
/// \brief Creates a `Result` from its successful value.
Result(T t) : data(std::move(t)) {}
/// \brief Creates a `Result` from an error value.
Result(E e) : data(std::move(e)) {}
/// \brief Returns `true` if this result is a success, `false` if it's an
/// error
explicit operator bool() const { return data.index() == 0; }
/// \brief Returns the error, if present, aborts if this is not an error.
E &&err() { return std::get<E>(std::move(data)); }
/// \brief Returns the error, if present, aborts if this is not an error.
const E &&err() const { return std::get<E>(std::move(data)); }
/// \brief Returns the success, if present, aborts if this is an error.
T &&ok() { return std::get<T>(std::move(data)); }
/// \brief Returns the success, if present, aborts if this is an error.
const T &&ok() const { return std::get<T>(std::move(data)); }
/// \brief Returns the success, if present, aborts if this is an error.
T &ok_ref() { return std::get<T>(data); }
/// \brief Returns the success, if present, aborts if this is an error.
const T &ok_ref() const { return std::get<T>(data); }
/// \brief Returns the error, if present, aborts if this is not an error.
E &err_ref() { return std::get<T>(data); }
/// \brief Returns the error, if present, aborts if this is not an error.
const E &err_ref() const { return std::get<T>(data); }
/// \brief Returns the success, if present, aborts if this is an error.
T unwrap() {
if (*this) {
return this->ok();
}
unwrap_failed();
}
private:
[[noreturn]] void unwrap_failed() {
fprintf(stderr, "error: %s\n", this->err().message().c_str()); // NOLINT
std::abort();
}
};
} // namespace wasmtime
#endif // WASMTIME_ERROR_HH