mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-17 01:28:44 +00:00
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:
@@ -23,13 +23,12 @@ use wasmer_wasi::{
|
|||||||
WasiStateBuilder, WasiVersion,
|
WasiStateBuilder, WasiVersion,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug)]
|
||||||
#[allow(non_camel_case_types)]
|
#[allow(non_camel_case_types)]
|
||||||
pub struct wasi_config_t {
|
pub struct wasi_config_t {
|
||||||
inherit_stdout: bool,
|
inherit_stdout: bool,
|
||||||
inherit_stderr: bool,
|
inherit_stderr: bool,
|
||||||
inherit_stdin: bool,
|
inherit_stdin: bool,
|
||||||
/// cbindgen:ignore
|
|
||||||
state_builder: WasiStateBuilder,
|
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());
|
let prog_name = c_try!(name_c_str.to_str());
|
||||||
|
|
||||||
Some(Box::new(wasi_config_t {
|
Some(Box::new(wasi_config_t {
|
||||||
|
inherit_stdout: true,
|
||||||
|
inherit_stderr: true,
|
||||||
|
inherit_stdin: true,
|
||||||
state_builder: WasiState::new(prog_name),
|
state_builder: WasiState::new(prog_name),
|
||||||
..wasi_config_t::default()
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,14 +133,31 @@ pub unsafe extern "C" fn wasi_config_mapdir(
|
|||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn wasi_config_capture_stdout(config: &mut wasi_config_t) {
|
||||||
|
config.inherit_stdout = false;
|
||||||
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn wasi_config_inherit_stdout(config: &mut wasi_config_t) {
|
pub extern "C" fn wasi_config_inherit_stdout(config: &mut wasi_config_t) {
|
||||||
config.inherit_stdout = true;
|
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]
|
#[no_mangle]
|
||||||
pub extern "C" fn wasi_config_inherit_stderr(config: &mut wasi_config_t) {
|
pub extern "C" fn wasi_config_inherit_stderr(config: &mut wasi_config_t) {
|
||||||
config.inherit_stderr = true;
|
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]
|
#[no_mangle]
|
||||||
pub extern "C" fn wasi_config_inherit_stdin(config: &mut wasi_config_t) {
|
pub extern "C" fn wasi_config_inherit_stdin(config: &mut wasi_config_t) {
|
||||||
config.inherit_stdin = true;
|
config.inherit_stdin = true;
|
||||||
@@ -154,18 +172,22 @@ pub struct wasi_env_t {
|
|||||||
/// Takes ownership over the `wasi_config_t`.
|
/// Takes ownership over the `wasi_config_t`.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn wasi_env_new(mut config: Box<wasi_config_t>) -> Option<Box<wasi_env_t>> {
|
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
|
config
|
||||||
.state_builder
|
.state_builder
|
||||||
.stdout(Box::new(capture_files::OutputCapturer::new()));
|
.stdout(Box::new(capture_files::OutputCapturer::new()));
|
||||||
}
|
}
|
||||||
if config.inherit_stderr {
|
|
||||||
|
if !config.inherit_stderr {
|
||||||
config
|
config
|
||||||
.state_builder
|
.state_builder
|
||||||
.stderr(Box::new(capture_files::OutputCapturer::new()));
|
.stderr(Box::new(capture_files::OutputCapturer::new()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: impl capturer for stdin
|
// TODO: impl capturer for stdin
|
||||||
|
|
||||||
let wasi_state = c_try!(config.state_builder.build());
|
let wasi_state = c_try!(config.state_builder.build());
|
||||||
|
|
||||||
Some(Box::new(wasi_env_t {
|
Some(Box::new(wasi_env_t {
|
||||||
inner: WasiEnv::new(wasi_state),
|
inner: WasiEnv::new(wasi_state),
|
||||||
}))
|
}))
|
||||||
|
|||||||
Reference in New Issue
Block a user