Skip to content

Commit 052740d

Browse files
committed
Save lua_State at the moment of constructing Debug instead of resolving it dynamically
1 parent faf547c commit 052740d

File tree

5 files changed

+23
-22
lines changed

5 files changed

+23
-22
lines changed

src/debug.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::borrow::Cow;
22
use std::os::raw::c_int;
33

4-
use ffi::lua_Debug;
4+
use ffi::{lua_Debug, lua_State};
55

66
use crate::state::RawLua;
77
use crate::util::{linenumber_to_usize, ptr_to_lossy_str, ptr_to_str};
@@ -15,16 +15,17 @@ use crate::util::{linenumber_to_usize, ptr_to_lossy_str, ptr_to_str};
1515
///
1616
/// [documentation]: https://www.lua.org/manual/5.4/manual.html#lua_Debug
1717
/// [`Lua::set_hook`]: crate::Lua::set_hook
18-
pub struct Debug<'a> {
19-
lua: &'a RawLua,
18+
pub struct Debug {
19+
state: *mut lua_State,
2020
#[cfg_attr(not(feature = "luau"), allow(unused))]
2121
level: c_int,
2222
ar: *mut lua_Debug,
2323
}
2424

25-
impl<'a> Debug<'a> {
26-
pub(crate) fn new(lua: &'a RawLua, level: c_int, ar: *mut lua_Debug) -> Self {
27-
Debug { lua, ar, level }
25+
impl Debug {
26+
pub(crate) fn new(lua: &RawLua, level: c_int, ar: *mut lua_Debug) -> Self {
27+
let state = lua.state();
28+
Debug { state, ar, level }
2829
}
2930

3031
/// Returns the specific event that triggered the hook.
@@ -53,12 +54,12 @@ impl<'a> Debug<'a> {
5354
unsafe {
5455
#[cfg(not(feature = "luau"))]
5556
mlua_assert!(
56-
ffi::lua_getinfo(self.lua.state(), cstr!("n"), self.ar) != 0,
57+
ffi::lua_getinfo(self.state, cstr!("n"), self.ar) != 0,
5758
"lua_getinfo failed with `n`"
5859
);
5960
#[cfg(feature = "luau")]
6061
mlua_assert!(
61-
ffi::lua_getinfo(self.lua.state(), self.level, cstr!("n"), self.ar) != 0,
62+
ffi::lua_getinfo(self.state, self.level, cstr!("n"), self.ar) != 0,
6263
"lua_getinfo failed with `n`"
6364
);
6465

@@ -80,12 +81,12 @@ impl<'a> Debug<'a> {
8081
unsafe {
8182
#[cfg(not(feature = "luau"))]
8283
mlua_assert!(
83-
ffi::lua_getinfo(self.lua.state(), cstr!("S"), self.ar) != 0,
84+
ffi::lua_getinfo(self.state, cstr!("S"), self.ar) != 0,
8485
"lua_getinfo failed with `S`"
8586
);
8687
#[cfg(feature = "luau")]
8788
mlua_assert!(
88-
ffi::lua_getinfo(self.lua.state(), self.level, cstr!("s"), self.ar) != 0,
89+
ffi::lua_getinfo(self.state, self.level, cstr!("s"), self.ar) != 0,
8990
"lua_getinfo failed with `s`"
9091
);
9192

@@ -110,12 +111,12 @@ impl<'a> Debug<'a> {
110111
unsafe {
111112
#[cfg(not(feature = "luau"))]
112113
mlua_assert!(
113-
ffi::lua_getinfo(self.lua.state(), cstr!("l"), self.ar) != 0,
114+
ffi::lua_getinfo(self.state, cstr!("l"), self.ar) != 0,
114115
"lua_getinfo failed with `l`"
115116
);
116117
#[cfg(feature = "luau")]
117118
mlua_assert!(
118-
ffi::lua_getinfo(self.lua.state(), self.level, cstr!("l"), self.ar) != 0,
119+
ffi::lua_getinfo(self.state, self.level, cstr!("l"), self.ar) != 0,
119120
"lua_getinfo failed with `l`"
120121
);
121122

@@ -130,7 +131,7 @@ impl<'a> Debug<'a> {
130131
pub fn is_tail_call(&self) -> bool {
131132
unsafe {
132133
mlua_assert!(
133-
ffi::lua_getinfo(self.lua.state(), cstr!("t"), self.ar) != 0,
134+
ffi::lua_getinfo(self.state, cstr!("t"), self.ar) != 0,
134135
"lua_getinfo failed with `t`"
135136
);
136137
(*self.ar).currentline != 0
@@ -142,12 +143,12 @@ impl<'a> Debug<'a> {
142143
unsafe {
143144
#[cfg(not(feature = "luau"))]
144145
mlua_assert!(
145-
ffi::lua_getinfo(self.lua.state(), cstr!("u"), self.ar) != 0,
146+
ffi::lua_getinfo(self.state, cstr!("u"), self.ar) != 0,
146147
"lua_getinfo failed with `u`"
147148
);
148149
#[cfg(feature = "luau")]
149150
mlua_assert!(
150-
ffi::lua_getinfo(self.lua.state(), self.level, cstr!("au"), self.ar) != 0,
151+
ffi::lua_getinfo(self.state, self.level, cstr!("au"), self.ar) != 0,
151152
"lua_getinfo failed with `au`"
152153
);
153154

src/state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ impl Lua {
544544
#[cfg_attr(docsrs, doc(cfg(not(feature = "luau"))))]
545545
pub fn set_global_hook<F>(&self, triggers: HookTriggers, callback: F) -> Result<()>
546546
where
547-
F: Fn(&Lua, Debug) -> Result<VmState> + MaybeSend + 'static,
547+
F: Fn(&Lua, &Debug) -> Result<VmState> + MaybeSend + 'static,
548548
{
549549
let lua = self.lock();
550550
unsafe {
@@ -594,7 +594,7 @@ impl Lua {
594594
#[cfg_attr(docsrs, doc(cfg(not(feature = "luau"))))]
595595
pub fn set_hook<F>(&self, triggers: HookTriggers, callback: F) -> Result<()>
596596
where
597-
F: Fn(&Lua, Debug) -> Result<VmState> + MaybeSend + 'static,
597+
F: Fn(&Lua, &Debug) -> Result<VmState> + MaybeSend + 'static,
598598
{
599599
let lua = self.lock();
600600
unsafe { lua.set_thread_hook(lua.state(), HookKind::Thread(triggers, XRc::new(callback))) }

src/state/raw.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ impl RawLua {
436436
Some(hook_callback) => {
437437
let rawlua = (*extra).raw_lua();
438438
let debug = Debug::new(rawlua, 0, ar);
439-
hook_callback((*extra).lua(), debug)
439+
hook_callback((*extra).lua(), &debug)
440440
}
441441
None => {
442442
ffi::lua_sethook(state, None, 0, 0);
@@ -467,7 +467,7 @@ impl RawLua {
467467
let rawlua = (*extra).raw_lua();
468468
let debug = Debug::new(rawlua, 0, ar);
469469
let hook_callback = (*hook_callback_ptr).clone();
470-
hook_callback((*extra).lua(), debug)
470+
hook_callback((*extra).lua(), &debug)
471471
});
472472
process_status(state, (*ar).event, status)
473473
}

src/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ impl Thread {
269269
#[cfg_attr(docsrs, doc(cfg(not(feature = "luau"))))]
270270
pub fn set_hook<F>(&self, triggers: HookTriggers, callback: F) -> Result<()>
271271
where
272-
F: Fn(&crate::Lua, Debug) -> Result<crate::VmState> + crate::MaybeSend + 'static,
272+
F: Fn(&crate::Lua, &Debug) -> Result<crate::VmState> + crate::MaybeSend + 'static,
273273
{
274274
let lua = self.0.lua.lock();
275275
unsafe {

src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ pub(crate) enum HookKind {
7979
}
8080

8181
#[cfg(all(feature = "send", not(feature = "luau")))]
82-
pub(crate) type HookCallback = XRc<dyn Fn(&Lua, Debug) -> Result<VmState> + Send>;
82+
pub(crate) type HookCallback = XRc<dyn Fn(&Lua, &Debug) -> Result<VmState> + Send>;
8383

8484
#[cfg(all(not(feature = "send"), not(feature = "luau")))]
85-
pub(crate) type HookCallback = XRc<dyn Fn(&Lua, Debug) -> Result<VmState>>;
85+
pub(crate) type HookCallback = XRc<dyn Fn(&Lua, &Debug) -> Result<VmState>>;
8686

8787
#[cfg(all(feature = "send", feature = "luau"))]
8888
pub(crate) type InterruptCallback = XRc<dyn Fn(&Lua) -> Result<VmState> + Send>;

0 commit comments

Comments
 (0)