From 0b7a5dbb78b9bf0319d1ec86cbe75d5239702556 Mon Sep 17 00:00:00 2001 From: Michael Benfield Date: Tue, 12 Mar 2024 10:32:41 -0700 Subject: [PATCH] exit_early --- stylus-sdk/src/evm.rs | 27 ++++++++++++++++++++++++++- stylus-sdk/src/hostio.rs | 5 +++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/stylus-sdk/src/evm.rs b/stylus-sdk/src/evm.rs index 9ade5816..6a47bf00 100644 --- a/stylus-sdk/src/evm.rs +++ b/stylus-sdk/src/evm.rs @@ -57,6 +57,31 @@ pub fn pay_for_memory_grow(pages: u16) { unsafe { hostio::pay_for_memory_grow(pages) } } +/// Print the message to the console and revert, with revert data supplied by a call to `output`. +#[macro_export] +macro_rules! revert { + ($($x:tt)*) => { + ::stylus_sdk::console!($($x)*); + unsafe { ::stylus_sdk::evm::exit_early(1) }; + } +} + +/// Print the message to the console and exit successfully, with data supplied +/// by a call to `output`. +#[macro_export] +macro_rules! succeed { + ($($x:tt)*) => { + ::stylus_sdk::console!($($x)*); + unsafe { ::stylus_sdk::evm::exit_early(0) }; + } +} + +/// Not intended for end users. +#[inline] +pub fn exit_early(status: u32) { + unsafe { hostio::exit_early(status) } +} + wrap_hostio!( /// Gets the amount of gas remaining. See [`Ink and Gas`] for more information on Stylus's compute pricing. /// @@ -67,6 +92,6 @@ wrap_hostio!( wrap_hostio!( /// Gets the amount of ink remaining. See [`Ink and Gas`] for more information on Stylus's compute pricing. /// - /// [`Ink and Gas`]: https://developer.arbitrum.io/TODO + /// [`Ink and Gas`]: https://docs.arbitrum.io/TODO ink_left evm_ink_left u64 ); diff --git a/stylus-sdk/src/hostio.rs b/stylus-sdk/src/hostio.rs index 82792bff..2114c7cc 100644 --- a/stylus-sdk/src/hostio.rs +++ b/stylus-sdk/src/hostio.rs @@ -286,6 +286,11 @@ extern "C" { /// naturally when `user_entrypoint` returns. pub fn write_result(data: *const u8, len: usize); + /// Exits program execution early with the given status code. + /// If `0`, the program returns successfully with any data supplied by `write_result`. + /// Otherwise, the program reverts and treats any `write_result` data as revert data. + pub fn exit_early(status: u32) -> !; + /// Returns the length of the last EVM call or deployment return result, or `0` if neither have /// happened during the program's execution. The semantics are equivalent to that of the EVM's /// [`RETURN_DATA_SIZE`] opcode.