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
14#ifdef WASMTIME_FEATURE_WASI
15
16namespace wasmtime {
17
24 friend class Store;
25
26 struct deleter {
27 void operator()(wasi_config_t *p) const { wasi_config_delete(p); }
28 };
29
30 std::unique_ptr<wasi_config_t, deleter> ptr;
31
32public:
35
37 void argv(const std::vector<std::string> &args) {
38 std::vector<const char *> ptrs;
39 ptrs.reserve(args.size());
40 for (const auto &arg : args) {
41 ptrs.push_back(arg.c_str());
42 }
43
44 wasi_config_set_argv(ptr.get(), (int)args.size(), ptrs.data());
45 }
46
49
53 void env(const std::vector<std::pair<std::string, std::string>> &env) {
54 std::vector<const char *> names;
55 std::vector<const char *> values;
56 for (const auto &[name, value] : env) {
57 names.push_back(name.c_str());
58 values.push_back(value.c_str());
59 }
60 wasi_config_set_env(ptr.get(), (int)env.size(), names.data(),
61 values.data());
62 }
63
66 void inherit_env() { wasi_config_inherit_env(ptr.get()); }
67
70 [[nodiscard]] bool stdin_file(const std::string &path) {
71 return wasi_config_set_stdin_file(ptr.get(), path.c_str());
72 }
73
76 void inherit_stdin() { return wasi_config_inherit_stdin(ptr.get()); }
77
80 [[nodiscard]] bool stdout_file(const std::string &path) {
81 return wasi_config_set_stdout_file(ptr.get(), path.c_str());
82 }
83
86 void inherit_stdout() { return wasi_config_inherit_stdout(ptr.get()); }
87
90 [[nodiscard]] bool stderr_file(const std::string &path) {
91 return wasi_config_set_stderr_file(ptr.get(), path.c_str());
92 }
93
96 void inherit_stderr() { return wasi_config_inherit_stderr(ptr.get()); }
97
99 [[nodiscard]] bool preopen_dir(const std::string &path,
100 const std::string &guest_path,
101 size_t dir_perms,
102 size_t file_perms) {
103 return wasi_config_preopen_dir(ptr.get(), path.c_str(), guest_path.c_str(), dir_perms, file_perms);
104 }
105};
106
107} // namespace wasmtime
108
109#endif // WASMTIME_FEATURE_WASI
110
111#endif // WASMTIME_WASI_HH
Owner of all WebAssembly objects.
Definition: store.hh:33
Configuration for an instance of WASI.
Definition: wasi.hh:23
void inherit_stderr()
Definition: wasi.hh:96
void inherit_env()
Definition: wasi.hh:66
bool stdin_file(const std::string &path)
Definition: wasi.hh:70
void inherit_stdout()
Definition: wasi.hh:86
bool stderr_file(const std::string &path)
Definition: wasi.hh:90
bool preopen_dir(const std::string &path, const std::string &guest_path, size_t dir_perms, size_t file_perms)
Opens path to be opened as guest_path in the WASI pseudo-filesystem.
Definition: wasi.hh:99
bool stdout_file(const std::string &path)
Definition: wasi.hh:80
void env(const std::vector< std::pair< std::string, std::string > > &env)
Definition: wasi.hh:53
WasiConfig()
Creates a new configuration object with default settings.
Definition: wasi.hh:34
void argv(const std::vector< std::string > &args)
Configures the argv explicitly with the given string array.
Definition: wasi.hh:37
void inherit_argv()
Configures the argv for wasm to be inherited from this process itself.
Definition: wasi.hh:48
void inherit_stdin()
Definition: wasi.hh:76
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.
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.
void wasi_config_delete(wasi_config_t *)
Deletes a configuration object.
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_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.