diff options
Diffstat (limited to 'src/thread.rs')
-rw-r--r-- | src/thread.rs | 90 |
1 files changed, 44 insertions, 46 deletions
diff --git a/src/thread.rs b/src/thread.rs index 7174240..cb87873 100644 --- a/src/thread.rs +++ b/src/thread.rs @@ -2,7 +2,7 @@ use std::os::raw::c_int; use ffi; use error::{Error, Result}; -use util::{check_stack, check_stack_err, error_traceback, pop_error, stack_guard}; +use util::{check_stack, check_stack_err, error_traceback, pop_error, StackGuard}; use types::LuaRef; use value::{FromLuaMulti, MultiValue, ToLuaMulti}; @@ -78,45 +78,44 @@ impl<'lua> Thread<'lua> { { let lua = self.0.lua; unsafe { - stack_guard(lua.state, || { - check_stack(lua.state, 1); + let _sg = StackGuard::new(lua.state); + check_stack(lua.state, 1); - lua.push_ref(&self.0); - let thread_state = ffi::lua_tothread(lua.state, -1); + lua.push_ref(&self.0); + let thread_state = ffi::lua_tothread(lua.state, -1); - let status = ffi::lua_status(thread_state); - if status != ffi::LUA_YIELD && ffi::lua_gettop(thread_state) == 0 { - return Err(Error::CoroutineInactive); - } + let status = ffi::lua_status(thread_state); + if status != ffi::LUA_YIELD && ffi::lua_gettop(thread_state) == 0 { + return Err(Error::CoroutineInactive); + } - ffi::lua_pop(lua.state, 1); + ffi::lua_pop(lua.state, 1); - let args = args.to_lua_multi(lua)?; - let nargs = args.len() as c_int; - check_stack_err(lua.state, nargs)?; - check_stack_err(thread_state, nargs + 1)?; + let args = args.to_lua_multi(lua)?; + let nargs = args.len() as c_int; + check_stack_err(lua.state, nargs)?; + check_stack_err(thread_state, nargs + 1)?; - for arg in args { - lua.push_value(arg); - } - ffi::lua_xmove(lua.state, thread_state, nargs); + for arg in args { + lua.push_value(arg); + } + ffi::lua_xmove(lua.state, thread_state, nargs); - let ret = ffi::lua_resume(thread_state, lua.state, nargs); - if ret != ffi::LUA_OK && ret != ffi::LUA_YIELD { - error_traceback(thread_state); - return Err(pop_error(thread_state, ret)); - } + let ret = ffi::lua_resume(thread_state, lua.state, nargs); + if ret != ffi::LUA_OK && ret != ffi::LUA_YIELD { + error_traceback(thread_state); + return Err(pop_error(thread_state, ret)); + } - let nresults = ffi::lua_gettop(thread_state); - let mut results = MultiValue::new(); - ffi::lua_xmove(thread_state, lua.state, nresults); + let nresults = ffi::lua_gettop(thread_state); + let mut results = MultiValue::new(); + ffi::lua_xmove(thread_state, lua.state, nresults); - check_stack(lua.state, 2); - for _ in 0..nresults { - results.push_front(lua.pop_value()); - } - R::from_lua_multi(results, lua) - }) + check_stack(lua.state, 2); + for _ in 0..nresults { + results.push_front(lua.pop_value()); + } + R::from_lua_multi(results, lua) } } @@ -124,22 +123,21 @@ impl<'lua> Thread<'lua> { pub fn status(&self) -> ThreadStatus { let lua = self.0.lua; unsafe { - stack_guard(lua.state, || { - check_stack(lua.state, 1); + let _sg = StackGuard::new(lua.state); + check_stack(lua.state, 1); - lua.push_ref(&self.0); - let thread_state = ffi::lua_tothread(lua.state, -1); - ffi::lua_pop(lua.state, 1); + lua.push_ref(&self.0); + let thread_state = ffi::lua_tothread(lua.state, -1); + ffi::lua_pop(lua.state, 1); - let status = ffi::lua_status(thread_state); - if status != ffi::LUA_OK && status != ffi::LUA_YIELD { - ThreadStatus::Error - } else if status == ffi::LUA_YIELD || ffi::lua_gettop(thread_state) > 0 { - ThreadStatus::Resumable - } else { - ThreadStatus::Unresumable - } - }) + let status = ffi::lua_status(thread_state); + if status != ffi::LUA_OK && status != ffi::LUA_YIELD { + ThreadStatus::Error + } else if status == ffi::LUA_YIELD || ffi::lua_gettop(thread_state) > 0 { + ThreadStatus::Resumable + } else { + ThreadStatus::Unresumable + } } } } |