feat: return strings and null instead of buffers

This commit is contained in:
Erica Marigold 2024-06-23 19:28:51 +05:30
parent 7ed656cf3e
commit c9cbaf6183
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
3 changed files with 16 additions and 11 deletions

View file

@ -29,11 +29,17 @@ impl<R: AsyncRead + Unpin> ChildProcessReader<R> {
impl<R: AsyncRead + Unpin + 'static> LuaUserData for ChildProcessReader<R> {
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_async_method_mut("read", |lua, this, chunk_size: Option<usize>| async move {
Ok(lua.create_buffer(this.read(chunk_size).await?))
let buf = this.read(chunk_size).await?;
if buf.is_empty() {
return Ok(LuaValue::Nil);
}
Ok(LuaValue::String(lua.create_string(buf)?))
});
methods.add_async_method_mut("readToEnd", |lua, this, ()| async {
Ok(lua.create_buffer(this.read_to_end().await?))
Ok(lua.create_string(this.read_to_end().await?))
});
}
}

View file

@ -7,7 +7,7 @@ local msg = "hello, world"
local catChild = process.spawn("cat")
catChild.stdin:write(msg)
assert(
msg == buffer.tostring(catChild.stdout:read(#msg)),
msg == catChild.stdout:read(#msg),
"Failed to write to stdin or read from stdout of child process"
)
@ -16,6 +16,6 @@ local echoChild = if process.os == "windows"
else process.spawn("echo", { msg, ">>/dev/stderr" }, { shell = true })
assert(
msg == buffer.tostring(echoChild.stderr:read(#msg)),
msg == echoChild.stderr:read(#msg),
"Failed to read from stderr of child process"
)

View file

@ -57,27 +57,26 @@ local ChildProcessReader = {}
@within ChildProcessReader
Reads a chunk of data (specified length or a default of 8 bytes at a time) from
the reader into a buffer. Returns a buffer of size 0 if there is no more data to
read.
the reader as a string. Returns nil if there is no more data to read.
This function may yield until there is new data to read from reader, if all data
till present has already been read, and the process has not exited.
@return The buffer containing the data read from the reader
@return The string containing the data read from the reader
]=]
function ChildProcessReader:read(chunkSize: number?): buffer
function ChildProcessReader:read(chunkSize: number?): string?
return nil :: any
end
--[=[
@within ChildProcessReader
Reads all the data currently present in the reader into a buffer.
Reads all the data currently present in the reader as a string.
This function will yield until the process exits.
@return The buffer containing the data read from the reader
@return The string containing the data read from the reader
]=]
function ChildProcessReader:readToEnd(): buffer
function ChildProcessReader:readToEnd(): string
return nil :: any
end