/**1* \file wasi.h2*3* C API for WASI4*/56#ifndef WASI_H7#define WASI_H89#include "wasm.h"10#include <stdint.h>11#include <wasmtime/conf.h>1213#ifdef WASMTIME_FEATURE_WASI1415#ifndef WASI_API_EXTERN16#ifdef _WIN3217#define WASI_API_EXTERN __declspec(dllimport)18#else19#define WASI_API_EXTERN20#endif21#endif2223#ifdef __cplusplus24extern "C" {25#endif2627#define own2829#define WASI_DECLARE_OWN(name) \30typedef struct wasi_##name##_t wasi_##name##_t; \31WASI_API_EXTERN void wasi_##name##_delete(own wasi_##name##_t *);3233/**34* \typedef wasi_config_t35* \brief Convenience alias for #wasi_config_t36*37* \struct wasi_config_t38* \brief TODO39*40* \fn void wasi_config_delete(wasi_config_t *);41* \brief Deletes a configuration object.42*/43WASI_DECLARE_OWN(config)4445/**46* \brief Creates a new empty configuration object.47*48* The caller is expected to deallocate the returned configuration49*/50WASI_API_EXTERN own wasi_config_t *wasi_config_new();5152/**53* \brief Sets the argv list for this configuration object.54*55* By default WASI programs have an empty argv list, but this can be used to56* explicitly specify what the argv list for the program is.57*58* The arguments are copied into the `config` object as part of this function59* call, so the `argv` pointer only needs to stay alive for this function call.60*61* This function returns `true` if all arguments were registered successfully,62* or `false` if an argument was not valid UTF-8.63*/64WASI_API_EXTERN bool wasi_config_set_argv(wasi_config_t *config, size_t argc,65const char *argv[]);6667/**68* \brief Indicates that the argv list should be inherited from this process's69* argv list.70*/71WASI_API_EXTERN void wasi_config_inherit_argv(wasi_config_t *config);7273/**74* \brief Sets the list of environment variables available to the WASI instance.75*76* By default WASI programs have a blank environment, but this can be used to77* define some environment variables for them.78*79* It is required that the `names` and `values` lists both have `envc` entries.80*81* The env vars are copied into the `config` object as part of this function82* call, so the `names` and `values` pointers only need to stay alive for this83* function call.84*85* This function returns `true` if all environment variables were successfully86* registered. This returns `false` if environment variables are not valid87* UTF-8.88*/89WASI_API_EXTERN bool wasi_config_set_env(wasi_config_t *config, size_t envc,90const char *names[],91const char *values[]);9293/**94* \brief Indicates that the entire environment of the calling process should be95* inherited by this WASI configuration.96*/97WASI_API_EXTERN void wasi_config_inherit_env(wasi_config_t *config);9899/**100* \brief Configures standard input to be taken from the specified file.101*102* By default WASI programs have no stdin, but this configures the specified103* file to be used as stdin for this configuration.104*105* If the stdin location does not exist or it cannot be opened for reading then106* `false` is returned. Otherwise `true` is returned.107*/108WASI_API_EXTERN bool wasi_config_set_stdin_file(wasi_config_t *config,109const char *path);110111/**112* \brief Configures standard input to be taken from the specified113* #wasm_byte_vec_t.114*115* By default WASI programs have no stdin, but this configures the specified116* bytes to be used as stdin for this configuration.117*118* This function takes ownership of the `binary` argument.119*/120WASI_API_EXTERN void wasi_config_set_stdin_bytes(wasi_config_t *config,121wasm_byte_vec_t *binary);122123/**124* \brief Configures this process's own stdin stream to be used as stdin for125* this WASI configuration.126*/127WASI_API_EXTERN void wasi_config_inherit_stdin(wasi_config_t *config);128129/**130* \brief Configures standard output to be written to the specified file.131*132* By default WASI programs have no stdout, but this configures the specified133* file to be used as stdout.134*135* If the stdout location could not be opened for writing then `false` is136* returned. Otherwise `true` is returned.137*/138WASI_API_EXTERN bool wasi_config_set_stdout_file(wasi_config_t *config,139const char *path);140141/**142* \brief Configures this process's own stdout stream to be used as stdout for143* this WASI configuration.144*/145WASI_API_EXTERN void wasi_config_inherit_stdout(wasi_config_t *config);146147/**148* \brief Configures standard output to be directed to \p callback149*150* \param config The config to operate on151* \param callback A non-null callback must be provided, that will get called152* for each write with the buffer. A positive return value indicates the amount153* of bytes written. Negative return values are treated as OS error codes.154* \param data An optional user provided data that will be passed to \p callback155* \param finalizer An optional callback to be called to destroy \p data156*/157WASI_API_EXTERN void wasi_config_set_stdout_custom(158wasi_config_t *config,159ptrdiff_t (*callback)(void *, const unsigned char *, size_t), void *data,160void (*finalizer)(void *));161162/**163* \brief Configures standard output to be written to the specified file.164*165* By default WASI programs have no stderr, but this configures the specified166* file to be used as stderr.167*168* If the stderr location could not be opened for writing then `false` is169* returned. Otherwise `true` is returned.170*/171WASI_API_EXTERN bool wasi_config_set_stderr_file(wasi_config_t *config,172const char *path);173174/**175* \brief Configures this process's own stderr stream to be used as stderr for176* this WASI configuration.177*/178WASI_API_EXTERN void wasi_config_inherit_stderr(wasi_config_t *config);179180/**181* \brief Configures standard error output to be directed to \p callback182*183* \param config The config to operate on184* \param callback A non-null callback must be provided, that will get called185* for each write with the buffer. A positive return value indicates the amount186* of bytes written. Negative return values are treated as OS error codes.187* \param data An optional user provided data that will be passed to \p callback188* \param finalizer An optional callback to be called to destroy \p data189*/190WASI_API_EXTERN void wasi_config_set_stderr_custom(191wasi_config_t *config,192ptrdiff_t (*callback)(void *, const unsigned char *, size_t), void *data,193void (*finalizer)(void *));194195/**196* \brief The permissions granted for a directory when preopening it.197*/198enum wasi_dir_perms_flags {199/**200* \brief This directory can be read, for example its entries can be iterated201*/202WASMTIME_WASI_DIR_PERMS_READ = 1,203204/**205* \brief This directory can be written to, for example new files can be206* created within it.207*/208WASMTIME_WASI_DIR_PERMS_WRITE = 2,209};210211/**212* \brief The permissions granted for directories when preopening them,213* which is a bitmask with flag values from wasi_dir_perms_flags.214*/215typedef size_t wasi_dir_perms;216217/**218* \brief The permissions granted for files when preopening a directory.219*/220enum wasi_file_perms_flags {221/**222* \brief Files can be read.223*/224WASMTIME_WASI_FILE_PERMS_READ = 1,225226/**227* \brief Files can be written to.228*/229WASMTIME_WASI_FILE_PERMS_WRITE = 2,230};231232/**233* \brief The max permissions granted a file within a preopened directory,234* which is a bitmask with flag values from wasi_file_perms_flags.235*/236typedef size_t wasi_file_perms;237238/**239* \brief Configures a "preopened directory" to be available to WASI APIs.240*241* By default WASI programs do not have access to anything on the filesystem.242* This API can be used to grant WASI programs access to a directory on the243* filesystem, but only that directory (its whole contents but nothing above244* it).245*246* The `host_path` argument here is a path name on the host filesystem, and247* `guest_path` is the name by which it will be known in wasm.248*249* The `dir_perms` argument is the permissions that wasm will have to operate on250* `guest_path`. This can be used, for example, to provide readonly access to a251* directory. This argument is a bitmask with the following flag values:252* - WASMTIME_WASI_DIR_PERMS_READ253* - WASMTIME_WASI_DIR_PERMS_WRITE254*255* The `file_perms` argument is similar to `dir_perms` but corresponds to the256* maximum set of permissions that can be used for any file in this directory.257* This argument is a bitmask with the following flag values:258* - WASMTIME_WASI_FILE_PERMS_READ259* - WASMTIME_WASI_FILE_PERMS_WRITE260*/261WASI_API_EXTERN bool wasi_config_preopen_dir(wasi_config_t *config,262const char *host_path,263const char *guest_path,264wasi_dir_perms dir_perms,265wasi_file_perms file_perms);266267#undef own268269#ifdef __cplusplus270} // extern "C"271#endif272273#endif // WASMTIME_FEATURE_WASI274275#endif // #ifdef WASI_H276277278