Skip to content

Commit 6c5152e

Browse files
authored
Process (#129)
Adds a basic `process.create` method to create a subprocess. Supports shell, cwd, and env options. See `examples/process.luau` for some usage. ```luau type ProcessOptions = { shell: boolean | string?, cwd: string?, env: { [string]: string }?, } type ProcessResult = { ok: boolean, exitcode: number, stdout: string, stderr: string, signal: string?, } type process = { create: (args: string | {string}, options: ProcessOptions?) -> ProcessResult, } ``` Closes #115
1 parent b5306d0 commit 6c5152e

File tree

6 files changed

+467
-1
lines changed

6 files changed

+467
-1
lines changed

CMakeLists.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_library(Lute.Luau STATIC)
1515
add_library(Lute.Net STATIC)
1616
add_library(Lute.Task STATIC)
1717
add_library(Lute.VM STATIC)
18+
add_library(Lute.Process STATIC)
1819

1920
# luau setup
2021
set(LUAU_BUILD_CLI OFF)
@@ -83,20 +84,23 @@ target_compile_features(Lute.Luau PUBLIC cxx_std_17)
8384
target_compile_features(Lute.Net PUBLIC cxx_std_17)
8485
target_compile_features(Lute.Task PUBLIC cxx_std_17)
8586
target_compile_features(Lute.VM PUBLIC cxx_std_17)
87+
target_compile_features(Lute.Process PUBLIC cxx_std_17)
8688
target_include_directories(Lute.Runtime PUBLIC runtime/include ${LIBUV_INCLUDE_DIR})
8789
target_include_directories(Lute.Fs PUBLIC fs/include ${LIBUV_INCLUDE_DIR})
8890
target_include_directories(Lute.Luau PUBLIC luau/include ${LIBUV_INCLUDE_DIR})
8991
target_include_directories(Lute.Net PUBLIC net/include ${LIBUV_INCLUDE_DIR} ${UWEBSOCKETS_INCLUDE_DIR})
9092
target_include_directories(Lute.Task PUBLIC task/include ${LIBUV_INCLUDE_DIR})
9193
target_include_directories(Lute.VM PUBLIC vm/include ${LIBUV_INCLUDE_DIR})
94+
target_include_directories(Lute.Process PUBLIC process/include ${LIBUV_INCLUDE_DIR})
9295

9396
target_link_libraries(Lute.Runtime PRIVATE Luau.CLI.lib Luau.Compiler Luau.Config Luau.CodeGen Luau.VM uv_a)
9497
target_link_libraries(Lute.Fs PRIVATE Lute.Runtime Luau.VM uv_a)
9598
target_link_libraries(Lute.Luau PRIVATE Lute.Runtime Luau.VM uv_a Luau.Analysis Luau.Ast Luau.Compiler)
9699
target_link_libraries(Lute.Net PRIVATE Lute.Runtime Luau.VM uv_a ${WOLFSSL_LIBRARY} libcurl uWS)
97100
target_link_libraries(Lute.Task PRIVATE Lute.Runtime Luau.VM uv_a)
98101
target_link_libraries(Lute.VM PRIVATE Lute.Runtime Luau.VM uv_a)
99-
target_link_libraries(Lute.CLI PRIVATE Luau.CLI.lib Luau.Compiler Luau.Config Luau.CodeGen Luau.Analysis Luau.VM Lute.Runtime Lute.Fs Lute.Luau Lute.Net Lute.Task Lute.VM)
102+
target_link_libraries(Lute.Process PRIVATE Lute.Runtime Luau.VM uv_a)
103+
target_link_libraries(Lute.CLI PRIVATE Luau.CLI.lib Luau.Compiler Luau.Config Luau.CodeGen Luau.Analysis Luau.VM Lute.Runtime Lute.Fs Lute.Luau Lute.Net Lute.Task Lute.VM Lute.Process)
100104

101105
set(LUTE_OPTIONS)
102106

@@ -119,4 +123,5 @@ target_compile_options(Lute.Luau PRIVATE ${LUTE_OPTIONS})
119123
target_compile_options(Lute.Net PRIVATE ${LUTE_OPTIONS})
120124
target_compile_options(Lute.Task PRIVATE ${LUTE_OPTIONS})
121125
target_compile_options(Lute.VM PRIVATE ${LUTE_OPTIONS})
126+
target_compile_options(Lute.Process PRIVATE ${LUTE_OPTIONS})
122127
target_compile_options(Lute.CLI PRIVATE ${LUTE_OPTIONS})

Sources.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ target_sources(Lute.VM PRIVATE
4242
vm/src/vm.cpp
4343
)
4444

45+
target_sources(Lute.Process PRIVATE
46+
process/include/lute/process.h
47+
48+
process/src/process.cpp
49+
)
50+
4551
target_sources(Lute.CLI PRIVATE
4652
cli/main.cpp
4753
cli/tc.h

cli/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "lute/luau.h"
1313
#include "lute/net.h"
1414
#include "lute/options.h"
15+
#include "lute/process.h"
1516
#include "lute/ref.h"
1617
#include "lute/require.h"
1718
#include "lute/runtime.h"
@@ -61,6 +62,9 @@ lua_State* setupState(Runtime& runtime)
6162
luteopen_net(L);
6263
lua_setfield(L, -2, "@lute/net");
6364

65+
luteopen_process(L);
66+
lua_setfield(L, -2, "@lute/process");
67+
6468
luteopen_task(L);
6569
lua_setfield(L, -2, "@lute/task");
6670

examples/process.luau

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
local process = require("@lute/process")
2+
local task = require("@std/task")
3+
4+
local result = process.create({ "echo", "Hello, lute!" })
5+
6+
print(result.exitcode)
7+
print(result.stdout)
8+
9+
local t1 = task.create(process.create, { "echo", "-n", "One" })
10+
local t2 = task.create(process.create, { "echo", "-n", "Two" })
11+
local t3 = task.create(process.create, { "echo", "-n", "Three" })
12+
13+
local r1, r2, r3 = task.awaitall(t1, t2, t3)
14+
15+
print(r1.stdout)
16+
print(r2.stdout)
17+
print(r3.stdout)
18+
19+
local r4 = process.create("echo Hello, lute!", { shell = true })
20+
print(r4.stdout)
21+
22+
local r5 = process.create({ "echo", "$HOME" }, { env = { HOME = "/home/lute" }, shell = true })
23+
print(r5.stdout)
24+
25+
local r6 = process.create({ "pwd" }, { cwd = "/" })
26+
print(r6.stdout)
27+
28+
local r7 = process.create("echo $0", { shell = true })
29+
print(r7.stdout)
30+
31+
local r8 = process.create("echo $0", { shell = "/bin/sh" })
32+
print(r8.stdout)

process/include/lute/process.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include "lua.h"
4+
#include "lualib.h"
5+
6+
// open the library as a standard global luau library
7+
int luaopen_process(lua_State* L);
8+
// open the library as a table on top of the stack
9+
int luteopen_process(lua_State* L);
10+
11+
namespace process
12+
{
13+
14+
int create(lua_State* L);
15+
16+
static const luaL_Reg lib[] = {
17+
{"create", create},
18+
{nullptr, nullptr}
19+
};
20+
21+
} // namespace process

0 commit comments

Comments
 (0)