Skip to content

Commit

Permalink
Support a reference as an entry point arg (#280)
Browse files Browse the repository at this point in the history
* Support a reference as an entry point arg
  • Loading branch information
kpob authored Dec 7, 2023
1 parent cdf2818 commit 49ec530
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 11 deletions.
8 changes: 4 additions & 4 deletions examples2/src/erc20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ impl Erc20 {
});
}

pub fn cross_total(&self, other: Address) -> U256 {
pub fn cross_total(&self, other: &Address) -> U256 {
let other_erc20 = Erc20ContractRef {
address: other,
address: *other,
env: self.env()
};

Expand All @@ -109,10 +109,10 @@ impl Erc20 {
self.env().get_block_time()
}

pub fn burn_and_get_paid(&mut self, amount: U256) {
pub fn burn_and_get_paid(&mut self, amount: &U256) {
let caller = self.env().caller();
let caller_balance = self.balance_of(caller);
if amount > caller_balance {
if *amount > caller_balance {
self.env().revert(Erc20Error::InsufficientBalance)
}

Expand Down
11 changes: 8 additions & 3 deletions odra-macros/src/ast/exec_parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ impl TryFrom<(&'_ ModuleIR, &'_ FnIR)> for ExecFunctionItem {
.then(|| utils::stmt::new_execution_env(&exec_env_ident, &env_rc_ident));
let contract_ident = utils::ident::contract();
let module_ident = module.module_ident()?;
let fn_args = func.arg_names();
let fn_args = func.named_args().iter().map(|arg| {
let ident = arg.name()?;
let ref_token = arg.is_ref().then(|| quote::quote!(&));
let expr: syn::Expr = parse_quote!(#ref_token #ident);
Ok(expr)
}).collect::<Result<syn::punctuated::Punctuated<syn::Expr, syn::token::Comma>, syn::Error>>()?;

let args = func
.arg_names()
Expand All @@ -124,7 +129,7 @@ impl TryFrom<(&'_ ModuleIR, &'_ FnIR)> for ExecFunctionItem {
handle_attached_value_stmt: func.is_payable().then(ExecEnvStmt::handle_attached_value),
args,
init_contract_stmt,
call_contract_stmt: parse_quote!(let #result_ident = #contract_ident.#fn_ident(#(#fn_args),*);),
call_contract_stmt: parse_quote!(let #result_ident = #contract_ident.#fn_ident(#fn_args);),
clear_attached_value_stmt: func.is_payable().then(ExecEnvStmt::clear_attached_value),
non_reentrant_after_stmt: func
.is_non_reentrant()
Expand Down Expand Up @@ -263,7 +268,7 @@ mod test {
let to = exec_env.get_named_arg("to");
let amount = exec_env.get_named_arg("amount");
let mut contract = <Erc20 as odra::Module>::new(env_rc);
let result = contract.approve(to, amount);
let result = contract.approve(&to, &amount);
exec_env.non_reentrant_after();
return result;
}
Expand Down
4 changes: 2 additions & 2 deletions odra-macros/src/ast/ref_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn call_def_with_amount(fun: &FnIR) -> syn::Expr {

fn function_signature(fun: &FnIR) -> syn::Signature {
let fun_name = fun.name();
let args = fun.typed_args();
let args = fun.raw_typed_args();
let return_type = fun.return_type();
let mutability = fun.is_mut().then(|| quote::quote!(mut));

Expand All @@ -72,7 +72,7 @@ fn function_signature(fun: &FnIR) -> syn::Signature {

fn try_function_signature(fun: &FnIR) -> syn::Signature {
let fun_name = fun.try_name();
let args = fun.typed_args();
let args = fun.raw_typed_args();
let return_type = fun.try_return_type();
let mutability = fun.is_mut().then(|| quote::quote!(mut));

Expand Down
17 changes: 17 additions & 0 deletions odra-macros/src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ impl FnIR {
utils::syn::function_typed_args(&self.code)
}

pub fn raw_typed_args(&self) -> Vec<syn::PatType> {
self.typed_args()
.into_iter()
.map(|pat_ty| syn::PatType {
ty: Box::new(utils::syn::unreferenced_ty(&pat_ty.ty)),
..pat_ty
})
.collect()
}

pub fn is_mut(&self) -> bool {
let receiver = utils::syn::receiver_arg(&self.code);
receiver.map(|r| r.mutability.is_some()).unwrap_or_default()
Expand Down Expand Up @@ -396,4 +406,11 @@ impl FnArgIR {
_ => Err(syn::Error::new_spanned(&self.code, "Unnamed arg"))
}
}

pub fn is_ref(&self) -> bool {
match &self.code {
syn::FnArg::Typed(syn::PatType { box ty, .. }) => utils::syn::is_ref(ty),
_ => false
}
}
}
2 changes: 1 addition & 1 deletion odra-macros/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn mock_module() -> ModuleIR {
}

#[odra(non_reentrant)]
pub fn approve(&mut self, to: Address, amount: U256) {
pub fn approve(&mut self, to: &Address, amount: &U256) {
self.env.emit_event(Approval {
owner: self.env.caller(),
spender: to,
Expand Down
3 changes: 2 additions & 1 deletion odra-macros/src/utils/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ pub fn new_parameter(name: String, ty: syn::Type) -> syn::Expr {
}

pub fn as_cl_type(ty: &syn::Type) -> syn::Expr {
let ty = super::syn::unreferenced_ty(ty);
let ty_cl_typed = super::ty::cl_typed();
let ty = super::syn::as_casted_ty_stream(ty, ty_cl_typed);
let ty = super::syn::as_casted_ty_stream(&ty, ty_cl_typed);
parse_quote!(#ty::cl_type())
}

Expand Down
11 changes: 11 additions & 0 deletions odra-macros/src/utils/syn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,17 @@ pub fn as_casted_ty_stream(ty: &syn::Type, as_ty: syn::Type) -> TokenStream {
parse_quote!(<#ty as #as_ty>)
}

pub fn is_ref(ty: &syn::Type) -> bool {
matches!(ty, syn::Type::Reference(_))
}

pub fn unreferenced_ty(ty: &syn::Type) -> syn::Type {
match ty {
syn::Type::Reference(syn::TypeReference { elem, .. }) => *elem.clone(),
_ => ty.clone()
}
}

fn clear_path(ty: &syn::TypePath) -> Result<syn::TypePath, syn::Error> {
let mut owned_ty = ty.to_owned();

Expand Down

0 comments on commit 49ec530

Please sign in to comment.