-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement loading shared libraries for proc macro plugins
commit-id:a3155bbf
- Loading branch information
Showing
8 changed files
with
190 additions
and
56 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
use crate::core::Package; | ||
use anyhow::Result; | ||
use cairo_lang_defs::patcher::PatchBuilder; | ||
use cairo_lang_syntax::node::db::SyntaxGroup; | ||
use cairo_lang_syntax::node::{ast, TypedSyntaxNode}; | ||
use camino::Utf8PathBuf; | ||
use libloading::{Library, Symbol}; | ||
use scarb_proc_macro_interface::shared::{FfiProcMacroResult, FfiTokenStream}; | ||
use std::ffi::CString; | ||
use std::fmt::Debug; | ||
|
||
#[cfg(not(windows))] | ||
use libloading::os::unix::Symbol as RawSymbol; | ||
#[cfg(windows)] | ||
use libloading::os::windows::Symbol as RawSymbol; | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct TokenStream(String); | ||
|
||
impl TokenStream { | ||
/// Convert to struct with stable ABI, `FfiTokenStream`. | ||
/// | ||
pub fn to_ffi(&self) -> FfiTokenStream { | ||
let cstring = CString::new(self.0.clone()).expect("CString::new failed"); | ||
FfiTokenStream(cstring.into_raw()) | ||
} | ||
|
||
/// Convert from struct with stable ABI, `FfiTokenStream`. | ||
/// | ||
/// # Safety | ||
pub unsafe fn from_ffi(token_stream: FfiTokenStream) -> Self { | ||
Self(token_stream.to_string()) | ||
} | ||
} | ||
|
||
impl TokenStream { | ||
pub fn from_item_ast(db: &dyn SyntaxGroup, item_ast: ast::ModuleItem) -> Self { | ||
let mut builder = PatchBuilder::new(db); | ||
builder.add_node(item_ast.as_syntax_node()); | ||
let cairo = builder.code.clone(); | ||
Self(cairo) | ||
} | ||
|
||
pub fn collect(self) -> String { | ||
self.0 | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
#[allow(dead_code)] | ||
pub enum ProcMacroResult { | ||
/// Plugin has not taken any action. | ||
Leave, | ||
/// Plugin generated TokenStream replacement. | ||
Replace(TokenStream), | ||
/// Plugin ordered item removal. | ||
Remove, | ||
} | ||
|
||
impl ProcMacroResult { | ||
/// Convert from struct with stable ABI, `FfiProcMacroResult`. | ||
/// | ||
/// # Safety | ||
pub unsafe fn from_ffi(ffi_result: FfiProcMacroResult) -> Self { | ||
match ffi_result { | ||
FfiProcMacroResult::Leave => Self::Leave, | ||
FfiProcMacroResult::Remove => Self::Remove, | ||
FfiProcMacroResult::Replace(token_stream) => { | ||
Self::Replace(TokenStream::from_ffi(token_stream)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
#[non_exhaustive] | ||
pub struct ProcMacroInstance { | ||
plugin: Plugin, | ||
} | ||
|
||
impl Debug for ProcMacroInstance { | ||
fn fmt(&self, _f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
Ok(()) | ||
} | ||
} | ||
|
||
fn shared_lib_path(package: &Package) -> Utf8PathBuf { | ||
let lib_name = format!( | ||
"{}{}.{}", | ||
shared_lib_prefix(), | ||
package.id.name, | ||
shared_lib_ext() | ||
); | ||
package.root().join("target").join("release").join(lib_name) | ||
} | ||
|
||
fn shared_lib_prefix() -> &'static str { | ||
#[cfg(windows)] | ||
return ""; | ||
#[cfg(not(windows))] | ||
return "lib"; | ||
} | ||
|
||
fn shared_lib_ext() -> &'static str { | ||
#[cfg(target_os = "windows")] | ||
return "dll"; | ||
#[cfg(target_os = "macos")] | ||
return "dylib"; | ||
#[cfg(not(target_os = "windows"))] | ||
#[cfg(not(target_os = "macos"))] | ||
return "so"; | ||
} | ||
|
||
impl ProcMacroInstance { | ||
/// Load shared library | ||
pub fn try_new(package: Package) -> Result<Self> { | ||
let plugin = unsafe { Plugin::try_new(shared_lib_path(&package))? }; | ||
Ok(Self { plugin }) | ||
} | ||
|
||
/// Apply expansion to token stream. | ||
pub(crate) fn generate_code(&self, token_stream: TokenStream) -> ProcMacroResult { | ||
let ffi_token_stream = token_stream.to_ffi(); | ||
let result = (self.plugin.vtable.expand)(ffi_token_stream); | ||
unsafe { ProcMacroResult::from_ffi(result) } | ||
} | ||
} | ||
|
||
type ExpandCode = extern "C" fn(FfiTokenStream) -> FfiProcMacroResult; | ||
|
||
struct VTableV0 { | ||
expand: RawSymbol<ExpandCode>, | ||
} | ||
|
||
impl VTableV0 { | ||
unsafe fn try_new(library: &Library) -> Result<VTableV0> { | ||
println!("Loading plugin API version 0..."); | ||
|
||
let expand: Symbol<'_, ExpandCode> = library.get(b"expand\0")?; | ||
let expand = expand.into_raw(); | ||
|
||
Ok(VTableV0 { expand }) | ||
} | ||
} | ||
|
||
struct Plugin { | ||
#[allow(dead_code)] | ||
library: Library, | ||
vtable: VTableV0, | ||
} | ||
|
||
impl Plugin { | ||
unsafe fn try_new(library_name: Utf8PathBuf) -> Result<Plugin> { | ||
let library = Library::new(library_name)?; | ||
let vtable = VTableV0::try_new(&library)?; | ||
|
||
Ok(Plugin { library, vtable }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod ffi; | ||
mod host; | ||
|
||
pub use ffi::*; | ||
pub use host::*; |