1 /// \file wasmtime/component/instance.hh 2 3 #ifndef WASMTIME_COMPONENT_INSTANCE_HH 4 #define WASMTIME_COMPONENT_INSTANCE_HH 5 6 #include <wasmtime/conf.h> 7 8 #ifdef WASMTIME_FEATURE_COMPONENT_MODEL 9 10 #include <string_view> 11 #include <wasmtime/component/component.hh> 12 #include <wasmtime/component/func.hh> 13 #include <wasmtime/component/instance.h> 14 #include <wasmtime/store.hh> 15 16 namespace wasmtime { 17 namespace component { 18 19 /** 20 * \brief Class representing an instantiated WebAssembly component. 21 */ 22 class Instance { 23 wasmtime_component_instance_t instance; 24 25 public: 26 /// \brief Constructs an Instance from the underlying C API struct. 27 explicit Instance(const wasmtime_component_instance_t &inst) 28 : instance(inst) {} 29 30 /// \brief Looks up an exported item from this instance by name, returning the 31 /// index at which it can be found. 32 /// 33 /// The returned `ExportIndex` references the underlying item within this 34 /// instance which can then be accessed via that index specifically. The 35 /// `instance` provided as an argument to this function is the containing 36 /// export instance, if any, that `name` is looked up under. 37 std::optional<ExportIndex> get_export_index(Store::Context cx, 38 const ExportIndex *instance, 39 std::string_view name) const { 40 wasmtime_component_export_index_t *ret = 41 wasmtime_component_instance_get_export_index( 42 &this->instance, cx.capi(), instance ? instance->capi() : nullptr, 43 name.data(), name.size()); 44 if (ret == nullptr) { 45 return std::nullopt; 46 } 47 return ExportIndex(ret); 48 } 49 50 /// \brief Looks up an exported function by its export index. 51 std::optional<Func> get_func(Store::Context cx, 52 const ExportIndex &index) const { 53 wasmtime_component_func_t ret; 54 bool found = wasmtime_component_instance_get_func(&instance, cx.capi(), 55 index.capi(), &ret); 56 if (!found) 57 return std::nullopt; 58 return Func(ret); 59 } 60 61 /// \brief Returns the underlying C API pointer. 62 const wasmtime_component_instance_t *capi() const { return &instance; } 63 }; 64 65 } // namespace component 66 } // namespace wasmtime 67 68 #endif // WASMTIME_FEATURE_COMPONENT_MODEL 69 70 #endif // WASMTIME_COMPONENT_INSTANCE_H 71