@@ -22,6 +22,7 @@ namespace wasmtime {
2222namespace component {
2323
2424class ComponentItem ;
25+ class ComponentExtern ;
2526
2627/* *
2728 * \brief Represents the type of a WebAssembly component.
@@ -35,11 +36,17 @@ class ComponentType {
3536 }
3637
3738 // / Retrieves the import with the specified name.
38- std::optional<ComponentItem> import_get (const Engine &engine,
39- std::string_view name) const ;
39+ // /
40+ // / The returned `ComponentExtern` borrows from this type and the engine and
41+ // / must not outlive either of them.
42+ std::optional<ComponentExtern> import_get (const Engine &engine,
43+ std::string_view name) const ;
4044
4145 // / Retrieves the nth import.
42- std::optional<std::pair<std::string_view, ComponentItem>>
46+ // /
47+ // / The returned `ComponentExtern` borrows from this type and the engine and
48+ // / must not outlive either of them.
49+ std::optional<std::pair<std::string_view, ComponentExtern>>
4350 import_nth (const Engine &engine, size_t nth) const ;
4451
4552 // / Returns the number of exports of this component type.
@@ -48,11 +55,17 @@ class ComponentType {
4855 }
4956
5057 // / Retrieves the export with the specified name.
51- std::optional<ComponentItem> export_get (const Engine &engine,
52- std::string_view name) const ;
58+ // /
59+ // / The returned `ComponentExtern` borrows from this type and the engine and
60+ // / must not outlive either of them.
61+ std::optional<ComponentExtern> export_get (const Engine &engine,
62+ std::string_view name) const ;
5363
5464 // / Retrieves the nth export.
55- std::optional<std::pair<std::string_view, ComponentItem>>
65+ // /
66+ // / The returned `ComponentExtern` borrows from this type and the engine and
67+ // / must not outlive either of them.
68+ std::optional<std::pair<std::string_view, ComponentExtern>>
5669 export_nth (const Engine &engine, size_t nth) const ;
5770};
5871
@@ -170,6 +183,59 @@ public:
170183 const ValType &type () const ;
171184};
172185
186+ /* *
187+ * \brief Full description of a single import or export of a component or a
188+ * component instance.
189+ *
190+ * This carries the type of the item being imported or exported along with any
191+ * `(implements "...")` or `(external-id "...")` annotations attached to it.
192+ *
193+ * Note that this borrows string data from the `ComponentType` or
194+ * `ComponentInstanceType` it was acquired from (and the `Engine` used to
195+ * acquire it), so it must not outlive either of those. Copies of this class
196+ * share the same borrow and are subject to the same restriction.
197+ */
198+ class ComponentExtern {
199+ WASMTIME_CLONE_WRAPPER (ComponentExtern, wasmtime_component_extern);
200+
201+ // / Returns the type of the item that this import/export refers to.
202+ ComponentItem type () const ;
203+
204+ // / Returns the `(implements "...")` annotation of this import/export, if
205+ // / present.
206+ std::optional<std::string_view> implements () const {
207+ size_t len = 0 ;
208+ const char *ptr = wasmtime_component_extern_implements (capi (), &len);
209+ if (ptr == nullptr ) {
210+ return std::nullopt ;
211+ }
212+ return std::string_view (ptr, len);
213+ }
214+
215+ // / Returns whether this import/export has an `(implements "...")`
216+ // / annotation which is compatible with `name`.
217+ // /
218+ // / This returns `false` if there is no `implements` annotation. Otherwise
219+ // / this returns `true` if the annotation is exactly `name` or a
220+ // / semver-compatible version of it, for example `a:b/c@1.1.0` matches
221+ // / `a:b/c@1.0.0` and `a:b/c@1.2.0`.
222+ bool is_implements (std::string_view name) const {
223+ return wasmtime_component_extern_is_implements (capi (), name.data (),
224+ name.size ());
225+ }
226+
227+ // / Returns the `(external-id "...")` annotation of this import/export, if
228+ // / present.
229+ std::optional<std::string_view> external_id () const {
230+ size_t len = 0 ;
231+ const char *ptr = wasmtime_component_extern_external_id (capi (), &len);
232+ if (ptr == nullptr ) {
233+ return std::nullopt ;
234+ }
235+ return std::string_view (ptr, len);
236+ }
237+ };
238+
173239} // namespace component
174240} // namespace wasmtime
175241
@@ -178,60 +244,67 @@ public:
178244#include < wasmtime/component/types/module.hh>
179245#include < wasmtime/component/types/val.hh>
180246
181- inline std::optional<wasmtime::component::ComponentItem>
247+ inline wasmtime::component::ComponentItem
248+ wasmtime::component::ComponentExtern::type () const {
249+ wasmtime_component_item_t item;
250+ wasmtime_component_extern_type (capi (), &item);
251+ return wasmtime::component::ComponentItem (std::move (item));
252+ }
253+
254+ inline std::optional<wasmtime::component::ComponentExtern>
182255wasmtime::component::ComponentType::import_get (const wasmtime::Engine &engine,
183256 std::string_view name) const {
184- wasmtime_component_item_t item ;
185- bool found = wasmtime_component_type_import_get (
186- capi (), engine. capi (), name.data (), name.size (), &item );
257+ wasmtime_component_extern_t *e ;
258+ bool found = wasmtime_component_type_import_get (capi (), engine. capi (),
259+ name.data (), name.size (), &e );
187260 if (!found) {
188261 return std::nullopt ;
189262 }
190- return wasmtime::component::ComponentItem ( std::move (item) );
263+ return wasmtime::component::ComponentExtern (e );
191264}
192265
193266inline std::optional<
194- std::pair<std::string_view, wasmtime::component::ComponentItem >>
267+ std::pair<std::string_view, wasmtime::component::ComponentExtern >>
195268wasmtime::component::ComponentType::import_nth (const wasmtime::Engine &engine,
196269 size_t nth) const {
197- wasmtime_component_item_t item ;
270+ wasmtime_component_extern_t *e ;
198271 const char *name_data;
199272 size_t name_size;
200- bool found = wasmtime_component_type_import_nth (
201- capi (), engine. capi (), nth, &name_data, &name_size, &item );
273+ bool found = wasmtime_component_type_import_nth (capi (), engine. capi (), nth,
274+ &name_data, &name_size, &e );
202275 if (!found) {
203276 return std::nullopt ;
204277 }
205278 return std::make_pair (std::string_view (name_data, name_size),
206- wasmtime::component::ComponentItem ( std::move (item) ));
279+ wasmtime::component::ComponentExtern (e ));
207280}
208281
209- inline std::optional<wasmtime::component::ComponentItem >
282+ inline std::optional<wasmtime::component::ComponentExtern >
210283wasmtime::component::ComponentType::export_get (const wasmtime::Engine &engine,
211284 std::string_view name) const {
212- wasmtime_component_item_t item ;
213- bool found = wasmtime_component_type_export_get (
214- capi (), engine. capi (), name.data (), name.size (), &item );
285+ wasmtime_component_extern_t *e ;
286+ bool found = wasmtime_component_type_export_get (capi (), engine. capi (),
287+ name.data (), name.size (), &e );
215288 if (!found) {
216289 return std::nullopt ;
217290 }
218- return wasmtime::component::ComponentItem ( std::move (item) );
291+ return wasmtime::component::ComponentExtern (e );
219292}
220293
221294inline std::optional<
222- std::pair<std::string_view, wasmtime::component::ComponentItem >>
295+ std::pair<std::string_view, wasmtime::component::ComponentExtern >>
223296wasmtime::component::ComponentType::export_nth (const wasmtime::Engine &engine,
224297 size_t nth) const {
225- wasmtime_component_item_t item ;
298+ wasmtime_component_extern_t *e ;
226299 const char *name_data;
227300 size_t name_size;
228- bool found = wasmtime_component_type_export_nth (
229- capi (), engine. capi (), nth, &name_data, &name_size, &item );
301+ bool found = wasmtime_component_type_export_nth (capi (), engine. capi (), nth,
302+ &name_data, &name_size, &e );
230303 if (!found) {
231304 return std::nullopt ;
232305 }
233306 return std::make_pair (std::string_view (name_data, name_size),
234- wasmtime::component::ComponentItem ( std::move (item) ));
307+ wasmtime::component::ComponentExtern (e ));
235308}
236309
237310inline const wasmtime::component::ComponentInstanceType &
0 commit comments