Add `lua_stack_guard` macro
This commit is contained in:
parent
6ebb763f14
commit
4acec5adf2
|
@ -109,7 +109,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "gmod"
|
name = "gmod"
|
||||||
version = "4.0.1"
|
version = "4.0.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg_table",
|
"cfg_table",
|
||||||
"cstr",
|
"cstr",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "gmod"
|
name = "gmod"
|
||||||
version = "4.0.1"
|
version = "4.0.2"
|
||||||
authors = ["William Venner <william@venner.io>"]
|
authors = ["William Venner <william@venner.io>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
|
|
@ -44,3 +44,37 @@ macro_rules! lua_string {
|
||||||
$crate::cstr::cstr!($str).as_ptr()
|
$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
|
||||||
|
}};
|
||||||
|
}
|
Loading…
Reference in New Issue