Don't store (main)state for lua5.1

This commit is contained in:
Alex Orlenko 2020-04-17 00:18:15 +01:00
parent 28ac57f2e0
commit f3dd0d4949
2 changed files with 6 additions and 25 deletions

View file

@ -27,8 +27,6 @@ use crate::table::Table;
use crate::thread::Thread;
use crate::types::{AsyncCallback, Callback, Integer, LightUserData, LuaRef, Number, RegistryKey};
use crate::userdata::{AnyUserData, MetaMethod, UserData, UserDataMethods};
#[cfg(any(feature = "lua51", feature = "luajit"))]
use crate::util::set_main_state;
use crate::util::{
assert_stack, callback_error, check_stack, get_gc_userdata, get_main_state,
get_meta_gc_userdata, get_wrapped_error, init_error_registry, init_gc_metatable_for,
@ -128,13 +126,7 @@ impl Lua {
/// Constructs a new Lua instance from the existing state.
pub unsafe fn init_from_ptr(state: *mut ffi::lua_State) -> Lua {
#[cfg(any(feature = "lua53", feature = "lua52"))]
let main_state = get_main_state(state);
#[cfg(any(feature = "lua51", feature = "luajit"))]
let main_state = {
set_main_state(state);
state
};
let main_state_top = ffi::lua_gettop(state);
let ref_thread = mlua_expect!(

View file

@ -483,26 +483,17 @@ pub unsafe extern "C" fn error_traceback(state: *mut ffi::lua_State) -> c_int {
1
}
// Does not call lua_checkstack, uses 2 stack spaces.
#[cfg(any(feature = "lua51", feature = "luajit"))]
pub unsafe fn set_main_state(state: *mut ffi::lua_State) {
ffi::lua_pushlightuserdata(state, &MAIN_THREAD_REGISTRY_KEY as *const u8 as *mut c_void);
ffi::lua_pushthread(state);
ffi::lua_rawset(state, ffi::LUA_REGISTRYINDEX);
}
// Does not call lua_checkstack, uses 1 stack space.
pub unsafe fn get_main_state(state: *mut ffi::lua_State) -> *mut ffi::lua_State {
#[cfg(any(feature = "lua53", feature = "lua52"))]
ffi::lua_rawgeti(state, ffi::LUA_REGISTRYINDEX, ffi::LUA_RIDX_MAINTHREAD);
#[cfg(any(feature = "lua51", feature = "luajit"))]
{
ffi::lua_pushlightuserdata(state, &MAIN_THREAD_REGISTRY_KEY as *const u8 as *mut c_void);
ffi::lua_rawget(state, ffi::LUA_REGISTRYINDEX);
ffi::lua_rawgeti(state, ffi::LUA_REGISTRYINDEX, ffi::LUA_RIDX_MAINTHREAD);
let main_state = ffi::lua_tothread(state, -1);
ffi::lua_pop(state, 1);
main_state
}
let main_state = ffi::lua_tothread(state, -1);
ffi::lua_pop(state, 1);
main_state
#[cfg(any(feature = "lua51", feature = "luajit"))]
state
}
// Pushes a WrappedError to the top of the stack. Uses two stack spaces and does not call
@ -744,7 +735,5 @@ unsafe fn get_destructed_userdata_metatable(state: *mut ffi::lua_State) {
ffi::lua_rawget(state, ffi::LUA_REGISTRYINDEX);
}
#[cfg(any(feature = "lua51", feature = "luajit"))]
static MAIN_THREAD_REGISTRY_KEY: u8 = 0;
static DESTRUCTED_USERDATA_METATABLE: u8 = 0;
static ERROR_PRINT_BUFFER_KEY: u8 = 0;