Path: blob/main/crates/wasi-preview1-component-adapter/src/macros.rs
1692 views
//! Minimal versions of standard-library panicking and printing macros.1//!2//! We're avoiding static initializers, so we can't have things like string3//! literals. Replace the standard assert macros with simpler implementations.45use crate::bindings::wasi::cli::stderr::get_stderr;67#[allow(dead_code, reason = "useful for debugging")]8#[doc(hidden)]9pub fn print(message: &[u8]) {10let _ = get_stderr().blocking_write_and_flush(message);11}1213/// A minimal `eprint` for debugging.14#[allow(unused_macros, reason = "useful for debugging")]15macro_rules! eprint {16($arg:tt) => {{17// We have to expand string literals into byte arrays to prevent them18// from getting statically initialized.19let message = byte_array_literals::str!($arg);20$crate::macros::print(&message);21}};22}2324/// A minimal `eprintln` for debugging.25#[allow(unused_macros, reason = "useful for debugging")]26macro_rules! eprintln {27($arg:tt) => {{28// We have to expand string literals into byte arrays to prevent them29// from getting statically initialized.30let message = byte_array_literals::str_nl!($arg);31$crate::macros::print(&message);32}};33}3435#[allow(dead_code, reason = "useful for debugging")]36#[doc(hidden)]37pub fn eprint_unreachable(line: u32) {38eprint!("unreachable executed at adapter line ");39crate::macros::eprint_u32(line);40}4142fn eprint_u32(x: u32) {43if x == 0 {44eprint!("0");45} else {46eprint_u32_impl(x)47}4849fn eprint_u32_impl(x: u32) {50if x != 0 {51eprint_u32_impl(x / 10);5253let digit = [b'0' + ((x % 10) as u8)];54crate::macros::print(&digit);55}56}57}5859#[allow(dead_code, reason = "useful for debugging")]60#[doc(hidden)]61pub fn unreachable(line: u32) -> ! {62crate::macros::eprint_unreachable(line);63eprint!("\n");64#[cfg(target_arch = "wasm32")]65core::arch::wasm32::unreachable();66// This is here to keep rust-analyzer happy when building for native:67#[cfg(not(target_arch = "wasm32"))]68std::process::abort();69}7071/// A minimal `unreachable`.72macro_rules! unreachable {73() => {{74crate::macros::unreachable(line!());75}};7677($arg:tt) => {{78crate::macros::eprint_unreachable(line!());79eprint!(": ");80eprintln!($arg);81eprint!("\n");82#[cfg(target_arch = "wasm32")]83core::arch::wasm32::unreachable();84// This is here to keep rust-analyzer happy when building for native:85#[cfg(not(target_arch = "wasm32"))]86std::process::abort();87}};88}8990#[allow(dead_code, reason = "useful for debugging")]91#[doc(hidden)]92pub fn assert_fail(line: u32) -> ! {93eprint!("assertion failed at adapter line ");94crate::macros::eprint_u32(line);95#[cfg(target_arch = "wasm32")]96core::arch::wasm32::unreachable();97// This is here to keep rust-analyzer happy when building for native:98#[cfg(not(target_arch = "wasm32"))]99std::process::abort();100}101102/// A minimal `assert`.103macro_rules! assert {104($cond:expr $(,)?) => {105if !$cond {106crate::macros::assert_fail(line!());107}108};109}110111/// A minimal `assert_eq`.112macro_rules! assert_eq {113($left:expr, $right:expr $(,)?) => {114assert!($left == $right);115};116}117118119