Skip to main content

luaur_vm/functions/
shrinkstackprotected.rs

1//! Node: `cxx:Function:Luau.VM:VM/src/lgc.cpp:485:shrinkstackprotected`
2//! Source: `VM/src/lgc.cpp` (lgc.cpp:485-498, hand-ported)
3
4use crate::enums::lua_status::lua_Status;
5use crate::functions::lua_d_rawrunprotected_ldo_alt_b::lua_d_rawrunprotected_mut;
6use crate::functions::shrinkstack::shrinkstack;
7use crate::type_aliases::lua_state::lua_State;
8use luaur_common::macros::luau_assert::LUAU_ASSERT;
9
10// C++ uses a local `struct CallContext { static void run(...) }`; a local fn is
11// the Rust equivalent of that protected-call trampoline.
12unsafe fn run(l: *mut lua_State, _ud: *mut core::ffi::c_void) {
13    shrinkstack(l);
14}
15
16#[allow(non_snake_case)]
17pub(crate) unsafe fn shrinkstackprotected(l: *mut lua_State) {
18    // the resize call can fail on exception, in which case we will continue with original size
19    let status = lua_d_rawrunprotected_mut(l, Some(run), core::ptr::null_mut());
20    LUAU_ASSERT!(status == lua_Status::LUA_OK as i32 || status == lua_Status::LUA_ERRMEM as i32);
21}