1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* \file wasmtime/wasi.hh
*/
#ifndef WASMTIME_WASI_HH
#define WASMTIME_WASI_HH
#include <memory>
#include <string>
#include <vector>
#include <wasi.h>
#include <wasmtime/conf.h>
#include <wasmtime/helpers.hh>
#ifdef WASMTIME_FEATURE_WASI
namespace wasmtime {
/**
* \brief Configuration for an instance of WASI.
*
* This is inserted into a store with `Store::Context::set_wasi`.
*/
class WasiConfig {
WASMTIME_OWN_WRAPPER(WasiConfig, wasi_config);
/// Creates a new configuration object with default settings.
WasiConfig() : ptr(wasi_config_new()) {}
/// Configures the argv explicitly with the given string array.
void argv(const std::vector<std::string> &args) {
std::vector<const char *> ptrs;
ptrs.reserve(args.size());
for (const auto &arg : args) {
ptrs.push_back(arg.c_str());
}
wasi_config_set_argv(ptr.get(), (int)args.size(), ptrs.data());
}
/// Configures the argv for wasm to be inherited from this process itself.
void inherit_argv() { wasi_config_inherit_argv(ptr.get()); }
/// Configures the environment variables available to wasm, specified here as
/// a list of pairs where the first element of the pair is the key and the
/// second element is the value.
void env(const std::vector<std::pair<std::string, std::string>> &env) {
std::vector<const char *> names;
std::vector<const char *> values;
for (const auto &[name, value] : env) {
names.push_back(name.c_str());
values.push_back(value.c_str());
}
wasi_config_set_env(ptr.get(), (int)env.size(), names.data(),
values.data());
}
/// Indicates that the entire environment of this process should be inherited
/// by the wasi configuration.
void inherit_env() { wasi_config_inherit_env(ptr.get()); }
/// Configures the provided file to be used for the stdin of this WASI
/// configuration.
[[nodiscard]] bool stdin_file(const std::string &path) {
return wasi_config_set_stdin_file(ptr.get(), path.c_str());
}
/// Configures this WASI configuration to inherit its stdin from the host
/// process.
void inherit_stdin() { return wasi_config_inherit_stdin(ptr.get()); }
/// Configures the provided file to be created and all stdout output will be
/// written there.
[[nodiscard]] bool stdout_file(const std::string &path) {
return wasi_config_set_stdout_file(ptr.get(), path.c_str());
}
/// Configures this WASI configuration to inherit its stdout from the host
/// process.
void inherit_stdout() { return wasi_config_inherit_stdout(ptr.get()); }
/// Configures the provided file to be created and all stderr output will be
/// written there.
[[nodiscard]] bool stderr_file(const std::string &path) {
return wasi_config_set_stderr_file(ptr.get(), path.c_str());
}
/// Configures this WASI configuration to inherit its stdout from the host
/// process.
void inherit_stderr() { return wasi_config_inherit_stderr(ptr.get()); }
/// Opens `path` to be opened as `guest_path` in the WASI pseudo-filesystem.
[[nodiscard]] bool preopen_dir(const std::string &path,
const std::string &guest_path,
size_t dir_perms, size_t file_perms) {
return wasi_config_preopen_dir(ptr.get(), path.c_str(), guest_path.c_str(),
dir_perms, file_perms);
}
};
} // namespace wasmtime
#endif // WASMTIME_FEATURE_WASI
#endif // WASMTIME_WASI_HH