Wasmtime
extern.hh
Go to the documentation of this file.
1
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
16namespace wasmtime {
17
18// Internal helpers for converting between `Extern`, a `std::variant`, and
19// `wasmtime_extern_t`.
20namespace detail {
21static Extern cvt_extern(wasmtime_extern_t &e) {
22 switch (e.kind) {
24 return Func(e.of.func);
26 return Global(e.of.global);
28 return Memory(e.of.memory);
30 return Table(e.of.table);
32 return Tag(e.of.tag);
33 }
34 std::abort();
35}
36
37static void cvt_extern(const Extern &e, wasmtime_extern_t &raw) {
38 if (const auto *func = std::get_if<Func>(&e)) {
40 raw.of.func = func->capi();
41 } else if (const auto *global = std::get_if<Global>(&e)) {
43 raw.of.global = global->capi();
44 } else if (const auto *table = std::get_if<Table>(&e)) {
46 raw.of.table = table->capi();
47 } else if (const auto *memory = std::get_if<Memory>(&e)) {
49 raw.of.memory = memory->capi();
50 } else if (const auto *tag = std::get_if<Tag>(&e)) {
52 raw.of.tag = tag->capi();
53 } else {
54 std::abort();
55 }
56}
57} // namespace detail
58
59inline std::optional<Extern> Caller::get_export(std::string_view name) {
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
std::optional< Extern > get_export(std::string_view name)
Definition: extern.hh:59
Definition of wasmtime_extern_t and external items.
#define WASMTIME_EXTERN_TABLE
Value of wasmtime_extern_kind_t meaning that wasmtime_extern_t is a table.
Definition: extern.h:102
#define WASMTIME_EXTERN_GLOBAL
Value of wasmtime_extern_kind_t meaning that wasmtime_extern_t is a global.
Definition: extern.h:99
#define WASMTIME_EXTERN_FUNC
Value of wasmtime_extern_kind_t meaning that wasmtime_extern_t is a function.
Definition: extern.h:96
#define WASMTIME_EXTERN_TAG
Value of wasmtime_extern_kind_t meaning that wasmtime_extern_t is a tag.
Definition: extern.h:111
#define WASMTIME_EXTERN_MEMORY
Value of wasmtime_extern_kind_t meaning that wasmtime_extern_t is a memory.
Definition: extern.h:105
WASM_API_EXTERN bool wasmtime_caller_export_get(wasmtime_caller_t *caller, const char *name, size_t name_len, wasmtime_extern_t *item)
Loads a wasmtime_extern_t from the caller's context.
Container for different kinds of extern items.
Definition: extern.h:151
wasmtime_extern_union_t of
Container for the extern item's value.
Definition: extern.h:155
wasmtime_extern_kind_t kind
Discriminant of which field of of is valid.
Definition: extern.h:153
wasmtime_memory_t memory
Field used if wasmtime_extern_t::kind is WASMTIME_EXTERN_MEMORY.
Definition: extern.h:131
wasmtime_func_t func
Field used if wasmtime_extern_t::kind is WASMTIME_EXTERN_FUNC.
Definition: extern.h:125
wasmtime_global_t global
Field used if wasmtime_extern_t::kind is WASMTIME_EXTERN_GLOBAL.
Definition: extern.h:127
wasmtime_table_t table
Field used if wasmtime_extern_t::kind is WASMTIME_EXTERN_TABLE.
Definition: extern.h:129
wasmtime_tag_t tag
Field used if wasmtime_extern_t::kind is WASMTIME_EXTERN_TAG.
Definition: extern.h:135