Skip to content

Commit 2cd34db

Browse files
author
Your Name
committed
support empty yield args
1 parent 198b857 commit 2cd34db

File tree

4 files changed

+81
-7
lines changed

4 files changed

+81
-7
lines changed

src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,7 @@ impl Lua {
21712171
return Err(Error::runtime("cannot yield across Rust/Lua boundary."));
21722172
}
21732173
unsafe {
2174-
raw.extra.get().as_mut().unwrap_unchecked().yielded_values = args.into_lua_multi(self)?;
2174+
raw.extra.get().as_mut().unwrap_unchecked().yielded_values = Some(args.into_lua_multi(self)?);
21752175
}
21762176
Ok(())
21772177
}

src/state/extra.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub(crate) struct ExtraData {
9696
pub(super) enable_jit: bool,
9797

9898
// Values currently being yielded from Lua.yield()
99-
pub(super) yielded_values: MultiValue,
99+
pub(super) yielded_values: Option<MultiValue>,
100100
}
101101

102102
impl Drop for ExtraData {
@@ -198,7 +198,7 @@ impl ExtraData {
198198
enable_jit: true,
199199
#[cfg(feature = "luau")]
200200
running_gc: false,
201-
yielded_values: MultiValue::with_capacity(0),
201+
yielded_values: None,
202202
}));
203203

204204
// Store it in the registry

src/state/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ where
188188
let raw = extra.as_ref().unwrap_unchecked().raw_lua();
189189
let values = take(&mut extra.as_mut().unwrap_unchecked().yielded_values);
190190

191-
if !values.is_empty() {
191+
if let Some(values) = values {
192192
if raw.state() == state {
193193
// Edge case: main thread is being yielded
194194
//

tests/luau/cont.rs

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,84 @@ use mlua::Lua;
44

55
#[test]
66
fn test_luau_continuation() {
7-
// Yielding continuation
8-
mlua::Lua::set_fflag("LuauYieldableContinuations", true).unwrap();
9-
107
let lua = Lua::new();
8+
// No yielding continuation fflag test
9+
let cont_func = lua
10+
.create_function_with_luau_continuation(
11+
|lua, a: u64| {
12+
match lua.set_yield_args(a) {
13+
Ok(()) => println!("set_yield_args called"),
14+
Err(e) => println!("{:?}", e),
15+
};
16+
Ok(())
17+
},
18+
|_lua, _status, a: u64| {
19+
println!("Reached cont");
20+
Ok(a + 39)
21+
},
22+
)
23+
.expect("Failed to create cont_func");
24+
25+
let luau_func = lua
26+
.load(
27+
"
28+
local cont_func = ...
29+
local res = cont_func(1)
30+
return res + 1
31+
",
32+
)
33+
.into_function()
34+
.expect("Failed to create function");
35+
36+
let th = lua
37+
.create_thread(luau_func)
38+
.expect("Failed to create luau thread");
39+
40+
let v = th
41+
.resume::<mlua::MultiValue>(cont_func)
42+
.expect("Failed to resume");
43+
let v = th.resume::<i32>(v).expect("Failed to load continuation");
44+
45+
assert_eq!(v, 41);
46+
47+
// empty yield args test
48+
let cont_func = lua
49+
.create_function_with_luau_continuation(
50+
|lua, _: ()| {
51+
match lua.set_yield_args(()) {
52+
Ok(()) => println!("set_yield_args called"),
53+
Err(e) => println!("{:?}", e),
54+
};
55+
Ok(())
56+
},
57+
|_lua, _status, mv: mlua::MultiValue| Ok(mv.len()),
58+
)
59+
.expect("Failed to create cont_func");
60+
61+
let luau_func = lua
62+
.load(
63+
"
64+
local cont_func = ...
65+
local res = cont_func(1)
66+
return res - 1
67+
",
68+
)
69+
.into_function()
70+
.expect("Failed to create function");
71+
72+
let th = lua
73+
.create_thread(luau_func)
74+
.expect("Failed to create luau thread");
75+
76+
let v = th
77+
.resume::<mlua::MultiValue>(cont_func)
78+
.expect("Failed to resume");
79+
assert!(v.is_empty());
80+
let v = th.resume::<i32>(v).expect("Failed to load continuation");
81+
assert_eq!(v, -1);
82+
83+
// Yielding continuation fflag test
84+
mlua::Lua::set_fflag("LuauYieldableContinuations", true).unwrap();
1185

1286
let cont_func = lua
1387
.create_function_with_luau_continuation(

0 commit comments

Comments
 (0)