Skip to content

Commit

Permalink
Merge pull request #95 from byeongkeunahn/add-cargo-fmt
Browse files Browse the repository at this point in the history
Add cargo fmt
  • Loading branch information
byeongkeunahn authored Jun 5, 2024
2 parents afbb0ea + c3d9a88 commit 8af1e04
Show file tree
Hide file tree
Showing 61 changed files with 1,743 additions and 791 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jobs:
run: cargo clippy --all-targets
env:
RUSTFLAGS: "-D warnings -A clippy::missing_safety_doc"
- name: Rustfmt
run: cargo fmt --check --all
- name: Test
if: ${{ matrix.target != 'wasm32-unknown-unknown' && matrix.target != 'x86_64-pc-windows-gnu' }}
run: cargo test --lib -- --test-threads 1
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/build-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ jobs:
run: cargo clippy --all-targets
env:
RUSTFLAGS: "-D warnings -A clippy::missing_safety_doc"
- name: Rustfmt
run: cargo fmt --check --all
- name: Test
if: ${{ matrix.target != 'wasm32-unknown-unknown' }}
run: cargo test --lib -- --test-threads 1
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/build-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
vsversion: 2022
- name: Install Rust toolchain
run: |
rustup toolchain install nightly --target ${{ matrix.target }} --profile minimal --component clippy
rustup toolchain install nightly --target ${{ matrix.target }} --profile default --component clippy
- name: Install node.js
uses: actions/setup-node@v4
with:
Expand All @@ -40,6 +40,8 @@ jobs:
run: cargo clippy --all-targets
env:
RUSTFLAGS: "-D warnings -A clippy::missing_safety_doc"
- name: Rustfmt
run: cargo fmt --check --all
- name: Test
if: ${{ matrix.target != 'wasm32-unknown-unknown' }}
run: cargo test --lib -- --test-threads 1
Expand Down
30 changes: 18 additions & 12 deletions basm-macro/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,33 @@ use proc_macro2::TokenStream;
use quote::quote;
use syn::ItemFn;

use super::types::{TFunction, Mangle};
use super::types::{Mangle, TFunction};

pub fn export_impl(_attr: TokenStream, item: TokenStream) -> TokenStream {
let itemfn: &ItemFn = &syn::parse2(item).unwrap();
let sig = &itemfn.sig;
super::utils::verify_signature(sig);
let tfn = match TFunction::try_from(sig) {
Ok(x) => x,
Err(x) => panic!("{}", x)
Err(x) => panic!("{}", x),
};
let arg_names_anonymous = tfn.arg_names_anonymous();
let arg_borrows = tfn.arg_borrows();
let arg_muts = tfn.arg_muts();
let arg_pure_types: Vec<_> = sig.inputs.iter().map(|x| {
let syn::FnArg::Typed(pattype) = x else { panic!() };
let ty = &*pattype.ty;
match ty {
syn::Type::Reference(x) => &*x.elem,
_ => ty,
}
}).collect();
let arg_pure_types: Vec<_> = sig
.inputs
.iter()
.map(|x| {
let syn::FnArg::Typed(pattype) = x else {
panic!()
};
let ty = &*pattype.ty;
match ty {
syn::Type::Reference(x) => &*x.elem,
_ => ty,
}
})
.collect();
let mangled = tfn.mangle();

let basm_export_mod: TokenStream = ("basm_export_mod_".to_owned() + &mangled).parse().unwrap();
Expand All @@ -39,7 +45,7 @@ pub fn export_impl(_attr: TokenStream, item: TokenStream) -> TokenStream {
#[cfg(target_arch = "x86_64")]
#[inline(never)]
pub unsafe extern "win64" fn free() { SER_VEC.clear() }

#[cfg(not(target_arch = "x86_64"))]
#[inline(never)]
pub unsafe extern "C" fn free() { SER_VEC.clear() }
Expand Down Expand Up @@ -79,4 +85,4 @@ pub fn export_impl(_attr: TokenStream, item: TokenStream) -> TokenStream {
}
};
out
}
}
35 changes: 19 additions & 16 deletions basm-macro/src/import.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Signature, parse::{Parse, ParseStream}, Result, Token};
use syn::{
parse::{Parse, ParseStream},
Result, Signature, Token,
};

use super::types::{TFunction, Mangle};
use super::types::{Mangle, TFunction};

struct VecSignature {
sigs: Vec<Signature>
sigs: Vec<Signature>,
}

