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