1 /** 2 * \file wasmtime/extern.hh 3 */ 4 5 #ifndef WASMTIME_EXTERN_HH 6 #define WASMTIME_EXTERN_HH 7 8 #include <wasmtime/extern.h> 9 #include <wasmtime/extern_declare.hh> 10 #include <wasmtime/func.hh> 11 #include <wasmtime/global.hh> 12 #include <wasmtime/memory.hh> 13 #include <wasmtime/table.hh> 14 #include <wasmtime/tag.hh> 15 16 namespace wasmtime { 17 18 // Internal helpers for converting between `Extern`, a `std::variant`, and 19 // `wasmtime_extern_t`. 20 namespace detail { cvt_extern(wasmtime_extern_t & e)21static Extern cvt_extern(wasmtime_extern_t &e) { 22 switch (e.kind) { 23 case WASMTIME_EXTERN_FUNC: 24 return Func(e.of.func); 25 case WASMTIME_EXTERN_GLOBAL: 26 return Global(e.of.global); 27 case WASMTIME_EXTERN_MEMORY: 28 return Memory(e.of.memory); 29 case WASMTIME_EXTERN_TABLE: 30 return Table(e.of.table); 31 case WASMTIME_EXTERN_TAG: 32 return Tag(e.of.tag); 33 } 34 std::abort(); 35 } 36 cvt_extern(const Extern & e,wasmtime_extern_t & raw)37static void cvt_extern(const Extern &e, wasmtime_extern_t &raw) { 38 if (const auto *func = std::get_if<Func>(&e)) { 39 raw.kind = WASMTIME_EXTERN_FUNC; 40 raw.of.func = func->capi(); 41 } else if (const auto *global = std::get_if<Global>(&e)) { 42 raw.kind = WASMTIME_EXTERN_GLOBAL; 43 raw.of.global = global->capi(); 44 } else if (const auto *table = std::get_if<Table>(&e)) { 45 raw.kind = WASMTIME_EXTERN_TABLE; 46 raw.of.table = table->capi(); 47 } else if (const auto *memory = std::get_if<Memory>(&e)) { 48 raw.kind = WASMTIME_EXTERN_MEMORY; 49 raw.of.memory = memory->capi(); 50 } else if (const auto *tag = std::get_if<Tag>(&e)) { 51 raw.kind = WASMTIME_EXTERN_TAG; 52 raw.of.tag = tag->capi(); 53 } else { 54 std::abort(); 55 } 56 } 57 } // namespace detail 58 get_export(std::string_view name)59inline std::optional<Extern> Caller::get_export(std::string_view name) { 60 wasmtime_extern_t item; 61 if (wasmtime_caller_export_get(ptr, name.data(), name.size(), &item)) { 62 return detail::cvt_extern(item); 63 } 64 return std::nullopt; 65 } 66 67 } // namespace wasmtime 68 69 #endif // WASMTIME_EXTERN_HH 70