Skip to content

Commit eb63755

Browse files
author
Your Name
committed
fix yieldable continuation on non-luau
1 parent 9e77e5c commit eb63755

File tree

4 files changed

+179
-176
lines changed

4 files changed

+179
-176
lines changed

src/state/extra.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ pub(crate) struct ExtraData {
9898

9999
// Values currently being yielded from Lua.yield()
100100
pub(super) yielded_values: Option<MultiValue>,
101-
102-
#[cfg(all(not(feature = "luau"), not(feature = "lua51"), not(feature = "luajit")))]
103-
pub(super) yield_continuation: bool,
104101
}
105102

106103
impl Drop for ExtraData {
@@ -203,8 +200,6 @@ impl ExtraData {
203200
#[cfg(feature = "luau")]
204201
running_gc: false,
205202
yielded_values: None,
206-
#[cfg(all(not(feature = "luau"), not(feature = "lua51"), not(feature = "luajit")))]
207-
yield_continuation: false,
208203
}));
209204

210205
// Store it in the registry

src/state/raw.rs

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,15 +1164,21 @@ impl RawLua {
11641164
pub(crate) fn create_callback(&self, func: Callback) -> Result<Function> {
11651165
unsafe extern "C-unwind" fn call_callback(state: *mut ffi::lua_State) -> c_int {
11661166
let upvalue = get_userdata::<CallbackUpvalue>(state, ffi::lua_upvalueindex(1));
1167-
callback_error_ext_yieldable(state, (*upvalue).extra.get(), true, |extra, nargs| {
1168-
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments)
1169-
// The lock must be already held as the callback is executed
1170-
let rawlua = (*extra).raw_lua();
1171-
match (*upvalue).data {
1172-
Some(ref func) => func(rawlua, nargs),
1173-
None => Err(Error::CallbackDestructed),
1174-
}
1175-
})
1167+
callback_error_ext_yieldable(
1168+
state,
1169+
(*upvalue).extra.get(),
1170+
true,
1171+
|extra, nargs| {
1172+
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments)
1173+
// The lock must be already held as the callback is executed
1174+
let rawlua = (*extra).raw_lua();
1175+
match (*upvalue).data {
1176+
Some(ref func) => func(rawlua, nargs),
1177+
None => Err(Error::CallbackDestructed),
1178+
}
1179+
},
1180+
false,
1181+
)
11761182
}
11771183

11781184
let state = self.state();
@@ -1260,21 +1266,22 @@ impl RawLua {
12601266
{
12611267
unsafe extern "C-unwind" fn call_callback(state: *mut ffi::lua_State) -> c_int {
12621268
let upvalue = get_userdata::<ContinuationUpvalue>(state, ffi::lua_upvalueindex(1));
1263-
callback_error_ext_yieldable(state, (*upvalue).extra.get(), true, |extra, nargs| {
1264-
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing arguments)
1265-
// The lock must be already held as the callback is executed
1266-
let rawlua = (*extra).raw_lua();
1267-
match (*upvalue).data {
1268-
Some((ref func, _)) => match func(rawlua, nargs) {
1269-
Ok(r) => {
1270-
(*extra).yield_continuation = true;
1271-
Ok(r)
1272-
}
1273-
Err(e) => Err(e),
1274-
},
1275-
None => Err(Error::CallbackDestructed),
1276-
}
1277-
})
1269+
callback_error_ext_yieldable(
1270+
state,
1271+
(*upvalue).extra.get(),
1272+
true,
1273+
|extra, nargs| {
1274+
// Lua ensures that `LUA_MINSTACK` stack spaces are available (after pushing
1275+
// arguments) The lock must be already held as the callback is
1276+
// executed
1277+
let rawlua = (*extra).raw_lua();
1278+
match (*upvalue).data {
1279+
Some((ref func, _)) => func(rawlua, nargs),
1280+
None => Err(Error::CallbackDestructed),
1281+
}
1282+
},
1283+
true,
1284+
)
12781285
}
12791286

12801287
let state = self.state();

src/state/util.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ where
115115
Ok(Ok(r)) => {
116116
// Ensure yielded values are cleared
117117
take(&mut extra.as_mut().unwrap_unchecked().yielded_values);
118-
#[cfg(all(not(feature = "luau"), not(feature = "lua51"), not(feature = "luajit")))]
119-
take(&mut extra.as_mut().unwrap_unchecked().yield_continuation);
120118

121119
// Return unused `WrappedFailure` to the pool
122120
prealloc_failure.release(state, extra);
@@ -170,6 +168,7 @@ pub(crate) unsafe fn callback_error_ext_yieldable<F>(
170168
mut extra: *mut ExtraData,
171169
wrap_error: bool,
172170
f: F,
171+
in_callback_with_continuation: bool,
173172
) -> c_int
174173
where
175174
F: FnOnce(*mut ExtraData, c_int) -> Result<c_int>,
@@ -193,9 +192,6 @@ where
193192
let raw = extra.as_ref().unwrap_unchecked().raw_lua();
194193
let values = take(&mut extra.as_mut().unwrap_unchecked().yielded_values);
195194

196-
#[cfg(all(not(feature = "luau"), not(feature = "lua51"), not(feature = "luajit")))]
197-
let yield_cont = take(&mut extra.as_mut().unwrap_unchecked().yield_continuation);
198-
199195
if let Some(values) = values {
200196
if raw.state() == state {
201197
// Edge case: main thread is being yielded
@@ -230,7 +226,7 @@ where
230226
{
231227
// Yield to a continuation. Unlike luau, we need to do this manually and on the
232228
// fly using a yieldk call
233-
if yield_cont {
229+
if in_callback_with_continuation {
234230
// On Lua 5.2, status and ctx are not present, so use 0 as status for
235231
// compatibility
236232
#[cfg(feature = "lua52")]
@@ -253,6 +249,7 @@ where
253249
None => Err(Error::CallbackDestructed),
254250
}
255251
},
252+
true,
256253
)
257254
}
258255

@@ -279,6 +276,7 @@ where
279276
None => Err(Error::CallbackDestructed),
280277
}
281278
},
279+
true,
282280
)
283281
}
284282

0 commit comments

Comments
 (0)