Add `lua_stack_guard` macro

This commit is contained in:
William Venner 2021-10-06 18:24:12 +01:00
parent 6ebb763f14
commit 4acec5adf2
3 changed files with 36 additions and 2 deletions

2
Cargo.lock generated
View File

@ -109,7 +109,7 @@ dependencies = [
[[package]]
name = "gmod"
version = "4.0.1"
version = "4.0.2"
dependencies = [
"cfg_table",
"cstr",

View File

@ -1,6 +1,6 @@
[package]
name = "gmod"
version = "4.0.1"
version = "4.0.2"
authors = ["William Venner <william@venner.io>"]
edition = "2018"
license = "MIT"

View File

@ -44,3 +44,37 @@ macro_rules! lua_string {
$crate::cstr::cstr!($str).as_ptr()
};
}
/// Enforces a debug assertion that the Lua stack is unchanged after this block of code is executed.
///
/// Useful for ensuring stack hygiene.
///
/// `lua` is the Lua state to check.
///
/// # Example
///
/// ```rust,norun
/// lua_stack_guard!(lua => {
/// lua.get_global(lua_string!("hook"));
/// lua.get_field(-1, lua_string!("Add"));
/// lua.push_string("PlayerInitialSpawn");
/// lua.push_string("RustHook");
/// lua.push_function(player_initial_spawn);
/// lua.call(3, 0);
/// // lua.pop();
/// });
/// // PANIC: stack is dirty! We forgot to pop the hook library off the stack.
/// ```
#[macro_export]
macro_rules! lua_stack_guard {
( $lua:ident => $code:block ) => {{
#[cfg(debug_assertions)] {
let top = $lua.get_top();
$code
assert_eq!(top, $lua.get_top(), "Stack is dirty!");
}
#[cfg(not(debug_assertions))]
$code
}};
}