diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2020-06-07 15:16:12 +0100 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2020-06-07 20:38:19 +0100 |
commit | 3d42bc4ca67a1930c49d6cbb4bfa9d60aff8fbbb (patch) | |
tree | dfa55adf01abd63efa80edf2a262c2075e074d11 /src/util.rs | |
parent | 2eb40deafd13fd0260de6b911d84a677f801897b (diff) | |
download | mlua-3d42bc4ca67a1930c49d6cbb4bfa9d60aff8fbbb.zip |
Refactor main_state handling
Don't allow to set hook if main_state is not available
Remove Lua 5.1 dirty hack
Diffstat (limited to 'src/util.rs')
-rw-r--r-- | src/util.rs | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/src/util.rs b/src/util.rs index d729155..aed3128 100644 --- a/src/util.rs +++ b/src/util.rs @@ -485,28 +485,25 @@ pub unsafe extern "C" fn error_traceback(state: *mut ffi::lua_State) -> c_int { } // Does not call lua_checkstack, uses 1 stack space. -pub unsafe fn get_main_state(state: *mut ffi::lua_State) -> *mut ffi::lua_State { +pub unsafe fn get_main_state(state: *mut ffi::lua_State) -> Option<*mut ffi::lua_State> { #[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52"))] { 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 + Some(main_state) } - #[cfg(feature = "lua51")] + #[cfg(any(feature = "lua51", feature = "luajit"))] { // Check the current state first let is_main_state = ffi::lua_pushthread(state) == 1; ffi::lua_pop(state, 1); if is_main_state { - state + Some(state) } else { - // The function below is a dirty hack and uses Lua private internals - ffi::lua_getmainstate(state) + None } } - #[cfg(feature = "luajit")] - state } // Pushes a WrappedError to the top of the stack. Uses two stack spaces and does not call |