From a2b77f37a232a487582c72030c1d625ba39a808f Mon Sep 17 00:00:00 2001 From: kyren Date: Thu, 27 Jul 2017 16:47:06 -0400 Subject: [PATCH] 'main_state' fix Dont' confuse the state we're pushing the registry value for the main state to with the main state itself, pop from the correct state. --- src/tests.rs | 11 +++++++++++ src/util.rs | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/tests.rs b/src/tests.rs index 3a19d05..b5a7ca0 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -882,3 +882,14 @@ fn string_views() { assert!(err.to_str().is_err()); assert_eq!(err.as_bytes(), &b"but \xff isn't :("[..]); } + +#[test] +fn coroutine_from_closure() { + let lua = Lua::new(); + let thrd_main = lua.create_function(|lua, _| { + lua.pack(()) + }); + lua.globals().set("main", thrd_main).unwrap(); + let thrd: Thread = lua.eval("coroutine.create(main)", None).unwrap(); + thrd.resume::<_, ()>(()).unwrap(); +} diff --git a/src/util.rs b/src/util.rs index a29a8c9..7808fd2 100644 --- a/src/util.rs +++ b/src/util.rs @@ -478,9 +478,9 @@ pub unsafe extern "C" fn safe_xpcall(state: *mut ffi::lua_State) -> c_int { /// Does not call checkstack, uses 1 stack space pub unsafe fn main_state(state: *mut ffi::lua_State) -> *mut ffi::lua_State { ffi::lua_rawgeti(state, ffi::LUA_REGISTRYINDEX, ffi::LUA_RIDX_MAINTHREAD); - let state = ffi::lua_tothread(state, -1); + let main_state = ffi::lua_tothread(state, -1); ffi::lua_pop(state, 1); - state + main_state } pub struct WrappedError(pub Error);