impl Parse for VecSignature {
Expand All @@ -24,7 +27,7 @@ fn import_impl_single(sig: &Signature) -> TokenStream {
super::utils::verify_signature(sig);
let tfn = match TFunction::try_from(sig) {
Ok(x) => x,
Err(x) => panic!("{}", x)
Err(x) => panic!("{}", x),
};
let arg_names = tfn.arg_names();
let mangled = tfn.mangle();
Expand All @@ -34,34 +37,36 @@ fn import_impl_single(sig: &Signature) -> TokenStream {
let internals: TokenStream = ("internals_".to_owned() + &mangled).parse().unwrap();
let fn_name = &tfn.ident;
let return_type: TokenStream = match &sig.output {
syn::ReturnType::Default => { "()".parse().unwrap() }
syn::ReturnType::Type(_x, y) => { quote!(#y) }
syn::ReturnType::Default => "()".parse().unwrap(),
syn::ReturnType::Type(_x, y) => {
quote!(#y)
}
};
let out = quote! {
mod #basm_import_mod {
mod #internals {
pub static mut SER_VEC: alloc::vec::Vec::<u8> = alloc::vec::Vec::<u8>::new();
pub static mut PTR_FN: usize = 0;

#[cfg(target_arch = "x86_64")]
#[inline(never)]
pub unsafe extern "win64" fn free() { SER_VEC.clear() }

#[cfg(not(target_arch = "x86_64"))]
#[inline(never)]
pub unsafe extern "C" fn free() { SER_VEC.clear() }

#[cfg(target_arch = "x86_64")]
#[no_mangle]
#[inline(never)]
pub unsafe extern "win64" fn #basm_import(ptr_fn: usize) { PTR_FN = ptr_fn; }

#[cfg(not(target_arch = "x86_64"))]
#[no_mangle]
#[inline(never)]
pub unsafe extern "C" fn #basm_import(ptr_fn: usize) { PTR_FN = ptr_fn; }
}

use super::*;
#[allow(clippy::ptr_arg)]
pub #sig {
Expand All @@ -72,7 +77,7 @@ fn import_impl_single(sig: &Signature) -> TokenStream {
#( #arg_names.ser_len(&mut #internals::SER_VEC, 0); )*
(#internals::free as usize).ser_len(&mut #internals::SER_VEC, 0);
let ptr_serialized = basm_std::serialization::call_import(#internals::PTR_FN, #internals::SER_VEC.as_ptr() as usize);

let mut buf: &'static [u8] = basm_std::serialization::eat(ptr_serialized);
type return_type = #return_type;
let out = return_type::de(&mut buf);
Expand All @@ -91,10 +96,8 @@ fn import_impl_single(sig: &Signature) -> TokenStream {

pub fn import_impl(input: TokenStream) -> TokenStream {
let vecsig: VecSignature = syn::parse2(input).unwrap();
let out = vecsig.sigs.iter().map(|sig| {
import_impl_single(sig)
});
let out = vecsig.sigs.iter().map(import_impl_single);
quote! {
#(#out)*
}
}
}
6 changes: 3 additions & 3 deletions basm-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ extern crate proc_macro2;
extern crate quote;
extern crate syn;

mod utils;
mod types;
mod export;
mod import;
mod types;
mod utils;

use proc_macro::TokenStream;
use syn::parse_macro_input;
Expand All @@ -22,4 +22,4 @@ pub fn basm_export(attr: TokenStream, item: TokenStream) -> TokenStream {
pub fn basm_import(item: TokenStream) -> TokenStream {
let item = parse_macro_input!(item);
import::import_impl(item).into()
}
}
48 changes: 30 additions & 18 deletions basm-macro/src/types/mangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,28 @@ impl Mangle for TInteger {
Self::U32 => "u32",
Self::U64 => "u64",
Self::Usize => "usize",
Self::Bool => "bool"
}.into()
Self::Bool => "bool",
}
.into()
}
}

impl Mangle for TPrimitive {
fn mangle(&self) -> String {
"prim_".to_owned() + &match self {
Self::Integer(sp, ty) => {
match sp {
PtrSpecifier::None => "",
PtrSpecifier::PtrConst => "ptr_",
PtrSpecifier::PtrMut => "ptrmut_"
}.to_owned() + &ty.mangle()
},
Self::String => "string".into(),
Self::Unit => "unit".into()
}
"prim_".to_owned()
+ &match self {
Self::Integer(sp, ty) => {
match sp {
PtrSpecifier::None => "",
PtrSpecifier::PtrConst => "ptr_",
PtrSpecifier::PtrMut => "ptrmut_",
}
.to_owned()
+ &ty.mangle()
}
Self::String => "string".into(),
Self::Unit => "unit".into(),
}
}
}

Expand All @@ -51,7 +55,7 @@ impl Mangle for TBase {
match self {
Self::Prim(x) => x.mangle(),
Self::Pair(x) => x.mangle(),
Self::Vec(x) => x.mangle()
Self::Vec(x) => x.mangle(),
}
}
}
Expand All @@ -61,8 +65,10 @@ impl Mangle for TInput {
match self.borrow {
BorrowSpecifier::None => "",
BorrowSpecifier::BorrowConst => "bor_",
BorrowSpecifier::BorrowMut => "bormut_"
}.to_owned() + &self.ty.mangle()
BorrowSpecifier::BorrowMut => "bormut_",
}
.to_owned()
+ &self.ty.mangle()
}
}

Expand Down Expand Up @@ -93,6 +99,12 @@ impl Mangle for TFunction {
args_mangled.push(x.mangle());
}
let args_mangled_all = args_mangled.join("");
format!("{0}_{1}{2}_{3}", self.ident.mangle(), self.args.len(), args_mangled_all, self.output.mangle())
format!(
"{0}_{1}{2}_{3}",
self.ident.mangle(),
self.args.len(),
args_mangled_all,
self.output.mangle()
)
}
}
}
Loading

0 comments on commit 8af1e04

Please sign in to comment.