-
Notifications
You must be signed in to change notification settings - Fork 67
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
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
e0c5902
Add a macro for unwrapping and wrapping Vals
leighmcculloch 245f49b
wip
leighmcculloch e688737
it works
leighmcculloch e13dae3
add todos
leighmcculloch d519703
cleanup
leighmcculloch fbea664
wip
leighmcculloch d007b39
better err
leighmcculloch 0230283
add todo
leighmcculloch af06b16
cleanup
leighmcculloch 3388396
warnings
leighmcculloch 9b78a2c
consistent names
leighmcculloch 66a93a4
comment println
leighmcculloch 9388ff4
add tests
leighmcculloch 02202d3
clean up and tests pass
leighmcculloch 5eb2f9a
remove comments
leighmcculloch 223101d
Fix versions
leighmcculloch 5ac3ed1
remove test_add changes
leighmcculloch 76234e4
add tests for when vals are mixed with natives
leighmcculloch 58a605a
better shorter link name
leighmcculloch 89738f9
add support for more types like i64
leighmcculloch 1e78f33
remove need to import val
leighmcculloch eae5238
remove need for orabort
leighmcculloch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 | ||
|
||
[dependencies] | ||
syn = {version="1.0",features=["full"]} | ||
quote = "1.0" | ||
proc-macro2 = "1.0" |
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,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 | ||
} |
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
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"} |
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,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() | ||
); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.