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