Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bytecodealliance
GitHub Repository: bytecodealliance/wasmtime
Path: blob/main/crates/c-api/tests/wasip2.cc
3067 views
1
#include <gtest/gtest.h>
2
#include <string_view>
3
#include <wasmtime/component.hh>
4
#include <wasmtime/store.hh>
5
6
using namespace wasmtime::component;
7
8
TEST(wasip2, smoke) {
9
static constexpr auto component_text = std::string_view{
10
R"END(
11
(component
12
(import "wasi:cli/[email protected]" (instance
13
(export "get-arguments" (func (result (list string))))
14
))
15
)
16
)END",
17
};
18
19
wasmtime::Engine engine;
20
wasmtime::Store store(engine);
21
auto context = store.context();
22
23
wasmtime::WasiConfig config;
24
25
wasi_config_set_stdout_custom(
26
config.capi(),
27
[](void *, const unsigned char *buf, size_t len) -> ptrdiff_t {
28
std::cout << std::string_view{(const char *)(buf), len};
29
return len;
30
},
31
nullptr, nullptr);
32
wasi_config_set_stderr_custom(
33
config.capi(),
34
[](void *, const unsigned char *buf, size_t len) -> ptrdiff_t {
35
std::cerr << std::string_view{(const char *)(buf), len};
36
return len;
37
},
38
nullptr, nullptr);
39
40
context.set_wasi(std::move(config)).unwrap();
41
Component component = Component::compile(engine, component_text).unwrap();
42
43
Linker linker(engine);
44
linker.add_wasip2().unwrap();
45
linker.instantiate(context, component).unwrap();
46
}
47
48