2020-03-26 16:12:17 -05:00
|
|
|
//! Handle process life-time and message passing for proc-macro client
|
|
|
|
|
2020-03-26 15:26:34 -05:00
|
|
|
use crossbeam_channel::{bounded, Receiver, Sender};
|
|
|
|
use ra_tt::Subtree;
|
|
|
|
|
2020-03-28 05:25:19 -05:00
|
|
|
use crate::msg::{ErrorCode, Message, Request, Response, ResponseError};
|
2020-03-26 15:26:34 -05:00
|
|
|
use crate::rpc::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask, ProcMacroKind};
|
|
|
|
|
|
|
|
use io::{BufRead, BufReader};
|
|
|
|
use std::{
|
2020-03-28 05:12:51 -05:00
|
|
|
convert::{TryFrom, TryInto},
|
2020-03-26 15:26:34 -05:00
|
|
|
io::{self, Write},
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
process::{Child, Command, Stdio},
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
pub(crate) struct ProcMacroProcessSrv {
|
2020-03-27 00:58:12 -05:00
|
|
|
inner: Option<Sender<Task>>,
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|
|
|
|
|
2020-03-26 23:55:58 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub(crate) struct ProcMacroProcessThread {
|
2020-03-28 07:52:45 -05:00
|
|
|
// XXX: drop order is significant
|
|
|
|
sender: SenderGuard,
|
|
|
|
handle: jod_thread::JoinHandle<()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct SenderGuard(pub Sender<Task>);
|
|
|
|
|
|
|
|
impl std::ops::Drop for SenderGuard {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let _ = self.0.send(Task::Close);
|
|
|
|
}
|
2020-03-26 23:55:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
enum Task {
|
2020-03-28 05:12:51 -05:00
|
|
|
Request { req: Request, result_tx: Sender<Response> },
|
2020-03-26 23:55:58 -05:00
|
|
|
Close,
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Process {
|
|
|
|
path: PathBuf,
|
|
|
|
child: Child,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Process {
|
|
|
|
fn run(process_path: &Path) -> Result<Process, io::Error> {
|
|
|
|
let child = Command::new(process_path.clone())
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()?;
|
|
|
|
|
|
|
|
Ok(Process { path: process_path.into(), child })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn restart(&mut self) -> Result<(), io::Error> {
|
|
|
|
let _ = self.child.kill();
|
|
|
|
self.child =
|
|
|
|
Command::new(self.path.clone()).stdin(Stdio::piped()).stdout(Stdio::piped()).spawn()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stdio(&mut self) -> Option<(impl Write, impl BufRead)> {
|
|
|
|
let stdin = self.child.stdin.take()?;
|
|
|
|
let stdout = self.child.stdout.take()?;
|
|
|
|
let read = BufReader::new(stdout);
|
|
|
|
|
|
|
|
Some((stdin, read))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ProcMacroProcessSrv {
|
2020-03-26 23:55:58 -05:00
|
|
|
pub fn run(
|
|
|
|
process_path: &Path,
|
|
|
|
) -> Result<(ProcMacroProcessThread, ProcMacroProcessSrv), io::Error> {
|
2020-03-26 15:26:34 -05:00
|
|
|
let process = Process::run(process_path)?;
|
|
|
|
|
|
|
|
let (task_tx, task_rx) = bounded(0);
|
2020-03-28 07:52:45 -05:00
|
|
|
let handle = jod_thread::spawn(move || {
|
2020-03-26 15:26:34 -05:00
|
|
|
client_loop(task_rx, process);
|
|
|
|
});
|
2020-03-26 23:55:58 -05:00
|
|
|
|
2020-03-27 00:58:12 -05:00
|
|
|
let srv = ProcMacroProcessSrv { inner: Some(task_tx.clone()) };
|
2020-03-28 07:52:45 -05:00
|
|
|
let thread = ProcMacroProcessThread { handle, sender: SenderGuard(task_tx) };
|
2020-03-26 23:55:58 -05:00
|
|
|
|
|
|
|
Ok((thread, srv))
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_proc_macros(
|
|
|
|
&self,
|
|
|
|
dylib_path: &Path,
|
|
|
|
) -> Result<Vec<(String, ProcMacroKind)>, ra_tt::ExpansionError> {
|
|
|
|
let task = ListMacrosTask { lib: dylib_path.to_path_buf() };
|
|
|
|
|
2020-03-28 05:12:51 -05:00
|
|
|
let result: ListMacrosResult = self.send_task(Request::ListMacro(task))?;
|
2020-03-26 15:26:34 -05:00
|
|
|
Ok(result.macros)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn custom_derive(
|
|
|
|
&self,
|
|
|
|
dylib_path: &Path,
|
|
|
|
subtree: &Subtree,
|
|
|
|
derive_name: &str,
|
|
|
|
) -> Result<Subtree, ra_tt::ExpansionError> {
|
|
|
|
let task = ExpansionTask {
|
|
|
|
macro_body: subtree.clone(),
|
|
|
|
macro_name: derive_name.to_string(),
|
|
|
|
attributes: None,
|
|
|
|
lib: dylib_path.to_path_buf(),
|
|
|
|
};
|
|
|
|
|
2020-03-28 05:12:51 -05:00
|
|
|
let result: ExpansionResult = self.send_task(Request::ExpansionMacro(task))?;
|
2020-03-26 15:26:34 -05:00
|
|
|
Ok(result.expansion)
|
|
|
|
}
|
|
|
|
|
2020-03-28 05:12:51 -05:00
|
|
|
pub fn send_task<R>(&self, req: Request) -> Result<R, ra_tt::ExpansionError>
|
2020-03-26 15:26:34 -05:00
|
|
|
where
|
2020-03-28 05:12:51 -05:00
|
|
|
R: TryFrom<Response, Error = &'static str>,
|
2020-03-26 15:26:34 -05:00
|
|
|
{
|
2020-03-27 00:58:12 -05:00
|
|
|
let sender = match &self.inner {
|
|
|
|
None => return Err(ra_tt::ExpansionError::Unknown("No sender is found.".to_string())),
|
2020-03-26 15:26:34 -05:00
|
|
|
Some(it) => it,
|
|
|
|
};
|
|
|
|
|
|
|
|
let (result_tx, result_rx) = bounded(0);
|
|
|
|
|
2020-03-27 00:58:12 -05:00
|
|
|
sender.send(Task::Request { req: req.into(), result_tx }).map_err(|err| {
|
2020-03-26 23:55:58 -05:00
|
|
|
ra_tt::ExpansionError::Unknown(format!(
|
|
|
|
"Fail to send task in channel, reason : {:#?} ",
|
|
|
|
err
|
|
|
|
))
|
|
|
|
})?;
|
2020-03-26 15:26:34 -05:00
|
|
|
|
2020-03-28 05:12:51 -05:00
|
|
|
let res = result_rx.recv().unwrap();
|
|
|
|
match res {
|
|
|
|
Response::Error(err) => {
|
|
|
|
return Err(ra_tt::ExpansionError::ExpansionError(err.message));
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|
2020-03-28 05:12:51 -05:00
|
|
|
_ => Ok(res.try_into().map_err(|err| {
|
|
|
|
ra_tt::ExpansionError::Unknown(format!(
|
|
|
|
"Fail to get response, reason : {:#?} ",
|
|
|
|
err
|
|
|
|
))
|
|
|
|
})?),
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn client_loop(task_rx: Receiver<Task>, mut process: Process) {
|
|
|
|
let (mut stdin, mut stdout) = match process.stdio() {
|
|
|
|
None => return,
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let task = match task_rx.recv() {
|
|
|
|
Ok(task) => task,
|
|
|
|
Err(_) => break,
|
|
|
|
};
|
|
|
|
|
2020-03-26 23:55:58 -05:00
|
|
|
let (req, result_tx) = match task {
|
|
|
|
Task::Request { req, result_tx } => (req, result_tx),
|
|
|
|
Task::Close => break,
|
|
|
|
};
|
|
|
|
|
2020-03-28 05:12:51 -05:00
|
|
|
let res = match send_request(&mut stdin, &mut stdout, req) {
|
2020-03-26 15:26:34 -05:00
|
|
|
Ok(res) => res,
|
|
|
|
Err(_err) => {
|
2020-03-28 05:12:51 -05:00
|
|
|
let res = Response::Error(ResponseError {
|
|
|
|
code: ErrorCode::ServerErrorEnd,
|
|
|
|
message: "Server closed".into(),
|
|
|
|
});
|
2020-03-26 23:55:58 -05:00
|
|
|
if result_tx.send(res.into()).is_err() {
|
2020-03-26 15:26:34 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Restart the process
|
|
|
|
if process.restart().is_err() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let stdio = match process.stdio() {
|
|
|
|
None => break,
|
|
|
|
Some(it) => it,
|
|
|
|
};
|
|
|
|
stdin = stdio.0;
|
|
|
|
stdout = stdio.1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(res) = res {
|
2020-03-26 23:55:58 -05:00
|
|
|
if result_tx.send(res).is_err() {
|
2020-03-26 15:26:34 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = process.child.kill();
|
|
|
|
}
|
|
|
|
|
2020-03-28 05:12:51 -05:00
|
|
|
fn send_request(
|
2020-03-26 15:26:34 -05:00
|
|
|
mut writer: &mut impl Write,
|
|
|
|
mut reader: &mut impl BufRead,
|
2020-03-28 05:12:51 -05:00
|
|
|
req: Request,
|
|
|
|
) -> Result<Option<Response>, io::Error> {
|
|
|
|
req.write(&mut writer)?;
|
|
|
|
Ok(Response::read(&mut reader)?)
|
2020-03-26 15:26:34 -05:00
|
|
|
}
|