/** * \file wasmtime/extern.hh */ #ifndef WASMTIME_EXTERN_HH #define WASMTIME_EXTERN_HH #include #include #include #include #include #include #include namespace wasmtime { // Internal helpers for converting between `Extern`, a `std::variant`, and // `wasmtime_extern_t`. namespace detail { static Extern cvt_extern(wasmtime_extern_t &e) { switch (e.kind) { case WASMTIME_EXTERN_FUNC: return Func(e.of.func); case WASMTIME_EXTERN_GLOBAL: return Global(e.of.global); case WASMTIME_EXTERN_MEMORY: return Memory(e.of.memory); case WASMTIME_EXTERN_TABLE: return Table(e.of.table); case WASMTIME_EXTERN_TAG: return Tag(e.of.tag); } std::abort(); } static void cvt_extern(const Extern &e, wasmtime_extern_t &raw) { if (const auto *func = std::get_if(&e)) { raw.kind = WASMTIME_EXTERN_FUNC; raw.of.func = func->capi(); } else if (const auto *global = std::get_if(&e)) { raw.kind = WASMTIME_EXTERN_GLOBAL; raw.of.global = global->capi(); } else if (const auto *table = std::get_if(&e)) { raw.kind = WASMTIME_EXTERN_TABLE; raw.of.table = table->capi(); } else if (const auto *memory = std::get_if(&e)) { raw.kind = WASMTIME_EXTERN_MEMORY; raw.of.memory = memory->capi(); } else if (const auto *tag = std::get_if(&e)) { raw.kind = WASMTIME_EXTERN_TAG; raw.of.tag = tag->capi(); } else { std::abort(); } } } // namespace detail inline std::optional Caller::get_export(std::string_view name) { wasmtime_extern_t item; if (wasmtime_caller_export_get(ptr, name.data(), name.size(), &item)) { return detail::cvt_extern(item); } return std::nullopt; } } // namespace wasmtime #endif // WASMTIME_EXTERN_HH