Skip to main content

luaur_vm/functions/
f_luaopen.rs

1//! Node: `cxx:Function:Luau.VM:VM/src/lstate.cpp:59:f_luaopen`
2//! Source: `VM/src/lstate.cpp:59-70` (hand-ported)
3
4use crate::functions::lua_h_new::lua_h_new;
5use crate::functions::lua_s_resize::luaS_resize;
6use crate::functions::lua_t_init::lua_t_init;
7use crate::functions::stack_init::stack_init;
8use crate::macros::lua_minstrtabsize::LUA_MINSTRTABSIZE;
9use crate::macros::lua_s_fix::luaS_fix;
10use crate::macros::lua_s_newliteral::luaS_newliteral;
11use crate::macros::registry::registry;
12use crate::macros::sethvalue::sethvalue;
13use crate::type_aliases::lua_state::lua_State;
14use crate::type_aliases::t_value::TValue;
15
16/// open parts that may cause memory-allocation errors
17pub unsafe fn f_luaopen(L: *mut lua_State, _ud: *mut core::ffi::c_void) {
18    let g = (*L).global;
19    stack_init(L, L); // init stack
20    (*L).gt = lua_h_new(L, 0, 2); // table of globals
21    sethvalue!(
22        L,
23        registry!(L) as *const TValue as *mut TValue,
24        lua_h_new(L, 0, 2)
25    ); // registry
26    luaS_resize(L, LUA_MINSTRTABSIZE); // initial size of string table
27    lua_t_init(L);
28    luaS_fix!(luaS_newliteral(L, c"not enough memory".as_ptr())); // LUA_MEMERRMSG // pin to make sure we can always throw this error
29    luaS_fix!(luaS_newliteral(L, c"error in error handling".as_ptr())); // LUA_ERRERRMSG // pin to make sure we can always throw this error
30    (*g).GCthreshold = 4 * (*g).totalbytes;
31}