mlua/src/macros.rs

132 lines
3.1 KiB
Rust
Raw Normal View History

macro_rules! bug_msg {
($arg:expr) => {
concat!(
2019-10-01 11:11:12 -04:00
"mlua internal error: ",
$arg,
" (this is a bug, please file an issue)"
)
};
}
2017-12-02 17:04:33 -05:00
macro_rules! cstr {
2018-08-05 09:51:39 -04:00
($s:expr) => {
concat!($s, "\0") as *const str as *const [::std::os::raw::c_char]
as *const ::std::os::raw::c_char
};
2017-12-02 17:04:33 -05:00
}
2019-10-01 11:11:12 -04:00
macro_rules! mlua_panic {
2018-02-10 17:49:54 -05:00
($msg:expr) => {
2021-08-21 19:01:17 -04:00
panic!(bug_msg!($msg))
2018-02-10 17:49:54 -05:00
};
($msg:expr,) => {
2021-08-21 19:01:17 -04:00
mlua_panic!($msg)
2018-02-10 17:49:54 -05:00
};
($msg:expr, $($arg:expr),+) => {
2021-08-21 19:01:17 -04:00
panic!(bug_msg!($msg), $($arg),+)
};
($msg:expr, $($arg:expr),+,) => {
2021-08-21 19:01:17 -04:00
mlua_panic!($msg, $($arg),+)
};
}
2019-10-01 11:11:12 -04:00
macro_rules! mlua_assert {
($cond:expr, $msg:expr) => {
assert!($cond, bug_msg!($msg));
};
($cond:expr, $msg:expr,) => {
2019-10-01 11:11:12 -04:00
mlua_assert!($cond, $msg);
};
($cond:expr, $msg:expr, $($arg:expr),+) => {
assert!($cond, bug_msg!($msg), $($arg),+);
};
($cond:expr, $msg:expr, $($arg:expr),+,) => {
2019-10-01 11:11:12 -04:00
mlua_assert!($cond, $msg, $($arg),+);
2017-12-02 17:04:33 -05:00
};
}
2018-02-10 17:49:54 -05:00
2019-10-01 11:11:12 -04:00
macro_rules! mlua_debug_assert {
($cond:expr, $msg:expr) => {
debug_assert!($cond, bug_msg!($msg));
};
($cond:expr, $msg:expr,) => {
2019-10-01 11:11:12 -04:00
mlua_debug_assert!($cond, $msg);
};
($cond:expr, $msg:expr, $($arg:expr),+) => {
debug_assert!($cond, bug_msg!($msg), $($arg),+);
};
($cond:expr, $msg:expr, $($arg:expr),+,) => {
2019-10-01 11:11:12 -04:00
mlua_debug_assert!($cond, $msg, $($arg),+);
};
}
2023-07-05 16:34:17 -04:00
#[cfg(debug_assertions)]
2019-10-01 11:11:12 -04:00
macro_rules! mlua_expect {
2023-07-05 16:34:17 -04:00
($res:expr, $msg:expr $(,)?) => {
$res.expect(bug_msg!($msg))
2018-02-10 17:49:54 -05:00
};
2023-07-05 16:34:17 -04:00
}
2018-02-10 17:49:54 -05:00
2023-07-05 16:34:17 -04:00
#[cfg(not(debug_assertions))]
macro_rules! mlua_expect {
($res:expr, $msg:expr $(,)?) => {{
let x;
#[allow(unused_unsafe)]
{
x = unsafe { $res.into_iter().next().unwrap_unchecked() };
}
x
}};
2018-02-10 17:49:54 -05:00
}
#[cfg(feature = "module")]
#[doc(hidden)]
#[macro_export]
macro_rules! require_module_feature {
() => {};
}
#[cfg(not(feature = "module"))]
#[doc(hidden)]
#[macro_export]
macro_rules! require_module_feature {
() => {
compile_error!("Feature `module` must be enabled in the `mlua` crate");
};
}
macro_rules! protect_lua {
(@debug_name) => {
concat!("protect_lua[", file!(), ":", stringify!(line!()), ":", stringify!(column!()), "]")
.as_ptr().cast()
};
($state:expr, $nargs:expr, $nresults:expr, $f:expr) => {
crate::util::protect_lua_closure($state, $nargs, $nresults, $f, $crate::macros::protect_lua!(@debug_name))
};
2021-09-28 13:47:08 -04:00
($state:expr, $nargs:expr, $nresults:expr, fn($state_inner:ident) $code:expr) => {{
unsafe extern "C" fn do_call($state_inner: *mut ffi::lua_State) -> ::std::os::raw::c_int {
$code;
let nresults = $nresults;
if nresults == ::ffi::LUA_MULTRET {
ffi::lua_gettop($state_inner)
} else {
nresults
}
}
crate::util::protect_lua_call($state, $nargs, do_call, $crate::macros::protect_lua!(@debug_name))
}};
}
pub(crate) use protect_lua;