Skip to content

Commit c68e3c4

Browse files
committed
Some DebugStack improvements
1 parent 02d4cef commit c68e3c4

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

src/hook.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ impl<'a> Debug<'a> {
184184
);
185185
#[cfg(feature = "luau")]
186186
mlua_assert!(
187-
ffi::lua_getinfo(self.lua.state(), self.level, cstr!("a"), self.ar.get()) != 0,
188-
"lua_getinfo failed with `a`"
187+
ffi::lua_getinfo(self.lua.state(), self.level, cstr!("au"), self.ar.get()) != 0,
188+
"lua_getinfo failed with `au`"
189189
);
190190

191191
#[cfg(not(feature = "luau"))]
@@ -198,8 +198,8 @@ impl<'a> Debug<'a> {
198198
};
199199
#[cfg(feature = "luau")]
200200
let stack = DebugStack {
201-
num_ups: (*self.ar.get()).nupvals as i32,
202-
num_params: (*self.ar.get()).nparams as i32,
201+
num_ups: (*self.ar.get()).nupvals,
202+
num_params: (*self.ar.get()).nparams,
203203
is_vararg: (*self.ar.get()).isvararg != 0,
204204
};
205205
stack
@@ -262,10 +262,15 @@ pub struct DebugSource<'a> {
262262

263263
#[derive(Copy, Clone, Debug)]
264264
pub struct DebugStack {
265-
pub num_ups: i32,
265+
/// Number of upvalues.
266+
pub num_ups: u8,
267+
/// Number of parameters.
268+
///
266269
/// Requires `feature = "lua54/lua53/lua52/luau"`
267270
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52", feature = "luau"))]
268-
pub num_params: i32,
271+
pub num_params: u8,
272+
/// Whether the function is a vararg function.
273+
///
269274
/// Requires `feature = "lua54/lua53/lua52/luau"`
270275
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52", feature = "luau"))]
271276
pub is_vararg: bool,

tests/tests.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ fn test_exec() -> Result<()> {
106106
"#,
107107
)
108108
.eval()?;
109-
println!("checkpoint");
110109
assert!(module.contains_key("func")?);
111110
assert_eq!(module.get::<Function>("func")?.call::<String>(())?, "hello");
112111

@@ -631,8 +630,7 @@ fn test_recursive_mut_callback_error() -> Result<()> {
631630
// Whoops, this will recurse into the function and produce another mutable reference!
632631
lua.globals().get::<Function>("f")?.call::<()>(true)?;
633632
println!("Should not get here, mutable aliasing has occurred!");
634-
println!("value at {:p}", r as *mut _);
635-
println!("value is {}", r);
633+
println!("value at {:p} is {r}", r as *mut _);
636634
}
637635

638636
Ok(())
@@ -1134,6 +1132,13 @@ fn test_inspect_stack() -> Result<()> {
11341132
})?;
11351133
lua.globals().set("logline", logline)?;
11361134

1135+
let stack_info = lua.create_function(|lua, ()| {
1136+
let debug = lua.inspect_stack(1).unwrap(); // caller
1137+
let stack_info = debug.stack();
1138+
Ok(format!("{stack_info:?}"))
1139+
})?;
1140+
lua.globals().set("stack_info", stack_info)?;
1141+
11371142
lua.load(
11381143
r#"
11391144
local function foo()
@@ -1143,10 +1148,18 @@ fn test_inspect_stack() -> Result<()> {
11431148
local function bar()
11441149
return foo()
11451150
end
1151+
local stack_info = stack_info
1152+
local function baz(a, b, c, ...)
1153+
return stack_info()
1154+
end
11461155
11471156
assert(foo() == '[string "chunk"]:3 hello')
11481157
assert(bar() == '[string "chunk"]:3 hello')
1149-
assert(logline("world") == '[string "chunk"]:12 world')
1158+
assert(logline("world") == '[string "chunk"]:16 world')
1159+
assert(
1160+
baz() == 'DebugStack { num_ups: 1, num_params: 3, is_vararg: true }' or
1161+
baz() == 'DebugStack { num_ups: 1 }'
1162+
)
11501163
"#,
11511164
)
11521165
.set_name("chunk")

0 commit comments

Comments
 (0)