Skip to content

Commit d78420b

Browse files
committed
Communicate a little bit better about the checkstack constant
1 parent ace5cb4 commit d78420b

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

src/util.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,13 @@ where
5555
res
5656
}
5757

58-
// Run an operation on a lua_State and automatically clean up the stack before
59-
// returning. Takes the lua_State, the expected stack size change, and an
60-
// operation to run. If the operation results in success, then the stack is
61-
// inspected to make sure the change in stack size matches the expected change
62-
// and otherwise this is a logic error and will panic. If the operation results
63-
// in an error, the stack is shrunk to the value before the call. If the
64-
// operation results in an error and the stack is smaller than the value before
65-
// the call, then this is unrecoverable and this will panic. If this function
66-
// panics, it will clear the stack before panicking.
58+
// Run an operation on a lua_State and automatically clean up the stack before returning. Takes the
59+
// lua_State, the expected stack size change, and an operation to run. If the operation results in
60+
// success, then the stack is inspected to make sure the change in stack size matches the expected
61+
// change and otherwise this is a logic error and will panic. If the operation results in an error,
62+
// the stack is shrunk to the value before the call. If the operation results in an error and the
63+
// stack is smaller than the value before the call, then this is unrecoverable and this will panic.
64+
// If this function panics, it will clear the stack before panicking.
6765
pub unsafe fn stack_err_guard<F, R>(state: *mut ffi::lua_State, change: c_int, op: F) -> Result<R>
6866
where
6967
F: FnOnce() -> Result<R>,
@@ -312,10 +310,13 @@ where
312310
// traceback, and if it is a WrappedPanic, does not modify it.
313311
#[cfg_attr(unwind, unwind)]
314312
pub unsafe extern "C" fn error_traceback(state: *mut ffi::lua_State) -> c_int {
313+
// I believe luaL_traceback requires this much free stack to not error.
314+
const LUA_TRACEBACK_STACK: c_int = 11;
315+
315316
if ffi::lua_checkstack(state, 2) == 0 {
316317
// If we don't have enough stack space to even check the error type, do nothing
317318
} else if is_wrapped_error(state, 1) {
318-
let traceback = if ffi::lua_checkstack(state, 11) != 0 {
319+
let traceback = if ffi::lua_checkstack(state, LUA_TRACEBACK_STACK) != 0 {
319320
gc_guard(state, || {
320321
ffi::luaL_traceback(state, state, ptr::null(), 0);
321322
});
@@ -337,7 +338,7 @@ pub unsafe extern "C" fn error_traceback(state: *mut ffi::lua_State) -> c_int {
337338
},
338339
);
339340
} else if !is_wrapped_panic(state, 1) {
340-
if ffi::lua_checkstack(state, 11) != 0 {
341+
if ffi::lua_checkstack(state, LUA_TRACEBACK_STACK) != 0 {
341342
gc_guard(state, || {
342343
let s = ffi::lua_tostring(state, 1);
343344
let s = if s.is_null() {

0 commit comments

Comments
 (0)