refactor: store raw bytes for stdin

This commit is contained in:
Erica Marigold 2023-09-29 18:50:01 +05:30
parent 224463da3a
commit 33730e1147
No known key found for this signature in database
GPG key ID: 7843994FD1386E35
2 changed files with 5 additions and 8 deletions

View file

@ -203,7 +203,7 @@ async fn spawn_command(
options: ProcessSpawnOptions,
) -> LuaResult<(ExitStatus, Vec<u8>, Vec<u8>)> {
let inherit_stdio = options.inherit_stdio;
let stdin = options.stdin.clone();
let stdin = options.stdin;
let mut child = options
.into_command(program, args)
@ -218,10 +218,7 @@ async fn spawn_command(
// If the stdin option was provided, we write that to the child
if let Some(stdin) = stdin {
let mut child_stdin = child.stdin.take().unwrap();
child_stdin
.write_all(stdin.as_bytes())
.await
.into_lua_err()?;
child_stdin.write_all(stdin).await.into_lua_err()?;
}
if inherit_stdio {

View file

@ -14,7 +14,7 @@ pub struct ProcessSpawnOptions {
pub(crate) envs: HashMap<String, String>,
pub(crate) shell: Option<String>,
pub(crate) inherit_stdio: bool,
pub(crate) stdin: Option<String>,
pub(crate) stdin: Option<&'static [u8]>,
}
impl<'lua> FromLua<'lua> for ProcessSpawnOptions {
@ -138,8 +138,8 @@ impl<'lua> FromLua<'lua> for ProcessSpawnOptions {
If we have stdin contents, we need to pass those to the child process
*/
match value.get("stdin")? {
LuaValue::Nil => {},
LuaValue::String(s) => this.stdin = Some(s.to_string_lossy().to_string()),
LuaValue::Nil => {}
LuaValue::String(s) => this.stdin = Some(&*(s.as_bytes().to_vec().leak())),
value => {
return Err(LuaError::RuntimeError(format!(
"Invalid type for option 'stdin' - expected 'string', got '{}'",