Skip to content

Commit 085c62a

Browse files
author
Your Name
committed
handle mainthread edgecase
1 parent b3a54ac commit 085c62a

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/state/util.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,23 @@ where
189189
let values = take(&mut extra.as_mut().unwrap_unchecked().yielded_values);
190190

191191
if !values.is_empty() {
192+
if raw.state() == state {
193+
// Edge case: main thread is being yielded
194+
//
195+
// We need to pop/clear stack early, then push args
196+
ffi::lua_pop(state, -1);
197+
}
198+
192199
match values.push_into_stack_multi(raw) {
193200
Ok(nargs) => {
194-
ffi::lua_pop(state, -1);
195-
ffi::lua_xmove(raw.state(), state, nargs);
201+
// If not main thread, then clear and xmove to target thread
202+
if raw.state() != state {
203+
// luau preserves the stack making yieldable continuations ugly and leaky
204+
//
205+
// Even outside of luau, clearing the stack is probably desirable
206+
ffi::lua_pop(state, -1);
207+
ffi::lua_xmove(raw.state(), state, nargs);
208+
}
196209
return ffi::lua_yield(state, nargs);
197210
}
198211
Err(err) => {

0 commit comments

Comments
 (0)