Skip to content

Commit df5a093

Browse files
committed
Turn the debug feature into a macro.
1 parent af1783e commit df5a093

File tree

4 files changed

+176
-41
lines changed

4 files changed

+176
-41
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,5 @@ qimalloc = { version = "0.1", optional = true }
1515
[features]
1616
default = ["std", "wee_alloc"]
1717
std = []
18-
debug = []
1918
experimental = []
2019
eth2 = []

circle.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ jobs:
1919
cargo fmt --all -- --check
2020
- run:
2121
name: Test
22-
command: cargo test --target=x86_64-unknown-linux-gnu
22+
command: |
23+
cargo test --target=x86_64-unknown-linux-gnu
24+
cargo test --release --target=x86_64-unknown-linux-gnu
2325
- run:
2426
name: Build
2527
command: |
@@ -34,11 +36,9 @@ jobs:
3436
cargo build --release --no-default-features --features wee_alloc
3537
cargo build --release --no-default-features --features qimalloc
3638
# different feature sets
37-
cargo build --release --features debug
38-
cargo build --release --no-default-features --features debug
39+
cargo build --release --no-default-features
3940
cargo build --release --features experimental
4041
cargo build --release --no-default-features --features experimental
41-
cargo build --release --features experimental,debug
42-
cargo build --release --no-default-features --features experimental,debug
42+
cargo build --release --features experimental
4343
cargo build --release --features eth2
4444
cargo build --release --no-default-features --features eth2

