-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(zink): introduce proc-macro revert (#267)
* feat(zink): introduce macro revert * feat(zink): pre-calculate length of revert messages * feat(codegen): parse revert from host functions * feat(zink): example of revert
- Loading branch information
Showing
10 changed files
with
145 additions
and
9 deletions.
There are no files selected for viewing
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,24 @@ | ||
//! Example of revert | ||
#![cfg_attr(target_arch = "wasm32", no_std)] | ||
#![cfg_attr(target_arch = "wasm32", no_main)] | ||
|
||
extern crate zink; | ||
|
||
#[cfg(not(target_arch = "wasm32"))] | ||
fn main() {} | ||
|
||
/// check if the passing address is owner | ||
#[zink::external] | ||
pub fn run_revert() { | ||
zink::revert!("revert works") | ||
} | ||
|
||
#[test] | ||
fn test_revert() -> anyhow::Result<()> { | ||
use zint::Contract; | ||
let mut contract = Contract::search("revert")?.compile()?; | ||
|
||
let info = contract.execute(["revert()".as_bytes()])?; | ||
assert_eq!(info.revert, Some("revert works".into())); | ||
Ok(()) | ||
} |
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,36 @@ | ||
//! Revert macro | ||
use proc_macro::TokenStream; | ||
use proc_macro2::{Literal, Span}; | ||
use quote::{quote, ToTokens}; | ||
use syn::{Ident, LitStr}; | ||
|
||
/// Revert with message | ||
pub fn parse(input: LitStr) -> TokenStream { | ||
let message = input.value(); | ||
let len = message.len() as i32; | ||
if len > 128 { | ||
panic!("Only support revert message less than 128 bytes atm."); | ||
} | ||
|
||
// TODO: handle the string correctly | ||
let lit = Literal::string(&message.replace("\"", "")); | ||
let rev = Ident::new( | ||
&format!( | ||
"revert{}", | ||
match len { | ||
len if len > 96 => 4, | ||
len if len > 64 => 3, | ||
len if len > 32 => 2, | ||
len if len > 0 => 1, | ||
_ => panic!("Only support revert message less than 128 bytes atm."), | ||
}, | ||
), | ||
Span::call_site(), | ||
); | ||
|
||
quote! { | ||
unsafe { zink::ffi::asm::#rev(#lit) } | ||
} | ||
.into() | ||
} |
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