diff options
author | Alex Orlenko <zxteam@protonmail.com> | 2023-04-08 23:53:48 +0100 |
---|---|---|
committer | Alex Orlenko <zxteam@protonmail.com> | 2023-04-08 23:53:48 +0100 |
commit | 3e83753466a074a53ba59b47132e0d38b0de78a1 (patch) | |
tree | 17f7ce3356d78320651f00ae432d5a93907f31c1 /src/thread.rs | |
parent | 288934c82c22ea7732046d5b316320ff509f5d1b (diff) | |
download | mlua-3e83753466a074a53ba59b47132e0d38b0de78a1.zip |
Add `Thread::set_hook()` function
Diffstat (limited to 'src/thread.rs')
-rw-r--r-- | src/thread.rs | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/src/thread.rs b/src/thread.rs index 00e9226..174cb97 100644 --- a/src/thread.rs +++ b/src/thread.rs @@ -3,6 +3,8 @@ use std::os::raw::c_int; use crate::error::{Error, Result}; use crate::ffi; +#[allow(unused)] +use crate::lua::Lua; use crate::types::LuaRef; use crate::util::{check_stack, error_traceback_thread, pop_error, StackGuard}; use crate::value::{FromLuaMulti, IntoLuaMulti}; @@ -14,10 +16,16 @@ use crate::value::{FromLuaMulti, IntoLuaMulti}; ))] use crate::function::Function; +#[cfg(not(feature = "luau"))] +use crate::{ + hook::{Debug, HookTriggers}, + types::MaybeSend, +}; + #[cfg(feature = "async")] use { crate::{ - lua::{Lua, ASYNC_POLL_PENDING}, + lua::ASYNC_POLL_PENDING, value::{MultiValue, Value}, }, futures_core::{future::Future, stream::Stream}, @@ -178,6 +186,23 @@ impl<'lua> Thread<'lua> { } } + /// Sets a 'hook' function that will periodically be called as Lua code executes. + /// + /// This function is similar or [`Lua::set_hook()`] except that it sets for the thread. + /// To remove a hook call [`Lua::remove_hook()`]. + #[cfg(not(feature = "luau"))] + #[cfg_attr(docsrs, doc(cfg(not(feature = "luau"))))] + pub fn set_hook<F>(&self, triggers: HookTriggers, callback: F) + where + F: Fn(&Lua, Debug) -> Result<()> + MaybeSend + 'static, + { + let lua = self.0.lua; + unsafe { + let thread_state = ffi::lua_tothread(lua.ref_thread(), self.0.index); + lua.set_thread_hook(thread_state, triggers, callback); + } + } + /// Resets a thread /// /// In [Lua 5.4]: cleans its call stack and closes all pending to-be-closed variables. |