Skip to content

Commit

Permalink
Merge branch 'main' into feature/jit-toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
CompeyDev authored Oct 17, 2024
2 parents c70769c + 138221b commit eedcc1e
Show file tree
Hide file tree
Showing 43 changed files with 547 additions and 224 deletions.
10 changes: 2 additions & 8 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ jobs:
with:
components: rustfmt

- name: Install Just
uses: extractions/setup-just@v2

- name: Install Tooling
uses: ok-nick/setup-aftman@v0.4.2
uses: CompeyDev/setup-rokit@v0.1.0

- name: Check Formatting
run: just fmt-check
Expand All @@ -40,11 +37,8 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4

- name: Install Just
uses: extractions/setup-just@v2

- name: Install Tooling
uses: ok-nick/setup-aftman@v0.4.2
uses: CompeyDev/setup-rokit@v0.1.0

- name: Analyze
run: just analyze
Expand Down
2 changes: 1 addition & 1 deletion .lune/hello_lune.luau
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ end
]]

print("Sending 4 pings to google 🌏")
local result = process.spawn("ping", {
local result = process.exec("ping", {
"google.com",
"-c 4",
})
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions crates/lune-roblox/src/instance/terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ pub fn add_methods<'lua, M: LuaUserDataMethods<'lua, Instance>>(methods: &mut M)
}

fn get_or_create_material_colors(instance: &Instance) -> MaterialColors {
if let Some(Variant::MaterialColors(material_colors)) = instance.get_property("MaterialColors")
{
material_colors
if let Some(Variant::MaterialColors(inner)) = instance.get_property("MaterialColors") {
inner
} else {
MaterialColors::default()
}
Expand Down
4 changes: 2 additions & 2 deletions crates/lune-std-net/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ async fn net_request(lua: &Lua, config: RequestConfig) -> LuaResult<LuaTable> {
res.await?.into_lua_table(lua)
}

async fn net_socket(lua: &Lua, url: String) -> LuaResult<LuaTable> {
async fn net_socket(lua: &Lua, url: String) -> LuaResult<LuaValue> {
let (ws, _) = tokio_tungstenite::connect_async(url).await.into_lua_err()?;
NetWebSocket::new(ws).into_lua_table(lua)
NetWebSocket::new(ws).into_lua(lua)
}

async fn net_serve<'lua>(
Expand Down
4 changes: 2 additions & 2 deletions crates/lune-std-net/src/server/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ impl Service<Request<Incoming>> for Svc {
lua.spawn_local(async move {
let sock = sock.await.unwrap();
let lua_sock = NetWebSocket::new(sock);
let lua_tab = lua_sock.into_lua_table(&lua_inner).unwrap();
let lua_val = lua_sock.into_lua(&lua_inner).unwrap();

let handler_websocket: LuaFunction =
keys.websocket_handler(&lua_inner).unwrap().unwrap();

lua_inner
.push_thread_back(handler_websocket, lua_tab)
.push_thread_back(handler_websocket, lua_val)
.unwrap();
});

Expand Down
42 changes: 0 additions & 42 deletions crates/lune-std-net/src/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,6 @@ use hyper_tungstenite::{
WebSocketStream,
};

use lune_utils::TableBuilder;

// Wrapper implementation for compatibility and changing colon syntax to dot syntax
const WEB_SOCKET_IMPL_LUA: &str = r#"
return freeze(setmetatable({
close = function(...)
return websocket:close(...)
end,
send = function(...)
return websocket:send(...)
end,
next = function(...)
return websocket:next(...)
end,
}, {
__index = function(self, key)
if key == "closeCode" then
return websocket.closeCode
end
end,
}))
"#;

#[derive(Debug)]
pub struct NetWebSocket<T> {
close_code_exists: Arc<AtomicBool>,
Expand Down Expand Up @@ -125,25 +102,6 @@ where
let mut ws = self.write_stream.lock().await;
ws.close().await.into_lua_err()
}

pub fn into_lua_table(self, lua: &Lua) -> LuaResult<LuaTable> {
let setmetatable = lua.globals().get::<_, LuaFunction>("setmetatable")?;
let table_freeze = lua
.globals()
.get::<_, LuaTable>("table")?
.get::<_, LuaFunction>("freeze")?;

let env = TableBuilder::new(lua)?
.with_value("websocket", self.clone())?
.with_value("setmetatable", setmetatable)?
.with_value("freeze", table_freeze)?
.build_readonly()?;

lua.load(WEB_SOCKET_IMPL_LUA)
.set_name("websocket")
.set_environment(env)
.eval()
}
}

impl<T> LuaUserData for NetWebSocket<T>
Expand Down
3 changes: 3 additions & 0 deletions crates/lune-std-process/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ directories = "5.0"
pin-project = "1.0"
os_str_bytes = { version = "7.0", features = ["conversions"] }

bstr = "1.9"
bytes = "1.6.0"

tokio = { version = "1", default-features = false, features = [
"io-std",
"io-util",
Expand Down
126 changes: 106 additions & 20 deletions crates/lune-std-process/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,33 @@
#![allow(clippy::cargo_common_metadata)]

use std::{
cell::RefCell,
env::{
self,
consts::{ARCH, OS},
},
path::MAIN_SEPARATOR,
process::Stdio,
rc::Rc,
sync::Arc,
};

use mlua::prelude::*;

use lune_utils::TableBuilder;
use mlua_luau_scheduler::{Functions, LuaSpawnExt};
use options::ProcessSpawnOptionsStdio;
use os_str_bytes::RawOsString;
use tokio::io::AsyncWriteExt;
use stream::{ChildProcessReader, ChildProcessWriter};
use tokio::{io::AsyncWriteExt, process::Child, sync::RwLock};

mod options;
mod stream;
mod tee_writer;
mod wait_for_child;

use self::options::ProcessSpawnOptions;
use self::wait_for_child::{wait_for_child, WaitForChildResult};
use self::wait_for_child::wait_for_child;

use lune_utils::path::get_current_dir;

Expand Down Expand Up @@ -73,7 +79,8 @@ pub fn module(lua: &Lua) -> LuaResult<LuaTable> {
.with_value("cwd", cwd_str)?
.with_value("env", env_tab)?
.with_value("exit", process_exit)?
.with_async_function("spawn", process_spawn)?
.with_async_function("exec", process_exec)?
.with_function("create", process_create)?
.build_readonly()
}

Expand Down Expand Up @@ -141,11 +148,16 @@ fn process_env_iter<'lua>(
})
}

async fn process_spawn(
async fn process_exec(
lua: &Lua,
(program, args, options): (String, Option<Vec<String>>, ProcessSpawnOptions),
) -> LuaResult<LuaTable> {
let res = lua.spawn(spawn_command(program, args, options)).await?;
let res = lua
.spawn(async move {
let cmd = spawn_command_with_stdin(program, args, options.clone()).await?;
wait_for_child(cmd, options.stdio.stdout, options.stdio.stderr).await
})
.await?;

/*
NOTE: If an exit code was not given by the child process,
Expand All @@ -168,30 +180,104 @@ async fn process_spawn(
.build_readonly()
}

async fn spawn_command(
#[allow(clippy::await_holding_refcell_ref)]
fn process_create(
lua: &Lua,
(program, args, options): (String, Option<Vec<String>>, ProcessSpawnOptions),
) -> LuaResult<LuaTable> {
// We do not want the user to provide stdio options for process.create,
// so we reset the options, regardless of what the user provides us
let mut spawn_options = options.clone();
spawn_options.stdio = ProcessSpawnOptionsStdio::default();

let (code_tx, code_rx) = tokio::sync::broadcast::channel(4);
let code_rx_rc = Rc::new(RefCell::new(code_rx));

let child = spawn_command(program, args, spawn_options)?;

let child_arc = Arc::new(RwLock::new(child));

let child_arc_clone = Arc::clone(&child_arc);
let mut child_lock = tokio::task::block_in_place(|| child_arc_clone.blocking_write());

let stdin = child_lock.stdin.take().unwrap();
let stdout = child_lock.stdout.take().unwrap();
let stderr = child_lock.stderr.take().unwrap();

let child_arc_inner = Arc::clone(&child_arc);

// Spawn a background task to wait for the child to exit and send the exit code
let status_handle = tokio::spawn(async move {
let res = child_arc_inner.write().await.wait().await;

if let Ok(output) = res {
let code = output.code().unwrap_or_default();

code_tx
.send(code)
.expect("ExitCode receiver was unexpectedly dropped");
}
});

TableBuilder::new(lua)?
.with_value("stdout", ChildProcessReader(stdout))?
.with_value("stderr", ChildProcessReader(stderr))?
.with_value("stdin", ChildProcessWriter(stdin))?
.with_async_function("kill", move |_, ()| {
// First, stop the status task so the RwLock is dropped
status_handle.abort();
let child_arc_clone = Arc::clone(&child_arc);

// Then get another RwLock to write to the child process and kill it
async move { Ok(child_arc_clone.write().await.kill().await?) }
})?
.with_async_function("status", move |lua, ()| {
let code_rx_rc_clone = Rc::clone(&code_rx_rc);
async move {
// Exit code of 9 corresponds to SIGKILL, which should be the only case where
// the receiver gets suddenly dropped
let code = code_rx_rc_clone.borrow_mut().recv().await.unwrap_or(9);

TableBuilder::new(lua)?
.with_value("code", code)?
.with_value("ok", code == 0)?
.build_readonly()
}
})?
.build_readonly()
}

async fn spawn_command_with_stdin(
program: String,
args: Option<Vec<String>>,
mut options: ProcessSpawnOptions,
) -> LuaResult<WaitForChildResult> {
let stdout = options.stdio.stdout;
let stderr = options.stdio.stderr;
) -> LuaResult<Child> {
let stdin = options.stdio.stdin.take();

let mut child = options
.into_command(program, args)
.stdin(if stdin.is_some() {
Stdio::piped()
} else {
Stdio::null()
})
.stdout(stdout.as_stdio())
.stderr(stderr.as_stdio())
.spawn()?;
let mut child = spawn_command(program, args, options)?;

if let Some(stdin) = stdin {
let mut child_stdin = child.stdin.take().unwrap();
child_stdin.write_all(&stdin).await.into_lua_err()?;
}

wait_for_child(child, stdout, stderr).await
Ok(child)
}

fn spawn_command(
program: String,
args: Option<Vec<String>>,
options: ProcessSpawnOptions,
) -> LuaResult<Child> {
let stdout = options.stdio.stdout;
let stderr = options.stdio.stderr;

let child = options
.into_command(program, args)
.stdin(Stdio::piped())
.stdout(stdout.as_stdio())
.stderr(stderr.as_stdio())
.spawn()?;

Ok(child)
}
58 changes: 58 additions & 0 deletions crates/lune-std-process/src/stream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use bstr::BString;
use bytes::BytesMut;
use mlua::prelude::*;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};

const CHUNK_SIZE: usize = 8;

#[derive(Debug, Clone)]
pub struct ChildProcessReader<R: AsyncRead>(pub R);
#[derive(Debug, Clone)]
pub struct ChildProcessWriter<W: AsyncWrite>(pub W);

impl<R: AsyncRead + Unpin> ChildProcessReader<R> {
pub async fn read(&mut self, chunk_size: Option<usize>) -> LuaResult<Vec<u8>> {
let mut buf = BytesMut::with_capacity(chunk_size.unwrap_or(CHUNK_SIZE));
self.0.read_buf(&mut buf).await?;

Ok(buf.to_vec())
}

pub async fn read_to_end(&mut self) -> LuaResult<Vec<u8>> {
let mut buf = vec![];
self.0.read_to_end(&mut buf).await?;

Ok(buf)
}
}

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 {
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_string(this.read_to_end().await?))
});
}
}

impl<W: AsyncWrite + Unpin> ChildProcessWriter<W> {
pub async fn write(&mut self, data: BString) -> LuaResult<()> {
self.0.write_all(data.as_ref()).await?;
Ok(())
}
}

impl<W: AsyncWrite + Unpin + 'static> LuaUserData for ChildProcessWriter<W> {
fn add_methods<'lua, M: LuaUserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_async_method_mut("write", |_, this, data| async { this.write(data).await });
}
}
Loading

0 comments on commit eedcc1e

Please sign in to comment.