Skip to content

Commit 9da98d4

Browse files
committed
Move ref_stack_pop into ExtraData method.
1 parent f539f60 commit 9da98d4

File tree

3 files changed

+33
-33
lines changed

3 files changed

+33
-33
lines changed

src/state/extra.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,4 +259,32 @@ impl ExtraData {
259259
pub(super) unsafe fn weak(&self) -> &WeakLua {
260260
self.weak.assume_init_ref()
261261
}
262+
263+
/// Pops a reference from top of the auxiliary stack and move it to a first free slot.
264+
pub(super) unsafe fn ref_stack_pop(&mut self) -> c_int {
265+
if let Some(free) = self.ref_free.pop() {
266+
ffi::lua_replace(self.ref_thread, free);
267+
return free;
268+
}
269+
270+
// Try to grow max stack size
271+
if self.ref_stack_top >= self.ref_stack_size {
272+
let mut inc = self.ref_stack_size; // Try to double stack size
273+
while inc > 0 && ffi::lua_checkstack(self.ref_thread, inc) == 0 {
274+
inc /= 2;
275+
}
276+
if inc == 0 {
277+
// Pop item on top of the stack to avoid stack leaking and successfully run destructors
278+
// during unwinding.
279+
ffi::lua_pop(self.ref_thread, 1);
280+
let top = self.ref_stack_top;
281+
// It is a user error to create too many references to exhaust the Lua max stack size
282+
// for the ref thread.
283+
panic!("cannot create a Lua reference, out of auxiliary stack space (used {top} slots)");
284+
}
285+
self.ref_stack_size += inc;
286+
}
287+
self.ref_stack_top += 1;
288+
self.ref_stack_top
289+
}
262290
}

src/state/raw.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::chunk::ChunkMode;
1111
use crate::error::{Error, Result};
1212
use crate::function::Function;
1313
use crate::memory::{MemoryState, ALLOCATOR};
14-
use crate::state::util::{callback_error_ext, ref_stack_pop};
14+
use crate::state::util::callback_error_ext;
1515
use crate::stdlib::StdLib;
1616
use crate::string::String;
1717
use crate::table::Table;
@@ -816,21 +816,21 @@ impl RawLua {
816816
#[inline]
817817
pub(crate) unsafe fn pop_ref(&self) -> ValueRef {
818818
ffi::lua_xmove(self.state(), self.ref_thread(), 1);
819-
let index = ref_stack_pop(self.extra.get());
819+
let index = (*self.extra.get()).ref_stack_pop();
820820
ValueRef::new(self, index)
821821
}
822822

823823
// Same as `pop_ref` but assumes the value is already on the reference thread
824824
#[inline]
825825
pub(crate) unsafe fn pop_ref_thread(&self) -> ValueRef {
826-
let index = ref_stack_pop(self.extra.get());
826+
let index = (*self.extra.get()).ref_stack_pop();
827827
ValueRef::new(self, index)
828828
}
829829

830830
#[inline]
831831
pub(crate) unsafe fn clone_ref(&self, vref: &ValueRef) -> ValueRef {
832832
ffi::lua_pushvalue(self.ref_thread(), vref.index);
833-
let index = ref_stack_pop(self.extra.get());
833+
let index = (*self.extra.get()).ref_stack_pop();
834834
ValueRef::new(self, index)
835835
}
836836

src/state/util.rs

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ where
8989
PreallocatedFailure::New(_) => {
9090
ffi::lua_rotate(state, 1, -1);
9191
ffi::lua_xmove(state, ref_thread, 1);
92-
let index = ref_stack_pop(extra);
92+
let index = (*extra).ref_stack_pop();
9393
(*extra).wrapped_failure_pool.push(index);
9494
(*extra).wrapped_failure_top += 1;
9595
}
@@ -150,31 +150,3 @@ where
150150
}
151151
}
152152
}
153-
154-
pub(super) unsafe fn ref_stack_pop(extra: *mut ExtraData) -> c_int {
155-
let extra = &mut *extra;
156-
if let Some(free) = extra.ref_free.pop() {
157-
ffi::lua_replace(extra.ref_thread, free);
158-
return free;
159-
}
160-
161-
// Try to grow max stack size
162-
if extra.ref_stack_top >= extra.ref_stack_size {
163-
let mut inc = extra.ref_stack_size; // Try to double stack size
164-
while inc > 0 && ffi::lua_checkstack(extra.ref_thread, inc) == 0 {
165-
inc /= 2;
166-
}
167-
if inc == 0 {
168-
// Pop item on top of the stack to avoid stack leaking and successfully run destructors
169-
// during unwinding.
170-
ffi::lua_pop(extra.ref_thread, 1);
171-
let top = extra.ref_stack_top;
172-
// It is a user error to create enough references to exhaust the Lua max stack size for
173-
// the ref thread.
174-
panic!("cannot create a Lua reference, out of auxiliary stack space (used {top} slots)");
175-
}
176-
extra.ref_stack_size += inc;
177-
}
178-
extra.ref_stack_top += 1;
179-
extra.ref_stack_top
180-
}

0 commit comments

Comments
 (0)