Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a macro attribute for auto-unwrapping vals #9

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

members = [
"stellar_contract_sdk",
"stellar_contract_sdk_macros",
"test_macros",
"test_add",
"test_bignum",
"test_call_helper",
Expand Down
7 changes: 7 additions & 0 deletions stellar_contract_sdk_macros/Cargo.lock

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

14 changes: 14 additions & 0 deletions stellar_contract_sdk_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "stellar_contract_sdk_macros"
version = "0.0.1"
authors = ["Stellar Development Foundation <info@stellar.org>"]
license = "Apache-2.0"
edition = "2021"

[lib]
proc-macro = true
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to cause rust-lang/rust#93988 on macos, but probably an insignificant issue.


[dependencies]
syn = {version="1.0",features=["full"]}
quote = "1.0"
proc-macro2 = "1.0"
77 changes: 77 additions & 0 deletions stellar_contract_sdk_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
extern crate proc_macro;

use core::panic;
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use syn::{parse_macro_input, FnArg, ItemFn, Pat, PatType, ReturnType, Type};

#[proc_macro_attribute]
pub fn contractfn(_metadata: TokenStream, input: TokenStream) -> TokenStream {
let func = parse_macro_input!(input as ItemFn);
let sig = &func.sig;
let ident = &sig.ident;
let inputs = &sig.inputs;
let output = &sig.output;

let wrap_link_name = format!("${}", ident);
let wrap_ident = format_ident!("contractfn_{}", ident);
let wrap_inputs = inputs.iter().map(|f| {
if let &FnArg::Typed(pat_type) = &f {
return FnArg::Typed(PatType {
attrs: pat_type.attrs.clone(),
pat: pat_type.pat.clone(),
colon_token: pat_type.colon_token,
ty: Box::new(
Type::Verbatim(
TokenStream::from(quote! {
stellar_contract_sdk::Val
})
.into(),
)
.clone(),
),
});
}
panic!("This macro only accepts functions without a receiver.")
});
let wrap_call_inputs = inputs.iter().map(|f| {
if let &FnArg::Typed(pat_type) = &f {
if let Pat::Ident(pat_ident) = &*pat_type.pat {
let i = &pat_ident.ident;
let ts: TokenStream2 = quote! {
<_ as stellar_contract_sdk::OrAbort>::or_abort(#i.try_into())
}
.into();
return ts;
}
}
panic!("This macro only accepts functions without a receiver.")
});

let ts: TokenStream = match output {
ReturnType::Default => quote! {
#func
#[no_mangle]
#[link_name = #wrap_link_name]
fn #wrap_ident(#(#wrap_inputs),*) -> stellar_contract_sdk::Val {
#ident(#(#wrap_call_inputs),*);
stellar_contract_sdk::Val::from_void()
}
}
.into(),
ReturnType::Type(_, _) => quote! {
#func
#[no_mangle]
#[link_name = #wrap_link_name]
fn #wrap_ident(#(#wrap_inputs),*) -> stellar_contract_sdk::Val {
// TODO: Optimize this so that it is `try_from` for types that
// need it, and simply `from` for types that don't. It would
// remove the or_abort on most conversions.
<_ as stellar_contract_sdk::OrAbort>::or_abort(stellar_contract_sdk::Val::try_from(#ident(#(#wrap_call_inputs),*)))
}
}
.into(),
};
ts
}
14 changes: 14 additions & 0 deletions test_macros/Cargo.lock

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

13 changes: 13 additions & 0 deletions test_macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "test_macros"
version = "0.0.1"
authors = ["Stellar Development Foundation <info@stellar.org>"]
license = "Apache-2.0"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
stellar_contract_sdk = {path = "../stellar_contract_sdk"}
stellar_contract_sdk_macros = {path = "../stellar_contract_sdk_macros"}
123 changes: 123 additions & 0 deletions test_macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
#![no_std]
use sdk::Val;
use stellar_contract_sdk as sdk;
use stellar_contract_sdk_macros as sdkmacros;

#[sdkmacros::contractfn]
fn typed_fn() -> i32 {
return 1;
}

#[sdkmacros::contractfn]
fn typed_fn_one(a: i32) -> i32 {
return a;
}

#[sdkmacros::contractfn]
fn typed_fn_two(a: i32, b: i32) -> i32 {
return a + b;
}

#[sdkmacros::contractfn]
fn typed_fn_val(v: Val, b: i32) -> Val {
return Val::from_i32(v.as_i32() + b);
}

#[sdkmacros::contractfn]
fn typed_fn_try(a: i64) -> i64 {
return a;
}

#[sdkmacros::contractfn]
fn default_fn() {}

#[sdkmacros::contractfn]
fn default_fn_one(_a: i32) {}

#[sdkmacros::contractfn]
fn default_fn_two(_a: i32, _b: i32) {}

#[sdkmacros::contractfn]
fn default_fn_val(_a: Val, _b: i32) {}

#[cfg(test)]
mod test {
use super::{
contractfn_default_fn, contractfn_default_fn_one, contractfn_default_fn_two,
contractfn_default_fn_val, contractfn_typed_fn, contractfn_typed_fn_one,
contractfn_typed_fn_try, contractfn_typed_fn_two, contractfn_typed_fn_val, default_fn,
default_fn_one, default_fn_two, default_fn_val, typed_fn, typed_fn_one, typed_fn_try,
typed_fn_two, typed_fn_val,
};
use sdk::Val;
use stellar_contract_sdk as sdk;

#[test]
fn test_typed_fn() {
assert_eq!(typed_fn(), 1);
assert_eq!(contractfn_typed_fn(), Val::from_i32(1));
}

#[test]
fn test_typed_fn_one() {
assert_eq!(typed_fn_one(2), 2);
assert_eq!(contractfn_typed_fn_one(Val::from_i32(2)), Val::from_i32(2));
}

#[test]
fn test_typed_fn_two() {
assert_eq!(typed_fn_two(2, 4), 6);
assert_eq!(
contractfn_typed_fn_two(Val::from_i32(2), Val::from_i32(4)),
Val::from_i32(6)
);
}

#[test]
fn test_typed_fn_val() {
assert_eq!(typed_fn_val(Val::from_i32(2), 4), Val::from_i32(6));
assert_eq!(
contractfn_typed_fn_val(Val::from_i32(2), Val::from_i32(4)),
Val::from_i32(6)
);
}

#[test]
fn test_typed_fn_try() {
assert_eq!(typed_fn_try(2), 2);
assert_eq!(contractfn_typed_fn_try(Val::from_u63(2)), Val::from_u63(2));
}

#[test]
fn test_default_fn() {
assert_eq!(default_fn(), ());
assert_eq!(contractfn_default_fn(), Val::from_void());
}

#[test]
fn test_default_fn_one() {
assert_eq!(default_fn_one(2), ());
assert_eq!(
contractfn_default_fn_one(Val::from_i32(2)),
Val::from_void()
);
}

#[test]
fn test_default_fn_two() {
assert_eq!(default_fn_two(2, 4), ());
assert_eq!(
contractfn_default_fn_two(Val::from_i32(2), Val::from_i32(4)),
Val::from_void()
);
}

#[test]
fn test_default_fn_val() {
assert_eq!(default_fn_val(Val::from_i32(2), 4), ());
assert_eq!(
contractfn_default_fn_val(Val::from_i32(2), Val::from_i32(4)),
Val::from_void()
);
}
}