forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a `byte-array` proc-macro crate for converting strings into byte arrays that don't use static initializers, and use it to implement `eprintln`, an `unreachable` that prints the line number, and other macros.
- Loading branch information
1 parent
902ca94
commit 66e39bb
Showing
2 changed files
with
113 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//! Minimal versions of standard-library panicking and printing macros. | ||
//! | ||
//! We're avoiding static initializers, so we can't have things like string | ||
//! literals. Replace the standard assert macros with simpler implementations. | ||
/// A minimal `eprint` for debugging. | ||
#[allow(unused_macros)] | ||
macro_rules! eprint { | ||
($arg:tt) => {{ | ||
// We have to expand string literals into byte arrays to prevent them | ||
// from getting statically initialized. | ||
let message = byte_array::str!($arg); | ||
crate::bindings::wasi_stderr::print(&message); | ||
}}; | ||
} | ||
|
||
/// A minimal `eprintln` for debugging. | ||
#[allow(unused_macros)] | ||
macro_rules! eprintln { | ||
($arg:tt) => {{ | ||
// We have to expand string literals into byte arrays to prevent them | ||
// from getting statically initialized. | ||
let message = byte_array::str_nl!($arg); | ||
crate::bindings::wasi_stderr::print(&message); | ||
}}; | ||
} | ||
|
||
pub(crate) fn eprint_u32(x: u32) { | ||
if x == 0 { | ||
eprint!("0"); | ||
} else { | ||
eprint_u32_impl(x) | ||
} | ||
|
||
fn eprint_u32_impl(x: u32) { | ||
if x != 0 { | ||
eprint_u32_impl(x / 10); | ||
|
||
let digit = [b'0' + ((x % 10) as u8)]; | ||
crate::bindings::wasi_stderr::print(&digit); | ||
} | ||
} | ||
} | ||
|
||
/// A minimal `unreachable`. | ||
macro_rules! unreachable { | ||
() => {{ | ||
eprint!("unreachable executed at line "); | ||
crate::macros::eprint_u32(line!()); | ||
wasm32::unreachable() | ||
}}; | ||
|
||
($arg:tt) => {{ | ||
eprint!("unreachable executed at line "); | ||
crate::macros::eprint_u32(line!()); | ||
eprint!(": "); | ||
eprintln!($arg); | ||
wasm32::unreachable() | ||
}}; | ||
} | ||
|
||
/// A minimal `assert`. | ||
macro_rules! assert { | ||
($cond:expr $(,)?) => { | ||
if !$cond { | ||
unreachable!("assertion failed") | ||
} | ||
}; | ||
} | ||
|
||
/// A minimal `assert_eq`. | ||
macro_rules! assert_eq { | ||
($left:expr, $right:expr $(,)?) => { | ||
assert!($left == $right); | ||
}; | ||
} |