From 2658433bd187386cf0c6a10a61d4fa14f229c0fe Mon Sep 17 00:00:00 2001 From: Timidger Date: Sat, 28 Oct 2017 20:46:24 -0700 Subject: [PATCH 1/5] Added ability to load debug lib, Fixes #52 --- src/lua.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/lua.rs b/src/lua.rs index b765dfd..53a0b4b 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -557,6 +557,14 @@ 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) { + ffi::luaL_requiref(self.state, cstr!("debug"), ffi::luaopen_debug, 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 From 26b7901ab35b7094ef8e3dfe623045f2670aa686 Mon Sep 17 00:00:00 2001 From: Timidger Date: Sun, 29 Oct 2017 14:53:30 -0700 Subject: [PATCH 2/5] Added test for loading unsafe debug library in Lua --- src/tests.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index 327d997..028f0be 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.eval::<()>("debug", 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(); From 316c6976e45096e74d80e58e8efb46227a2b6afa Mon Sep 17 00:00:00 2001 From: Timidger Date: Sun, 29 Oct 2017 14:54:55 -0700 Subject: [PATCH 3/5] Added pop after loading lua library --- src/lua.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lua.rs b/src/lua.rs index 53a0b4b..18c0ca7 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -563,6 +563,7 @@ impl Lua { /// the guarantees of rlua. pub unsafe fn load_debug(&self) { 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. From 71fdca547700c5a206cd456b5ee37f06fa26e4e2 Mon Sep 17 00:00:00 2001 From: Timidger Date: Sun, 29 Oct 2017 15:24:54 -0700 Subject: [PATCH 4/5] Fixed test to use Lua assert --- src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests.rs b/src/tests.rs index 028f0be..c2e3e00 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -17,7 +17,7 @@ fn test_load() { #[test] fn test_load_debug() { let lua = Lua::new(); - lua.eval::<()>("debug", None).unwrap(); + lua.exec::<()>("assert(debug == nil)", None).unwrap(); unsafe { lua.load_debug(); } From a1370860642076caf23f4aca084c5df8a46d7d7e Mon Sep 17 00:00:00 2001 From: Timidger Date: Sun, 29 Oct 2017 15:26:40 -0700 Subject: [PATCH 5/5] Check that we have enough room on stack for module --- src/lua.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/lua.rs b/src/lua.rs index 18c0ca7..b9cdfe9 100644 --- a/src/lua.rs +++ b/src/lua.rs @@ -562,6 +562,7 @@ impl Lua { /// 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); }