Skip to content

Commit

Permalink
add wasmcov macro
Browse files Browse the repository at this point in the history
  • Loading branch information
njelich committed Jan 16, 2024
1 parent 1343059 commit c382ec6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ impl ImplItemMethodInfo {
fn void_return_body_tokens(&self) -> TokenStream2 {
let contract_init = self.contract_init_tokens();
let method_invocation = self.method_invocation_tokens();
let wasmcov = self.wasmcov_tokens();
let contract_ser = self.contract_ser_tokens();

quote! {
#contract_init
#method_invocation;
#wasmcov
#contract_ser
}
}
Expand All @@ -67,12 +69,14 @@ impl ImplItemMethodInfo {
let contract_ser = self.contract_ser_tokens();
let value_ser = self.value_ser_tokens();
let value_return = self.value_return_tokens();
let wasmcov = self.wasmcov_tokens();

quote! {
#contract_init
#method_invocation_with_return
#value_ser
#value_return
#wasmcov
#contract_ser
}
}
Expand All @@ -84,6 +88,7 @@ impl ImplItemMethodInfo {
let value_ser = self.value_ser_tokens();
let value_return = self.value_return_tokens();
let result_identifier = self.result_identifier();
let wasmcov = self.wasmcov_tokens();

quote! {
#contract_init
Expand All @@ -92,6 +97,7 @@ impl ImplItemMethodInfo {
::std::result::Result::Ok(#result_identifier) => {
#value_ser
#value_return
#wasmcov
#contract_ser
}
::std::result::Result::Err(err) => ::near_sdk::FunctionError::panic(&err)
Expand Down Expand Up @@ -247,6 +253,17 @@ impl ImplItemMethodInfo {
}
}

fn wasmcov_tokens(&self) -> TokenStream2 {
quote! {
let mut coverage = vec![];
unsafe {
// Note that this function is not thread-safe! Use a lock if needed.
wasmcov::capture_coverage(&mut coverage).unwrap();
};
::near_sdk::env::log_str();
}
}

fn contract_ser_tokens(&self) -> TokenStream2 {
use MethodKind::*;

Expand Down
1 change: 1 addition & 0 deletions near-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ near-vm-logic = { version = "0.17", optional = true }
near-primitives-core = { version = "0.17", optional = true }
near-primitives = { version = "0.17", optional = true }
near-crypto = { version = "0.17", optional = true }
wasmcov = { version = "0.1.0" }

[dev-dependencies]
rand = "0.8.4"
Expand Down
2 changes: 2 additions & 0 deletions near-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ pub use bs58;
pub use schemars;
pub use serde;
pub use serde_json;
#[doc(hidden)]
pub use wasmcov;

0 comments on commit c382ec6

Please sign in to comment.