MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / Trap

Class Trap

crates/c-api/include/wasmtime/trap.hh:113–150  ·  view source on GitHub ↗

* \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. */

Source from the content-addressed store, hash-verified

111 * frames on the stack.
112 */
113class 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`.
153struct TrapError {

Callers 15

createMethod · 0.70
typedMethod · 0.70
callMethod · 0.70
callMethod · 0.70
instantiateMethod · 0.70
throw_exceptionMethod · 0.70
stepFunction · 0.50
blockMethod · 0.50
trapFunction · 0.50
record_errorMethod · 0.50
variant_errorMethod · 0.50
fromMethod · 0.50

Calls

no outgoing calls

Tested by 2

trapFunction · 0.40
TESTFunction · 0.40