fix(wasi) Fix the logic behind inherited/captured stdin, stdout and stderr.

First, let's no longer derive from `Default` for `wasi_config_t`. By
default, we want to inherit `stdin`, `stdout` and `stderr`. The
default for `bool` is `false`; we want `true` here.

Second, let's update `wasi_config_new` to correctly set `inherit_*`
fields to `true`.

Third, lets' create `wasi_config_capture_*` functions. By default,
`std*` are inherited, so we need functions to capture them. That's the
new feature this patch introduces. The `wasi_config_inherit_*`
functions are kept for the sake of backward compatibility. Ideally, we
would want an API like `wasi_config_capture_*(capture: bool)`, but it
would duplicate the API somehow.

Fourth, let's fix `wasi_env_new`. We want to capture `stdout` and
`stderr` if and only if the `inherit_*` fields are set to
`false`. There was bug here. That's why everything was working
correctly by the way: `bool::default()` is `false`, and we have this
inverted condition here, so everything was working as expected because
of a double error. The only bug was that it wasn't possible to capture
`std*` before.
This commit is contained in:
Ivan Enderlin
2021-01-26 14:12:48 +01:00
parent fb50d19191
commit 70c31068be

View File

@@ -23,13 +23,12 @@ use wasmer_wasi::{
WasiStateBuilder, WasiVersion,
};
#[derive(Debug, Default)]
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub struct wasi_config_t {
inherit_stdout: bool,
inherit_stderr: bool,
inherit_stdin: bool,
/// cbindgen:ignore
state_builder: WasiStateBuilder,
}
@@ -43,8 +42,10 @@ pub unsafe extern "C" fn wasi_config_new(
let prog_name = c_try!(name_c_str.to_str());
Some(Box::new(wasi_config_t {
inherit_stdout: true,
inherit_stderr: true,
inherit_stdin: true,
state_builder: WasiState::new(prog_name),
..wasi_config_t::default()
}))
}
@@ -132,14 +133,31 @@ pub unsafe extern "C" fn wasi_config_mapdir(
true
}
#[no_mangle]
pub extern "C" fn wasi_config_capture_stdout(config: &mut wasi_config_t) {
config.inherit_stdout = false;
}
#[no_mangle]
pub extern "C" fn wasi_config_inherit_stdout(config: &mut wasi_config_t) {
config.inherit_stdout = true;
}
#[no_mangle]
pub extern "C" fn wasi_config_capture_stderr(config: &mut wasi_config_t) {
config.inherit_stderr = false;
}
#[no_mangle]
pub extern "C" fn wasi_config_inherit_stderr(config: &mut wasi_config_t) {
config.inherit_stderr = true;
}
#[no_mangle]
pub extern "C" fn wasi_config_capture_stdin(config: &mut wasi_config_t) {
config.inherit_stdin = false;
}
#[no_mangle]
pub extern "C" fn wasi_config_inherit_stdin(config: &mut wasi_config_t) {
config.inherit_stdin = true;
@@ -154,18 +172,22 @@ pub struct wasi_env_t {
/// Takes ownership over the `wasi_config_t`.
#[no_mangle]
pub extern "C" fn wasi_env_new(mut config: Box<wasi_config_t>) -> Option<Box<wasi_env_t>> {
if config.inherit_stdout {
if !config.inherit_stdout {
config
.state_builder
.stdout(Box::new(capture_files::OutputCapturer::new()));
}
if config.inherit_stderr {
if !config.inherit_stderr {
config
.state_builder
.stderr(Box::new(capture_files::OutputCapturer::new()));
}
// TODO: impl capturer for stdin
let wasi_state = c_try!(config.state_builder.build());
Some(Box::new(wasi_env_t {
inner: WasiEnv::new(wasi_state),
}))