Skip to content

Commit 104e242

Browse files
committed
Some cosmetic changes
1 parent 825bdbf commit 104e242

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

src/state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,8 +1127,8 @@ impl Lua {
11271127

11281128
/// Wraps a Rust async function or closure, creating a callable Lua function handle to it.
11291129
///
1130-
/// While executing the function Rust will poll Future and if the result is not ready, call
1131-
/// `yield()` passing internal representation of a `Poll::Pending` value.
1130+
/// While executing the function Rust will poll the Future and if the result is not ready,
1131+
/// call `yield()` passing internal representation of a `Poll::Pending` value.
11321132
///
11331133
/// The function must be called inside Lua coroutine ([`Thread`]) to be able to suspend its
11341134
/// execution. An executor should be used to poll [`AsyncThread`] and mlua will take a

src/state/raw.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,7 @@ impl RawLua {
10751075

10761076
#[cfg(feature = "async")]
10771077
pub(crate) fn create_async_callback(&self, func: AsyncCallback) -> Result<Function> {
1078+
// Ensure that the coroutine library is loaded
10781079
#[cfg(any(feature = "lua54", feature = "lua53", feature = "lua52", feature = "luau"))]
10791080
unsafe {
10801081
if !(*self.extra.get()).libs.contains(StdLib::COROUTINE) {
@@ -1130,7 +1131,7 @@ impl RawLua {
11301131
}
11311132
Poll::Ready(nresults) => {
11321133
match nresults? {
1133-
nresults @ 0..=2 => {
1134+
nresults if nresults < 3 => {
11341135
// Fast path for up to 2 results without creating a table
11351136
ffi::lua_pushinteger(state, nresults as _);
11361137
if nresults > 0 {
@@ -1182,13 +1183,11 @@ impl RawLua {
11821183
let lua = self.lua();
11831184
let coroutine = lua.globals().get::<Table>("coroutine")?;
11841185

1186+
// Prepare environment for the async poller
11851187
let env = lua.create_table_with_capacity(0, 3)?;
11861188
env.set("get_poll", get_poll)?;
1187-
// Cache `yield` function
11881189
env.set("yield", coroutine.get::<Function>("yield")?)?;
1189-
unsafe {
1190-
env.set("unpack", lua.create_c_function(unpack)?)?;
1191-
}
1190+
env.set("unpack", unsafe { lua.create_c_function(unpack)? })?;
11921191

11931192
lua.load(
11941193
r#"

tests/async.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ async fn test_async_call() -> Result<()> {
7575

7676
match hello.call::<()>("alex") {
7777
Err(Error::RuntimeError(_)) => {}
78-
_ => panic!("non-async executing async function must fail on the yield stage with RuntimeError"),
78+
err => panic!("expected `RuntimeError`, got {err:?}"),
7979
};
8080

8181
assert_eq!(hello.call_async::<String>("alex").await?, "hello, alex!");

tests/function.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ fn test_function_environment() -> Result<()> {
168168
lua.gc_collect()?;
169169
assert_eq!(lua_func2.call::<String>(())?, "local");
170170

171+
// Test getting environment set by chunk loader
172+
let chunk = lua
173+
.load("return hello")
174+
.set_environment(lua.create_table_from([("hello", "chunk")])?)
175+
.into_function()?;
176+
assert_eq!(chunk.environment().unwrap().get::<String>("hello")?, "chunk");
177+
171178
Ok(())
172179
}
173180

0 commit comments

Comments
 (0)