Skip to content

Commit 171cdf1

Browse files
committed
Some fixes and more tests for Function::info
1 parent 3f8f016 commit 171cdf1

2 files changed

Lines changed: 39 additions & 11 deletions

File tree

src/function.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct Function(pub(crate) ValueRef);
3232
///
3333
/// [`Lua Debug Interface`]: https://www.lua.org/manual/5.4/manual.html#4.7
3434
#[derive(Clone, Debug)]
35+
#[non_exhaustive]
3536
pub struct FunctionInfo {
3637
/// A (reasonable) name of the function (`None` if the name cannot be found).
3738
pub name: Option<String>,
@@ -50,15 +51,16 @@ pub struct FunctionInfo {
5051
pub line_defined: Option<usize>,
5152
/// The line number where the definition of the function ends (not set by Luau).
5253
pub last_line_defined: Option<usize>,
53-
/// Number of function parameters
54+
/// The number of upvalues of the function.
55+
pub num_upvalues: u8,
56+
/// The number of parameters of the function (always 0 for C).
5457
#[cfg(any(not(any(feature = "lua51", feature = "luajit")), doc))]
55-
pub num_params: usize,
56-
/// True if function accepts variable args
58+
#[cfg_attr(docsrs, doc(cfg(not(any(feature = "lua51", feature = "luajit")))))]
59+
pub num_params: u8,
60+
/// Whether the function is a variadic function (always true for C).
5761
#[cfg(any(not(any(feature = "lua51", feature = "luajit")), doc))]
62+
#[cfg_attr(docsrs, doc(cfg(not(any(feature = "lua51", feature = "luajit")))))]
5863
pub is_vararg: bool,
59-
/// Number of upvalues
60-
#[cfg(any(not(feature = "luau"), doc))]
61-
pub nups: usize,
6264
}
6365

6466
/// Luau function coverage snapshot.
@@ -365,11 +367,16 @@ impl Function {
365367

366368
let mut ar: ffi::lua_Debug = mem::zeroed();
367369
lua.push_ref(&self.0);
370+
368371
#[cfg(not(feature = "luau"))]
369372
let res = ffi::lua_getinfo(state, cstr!(">Snu"), &mut ar);
373+
#[cfg(not(feature = "luau"))]
374+
mlua_assert!(res != 0, "lua_getinfo failed with `>Snu`");
375+
376+
#[cfg(feature = "luau")]
377+
let res = ffi::lua_getinfo(state, -1, cstr!("snau"), &mut ar);
370378
#[cfg(feature = "luau")]
371-
let res = ffi::lua_getinfo(state, -1, cstr!("sn"), &mut ar);
372-
mlua_assert!(res != 0, "lua_getinfo failed with `>Sn`");
379+
mlua_assert!(res != 0, "lua_getinfo failed with `snau`");
373380

374381
FunctionInfo {
375382
name: ptr_to_lossy_str(ar.name).map(|s| s.into_owned()),
@@ -391,12 +398,14 @@ impl Function {
391398
last_line_defined: linenumber_to_usize(ar.lastlinedefined),
392399
#[cfg(feature = "luau")]
393400
last_line_defined: None,
401+
#[cfg(not(feature = "luau"))]
402+
num_upvalues: ar.nups as _,
403+
#[cfg(feature = "luau")]
404+
num_upvalues: ar.nupvals,
394405
#[cfg(not(any(feature = "lua51", feature = "luajit")))]
395-
num_params: ar.nparams as usize,
406+
num_params: ar.nparams,
396407
#[cfg(not(any(feature = "lua51", feature = "luajit")))]
397408
is_vararg: ar.isvararg != 0,
398-
#[cfg(not(feature = "luau"))]
399-
nups: ar.nups as usize,
400409
}
401410
}
402411
}

tests/function.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,25 @@ fn test_function_info() -> Result<()> {
194194
assert_eq!(print_info.what, "C");
195195
assert_eq!(print_info.line_defined, None);
196196

197+
// Function with upvalues and params
198+
#[cfg(not(any(feature = "lua51", feature = "luajit")))]
199+
{
200+
let func_with_upvalues = lua
201+
.load(
202+
r#"
203+
local x, y = ...
204+
return function(a, ...)
205+
return a*x + y
206+
end
207+
"#,
208+
)
209+
.call::<Function>((10, 20))?;
210+
let func_with_upvalues_info = func_with_upvalues.info();
211+
assert_eq!(func_with_upvalues_info.num_upvalues, 2);
212+
assert_eq!(func_with_upvalues_info.num_params, 1);
213+
assert_eq!(func_with_upvalues_info.is_vararg, true);
214+
}
215+
197216
Ok(())
198217
}
199218

0 commit comments

Comments
 (0)