1b2c64de1SMangoPeachGrape /// \file wasmtime/component/component.h
2b2c64de1SMangoPeachGrape 
36ba6e13bSMangoPeachGrape #ifndef WASMTIME_COMPONENT_COMPONENT_H
46ba6e13bSMangoPeachGrape #define WASMTIME_COMPONENT_COMPONENT_H
56ba6e13bSMangoPeachGrape 
66ba6e13bSMangoPeachGrape #include <wasm.h>
7*39ec36caSAlex Crichton #include <wasmtime/component/types/component.h>
8930d3543SMangoPeachGrape #include <wasmtime/conf.h>
96ba6e13bSMangoPeachGrape #include <wasmtime/error.h>
106ba6e13bSMangoPeachGrape 
11930d3543SMangoPeachGrape #ifdef WASMTIME_FEATURE_COMPONENT_MODEL
12930d3543SMangoPeachGrape 
136ba6e13bSMangoPeachGrape #ifdef __cplusplus
146ba6e13bSMangoPeachGrape extern "C" {
156ba6e13bSMangoPeachGrape #endif
166ba6e13bSMangoPeachGrape 
176ba6e13bSMangoPeachGrape /// Representation of a component in the component model.
186ba6e13bSMangoPeachGrape typedef struct wasmtime_component_t wasmtime_component_t;
196ba6e13bSMangoPeachGrape 
206ba6e13bSMangoPeachGrape #ifdef WASMTIME_FEATURE_COMPILER
216ba6e13bSMangoPeachGrape 
226ba6e13bSMangoPeachGrape /**
236ba6e13bSMangoPeachGrape  * \brief Compiles a WebAssembly binary into a #wasmtime_component_t
246ba6e13bSMangoPeachGrape  *
256ba6e13bSMangoPeachGrape  * This function will compile a WebAssembly binary into an owned
266ba6e13bSMangoPeachGrape  #wasmtime_component_t.
276ba6e13bSMangoPeachGrape  *
286ba6e13bSMangoPeachGrape  * It requires a component binary, such as what is produced by Rust `cargo
296ba6e13bSMangoPeachGrape  component` tooling.
306ba6e13bSMangoPeachGrape  *
316ba6e13bSMangoPeachGrape  * This function does not take ownership of any of its arguments, but the
326ba6e13bSMangoPeachGrape  * returned error and component are owned by the caller.
336ba6e13bSMangoPeachGrape 
346ba6e13bSMangoPeachGrape  * \param engine the #wasm_engine_t that will create the component
356ba6e13bSMangoPeachGrape  * \param buf the address of the buffer containing the WebAssembly binary
366ba6e13bSMangoPeachGrape  * \param len the length of the buffer containing the WebAssembly binary
376ba6e13bSMangoPeachGrape  * \param component_out on success, contains the address of the created
386ba6e13bSMangoPeachGrape  *        component
396ba6e13bSMangoPeachGrape  *
406ba6e13bSMangoPeachGrape  * \return NULL on success, else a #wasmtime_error_t describing the error
416ba6e13bSMangoPeachGrape  */
426ba6e13bSMangoPeachGrape WASM_API_EXTERN wasmtime_error_t *
436ba6e13bSMangoPeachGrape wasmtime_component_new(const wasm_engine_t *engine, const uint8_t *buf,
446ba6e13bSMangoPeachGrape                        size_t len, wasmtime_component_t **component_out);
456ba6e13bSMangoPeachGrape 
466ba6e13bSMangoPeachGrape /**
476ba6e13bSMangoPeachGrape  * \brief This function serializes compiled component artifacts as blob data.
486ba6e13bSMangoPeachGrape  *
496ba6e13bSMangoPeachGrape  * \param component the component
506ba6e13bSMangoPeachGrape  * \param ret if the conversion is successful, this byte vector is filled in
516ba6e13bSMangoPeachGrape  * with the serialized compiled component.
526ba6e13bSMangoPeachGrape  *
536ba6e13bSMangoPeachGrape  * \return a non-null error if parsing fails, or returns `NULL`. If parsing
546ba6e13bSMangoPeachGrape  * fails then `ret` isn't touched.
556ba6e13bSMangoPeachGrape  *
566ba6e13bSMangoPeachGrape  * This function does not take ownership of `component`, and the caller is
576ba6e13bSMangoPeachGrape  * expected to deallocate the returned #wasmtime_error_t and #wasm_byte_vec_t.
586ba6e13bSMangoPeachGrape  */
596ba6e13bSMangoPeachGrape WASM_API_EXTERN wasmtime_error_t *
606ba6e13bSMangoPeachGrape wasmtime_component_serialize(const wasmtime_component_t *component,
616ba6e13bSMangoPeachGrape                              wasm_byte_vec_t *ret);
626ba6e13bSMangoPeachGrape 
636ba6e13bSMangoPeachGrape #endif // WASMTIME_FEATURE_COMPILER
646ba6e13bSMangoPeachGrape 
656ba6e13bSMangoPeachGrape /**
666ba6e13bSMangoPeachGrape  * \brief Build a component from serialized data.
676ba6e13bSMangoPeachGrape  *
686ba6e13bSMangoPeachGrape  * This function does not take ownership of any of its arguments, but the
696ba6e13bSMangoPeachGrape  * returned error and component are owned by the caller.
706ba6e13bSMangoPeachGrape  *
716ba6e13bSMangoPeachGrape  * This function is not safe to receive arbitrary user input. See the Rust
726ba6e13bSMangoPeachGrape  * documentation for more information on what inputs are safe to pass in here
736ba6e13bSMangoPeachGrape  * (e.g. only that of `wasmtime_component_serialize`)
746ba6e13bSMangoPeachGrape  */
756ba6e13bSMangoPeachGrape WASM_API_EXTERN wasmtime_error_t *
766ba6e13bSMangoPeachGrape wasmtime_component_deserialize(const wasm_engine_t *engine, const uint8_t *buf,
776ba6e13bSMangoPeachGrape                                size_t len,
786ba6e13bSMangoPeachGrape                                wasmtime_component_t **component_out);
796ba6e13bSMangoPeachGrape 
806ba6e13bSMangoPeachGrape /**
816ba6e13bSMangoPeachGrape  * \brief Deserialize a component from an on-disk file.
826ba6e13bSMangoPeachGrape  *
836ba6e13bSMangoPeachGrape  * This function is the same as #wasmtime_component_deserialize except that it
846ba6e13bSMangoPeachGrape  * reads the data for the serialized component from the path on disk. This can
856ba6e13bSMangoPeachGrape  * be faster than the alternative which may require copying the data around.
866ba6e13bSMangoPeachGrape  *
876ba6e13bSMangoPeachGrape  * This function does not take ownership of any of its arguments, but the
886ba6e13bSMangoPeachGrape  * returned error and component are owned by the caller.
896ba6e13bSMangoPeachGrape  *
906ba6e13bSMangoPeachGrape  * This function is not safe to receive arbitrary user input. See the Rust
916ba6e13bSMangoPeachGrape  * documentation for more information on what inputs are safe to pass in here
926ba6e13bSMangoPeachGrape  * (e.g. only that of `wasmtime_component_serialize`)
936ba6e13bSMangoPeachGrape  */
946ba6e13bSMangoPeachGrape WASM_API_EXTERN wasmtime_error_t *
956ba6e13bSMangoPeachGrape wasmtime_component_deserialize_file(const wasm_engine_t *engine,
966ba6e13bSMangoPeachGrape                                     const char *path,
976ba6e13bSMangoPeachGrape                                     wasmtime_component_t **component_out);
986ba6e13bSMangoPeachGrape 
996ba6e13bSMangoPeachGrape /**
1006ba6e13bSMangoPeachGrape  * \brief Creates a shallow clone of the specified component, increasing the
1016ba6e13bSMangoPeachGrape  * internal reference count.
1026ba6e13bSMangoPeachGrape  */
1036ba6e13bSMangoPeachGrape WASM_API_EXTERN wasmtime_component_t *
1046ba6e13bSMangoPeachGrape wasmtime_component_clone(const wasmtime_component_t *component);
1056ba6e13bSMangoPeachGrape 
1066ba6e13bSMangoPeachGrape /**
107*39ec36caSAlex Crichton  * \brief Returns the type of this component.
108*39ec36caSAlex Crichton  *
109*39ec36caSAlex Crichton  * The returned pointer must be deallocatd with
110*39ec36caSAlex Crichton  * `wasmtime_component_type_delete`.
111*39ec36caSAlex Crichton  */
112*39ec36caSAlex Crichton WASM_API_EXTERN wasmtime_component_type_t *
113*39ec36caSAlex Crichton wasmtime_component_type(const wasmtime_component_t *component);
114*39ec36caSAlex Crichton 
115*39ec36caSAlex Crichton /**
116b2c64de1SMangoPeachGrape  * \brief Deletes a #wasmtime_component_t created by #wasmtime_component_new
1176ba6e13bSMangoPeachGrape  *
1186ba6e13bSMangoPeachGrape  * \param component the component to delete
1196ba6e13bSMangoPeachGrape  */
1206ba6e13bSMangoPeachGrape WASM_API_EXTERN void wasmtime_component_delete(wasmtime_component_t *component);
1216ba6e13bSMangoPeachGrape 
122b2c64de1SMangoPeachGrape /// A value which represents a known export of a component.
123ce3c1a72SMangoPeachGrape typedef struct wasmtime_component_export_index_t
124ce3c1a72SMangoPeachGrape     wasmtime_component_export_index_t;
125ce3c1a72SMangoPeachGrape 
126ce3c1a72SMangoPeachGrape /**
127ce3c1a72SMangoPeachGrape  * \brief Looks up a specific export of this component by \p name optionally
128ce3c1a72SMangoPeachGrape  * nested within the \p instance provided.
129ce3c1a72SMangoPeachGrape  *
130ce3c1a72SMangoPeachGrape  * \param component the component to look up \p name in
131ce3c1a72SMangoPeachGrape  * \param instance_export_index optional (i.e. nullable) instance to look up in
132ce3c1a72SMangoPeachGrape  * \param name the name of the export
133ce3c1a72SMangoPeachGrape  * \param name_len length of \p name in bytes
134ce3c1a72SMangoPeachGrape  * \return export index if found, else NULL
135ce3c1a72SMangoPeachGrape  */
136ce3c1a72SMangoPeachGrape WASM_API_EXTERN wasmtime_component_export_index_t *
137ce3c1a72SMangoPeachGrape wasmtime_component_get_export_index(
138ce3c1a72SMangoPeachGrape     const wasmtime_component_t *component,
139ce3c1a72SMangoPeachGrape     const wasmtime_component_export_index_t *instance_export_index,
140ce3c1a72SMangoPeachGrape     const char *name, size_t name_len);
141ce3c1a72SMangoPeachGrape 
142ce3c1a72SMangoPeachGrape /**
143579ec46bSAlex Crichton  * \brief Creates a new separately-owned copy of the specified index.
144579ec46bSAlex Crichton  */
145579ec46bSAlex Crichton WASM_API_EXTERN wasmtime_component_export_index_t *
146579ec46bSAlex Crichton wasmtime_component_export_index_clone(
147579ec46bSAlex Crichton     const wasmtime_component_export_index_t *index);
148579ec46bSAlex Crichton 
149579ec46bSAlex Crichton /**
150ce3c1a72SMangoPeachGrape  * \brief Deletes a #wasmtime_component_export_index_t
151ce3c1a72SMangoPeachGrape  *
152ce3c1a72SMangoPeachGrape  * \param export_index the export index to delete
153ce3c1a72SMangoPeachGrape  */
154ce3c1a72SMangoPeachGrape WASM_API_EXTERN void wasmtime_component_export_index_delete(
155ce3c1a72SMangoPeachGrape     wasmtime_component_export_index_t *export_index);
156ce3c1a72SMangoPeachGrape 
1576ba6e13bSMangoPeachGrape #ifdef __cplusplus
1586ba6e13bSMangoPeachGrape } // extern "C"
1596ba6e13bSMangoPeachGrape #endif
1606ba6e13bSMangoPeachGrape 
1616ba6e13bSMangoPeachGrape #endif // WASMTIME_FEATURE_COMPONENT_MODEL
1626ba6e13bSMangoPeachGrape 
1636ba6e13bSMangoPeachGrape #endif // WASMTIME_COMPONENT_COMPONENT_H
164