|
| 1 | +/** |
| 2 | + * \file wasmtime/exn.h |
| 3 | + * |
| 4 | + * \brief Wasmtime APIs for WebAssembly exception objects. |
| 5 | + * |
| 6 | + * Exception objects carry a tag and a set of field values. They are |
| 7 | + * allocated on the GC heap within a store. |
| 8 | + * |
| 9 | + * ## Throwing from host functions |
| 10 | + * |
| 11 | + * To throw an exception from a host function implemented via the C API: |
| 12 | + * |
| 13 | + * 1. Create an exception object with #wasmtime_exn_new. |
| 14 | + * 2. Set it as the pending exception with #wasmtime_context_set_exception. |
| 15 | + * 3. Return a trap from the host callback (e.g. via `wasmtime_trap_new`). |
| 16 | + * |
| 17 | + * The runtime will propagate the exception through WebAssembly |
| 18 | + * `try_table`/`catch` blocks. |
| 19 | + * |
| 20 | + * ## Catching exceptions from Wasm |
| 21 | + * |
| 22 | + * When a call to a WebAssembly function (e.g. via #wasmtime_func_call) |
| 23 | + * returns a trap, check for a pending exception: |
| 24 | + * |
| 25 | + * 1. Call #wasmtime_context_has_exception or #wasmtime_context_take_exception. |
| 26 | + * 2. If present, examine the exception's tag and fields. |
| 27 | + */ |
| 28 | + |
| 29 | +#ifndef WASMTIME_EXN_H |
| 30 | +#define WASMTIME_EXN_H |
| 31 | + |
| 32 | +#include <wasmtime/conf.h> |
| 33 | + |
| 34 | +#ifdef WASMTIME_FEATURE_GC |
| 35 | + |
| 36 | +#include <wasm.h> |
| 37 | +#include <wasmtime/error.h> |
| 38 | +#include <wasmtime/store.h> |
| 39 | +#include <wasmtime/tag.h> |
| 40 | +#include <wasmtime/val.h> |
| 41 | + |
| 42 | +#ifdef __cplusplus |
| 43 | +extern "C" { |
| 44 | +#endif |
| 45 | + |
| 46 | +/** |
| 47 | + * \typedef wasmtime_exn_t |
| 48 | + * \brief An opaque type representing a WebAssembly exception object. |
| 49 | + * |
| 50 | + * Exception objects are allocated on the GC heap and referenced through |
| 51 | + * this handle. The handle is owned by the caller and must be freed |
| 52 | + * with #wasmtime_exn_delete. |
| 53 | + */ |
| 54 | +typedef struct wasmtime_exn wasmtime_exn_t; |
| 55 | + |
| 56 | +/** |
| 57 | + * \brief Deletes a #wasmtime_exn_t. |
| 58 | + */ |
| 59 | +WASM_API_EXTERN void wasmtime_exn_delete(wasmtime_exn_t *exn); |
| 60 | + |
| 61 | +/** |
| 62 | + * \brief Creates a new exception object. |
| 63 | + * |
| 64 | + * \param store the store context |
| 65 | + * \param tag the tag to associate with this exception |
| 66 | + * \param fields pointer to an array of field values matching the tag's |
| 67 | + * payload signature |
| 68 | + * \param nfields the number of elements in `fields` |
| 69 | + * \param exn_ret on success, set to the newly allocated exception. |
| 70 | + * The caller owns the returned pointer and must free it with |
| 71 | + * #wasmtime_exn_delete. |
| 72 | + * |
| 73 | + * \return NULL on success, or an error on failure. |
| 74 | + */ |
| 75 | +WASM_API_EXTERN wasmtime_error_t *wasmtime_exn_new(wasmtime_context_t *store, |
| 76 | + const wasmtime_tag_t *tag, |
| 77 | + const wasmtime_val_t *fields, |
| 78 | + size_t nfields, |
| 79 | + wasmtime_exn_t **exn_ret); |
| 80 | + |
| 81 | +/** |
| 82 | + * \brief Returns the tag associated with this exception. |
| 83 | + * |
| 84 | + * \param store the store context |
| 85 | + * \param exn the exception to query |
| 86 | + * \param tag_ret on success, filled with the exception's tag |
| 87 | + * |
| 88 | + * \return NULL on success, or an error on failure. |
| 89 | + */ |
| 90 | +WASM_API_EXTERN wasmtime_error_t *wasmtime_exn_tag(wasmtime_context_t *store, |
| 91 | + const wasmtime_exn_t *exn, |
| 92 | + wasmtime_tag_t *tag_ret); |
| 93 | + |
| 94 | +/** |
| 95 | + * \brief Returns the number of fields in this exception. |
| 96 | + * |
| 97 | + * \param store the store context |
| 98 | + * \param exn the exception to query |
| 99 | + */ |
| 100 | +WASM_API_EXTERN size_t wasmtime_exn_field_count(wasmtime_context_t *store, |
| 101 | + const wasmtime_exn_t *exn); |
| 102 | + |
| 103 | +/** |
| 104 | + * \brief Reads a field value from this exception by index. |
| 105 | + * |
| 106 | + * \param store the store context |
| 107 | + * \param exn the exception to query |
| 108 | + * \param index the field index (0-based) |
| 109 | + * \param val_ret on success, filled with the field value |
| 110 | + * (caller-owned on return). |
| 111 | + * |
| 112 | + * \return NULL on success, or an error if the index is out of bounds. |
| 113 | + */ |
| 114 | +WASM_API_EXTERN wasmtime_error_t *wasmtime_exn_field(wasmtime_context_t *store, |
| 115 | + const wasmtime_exn_t *exn, |
| 116 | + size_t index, |
| 117 | + wasmtime_val_t *val_ret); |
| 118 | + |
| 119 | +/** |
| 120 | + * \brief Sets the pending exception on the store and returns a trap. |
| 121 | + * |
| 122 | + * This transfers ownership of `exn` to the store. After this call, |
| 123 | + * the caller must not use or free `exn`. |
| 124 | + * |
| 125 | + * Returns a `wasm_trap_t` that the host callback MUST return to signal |
| 126 | + * to the Wasm runtime that an exception was thrown. The caller owns |
| 127 | + * the returned trap. |
| 128 | + * |
| 129 | + * \param store the store context |
| 130 | + * \param exn the exception to throw (ownership transferred) |
| 131 | + * \return a trap to return from the host callback (caller-owned) |
| 132 | + */ |
| 133 | +WASM_API_EXTERN wasm_trap_t * |
| 134 | +wasmtime_context_set_exception(wasmtime_context_t *store, wasmtime_exn_t *exn); |
| 135 | + |
| 136 | +/** |
| 137 | + * \brief Takes the pending exception from the store, if any. |
| 138 | + * |
| 139 | + * If there is a pending exception, removes it from the store and |
| 140 | + * returns it. The caller owns the returned pointer and must free it |
| 141 | + * with #wasmtime_exn_delete. |
| 142 | + * |
| 143 | + * \param store the store context |
| 144 | + * \param exn_ret on success, set to the exception (caller-owned) |
| 145 | + * |
| 146 | + * \return true if there was a pending exception, false otherwise. |
| 147 | + */ |
| 148 | +WASM_API_EXTERN bool wasmtime_context_take_exception(wasmtime_context_t *store, |
| 149 | + wasmtime_exn_t **exn_ret); |
| 150 | + |
| 151 | +/** |
| 152 | + * \brief Tests whether there is a pending exception on the store. |
| 153 | + * |
| 154 | + * \param store the store context |
| 155 | + * |
| 156 | + * \return true if a pending exception is set, false otherwise. |
| 157 | + */ |
| 158 | +WASM_API_EXTERN bool wasmtime_context_has_exception(wasmtime_context_t *store); |
| 159 | + |
| 160 | +#ifdef __cplusplus |
| 161 | +} // extern "C" |
| 162 | +#endif |
| 163 | + |
| 164 | +#endif // WASMTIME_FEATURE_GC |
| 165 | + |
| 166 | +#endif // WASMTIME_EXN_H |
0 commit comments