Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/c-api/tests/component/define_module.cc
1692 views
1
#include "utils.h"
2
3
#include <gtest/gtest.h>
4
#include <wasmtime.h>
5
6
TEST(component, define_module) {
7
static constexpr auto module_wat = std::string_view{
8
R"END(
9
(module
10
(func $function (param $x i32) (result i32)
11
local.get $x)
12
(export "function" (func $function))
13
)
14
)END",
15
};
16
17
static constexpr auto component_text = std::string_view{
18
R"END(
19
(component
20
(import "x:y/z" (instance
21
(export "mod" (core module
22
(export "function" (func (param i32) (result i32)))
23
))
24
))
25
)
26
)END",
27
};
28
const auto engine = wasm_engine_new();
29
EXPECT_NE(engine, nullptr);
30
31
wasm_byte_vec_t wasm;
32
auto err = wasmtime_wat2wasm(module_wat.data(), module_wat.size(), &wasm);
33
CHECK_ERR(err);
34
35
wasmtime_module_t *module = nullptr;
36
err = wasmtime_module_new(
37
engine, reinterpret_cast<const uint8_t *>(wasm.data), wasm.size, &module);
38
CHECK_ERR(err);
39
40
const auto store = wasmtime_store_new(engine, nullptr, nullptr);
41
const auto context = wasmtime_store_context(store);
42
43
wasmtime_component_t *component = nullptr;
44
45
err = wasmtime_component_new(
46
engine, reinterpret_cast<const uint8_t *>(component_text.data()),
47
component_text.size(), &component);
48
49
CHECK_ERR(err);
50
51
const auto linker = wasmtime_component_linker_new(engine);
52
53
const auto root = wasmtime_component_linker_root(linker);
54
55
wasmtime_component_linker_instance_t *x_y_z = nullptr;
56
err = wasmtime_component_linker_instance_add_instance(
57
root, "x:y/z", strlen("x:y/z"), &x_y_z);
58
CHECK_ERR(err);
59
60
err = wasmtime_component_linker_instance_add_module(x_y_z, "mod",
61
strlen("mod"), module);
62
CHECK_ERR(err);
63
64
wasmtime_component_linker_instance_delete(x_y_z);
65
wasmtime_component_linker_instance_delete(root);
66
67
wasmtime_component_instance_t instance = {};
68
err = wasmtime_component_linker_instantiate(linker, context, component,
69
&instance);
70
CHECK_ERR(err);
71
72
wasmtime_component_linker_delete(linker);
73
74
wasmtime_store_delete(store);
75
wasm_engine_delete(engine);
76
}
77
78