Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/c-api/tests/component/lookup_func.cc
1692 views
1
#include "utils.h"
2
3
#include <gtest/gtest.h>
4
#include <wasmtime.h>
5
6
TEST(component, lookup_func) {
7
static constexpr auto component_text = std::string_view{
8
R"END(
9
(component
10
(core module $m
11
(func (export "f"))
12
)
13
(core instance $i (instantiate $m))
14
(func (export "f")
15
(canon lift (core func $i "f")))
16
)
17
)END",
18
};
19
const auto engine = wasm_engine_new();
20
EXPECT_NE(engine, nullptr);
21
22
const auto store = wasmtime_store_new(engine, nullptr, nullptr);
23
const auto context = wasmtime_store_context(store);
24
25
wasmtime_component_t *component = nullptr;
26
27
auto err = wasmtime_component_new(
28
engine, reinterpret_cast<const uint8_t *>(component_text.data()),
29
component_text.size(), &component);
30
31
CHECK_ERR(err);
32
33
auto f = wasmtime_component_get_export_index(component, nullptr, "ff",
34
strlen("ff"));
35
36
EXPECT_EQ(f, nullptr);
37
38
f = wasmtime_component_get_export_index(component, nullptr, "f", strlen("f"));
39
40
EXPECT_NE(f, nullptr);
41
42
const auto linker = wasmtime_component_linker_new(engine);
43
44
wasmtime_component_instance_t instance = {};
45
err = wasmtime_component_linker_instantiate(linker, context, component,
46
&instance);
47
CHECK_ERR(err);
48
49
wasmtime_component_func_t func = {};
50
const auto found =
51
wasmtime_component_instance_get_func(&instance, context, f, &func);
52
EXPECT_TRUE(found);
53
EXPECT_NE(func.store_id, 0);
54
55
wasmtime_component_export_index_delete(f);
56
57
f = wasmtime_component_instance_get_export_index(&instance, context, nullptr,
58
"f", strlen("f"));
59
EXPECT_NE(f, nullptr);
60
61
wasmtime_component_export_index_delete(f);
62
wasmtime_component_linker_delete(linker);
63
64
wasmtime_store_delete(store);
65
wasm_engine_delete(engine);
66
}
67
68