Skip to content

Commit

Permalink
Add DefinedMacros handler
Browse files Browse the repository at this point in the history
commit-id:7587de32
  • Loading branch information
Draggu committed Oct 24, 2024
1 parent b782912 commit 6853b12
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
9 changes: 9 additions & 0 deletions scarb/src/compiler/plugin/proc_macro/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ impl ProcMacroInstance {
.collect()
}

pub fn inline_macros(&self) -> Vec<String> {
self.get_expansions()
.iter()
.filter(|e| e.kind == ExpansionKind::Inline)
.map(|e| e.name.clone())
.map(Into::into)
.collect()
}

/// Apply expansion to token stream.
///
/// This function implements the actual calls to functions from the dynamic library.
Expand Down
4 changes: 4 additions & 0 deletions scarb/src/compiler/plugin/proc_macro/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,4 +1101,8 @@ impl ProcMacroHost {
pub fn into_plugin(self) -> Result<ProcMacroHostPlugin> {
ProcMacroHostPlugin::try_new(self.macros)
}

pub fn macros(&self) -> &[Arc<ProcMacroInstance>] {
&self.macros
}
}
8 changes: 8 additions & 0 deletions scarb/src/ops/proc_macro_server/json_rpc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
use crate::compiler::plugin::proc_macro::ProcMacroHost;
use anyhow::Result;
use proc_macro_server_api::Method;
use serde::Serialize;
use std::sync::Arc;

pub trait Handler: Method {
fn handle(proc_macros: Arc<ProcMacroHost>, params: Self::Params) -> Result<Self::Response>;
}

#[derive(Serialize)]
pub struct ErrResponse {
Expand Down
27 changes: 27 additions & 0 deletions scarb/src/ops/proc_macro_server/methods/defined_macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use crate::{
compiler::plugin::proc_macro::ProcMacroHost, ops::proc_macro_server::json_rpc::Handler,
};
use anyhow::Result;
use proc_macro_server_api::methods::defined_macros::{DefinedMacros, DefinedMacrosResponse};
use std::sync::Arc;

impl Handler for DefinedMacros {
fn handle(proc_macros: Arc<ProcMacroHost>, _params: Self::Params) -> Result<Self::Response> {
let mut response: DefinedMacrosResponse = proc_macros
.macros()
.into_iter()
.map(|e| DefinedMacrosResponse {
attributes: e.declared_attributes(),
inline_macros: e.inline_macros(),
derives: e.declared_derives(),
executables: e.executable_attributes(),
})
.sum();

response
.attributes
.retain(|attr| !response.executables.contains(attr));

Ok(response)
}
}
1 change: 1 addition & 0 deletions scarb/src/ops/proc_macro_server/methods/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod defined_macros;
18 changes: 15 additions & 3 deletions scarb/src/ops/proc_macro_server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
use crate::compiler::plugin::proc_macro::ProcMacroHost;
use anyhow::{anyhow, Result};
use connection::Connection;
use json_rpc::ErrResponse;
use proc_macro_server_api::RpcResponse;
use json_rpc::{ErrResponse, Handler};
use proc_macro_server_api::{methods::defined_macros::DefinedMacros, Method, RpcResponse};
use serde_json::Value;
use std::sync::Arc;

mod connection;
mod json_rpc;
mod methods;

pub fn start_proc_macro_server(proc_macros: ProcMacroHost) -> Result<()> {
let connection = Connection::new();
let proc_macros = Arc::new(proc_macros);

for i in 0..4 {
let receiver = connection.receiver.clone();
let sender = connection.sender.clone();
let proc_macros = proc_macros.clone();

std::thread::Builder::new()
.name(format!("proc-macro-server-worker-thread-{i}"))
.spawn(move || {
for request in receiver {
let response = match request.method.as_str() {
//TODO add method handlers
DefinedMacros::METHOD => {
run_handler::<DefinedMacros>(proc_macros.clone(), request.value)
}
_ => Err(anyhow!("method not found")),
};

Expand All @@ -41,3 +48,8 @@ pub fn start_proc_macro_server(proc_macros: ProcMacroHost) -> Result<()> {

Ok(())
}

fn run_handler<M: Handler>(proc_macros: Arc<ProcMacroHost>, value: Value) -> Result<Value> {
M::handle(proc_macros.clone(), serde_json::from_value(value).unwrap())
.map(|res| serde_json::to_value(res).unwrap())
}

0 comments on commit 6853b12

Please sign in to comment.