diff --git a/src/error.rs b/src/error.rs index 784434c..88dce2a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -47,11 +47,6 @@ pub enum Error { /// This error can only happen when Lua state was not created by us and does not have the /// custom allocator attached. MemoryLimitNotAvailable, - /// Main thread is not available. - /// - /// This error can only happen in Lua5.1/LuaJIT module mode, when module loaded within a coroutine. - /// These Lua versions does not have `LUA_RIDX_MAINTHREAD` registry key. - MainThreadNotAvailable, /// A mutable callback has triggered Lua code that has called the same mutable callback again. /// /// This is an error because a mutable callback can only be borrowed mutably once. @@ -226,9 +221,6 @@ impl fmt::Display for Error { Error::MemoryLimitNotAvailable => { write!(fmt, "setting memory limit is not available") } - Error::MainThreadNotAvailable => { - write!(fmt, "main thread is not available in Lua 5.1") - } Error::RecursiveMutCallback => write!(fmt, "mutable callback called recursively"), Error::CallbackDestructed => write!( fmt, diff --git a/src/lua.rs b/src/lua.rs index 21b8012..5edbcdf 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -836,8 +836,8 @@ impl Lua { /// limited form of execution limits by setting [`HookTriggers.every_nth_instruction`] and /// erroring once an instruction limit has been reached. /// - /// This method sets a hook function for the main thread (if available) of this Lua instance. - /// If you want to set a hook function for a thread (coroutine), use [`Thread::set_hook()`] instead. + /// This method sets a hook function for the current thread of this Lua instance. + /// If you want to set a hook function for another thread (coroutine), use [`Thread::set_hook()`] instead. /// /// Please note you cannot have more than one hook function set at a time for this Lua instance. /// @@ -852,7 +852,7 @@ impl Lua { /// lua.set_hook(HookTriggers::EVERY_LINE, |_lua, debug| { /// println!("line {}", debug.curr_line()); /// Ok(()) - /// })?; + /// }); /// /// lua.load(r#" /// local x = 2 + 3 @@ -866,15 +866,11 @@ impl Lua { /// [`HookTriggers.every_nth_instruction`]: crate::HookTriggers::every_nth_instruction #[cfg(not(feature = "luau"))] #[cfg_attr(docsrs, doc(cfg(not(feature = "luau"))))] - pub fn set_hook(&self, triggers: HookTriggers, callback: F) -> Result<()> + pub fn set_hook(&self, triggers: HookTriggers, callback: F) where F: Fn(&Lua, Debug) -> Result<()> + MaybeSend + 'static, { - unsafe { - let state = get_main_state(self.main_state).ok_or(Error::MainThreadNotAvailable)?; - self.set_thread_hook(state, triggers, callback); - } - Ok(()) + unsafe { self.set_thread_hook(self.state(), triggers, callback) }; } /// Sets a 'hook' function for a thread (coroutine). diff --git a/tests/hooks.rs b/tests/hooks.rs index 0f02f22..a993bb0 100644 --- a/tests/hooks.rs +++ b/tests/hooks.rs @@ -28,7 +28,7 @@ fn test_line_counts() -> Result<()> { assert_eq!(debug.event(), DebugEvent::Line); hook_output.lock().unwrap().push(debug.curr_line()); Ok(()) - })?; + }); lua.load( r#" local x = 2 + 3 @@ -63,7 +63,7 @@ fn test_function_calls() -> Result<()> { let name = names.name.map(|s| s.into_owned()); hook_output.lock().unwrap().push((name, source.what)); Ok(()) - })?; + }); lua.load( r#" @@ -98,7 +98,7 @@ fn test_error_within_hook() -> Result<()> { Err(Error::RuntimeError( "Something happened in there!".to_string(), )) - })?; + }); let err = lua .load("x = 1") @@ -135,7 +135,7 @@ fn test_limit_execution_instructions() -> Result<()> { Ok(()) } }, - )?; + ); lua.globals().set("x", Value::Integer(0))?; let _ = lua @@ -163,7 +163,7 @@ fn test_hook_removal() -> Result<()> { "this hook should've been removed by this time".to_string(), )) }, - )?; + ); assert!(lua.load("local x = 1").exec().is_err()); lua.remove_hook(); @@ -207,9 +207,10 @@ fn test_hook_swap_within_hook() -> Result<()> { Ok(()) }, ) - }) + }); + Ok(()) }) - })?; + }); TL_LUA.with(|tl| { let tl = tl.borrow();