* \brief Information about a WebAssembly trap. * * Traps can happen during normal wasm execution (such as the `unreachable` * instruction) but they can also happen in host-provided functions to a host * function can simulate raising a trap. * * Traps have a message associated with them as well as a trace of WebAssembly * frames on the stack. */
| 111 | * frames on the stack. |
| 112 | */ |
| 113 | class Trap { |
| 114 | WASMTIME_OWN_WRAPPER(Trap, wasm_trap); |
| 115 | |
| 116 | /// Creates a new host-defined trap with the specified message. |
| 117 | explicit Trap(std::string_view msg) |
| 118 | : Trap(wasmtime_trap_new(msg.data(), msg.size())) {} |
| 119 | |
| 120 | /// Creates a new trap with the given wasmtime trap code. |
| 121 | Trap(wasmtime_trap_code_enum code) : Trap(wasmtime_trap_new_code(code)) {} |
| 122 | |
| 123 | /// Returns the descriptive message associated with this trap. |
| 124 | std::string message() const { |
| 125 | wasm_byte_vec_t msg; |
| 126 | wasm_trap_message(ptr.get(), &msg); |
| 127 | std::string ret(msg.data, msg.size - 1); |
| 128 | wasm_byte_vec_delete(&msg); |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | /// Returns the trace of WebAssembly frames associated with this trap. |
| 133 | /// |
| 134 | /// Note that the `trace` cannot outlive this error object. |
| 135 | Trace trace() const { |
| 136 | wasm_frame_vec_t frames; |
| 137 | wasm_trap_trace(ptr.get(), &frames); |
| 138 | return Trace(frames); |
| 139 | } |
| 140 | |
| 141 | /// \brief Returns the trap code associated with this trap, or nothing if |
| 142 | /// it was a manually created trap. |
| 143 | std::optional<wasmtime_trap_code_t> code() const { |
| 144 | wasmtime_trap_code_t code; |
| 145 | bool present = wasmtime_trap_code(ptr.get(), &code); |
| 146 | if (present) |
| 147 | return code; |
| 148 | return std::nullopt; |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | /// Structure used to represent either a `Trap` or an `Error`. |
| 153 | struct TrapError { |
no outgoing calls