From 9da98d42c755ff13dd98d05c577324ecbcfc0707 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Mon, 16 Jun 2025 22:25:26 +0100 Subject: [PATCH] Move `ref_stack_pop` into `ExtraData` method. --- src/state/extra.rs | 28 ++++++++++++++++++++++++++++ src/state/raw.rs | 8 ++++---- src/state/util.rs | 30 +----------------------------- 3 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/state/extra.rs b/src/state/extra.rs index 9379f65..ddbd4fd 100644 --- a/src/state/extra.rs +++ b/src/state/extra.rs @@ -259,4 +259,32 @@ impl ExtraData { pub(super) unsafe fn weak(&self) -> &WeakLua { self.weak.assume_init_ref() } + + /// Pops a reference from top of the auxiliary stack and move it to a first free slot. + pub(super) unsafe fn ref_stack_pop(&mut self) -> c_int { + if let Some(free) = self.ref_free.pop() { + ffi::lua_replace(self.ref_thread, free); + return free; + } + + // Try to grow max stack size + if self.ref_stack_top >= self.ref_stack_size { + let mut inc = self.ref_stack_size; // Try to double stack size + while inc > 0 && ffi::lua_checkstack(self.ref_thread, inc) == 0 { + inc /= 2; + } + if inc == 0 { + // Pop item on top of the stack to avoid stack leaking and successfully run destructors + // during unwinding. + ffi::lua_pop(self.ref_thread, 1); + let top = self.ref_stack_top; + // It is a user error to create too many references to exhaust the Lua max stack size + // for the ref thread. + panic!("cannot create a Lua reference, out of auxiliary stack space (used {top} slots)"); + } + self.ref_stack_size += inc; + } + self.ref_stack_top += 1; + self.ref_stack_top + } } diff --git a/src/state/raw.rs b/src/state/raw.rs index 8732c55..cfa212e 100644 --- a/src/state/raw.rs +++ b/src/state/raw.rs @@ -11,7 +11,7 @@ use crate::chunk::ChunkMode; use crate::error::{Error, Result}; use crate::function::Function; use crate::memory::{MemoryState, ALLOCATOR}; -use crate::state::util::{callback_error_ext, ref_stack_pop}; +use crate::state::util::callback_error_ext; use crate::stdlib::StdLib; use crate::string::String; use crate::table::Table; @@ -816,21 +816,21 @@ impl RawLua { #[inline] pub(crate) unsafe fn pop_ref(&self) -> ValueRef { ffi::lua_xmove(self.state(), self.ref_thread(), 1); - let index = ref_stack_pop(self.extra.get()); + let index = (*self.extra.get()).ref_stack_pop(); ValueRef::new(self, index) } // Same as `pop_ref` but assumes the value is already on the reference thread #[inline] pub(crate) unsafe fn pop_ref_thread(&self) -> ValueRef { - let index = ref_stack_pop(self.extra.get()); + let index = (*self.extra.get()).ref_stack_pop(); ValueRef::new(self, index) } #[inline] pub(crate) unsafe fn clone_ref(&self, vref: &ValueRef) -> ValueRef { ffi::lua_pushvalue(self.ref_thread(), vref.index); - let index = ref_stack_pop(self.extra.get()); + let index = (*self.extra.get()).ref_stack_pop(); ValueRef::new(self, index) } diff --git a/src/state/util.rs b/src/state/util.rs index c3c7930..5c8a0af 100644 --- a/src/state/util.rs +++ b/src/state/util.rs @@ -89,7 +89,7 @@ where PreallocatedFailure::New(_) => { ffi::lua_rotate(state, 1, -1); ffi::lua_xmove(state, ref_thread, 1); - let index = ref_stack_pop(extra); + let index = (*extra).ref_stack_pop(); (*extra).wrapped_failure_pool.push(index); (*extra).wrapped_failure_top += 1; } @@ -150,31 +150,3 @@ where } } } - -pub(super) unsafe fn ref_stack_pop(extra: *mut ExtraData) -> c_int { - let extra = &mut *extra; - if let Some(free) = extra.ref_free.pop() { - ffi::lua_replace(extra.ref_thread, free); - return free; - } - - // Try to grow max stack size - if extra.ref_stack_top >= extra.ref_stack_size { - let mut inc = extra.ref_stack_size; // Try to double stack size - while inc > 0 && ffi::lua_checkstack(extra.ref_thread, inc) == 0 { - inc /= 2; - } - if inc == 0 { - // Pop item on top of the stack to avoid stack leaking and successfully run destructors - // during unwinding. - ffi::lua_pop(extra.ref_thread, 1); - let top = extra.ref_stack_top; - // It is a user error to create enough references to exhaust the Lua max stack size for - // the ref thread. - panic!("cannot create a Lua reference, out of auxiliary stack space (used {top} slots)"); - } - extra.ref_stack_size += inc; - } - extra.ref_stack_top += 1; - extra.ref_stack_top -}