diff --git a/src/lua.rs b/src/lua.rs index b765dfd..b9cdfe9 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -557,6 +557,16 @@ impl Lua { } } + /// Loads the Lua debug library. + /// + /// The debug library is very unsound, loading it and using it breaks all + /// the guarantees of rlua. + pub unsafe fn load_debug(&self) { + check_stack(self.state, 1); + ffi::luaL_requiref(self.state, cstr!("debug"), ffi::luaopen_debug, 1); + ffi::lua_pop(self.state, 1); + } + /// Loads a chunk of Lua code and returns it as a function. /// /// The source can be named by setting the `name` parameter. This is generally recommended as it diff --git a/src/tests.rs b/src/tests.rs index 327d997..c2e3e00 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -14,6 +14,23 @@ fn test_load() { assert!(lua.load("§$%§&$%&", None).is_err()); } +#[test] +fn test_load_debug() { + let lua = Lua::new(); + lua.exec::<()>("assert(debug == nil)", None).unwrap(); + unsafe { + lua.load_debug(); + } + match lua.eval("debug", None).unwrap() { + Value::Table(_) => {}, + val => { + panic!("Expected table for debug library, got {:#?}", val) + } + } + let traceback_output = lua.eval::("debug.traceback()", None).unwrap(); + assert_eq!(traceback_output.split("\n").next(), "stack traceback:".into()); +} + #[test] fn test_exec() { let lua = Lua::new();