Skip to content

Add stdio option to process.run #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions process/src/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ static void allocBuffer(uv_handle_t* handle, size_t suggestedSize, uv_buf_t* buf
}
}

const std::string kStdioKindDefault = "default";
const std::string kStdioKindInherit = "inherit";
const std::string kStdioKindNone = "none";
// TODO: add forwarding
// const std::string kStdioKindForward = "forward";

int run(lua_State* L)
{
std::vector<std::string> args;
Expand Down Expand Up @@ -210,6 +216,7 @@ int run(lua_State* L)
bool useShell = false;
std::string customShell;
std::string cwd;
std::string stdioKind;
std::map<std::string, std::string> env;

if (lua_istable(L, 2))
Expand All @@ -233,6 +240,14 @@ int run(lua_State* L)

lua_pop(L, 1);

lua_getfield(L, 2, "stdio");
if (lua_isstring(L, -1))
{
stdioKind = lua_tostring(L, -1);
// TODO: support stdin and separate stdout/stderr kinds
}
lua_pop(L, 1);

lua_getfield(L, 2, "env");
if (lua_istable(L, -1))
{
Expand Down Expand Up @@ -339,12 +354,31 @@ int run(lua_State* L)

options.stdio_count = 3;
uv_stdio_container_t stdio[3];
stdio[0].flags = UV_INHERIT_FD;
stdio[0].data.fd = 0; // Inherit stdin
stdio[1].flags = static_cast<uv_stdio_flags>(UV_CREATE_PIPE | UV_WRITABLE_PIPE);
stdio[1].data.stream = (uv_stream_t*)&handle->stdoutPipe;
stdio[2].flags = static_cast<uv_stdio_flags>(UV_CREATE_PIPE | UV_WRITABLE_PIPE);
stdio[2].data.stream = (uv_stream_t*)&handle->stderrPipe;
stdio[0].flags = UV_IGNORE;
if (stdioKind == kStdioKindNone)
{
stdio[1].flags = UV_IGNORE;
stdio[2].flags = UV_IGNORE;
}
else if (stdioKind == kStdioKindInherit)
{
stdio[1].flags = UV_INHERIT_FD;
stdio[1].data.fd = fileno(stdout);
stdio[2].flags = UV_INHERIT_FD;
stdio[2].data.fd = fileno(stderr);
}
else if (stdioKind == kStdioKindDefault || stdioKind.empty())
{
stdio[1].flags = static_cast<uv_stdio_flags>(UV_CREATE_PIPE | UV_WRITABLE_PIPE);
stdio[1].data.stream = (uv_stream_t*)&handle->stdoutPipe;
stdio[2].flags = static_cast<uv_stdio_flags>(UV_CREATE_PIPE | UV_WRITABLE_PIPE);
stdio[2].data.stream = (uv_stream_t*)&handle->stderrPipe;
}
else
{
luaL_error(L, "Invalid stdio kind: %s", stdioKind.c_str());
return 0;
}
options.stdio = stdio;

handle->process.data = handle.get();
Expand Down