#ifndef WASMTIME_EXTERN_HH
#define WASMTIME_EXTERN_HH
#include <wasmtime/extern.h>
#include <wasmtime/extern_declare.hh>
#include <wasmtime/func.hh>
#include <wasmtime/global.hh>
#include <wasmtime/memory.hh>
#include <wasmtime/table.hh>
#include <wasmtime/tag.hh>
namespace wasmtime {
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<Func>(&e)) {
raw.kind = WASMTIME_EXTERN_FUNC;
raw.of.func = func->capi();
} else if (const auto *global = std::get_if<Global>(&e)) {
raw.kind = WASMTIME_EXTERN_GLOBAL;
raw.of.global = global->capi();
} else if (const auto *table = std::get_if<Table>(&e)) {
raw.kind = WASMTIME_EXTERN_TABLE;
raw.of.table = table->capi();
} else if (const auto *memory = std::get_if<Memory>(&e)) {
raw.kind = WASMTIME_EXTERN_MEMORY;
raw.of.memory = memory->capi();
} else if (const auto *tag = std::get_if<Tag>(&e)) {
raw.kind = WASMTIME_EXTERN_TAG;
raw.of.tag = tag->capi();
} else {
std::abort();
}
}
}
inline std::optional<Extern> 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;
}
}
#endif