From 8ac27877e4e41c4000d1129d7fe598d9e81d5cea Mon Sep 17 00:00:00 2001 From: Timidger Date: Tue, 7 Nov 2017 20:13:30 -0800 Subject: [PATCH 1/2] Only get gc on setmetatable if metatable isn't nil --- src/util.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.rs b/src/util.rs index 788788b..41c76fc 100644 --- a/src/util.rs +++ b/src/util.rs @@ -512,7 +512,7 @@ pub unsafe extern "C" fn safe_setmetatable(state: *mut ffi::lua_State) -> c_int // Wrapping the __gc method in setmetatable ONLY works because Lua 5.3 only honors the __gc // method when it exists upon calling setmetatable, and ignores it if it is set later. push_string(state, "__gc"); - if ffi::lua_rawget(state, -2) == ffi::LUA_TFUNCTION { + if ffi::lua_istable(state, -2) == 1 && ffi::lua_rawget(state, -2) == ffi::LUA_TFUNCTION { unsafe extern "C" fn safe_gc(state: *mut ffi::lua_State) -> c_int { ffi::lua_pushvalue(state, ffi::lua_upvalueindex(1)); ffi::lua_insert(state, 1); From f9c451bd267a02d455eda0e3d79c85e544adce1f Mon Sep 17 00:00:00 2001 From: Timidger Date: Tue, 7 Nov 2017 20:13:52 -0800 Subject: [PATCH 2/2] Add test ensuring behaviour is correct --- src/tests.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index c2e3e00..e83b3ba 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -629,6 +629,16 @@ fn test_recursive_callback_panic() { .unwrap(); } +#[test] +fn test_set_metatable_nil() { + let lua = Lua::new(); + lua.exec::<()>( + r#" + a = {} + setmetatable(a, nil) + "#, None).unwrap(); +} + // TODO: Need to use compiletest-rs or similar to make sure these don't compile. /* #[test]