diff --git a/src/function.rs b/src/function.rs index fb08845..e34420f 100644 --- a/src/function.rs +++ b/src/function.rs @@ -44,15 +44,30 @@ impl OwnedFunction { } } +/// Contains information about a function. +/// +/// Please refer to the [`Lua Debug Interface`] for more information. +/// +/// [`Lua Debug Interface`]: https://www.lua.org/manual/5.4/manual.html#4.7 #[derive(Clone, Debug)] pub struct FunctionInfo { - pub name: Option>, - pub name_what: Option>, - pub what: Option>, + /// A (reasonable) name of the function. + pub name: Option, + /// Explains the `name` field ("global", "local", "method", "field", "upvalue", or ""). + /// + /// Always `None` for Luau. + pub name_what: Option, + /// A string "Lua" if the function is a Lua function, "C" if it is a C function, "main" if it is the main part of a chunk. + pub what: Option, + /// The source of the chunk that created the function. pub source: Option>, + /// A "printable" version of source, to be used in error messages. pub short_src: Option>, + /// The line number where the definition of the function starts. pub line_defined: i32, - #[cfg(not(feature = "luau"))] + /// The line number where the definition of the function ends. + /// + /// Always `-1` for Luau. pub last_line_defined: i32, } @@ -295,12 +310,13 @@ impl<'lua> Function<'lua> { mlua_assert!(res != 0, "lua_getinfo failed with `>Sn`"); FunctionInfo { - name: ptr_to_cstr_bytes(ar.name).map(|s| s.to_vec()), + name: ptr_to_cstr_bytes(ar.name).map(|s| String::from_utf8_lossy(s).into_owned()), #[cfg(not(feature = "luau"))] - name_what: ptr_to_cstr_bytes(ar.namewhat).map(|s| s.to_vec()), + name_what: ptr_to_cstr_bytes(ar.namewhat) + .map(|s| String::from_utf8_lossy(s).into_owned()), #[cfg(feature = "luau")] name_what: None, - what: ptr_to_cstr_bytes(ar.what).map(|s| s.to_vec()), + what: ptr_to_cstr_bytes(ar.what).map(|s| String::from_utf8_lossy(s).into_owned()), source: ptr_to_cstr_bytes(ar.source).map(|s| s.to_vec()), #[cfg(not(feature = "luau"))] short_src: ptr_to_cstr_bytes(ar.short_src.as_ptr()).map(|s| s.to_vec()), @@ -309,6 +325,8 @@ impl<'lua> Function<'lua> { line_defined: ar.linedefined, #[cfg(not(feature = "luau"))] last_line_defined: ar.lastlinedefined, + #[cfg(feature = "luau")] + last_line_defined: -1, } } } diff --git a/tests/function.rs b/tests/function.rs index e0cda3e..bc99ec0 100644 --- a/tests/function.rs +++ b/tests/function.rs @@ -135,34 +135,37 @@ fn test_function_info() -> Result<()> { let function1_info = function1.info(); #[cfg(feature = "luau")] - assert_eq!(function1_info.name, Some(b"function1".to_vec())); - assert_eq!(function1_info.source, Some(b"source1".to_vec())); + assert_eq!(function1_info.name.as_deref(), Some("function1")); + assert_eq!(function1_info.source.as_deref(), Some(b"source1".as_ref())); assert_eq!(function1_info.line_defined, 2); #[cfg(not(feature = "luau"))] assert_eq!(function1_info.last_line_defined, 4); - assert_eq!(function1_info.what, Some(b"Lua".to_vec())); + #[cfg(feature = "luau")] + assert_eq!(function1_info.last_line_defined, -1); + assert_eq!(function1_info.what.as_deref(), Some("Lua")); let function2_info = function2.info(); assert_eq!(function2_info.name, None); - assert_eq!(function2_info.source, Some(b"source1".to_vec())); + assert_eq!(function2_info.source.as_deref(), Some(b"source1".as_ref())); assert_eq!(function2_info.line_defined, 3); #[cfg(not(feature = "luau"))] assert_eq!(function2_info.last_line_defined, 3); - assert_eq!(function2_info.what, Some(b"Lua".to_vec())); + #[cfg(feature = "luau")] + assert_eq!(function2_info.last_line_defined, -1); + assert_eq!(function2_info.what.as_deref(), Some("Lua")); let function3_info = function3.info(); assert_eq!(function3_info.name, None); - assert_eq!(function3_info.source, Some(b"=[C]".to_vec())); + assert_eq!(function3_info.source.as_deref(), Some(b"=[C]".as_ref())); assert_eq!(function3_info.line_defined, -1); - #[cfg(not(feature = "luau"))] assert_eq!(function3_info.last_line_defined, -1); - assert_eq!(function3_info.what, Some(b"C".to_vec())); + assert_eq!(function3_info.what.as_deref(), Some("C")); let print_info = globals.get::<_, Function>("print")?.info(); #[cfg(feature = "luau")] - assert_eq!(print_info.name, Some(b"print".to_vec())); - assert_eq!(print_info.source, Some(b"=[C]".to_vec())); - assert_eq!(print_info.what, Some(b"C".to_vec())); + assert_eq!(print_info.name.as_deref(), Some("print")); + assert_eq!(print_info.source.as_deref(), Some(b"=[C]".as_ref())); + assert_eq!(print_info.what.as_deref(), Some("C")); assert_eq!(print_info.line_defined, -1); Ok(())