src/debug.rs

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,93 @@
11
//! The native debug interface exposed to the ewasm contract. These functions are for testing
22
//! purposes only. On a live VM, any bytecode trying to import these symbols will be rejected.
33
4-
use crate::types::StorageKey;
5-
64
/// The native interface for debugging functions.
7-
mod native {
5+
#[cfg(debug_assertions)]
6+
pub mod native {
87
extern "C" {
98
pub fn debug_print32(value: u32);
109
pub fn debug_print64(value: u64);
1110
pub fn debug_printMem(offset: *const u32, len: u32);
1211
pub fn debug_printMemHex(offset: *const u32, len: u32);
13-
pub fn debug_printStorage(pathOffset: *const u32);
14-
pub fn debug_printStorageHex(pathOffset: *const u32);
12+
pub fn debug_printStorage(path_offset: *const u32);
13+
pub fn debug_printStorageHex(path_offset: *const u32);
1514
}
1615
}
1716

17+
#[macro_export]
1818
/// Prints an unsigned 32-bit int.
19-
pub fn print32(value: u32) {
20-
unsafe { native::debug_print32(value) }
19+
macro_rules! print32 {
20+
($value:expr) => {
21+
#[cfg(debug_assertions)]
22+
{
23+
unsafe { $crate::debug::native::debug_print32($value) }
24+
}
25+
};
2126
}
2227

28+
#[macro_export]
2329
/// Prints an unsigned 64-bit int.
24-
pub fn print64(value: u64) {
25-
unsafe { native::debug_print64(value) }
30+
macro_rules! print64 {
31+
($value:expr) => {
32+
#[cfg(debug_assertions)]
33+
{
34+
unsafe { $crate::debug::native::debug_print64($value) }
35+
}
36+
};
2637
}
2738

39+
#[macro_export]
2840
/// Prints the contents of a slice.
29-
pub fn print_mem(slice: &[u8]) {
30-
unsafe { native::debug_printMem(slice.as_ptr() as *const u32, slice.len() as u32) }
41+
macro_rules! print_mem {
42+
($slice:expr) => {
43+
#[cfg(debug_assertions)]
44+
{
45+
unsafe {
46+
$crate::debug::native::debug_printMem(
47+
$slice.as_ptr() as *const u32,
48+
$slice.len() as u32,
49+
)
50+
}
51+
}
52+
};
3153
}
3254

55+
#[macro_export]
3356
/// Prints the contents of a slice in hexadecimal format.
34-
pub fn print_mem_hex(slice: &[u8]) {
35-
unsafe { native::debug_printMemHex(slice.as_ptr() as *const u32, slice.len() as u32) }
57+
macro_rules! print_mem_hex {
58+
($slice:expr) => {
59+
#[cfg(debug_assertions)]
60+
{
61+
unsafe {
62+
$crate::debug::native::debug_printMemHex(
63+
$slice.as_ptr() as *const u32,
64+
$slice.len() as u32,
65+
)
66+
}
67+
}
68+
};
3669
}
3770

71+
#[macro_export]
3872
/// Prints the value of a storage key.
39-
pub fn print_storage(key: &StorageKey) {
40-
unsafe { native::debug_printStorage(key.bytes.as_ptr() as *const u32) }
73+
macro_rules! print_storage {
74+
($key:expr) => {
75+
#[cfg(debug_assertions)]
76+
{
77+
unsafe { $crate::debug::native::debug_printStorage($key.bytes.as_ptr() as *const u32) }
78+
}
79+
};
4180
}
4281

82+
#[macro_export]
4383
/// Prints the value of a storage key in hexadecimal format.
44-
pub fn print_storage_hex(key: &StorageKey) {
45-
unsafe { native::debug_printStorageHex(key.bytes.as_ptr() as *const u32) }
84+
macro_rules! print_storage_hex {
85+
($key:expr) => {
86+
#[cfg(debug_assertions)]
87+
{
88+
unsafe {
89+
$crate::debug::native::debug_printStorageHex($key.bytes.as_ptr() as *const u32)
90+
}
91+
}
92+
};
4693
}

src/lib.rs

Lines changed: 107 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@
4141
#[macro_use]
4242
extern crate cfg_if;
4343

44+
#[cfg(feature = "std")]
45+
use std::vec::Vec;
46+
47+
use types::*;
48+
#[cfg(feature = "std")]
49+
use utils::*;
50+
4451
cfg_if! {
4552
if #[cfg(feature = "wee_alloc")] {
4653
extern crate wee_alloc;
@@ -58,7 +65,7 @@ mod utils;
5865

5966
pub mod types;
6067

61-
#[cfg(feature = "debug")]
68+
#[macro_use]
6269
pub mod debug;
6370

6471
#[cfg(feature = "experimental")]
@@ -70,30 +77,17 @@ pub mod eth2;
7077
#[cfg(not(feature = "std"))]
7178
pub mod convert;
7279

73-
#[cfg(feature = "std")]
74-
use std::vec::Vec;
75-
76-
use types::*;
77-
#[cfg(feature = "std")]
78-
use utils::*;
79-
8080
/// Re-export of all the basic features.
8181
pub mod prelude {
82-
pub use crate::*;
83-
84-
pub use crate::types::*;
85-
82+
#[cfg(feature = "experimental")]
83+
pub use crate::bignum;
8684
#[cfg(not(feature = "std"))]
8785
pub use crate::convert::*;
88-
89-
#[cfg(feature = "debug")]
9086
pub use crate::debug;
91-
92-
#[cfg(feature = "experimental")]
93-
pub use crate::bignum;
94-
9587
#[cfg(feature = "eth2")]
9688
pub use crate::eth2;
89+
pub use crate::types::*;
90+
pub use crate::*;
9791
}
9892

9993
/// Declare entry point for a contract. Expects a Rust function name to be executed.
@@ -667,3 +661,98 @@ pub fn selfdestruct(address: &Address) -> ! {
667661
native::ethereum_selfDestruct(address.bytes.as_ptr() as *const u32);
668662
}
669663
}
664+
665+
#[cfg(test)]
666+
mod debug_macros {
667+
use crate::types::StorageKey;
668+
669+
#[cfg(debug_assertions)]
670+
#[no_mangle]
671+
pub fn debug_print32(_value: u32) {}
672+
673+
#[cfg(debug_assertions)]
674+
#[no_mangle]
675+
pub fn debug_print64(_value: u64) {}
676+
677+
#[cfg(debug_assertions)]
678+
#[allow(non_snake_case)]
679+
#[no_mangle]
680+
pub fn debug_printMem(_offset: *const u32, _len: u32) {}
681+
682+
#[cfg(debug_assertions)]
683+
#[allow(non_snake_case)]
684+
#[no_mangle]
685+
pub fn debug_printMemHex(_offset: *const u32, _len: u32) {}
686+
687+
#[cfg(debug_assertions)]
688+
#[allow(non_snake_case)]
689+
#[no_mangle]
690+
pub fn debug_printStorage(_path_offset: *const u32) {}
691+
692+
#[cfg(debug_assertions)]
693+
#[allow(non_snake_case)]
694+
#[no_mangle]
695+
pub fn debug_printStorageHex(_path_offset: *const u32) {}
696+
697+
#[test]
698+
fn test_print32() {
699+
let _v: u32 = 42;
700+
print32!(_v);
701+
print32!(42);
702+
print32!(42 + 1);
703+
}
704+
705+
#[test]
706+
fn test_print64() {
707+
let _v: u64 = 4242;
708+
print64!(_v);
709+
print64!(4242);
710+
print64!(4242 + 1);
711+
}
712+
713+
#[test]
714+
fn test_print_mem() {
715+
let _mem: [u8; 0] = [];
716+
print_mem!(_mem);
717+
let _mem: [u8; 3] = [0, 1, 2];
718+
print_mem!(_mem);
719+
let _mem = [0, 1, 2, 3, 4, 5];
720+
print_mem!(_mem);
721+
print_mem!([0, 1]);
722+
}
723+
724+
#[test]
725+
fn test_print_mem_hex() {
726+
let _mem: [u8; 0] = [];
727+
print_mem_hex!(_mem);
728+
let _mem: [u8; 3] = [0, 1, 2];
729+
print_mem_hex!(_mem);
730+
let _mem = [0, 1, 2, 3, 4, 5];
731+
print_mem_hex!(_mem);
732+
print_mem_hex!([0, 1]);
733+
}
734+
735+
#[test]
736+
fn test_print_storage() {
737+
let _key = StorageKey {
738+
bytes: [
739+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
740+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
741+
0x00, 0x00, 0x00, 0x00,
742+
],
743+
};
744+
print_storage!(_key);
745+
}
746+
747+
#[test]
748+
fn test_print_storage_hex() {
749+
let _key = StorageKey {
750+
bytes: [
751+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
752+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
753+
0x00, 0x00, 0x00, 0x00,
754+
],
755+
};
756+
print_storage_hex!(_key);
757+
}
758+
}

0 commit comments

Comments
 (0)