Wasmtime
wasi.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_WASI_HH
6#define WASMTIME_WASI_HH
7
8#include <memory>
9#include <string>
10#include <vector>
11#include <wasi.h>
12#include <wasmtime/conf.h>
13#include <wasmtime/helpers.hh>
14
15#ifdef WASMTIME_FEATURE_WASI
16
17namespace wasmtime {
18
25 WASMTIME_OWN_WRAPPER(WasiConfig, wasi_config);
26
28 WasiConfig() : ptr(wasi_config_new()) {}
29
31 void argv(const std::vector<std::string> &args) {
32 std::vector<const char *> ptrs;
33 ptrs.reserve(args.size());
34 for (const auto &arg : args) {
35 ptrs.push_back(arg.c_str());
36 }
37
38 wasi_config_set_argv(ptr.get(), args.size(), ptrs.data());
39 }
40
42 void inherit_argv() { wasi_config_inherit_argv(ptr.get()); }
43
47 void env(const std::vector<std::pair<std::string, std::string>> &env) {
48 std::vector<const char *> names;
49 std::vector<const char *> values;
50 for (const auto &[name, value] : env) {
51 names.push_back(name.c_str());
52 values.push_back(value.c_str());
53 }
54 wasi_config_set_env(ptr.get(), env.size(), names.data(), values.data());
55 }
56
59 void inherit_env() { wasi_config_inherit_env(ptr.get()); }
60
63 [[nodiscard]] bool stdin_file(const std::string &path) {
64 return wasi_config_set_stdin_file(ptr.get(), path.c_str());
65 }
66
69 void inherit_stdin() { wasi_config_inherit_stdin(ptr.get()); }
70
73 [[nodiscard]] bool stdout_file(const std::string &path) {
74 return wasi_config_set_stdout_file(ptr.get(), path.c_str());
75 }
76
79 void inherit_stdout() { wasi_config_inherit_stdout(ptr.get()); }
80
83 [[nodiscard]] bool stderr_file(const std::string &path) {
84 return wasi_config_set_stderr_file(ptr.get(), path.c_str());
85 }
86
89 void inherit_stderr() { wasi_config_inherit_stderr(ptr.get()); }
90
92 [[nodiscard]] bool preopen_dir(const std::string &path,
93 const std::string &guest_path,
94 size_t dir_perms, size_t file_perms) {
95 return wasi_config_preopen_dir(ptr.get(), path.c_str(), guest_path.c_str(),
96 dir_perms, file_perms);
97 }
98
100 void inherit_network() { wasi_config_inherit_network(ptr.get()); }
101
103 void allow_ip_name_lookup(bool enable) {
104 wasi_config_allow_ip_name_lookup(ptr.get(), enable);
105 }
106};
107
108} // namespace wasmtime
109
110#endif // WASMTIME_FEATURE_WASI
111
112#endif // WASMTIME_WASI_HH
Configuration for an instance of WASI.
Definition: wasi.hh:24
Build-time defines for how the C API was built.
void wasi_config_inherit_env(wasi_config_t *config)
Indicates that the entire environment of the calling process should be inherited by this WASI configu...
void wasi_config_inherit_argv(wasi_config_t *config)
Indicates that the argv list should be inherited from this process's argv list.
void wasi_config_inherit_stderr(wasi_config_t *config)
Configures this process's own stderr stream to be used as stderr for this WASI configuration.
bool wasi_config_set_stdout_file(wasi_config_t *config, const char *path)
Configures standard output to be written to the specified file.
void wasi_config_inherit_network(wasi_config_t *config)
Allow all network addresses accessible to the host.
bool wasi_config_set_env(wasi_config_t *config, size_t envc, const char *names[], const char *values[])
Sets the list of environment variables available to the WASI instance.
bool wasi_config_set_argv(wasi_config_t *config, size_t argc, const char *argv[])
Sets the argv list for this configuration object.
void wasi_config_inherit_stdout(wasi_config_t *config)
Configures this process's own stdout stream to be used as stdout for this WASI configuration.
bool wasi_config_set_stderr_file(wasi_config_t *config, const char *path)
Configures standard output to be written to the specified file.
void wasi_config_allow_ip_name_lookup(wasi_config_t *config, bool enable)
Allow usage of wasi:sockets/ip-name-lookup
void wasi_config_inherit_stdin(wasi_config_t *config)
Configures this process's own stdin stream to be used as stdin for this WASI configuration.
bool wasi_config_set_stdin_file(wasi_config_t *config, const char *path)
Configures standard input to be taken from the specified file.
wasi_config_t * wasi_config_new()
Creates a new empty configuration object.
bool wasi_config_preopen_dir(wasi_config_t *config, const char *host_path, const char *guest_path, wasi_dir_perms dir_perms, wasi_file_perms file_perms)
Configures a "preopened directory" to be available to WASI APIs.