|
| 1 | +#ifndef WASMTIME_COMPONENT_COMPONENT_H |
| 2 | +#define WASMTIME_COMPONENT_COMPONENT_H |
| 3 | + |
| 4 | +#ifdef WASMTIME_FEATURE_COMPONENT_MODEL |
| 5 | + |
| 6 | +#include <wasm.h> |
| 7 | +#include <wasmtime/error.h> |
| 8 | + |
| 9 | +#ifdef __cplusplus |
| 10 | +extern "C" { |
| 11 | +#endif |
| 12 | + |
| 13 | +/// Representation of a component in the component model. |
| 14 | +typedef struct wasmtime_component_t wasmtime_component_t; |
| 15 | + |
| 16 | +#ifdef WASMTIME_FEATURE_COMPILER |
| 17 | + |
| 18 | +/** |
| 19 | + * \brief Compiles a WebAssembly binary into a #wasmtime_component_t |
| 20 | + * |
| 21 | + * This function will compile a WebAssembly binary into an owned |
| 22 | + #wasmtime_component_t. |
| 23 | + * |
| 24 | + * It requires a component binary, such as what is produced by Rust `cargo |
| 25 | + component` tooling. |
| 26 | + * |
| 27 | + * This function does not take ownership of any of its arguments, but the |
| 28 | + * returned error and component are owned by the caller. |
| 29 | +
|
| 30 | + * \param engine the #wasm_engine_t that will create the component |
| 31 | + * \param buf the address of the buffer containing the WebAssembly binary |
| 32 | + * \param len the length of the buffer containing the WebAssembly binary |
| 33 | + * \param component_out on success, contains the address of the created |
| 34 | + * component |
| 35 | + * |
| 36 | + * \return NULL on success, else a #wasmtime_error_t describing the error |
| 37 | + */ |
| 38 | +WASM_API_EXTERN wasmtime_error_t * |
| 39 | +wasmtime_component_new(const wasm_engine_t *engine, const uint8_t *buf, |
| 40 | + size_t len, wasmtime_component_t **component_out); |
| 41 | + |
| 42 | +/** |
| 43 | + * \brief This function serializes compiled component artifacts as blob data. |
| 44 | + * |
| 45 | + * \param component the component |
| 46 | + * \param ret if the conversion is successful, this byte vector is filled in |
| 47 | + * with the serialized compiled component. |
| 48 | + * |
| 49 | + * \return a non-null error if parsing fails, or returns `NULL`. If parsing |
| 50 | + * fails then `ret` isn't touched. |
| 51 | + * |
| 52 | + * This function does not take ownership of `component`, and the caller is |
| 53 | + * expected to deallocate the returned #wasmtime_error_t and #wasm_byte_vec_t. |
| 54 | + */ |
| 55 | +WASM_API_EXTERN wasmtime_error_t * |
| 56 | +wasmtime_component_serialize(const wasmtime_component_t *component, |
| 57 | + wasm_byte_vec_t *ret); |
| 58 | + |
| 59 | +#endif // WASMTIME_FEATURE_COMPILER |
| 60 | + |
| 61 | +/** |
| 62 | + * \brief Build a component from serialized data. |
| 63 | + * |
| 64 | + * This function does not take ownership of any of its arguments, but the |
| 65 | + * returned error and component are owned by the caller. |
| 66 | + * |
| 67 | + * This function is not safe to receive arbitrary user input. See the Rust |
| 68 | + * documentation for more information on what inputs are safe to pass in here |
| 69 | + * (e.g. only that of `wasmtime_component_serialize`) |
| 70 | + */ |
| 71 | +WASM_API_EXTERN wasmtime_error_t * |
| 72 | +wasmtime_component_deserialize(const wasm_engine_t *engine, const uint8_t *buf, |
| 73 | + size_t len, |
| 74 | + wasmtime_component_t **component_out); |
| 75 | + |
| 76 | +/** |
| 77 | + * \brief Deserialize a component from an on-disk file. |
| 78 | + * |
| 79 | + * This function is the same as #wasmtime_component_deserialize except that it |
| 80 | + * reads the data for the serialized component from the path on disk. This can |
| 81 | + * be faster than the alternative which may require copying the data around. |
| 82 | + * |
| 83 | + * This function does not take ownership of any of its arguments, but the |
| 84 | + * returned error and component are owned by the caller. |
| 85 | + * |
| 86 | + * This function is not safe to receive arbitrary user input. See the Rust |
| 87 | + * documentation for more information on what inputs are safe to pass in here |
| 88 | + * (e.g. only that of `wasmtime_component_serialize`) |
| 89 | + */ |
| 90 | +WASM_API_EXTERN wasmtime_error_t * |
| 91 | +wasmtime_component_deserialize_file(const wasm_engine_t *engine, |
| 92 | + const char *path, |
| 93 | + wasmtime_component_t **component_out); |
| 94 | + |
| 95 | +/** |
| 96 | + * \brief Creates a shallow clone of the specified component, increasing the |
| 97 | + * internal reference count. |
| 98 | + */ |
| 99 | +WASM_API_EXTERN wasmtime_component_t * |
| 100 | +wasmtime_component_clone(const wasmtime_component_t *component); |
| 101 | + |
| 102 | +/** |
| 103 | + * \brief Deletes a #wasmtime_component_t created by |
| 104 | + * #wasmtime_component_from_binary |
| 105 | + * |
| 106 | + * \param component the component to delete |
| 107 | + */ |
| 108 | +WASM_API_EXTERN void wasmtime_component_delete(wasmtime_component_t *component); |
| 109 | + |
| 110 | +#ifdef __cplusplus |
| 111 | +} // extern "C" |
| 112 | +#endif |
| 113 | + |
| 114 | +#endif // WASMTIME_FEATURE_COMPONENT_MODEL |
| 115 | + |
| 116 | +#endif // WASMTIME_COMPONENT_COMPONENT_H |
0 commit comments