|
| 1 | +/** |
| 2 | + * \file wasmtime/gc.h |
| 3 | + * |
| 4 | + * APIs for interacting with WebAssembly GC types in Wasmtime. |
| 5 | + * |
| 6 | + * This header provides types and functions for GC reference types beyond |
| 7 | + * the basic `anyref` and `externref` in val.h: `eqref`, `structref`, |
| 8 | + * and `arrayref`. |
| 9 | + */ |
| 10 | + |
| 11 | +#ifndef WASMTIME_GC_H |
| 12 | +#define WASMTIME_GC_H |
| 13 | + |
| 14 | +#include <wasmtime/val.h> |
| 15 | + |
| 16 | +#ifdef __cplusplus |
| 17 | +extern "C" { |
| 18 | +#endif |
| 19 | + |
| 20 | +/** |
| 21 | + * \typedef wasmtime_eqref_t |
| 22 | + * \brief Convenience alias for #wasmtime_eqref |
| 23 | + * |
| 24 | + * \struct wasmtime_eqref |
| 25 | + * \brief A WebAssembly `eqref` value. |
| 26 | + * |
| 27 | + * This structure represents a reference to a GC object that can be tested for |
| 28 | + * equality. The subtypes of `eqref` include `structref`, `arrayref`, and |
| 29 | + * `i31ref`. |
| 30 | + * |
| 31 | + * This type has the same representation and ownership semantics as |
| 32 | + * #wasmtime_anyref_t. Values must be explicitly unrooted via |
| 33 | + * #wasmtime_eqref_unroot to enable garbage collection. |
| 34 | + */ |
| 35 | +typedef struct wasmtime_eqref { |
| 36 | + /// Internal metadata tracking within the store, embedders should not |
| 37 | + /// configure or modify these fields. |
| 38 | + uint64_t store_id; |
| 39 | + /// Internal to Wasmtime. |
| 40 | + uint32_t __private1; |
| 41 | + /// Internal to Wasmtime. |
| 42 | + uint32_t __private2; |
| 43 | + /// Internal to Wasmtime. |
| 44 | + void *__private3; |
| 45 | +} wasmtime_eqref_t; |
| 46 | + |
| 47 | +/// \brief Initialize the `ref` to a null `eqref` value. |
| 48 | +static inline void wasmtime_eqref_set_null(wasmtime_eqref_t *ref) { |
| 49 | + ref->store_id = 0; |
| 50 | +} |
| 51 | + |
| 52 | +/// \brief Returns whether the provided `ref` is a null `eqref` value. |
| 53 | +static inline bool wasmtime_eqref_is_null(const wasmtime_eqref_t *ref) { |
| 54 | + return ref->store_id == 0; |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * \brief Clone an `eqref`, creating a new root. |
| 59 | + * |
| 60 | + * The cloned reference is stored in `out`. |
| 61 | + */ |
| 62 | +WASM_API_EXTERN void wasmtime_eqref_clone(const wasmtime_eqref_t *eqref, |
| 63 | + wasmtime_eqref_t *out); |
| 64 | + |
| 65 | +/** |
| 66 | + * \brief Unroot an `eqref` to allow garbage collection. |
| 67 | + * |
| 68 | + * After calling this, `ref` is left in an undefined state and should not be |
| 69 | + * used again. |
| 70 | + */ |
| 71 | +WASM_API_EXTERN void wasmtime_eqref_unroot(wasmtime_eqref_t *ref); |
| 72 | + |
| 73 | +/** |
| 74 | + * \brief Upcast an `eqref` to an `anyref`. |
| 75 | + * |
| 76 | + * The original `eqref` is not consumed; `out` receives a new cloned root |
| 77 | + * pointing to the same GC object as `anyref`. |
| 78 | + */ |
| 79 | +WASM_API_EXTERN void wasmtime_eqref_to_anyref(const wasmtime_eqref_t *eqref, |
| 80 | + wasmtime_anyref_t *out); |
| 81 | + |
| 82 | +/** |
| 83 | + * \brief Create a new `i31ref` value. |
| 84 | + * |
| 85 | + * Creates a new `i31ref` value (which is a subtype of `eqref`) and returns a |
| 86 | + * pointer to it. |
| 87 | + * |
| 88 | + * If `i31val` does not fit in 31 bits, it is wrapped. |
| 89 | + */ |
| 90 | +WASM_API_EXTERN void wasmtime_eqref_from_i31(wasmtime_context_t *context, |
| 91 | + uint32_t i31val, |
| 92 | + wasmtime_eqref_t *out); |
| 93 | + |
| 94 | +/** |
| 95 | + * \brief Test whether this `eqref` is an `i31ref`. |
| 96 | + * |
| 97 | + * Returns `true` if the given `eqref` is an `i31ref`, `false` otherwise. |
| 98 | + * Returns `false` for null references. |
| 99 | + */ |
| 100 | +WASM_API_EXTERN bool wasmtime_eqref_is_i31(wasmtime_context_t *context, |
| 101 | + const wasmtime_eqref_t *eqref); |
| 102 | + |
| 103 | +/** |
| 104 | + * \brief Get the `eqref`'s underlying `i31ref` value, zero extended. |
| 105 | + * |
| 106 | + * If the given `eqref` is an instance of `i31ref`, then its value is zero |
| 107 | + * extended to 32 bits, written to `dst`, and `true` is returned. |
| 108 | + * |
| 109 | + * If the given `eqref` is not an instance of `i31ref`, then `false` is |
| 110 | + * returned and `dst` is left unmodified. |
| 111 | + */ |
| 112 | +WASM_API_EXTERN bool wasmtime_eqref_i31_get_u(wasmtime_context_t *context, |
| 113 | + const wasmtime_eqref_t *eqref, |
| 114 | + uint32_t *dst); |
| 115 | + |
| 116 | +/** |
| 117 | + * \brief Get the `eqref`'s underlying `i31ref` value, sign extended. |
| 118 | + * |
| 119 | + * If the given `eqref` is an instance of `i31ref`, then its value is sign |
| 120 | + * extended to 32 bits, written to `dst`, and `true` is returned. |
| 121 | + * |
| 122 | + * If the given `eqref` is not an instance of `i31ref`, then `false` is |
| 123 | + * returned and `dst` is left unmodified. |
| 124 | + */ |
| 125 | +WASM_API_EXTERN bool wasmtime_eqref_i31_get_s(wasmtime_context_t *context, |
| 126 | + const wasmtime_eqref_t *eqref, |
| 127 | + int32_t *dst); |
| 128 | + |
| 129 | +#ifdef __cplusplus |
| 130 | +} // extern "C" |
| 131 | +#endif |
| 132 | + |
| 133 | +#endif // WASMTIME_GC_H |
0 commit comments