Move several asserts to only be active with debug, bump alpha version number

This commit is contained in:
kyren 2018-03-12 16:14:52 -04:00
parent f79d771f1a
commit f0775f4a1a
8 changed files with 35 additions and 17 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "rlua" name = "rlua"
version = "0.13.1-alpha.0" version = "0.14.0-alpha.0"
authors = ["kyren <catherine@chucklefish.org>"] authors = ["kyren <catherine@chucklefish.org>"]
description = "High level bindings to Lua 5.3" description = "High level bindings to Lua 5.3"
repository = "https://github.com/chucklefish/rlua" repository = "https://github.com/chucklefish/rlua"

View File

@ -46,6 +46,11 @@ unsafe impl Send for Lua {}
impl Drop for Lua { impl Drop for Lua {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { unsafe {
if cfg!(debug_assertions) {
for use_count in &self.ref_stack_slots {
rlua_assert!(use_count.get() == 0, "live stack reference detected");
}
if !self.ephemeral { if !self.ephemeral {
let top = ffi::lua_gettop(self.state); let top = ffi::lua_gettop(self.state);
rlua_assert!( rlua_assert!(
@ -53,7 +58,10 @@ impl Drop for Lua {
"stack problem detected, stack top is {}", "stack problem detected, stack top is {}",
top - REF_STACK_SIZE top - REF_STACK_SIZE
); );
}
}
if !self.ephemeral {
let extra_data = *(ffi::lua_getextraspace(self.state) as *mut *mut ExtraData); let extra_data = *(ffi::lua_getextraspace(self.state) as *mut *mut ExtraData);
*(*extra_data).registry_unref_list.lock().unwrap() = None; *(*extra_data).registry_unref_list.lock().unwrap() = None;
Box::from_raw(extra_data); Box::from_raw(extra_data);
@ -819,7 +827,7 @@ impl Lua {
RefType::Stack { stack_slot } => { RefType::Stack { stack_slot } => {
let ref_slot = &self.ref_stack_slots[(stack_slot - 1) as usize]; let ref_slot = &self.ref_stack_slots[(stack_slot - 1) as usize];
let ref_count = ref_slot.get(); let ref_count = ref_slot.get();
rlua_assert!(ref_count > 0, "ref slot use count has gone below zero"); rlua_debug_assert!(ref_count > 0, "ref slot use count has gone below zero");
ref_slot.set(ref_count - 1); ref_slot.set(ref_count - 1);
if ref_count == 1 { if ref_count == 1 {
ffi::lua_pushnil(self.state); ffi::lua_pushnil(self.state);
@ -1125,7 +1133,7 @@ impl Lua {
})); }));
*(ffi::lua_getextraspace(state) as *mut *mut ExtraData) = extra_data; *(ffi::lua_getextraspace(state) as *mut *mut ExtraData) = extra_data;
rlua_assert!(ffi::lua_gettop(state) == 0, "stack leak during creation"); rlua_debug_assert!(ffi::lua_gettop(state) == 0, "stack leak during creation");
check_stack(state, REF_STACK_SIZE); check_stack(state, REF_STACK_SIZE);
ffi::lua_settop(state, REF_STACK_SIZE); ffi::lua_settop(state, REF_STACK_SIZE);

View File

@ -40,6 +40,16 @@ macro_rules! rlua_assert {
}; };
} }
macro_rules! rlua_debug_assert {
($cond:expr, $msg:expr) => {
debug_assert!($cond, concat!("rlua internal error: ", $msg));
};
($cond:expr, $msg:expr, $($arg:tt)+) => {
debug_assert!($cond, concat!("rlua internal error: ", $msg), $($arg)+);
};
}
macro_rules! rlua_abort { macro_rules! rlua_abort {
($msg:expr) => { ($msg:expr) => {
{ {

View File

@ -73,7 +73,7 @@ impl<'lua> String<'lua> {
check_stack(lua.state, 1); check_stack(lua.state, 1);
lua.push_ref(&self.0); lua.push_ref(&self.0);
rlua_assert!( rlua_debug_assert!(
ffi::lua_type(lua.state, -1) == ffi::LUA_TSTRING, ffi::lua_type(lua.state, -1) == ffi::LUA_TSTRING,
"string ref is not string type" "string ref is not string type"
); );

View File

@ -781,9 +781,9 @@ fn large_args() {
); );
} }
#[test]
fn large_args_ref() { fn large_args_ref() {
let lua = Lua::new(); let lua = Lua::new();
let globals = lua.globals();
let f = lua.create_function(|_, args: Variadic<String>| { let f = lua.create_function(|_, args: Variadic<String>| {
for i in 0..args.len() { for i in 0..args.len() {

View File

@ -100,7 +100,7 @@ fn coroutine_panic() {
match catch_unwind(|| -> Result<()> { match catch_unwind(|| -> Result<()> {
// check that coroutines propagate panics correctly // check that coroutines propagate panics correctly
let lua = Lua::new(); let lua = Lua::new();
let thrd_main = lua.create_function(|lua, ()| -> Result<()> { let thrd_main = lua.create_function(|_, ()| -> Result<()> {
panic!("test_panic"); panic!("test_panic");
})?; })?;
lua.globals().set("main", thrd_main.clone())?; lua.globals().set("main", thrd_main.clone())?;

View File

@ -420,7 +420,7 @@ impl<'lua> AnyUserData<'lua> {
lua.push_ref(&self.0); lua.push_ref(&self.0);
rlua_assert!( rlua_debug_assert!(
ffi::lua_getmetatable(lua.state, -1) != 0, ffi::lua_getmetatable(lua.state, -1) != 0,
"AnyUserData missing metatable" "AnyUserData missing metatable"
); );

View File

@ -158,7 +158,7 @@ where
// 3) Otherwise, interprets the error as the appropriate lua error. // 3) Otherwise, interprets the error as the appropriate lua error.
// Uses 2 stack spaces, does not call lua_checkstack. // Uses 2 stack spaces, does not call lua_checkstack.
pub unsafe fn pop_error(state: *mut ffi::lua_State, err_code: c_int) -> Error { pub unsafe fn pop_error(state: *mut ffi::lua_State, err_code: c_int) -> Error {
rlua_assert!( rlua_debug_assert!(
err_code != ffi::LUA_OK && err_code != ffi::LUA_YIELD, err_code != ffi::LUA_OK && err_code != ffi::LUA_YIELD,
"pop_error called with non-error return code" "pop_error called with non-error return code"
); );
@ -229,7 +229,7 @@ pub unsafe fn push_userdata<T>(state: *mut ffi::lua_State, t: T) -> Result<()> {
pub unsafe fn get_userdata<T>(state: *mut ffi::lua_State, index: c_int) -> *mut T { pub unsafe fn get_userdata<T>(state: *mut ffi::lua_State, index: c_int) -> *mut T {
let ud = ffi::lua_touserdata(state, index) as *mut T; let ud = ffi::lua_touserdata(state, index) as *mut T;
rlua_assert!(!ud.is_null(), "userdata pointer is null"); rlua_debug_assert!(!ud.is_null(), "userdata pointer is null");
ud ud
} }
@ -243,7 +243,7 @@ pub unsafe fn take_userdata<T>(state: *mut ffi::lua_State) -> T {
get_destructed_userdata_metatable(state); get_destructed_userdata_metatable(state);
ffi::lua_setmetatable(state, -2); ffi::lua_setmetatable(state, -2);
let ud = ffi::lua_touserdata(state, -1) as *mut T; let ud = ffi::lua_touserdata(state, -1) as *mut T;
rlua_assert!(!ud.is_null(), "userdata pointer is null"); rlua_debug_assert!(!ud.is_null(), "userdata pointer is null");
ffi::lua_pop(state, 1); ffi::lua_pop(state, 1);
ptr::read(ud) ptr::read(ud)
} }